应用程序: CreateViewSection

Revit平台: 结构

Revit版本: 2011.0

首次发布版本: 9.0

编程语言: C#

技能水平: 中等

类别: 视图

类型: 外部命令

主题: 创建剖面视图。

摘要:该示例展示了如何在线性元素(如墙、地板或梁)的中点处生成一张剖面视图。

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.DB.Document

Autodesk.Revit.DB.BoundingBoxXYZ

Autodesk.Revit.DB.Element

Autodesk.Revit.DB.ElementSet

Autodesk.Revit.DB.Wall

Autodesk.Revit.DB.LocationCurve

Autodesk.Revit.DB.Line

Autodesk.Revit.DB.FamilyInstance

Autodesk.Revit.DB.Transform

Autodesk.Revit.DB.Structural.AnalyticalModel

Autodesk.Revit.DB.Structural.AnalyticalModelFrame

Autodesk.Revit.DB.Curve

Autodesk.Revit.DB.Floor

Autodesk.Revit.DB.Structural.AnalyticalModelFloor

项目文件:

Command.vb文件包含Command类,该类实现了IExternalCommand接口和在线性元素中点处生成剖面视图的生成过程。它还包含了一个名为CreateDraftingView的类,用于创建草图视图。

XYZMath.cs 它提供了关于由XYZ结构表示的点和向量的操作。

功能:

此示例提供以下功能:

-     检索所选的线性元素。

-     生成一个BoundingBoxXYZ实例,该实例将在NewViewSection()方法中使用。

-     设置其Max和Min属性。

-     生成一个Transform实例作为BoundingBoxXYZ的Transform属性,该实例定义了创建的视图的原点和方向(包括RightDirection、UpDirection和ViewDirection)。

-     使用BoundingBoxXYZ创建剖面视图。

实施:

1. 打开 Revit 结构。

2. 绘制一个线性元素(墙、结构地板或结构梁)。

3. 选择这个元素,执行外部命令 Command。

1. 打开 Revit 结构。

2. 执行外部命令 CreateDraftingView.

完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

XYZMath.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//


using System;
using System.Collections.Generic;
using System.Text;

using Autodesk.Revit.DB;

namespace Revit.SDK.Samples.CreateViewSection.CS
{
    /// <summary>
    /// The helper class which give some operation about point and vector.
    /// The point and vector are both presented by Autodesk.Revit.DB.XYZ structure.
    /// </summary>
    public class XYZMath
    {
        // Private Members
        const Double PRECISION = 0.0000000001;  // Define a precision of double data


        // Methods
        /// <summary>
        /// Find the middle point of the line.
        /// </summary>
        /// <param name="first">the start point of the line</param>
        /// <param name="second">the end point of the line</param>
        /// <returns>the middle point of the line</returns>
        public static Autodesk.Revit.DB.XYZ FindMidPoint(Autodesk.Revit.DB.XYZ first, Autodesk.Revit.DB.XYZ second)
        {
            double x = (first.X + second.X) / 2;
            double y = (first.Y + second.Y) / 2;
            double z = (first.Z + second.Z) / 2;
            Autodesk.Revit.DB.XYZ midPoint = new Autodesk.Revit.DB.XYZ (x, y, z);
            return midPoint;
        }

        /// <summary>
        /// Find the distance between two points
        /// </summary>
        /// <param name="first">the first point</param>
        /// <param name="second">the first point</param>
        /// <returns>the distance of two points</returns>
        public static double FindDistance(Autodesk.Revit.DB.XYZ first, Autodesk.Revit.DB.XYZ second)
        {
            double x = first.X - second.X;
            double y = first.Y - second.Y;
            double z = first.Z - second.Z;
            return Math.Sqrt(x * x + y * y + z * z);
        }

        /// <summary>
        /// Find the direction vector from first point to second point
        /// </summary>
        /// <param name="first">the first point</param>
        /// <param name="second">the second point</param>
        /// <returns>the direction vector</returns>
        public static Autodesk.Revit.DB.XYZ FindDirection(Autodesk.Revit.DB.XYZ first, Autodesk.Revit.DB.XYZ second)
        {
            double x = second.X - first.X;
            double y = second.Y - first.Y;
            double z = second.Z - first.Z;
            double distance = FindDistance(first, second);
            Autodesk.Revit.DB.XYZ direction = new Autodesk.Revit.DB.XYZ (x / distance, y / distance, z / distance);
            return direction;
        }

        /// <summary>
        /// Find the right direction vector, 
        /// which is the same meaning of RightDirection property in View class
        /// </summary>
        /// <param name="viewDirection">the view direction vector</param>
        /// <returns>the right direction vector</returns>
        public static Autodesk.Revit.DB.XYZ FindRightDirection(Autodesk.Revit.DB.XYZ viewDirection)
        {
            // Because this example only allow the beam to be horizontal,
            // the created viewSection should be vertical, 
            // the same thing can also be found when the user select wall or floor.
            // So only need to turn 90 degree around Z axes will get Right Direction.  

            double x = -viewDirection.Y;
            double y = viewDirection.X;
            double z = viewDirection.Z;
            Autodesk.Revit.DB.XYZ direction = new Autodesk.Revit.DB.XYZ (x, y, z);
            return direction;         
        }

        /// <summary>
        /// Find the up direction vector, 
        /// which is the same meaning of UpDirection property in View class
        /// </summary>
        /// <param name="viewDirection">the view direction vector</param>
        /// <returns>the up direction vector</returns>
        public static Autodesk.Revit.DB.XYZ FindUpDirection(Autodesk.Revit.DB.XYZ viewDirection)
        {
            // Because this example only allow the beam to be horizontal,
            // the created viewSection should be vertical, 
            // the same thing can also be found when the user select wall or floor.
            // So UpDirection should be z axes.
            Autodesk.Revit.DB.XYZ direction = new Autodesk.Revit.DB.XYZ (0, 0, 1);
            return direction;
        }

        /// <summary>
        /// Find the middle point of a profile.
        /// This method is used to find out middle point of the selected wall or floor.
        /// </summary>
        /// <param name="curveArray">the array of curve which form the profile</param>
        /// <returns>the middle point of the profile</returns>
        public static Autodesk.Revit.DB.XYZ FindMiddlePoint(CurveArray curveArray)
        {
            // First form a point array which include all the end points of the curves
            List<Autodesk.Revit.DB.XYZ> array = new List<Autodesk.Revit.DB.XYZ>();
            foreach (Curve curve in curveArray)
            {
                Autodesk.Revit.DB.XYZ first = curve.GetEndPoint(0);
                Autodesk.Revit.DB.XYZ second = curve.GetEndPoint(1);
                array.Add(first);
                array.Add(second);
            }

            // Second find the max and min value of three coordinate
            double maxX = array[0].X;  // the max x coordinate in the array
            double minX = array[0].X;  // the min x coordinate in the array
            double maxY = array[0].Y;  // the max y coordinate in the array
            double minY = array[0].Y;  // the min y coordinate in the array
            double maxZ = array[0].Z;  // the max z coordinate in the array
            double minZ = array[0].Z;  // the min z coordinate in the array

            foreach (Autodesk.Revit.DB.XYZ curve in array)
            {
                if (maxX < curve.X)
                {
                    maxX = curve.X;
                }
                if (minX > curve.X)
                {
                    minX = curve.X;
                }
                if (maxY < curve.Y)
                {
                    maxY = curve.Y;
                }
                if (minY > curve.Y)
                {
                    minY = curve.Y;
                }
                if (maxZ < curve.Z)
                {
                    maxZ = curve.Z;
                }
                if (minZ > curve.Z)
                {
                    minZ = curve.Z;
                }
            }

            // Third form the middle point using the average of max and min values
            double x = (maxX + minX) / 2;
            double y = (maxY + minY) / 2;
            double z = (maxZ + minZ) / 2;
            Autodesk.Revit.DB.XYZ midPoint = new Autodesk.Revit.DB.XYZ (x, y, z);
            return midPoint;
        }

        /// <summary>
        /// Find the view direction vector, 
        /// which is the same meaning of ViewDirection property in View class
        /// </summary>
        /// <param name="curveArray">the curve array which form wall's AnalyticalModel</param>
        /// <returns>the view direction vector</returns>
        public static Autodesk.Revit.DB.XYZ FindWallViewDirection(CurveArray curveArray)
        {
            Autodesk.Revit.DB.XYZ direction = new Autodesk.Revit.DB.XYZ ();
            foreach (Curve curve in curveArray)
            {
                Autodesk.Revit.DB.XYZ startPoint = curve.GetEndPoint(0);
                Autodesk.Revit.DB.XYZ endPoint = curve.GetEndPoint(1);
                double distanceX = startPoint.X - endPoint.X;
                double distanceY = startPoint.Y - endPoint.Y;
                if(-PRECISION > distanceX || PRECISION < distanceX
                    || -PRECISION > distanceY || PRECISION < distanceY)
                {
                    Autodesk.Revit.DB.XYZ first = new Autodesk.Revit.DB.XYZ (startPoint.X, startPoint.Y, 0);
                    Autodesk.Revit.DB.XYZ second = new Autodesk.Revit.DB.XYZ (endPoint.X, endPoint.Y, 0);
                    direction = FindDirection(first, second);
                    break;
                }
            }
            return direction;
        }

        /// <summary>
        /// Find the view direction vector, 
        /// which is the same meaning of ViewDirection property in View class
        /// </summary>
        /// <param name="curveArray">the curve array which form floor's AnalyticalModel</param>
        /// <returns>the view direction vector</returns>
        public static Autodesk.Revit.DB.XYZ FindFloorViewDirection(CurveArray curveArray)
        {
            // Because the floor is always on the level,
            // so each curve can give the direction information.
            Curve curve = curveArray.get_Item(0);
            Autodesk.Revit.DB.XYZ first = curve.GetEndPoint(0);
            Autodesk.Revit.DB.XYZ second = curve.GetEndPoint(1);
            return FindDirection(first, second);
        }
    }
}