Revit二次开发——墙

WallType.WallKind枚举代表以下四种墙:
●Stacked (叠层墙)。
●Curtain (幕墙)。
●Basic (基础墙)。
●Unknown (未定义墙)。

创建墙

创建墙实例的方法一共5个重载

        public static Wall Create(Document document, Curve curve, ElementId levelId, bool structural);
        public static Wall Create(Document document, IList<Curve> profile, bool structural);
        public static Wall Create(Document document, IList<Curve> profile, ElementId wallTypeId, ElementId levelId, bool structural);
        public static Wall Create(Document document, IList<Curve> profile, ElementId wallTypeId, ElementId levelId, bool structural, XYZ normal);
        public static Wall Create(Document document, Curve curve, ElementId wallTypeId, ElementId levelId, double height, double offset, bool flip, bool structural);

介绍参数

public static Wall Create(
	Document document,
	Curve curve,
	ElementId wallTypeId,
	ElementId levelId,
	double height,
	double offset,
	bool flip,
	bool structural
)
参数类型说明
document文档Autodesk.Revit.DB.Document创建新墙的文档。
curve线:Autodesk.Revit.DB.Curve代表墙基线的曲线。Line或者Arc
wallTypeId墙类型idAutodesk.Revit.DB.ElementId新墙使用的墙类型Id,而不是默认类型。
levelId标高id:Autodesk.Revit.DB.ElementId墙被放置在的水平面上的Id。
height高度System.Double墙的高度,而不是默认高度。
offset底部偏移System.Double修改墙的基础偏移量参数以确定其垂直位置。
flip是否翻转System.Boolean改变墙的哪一侧被认为是墙的内部和外部。
structural结构System.Boolean如果设置,则指定该墙在本质上是结构性的。

可以看到墙图元除了指定几何信息和墙的类型信息外,还需要指定标高信息。这是因为,在Revit中由一部分图元是基于标高(Level)的,其他的还有基于视图的二维图元(向文字、详图线和填充等等的注释元素),基于面的图元(如基于墙面的灯),基于宿主的图元(如门,窗和洞口等)。
下面分别列出了标高、轴网和模型线API构造方法,从构造方法中我们可以判断你所创建的图元时是否是否基于标高、面或者其他。随着对API中各个图元的构造方法的熟练掌握,创建图元将变得得心应手。另外,也可以实现在revit中绘制要开发的图元,查看图元创建依赖于哪些信息,来指导API构造器的选择。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace ElementBasicDemo
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class CreateWallCurrentCmd : IExternalCommand
    {
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                //句柄
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                //视图
                View view = null;
                //id
                ElementId wallTypeId = null;
                //XYZ通常这指的是三维空间中的一个点或一个向量,这取决于实际用途。
                XYZ p0 = XYZ.Zero;
                XYZ p1 = XYZ.Zero;

                //视图,过滤器
                view = (View)new FilteredElementCollector(uidoc.Document).OfCategory(BuiltInCategory.OST_Views)
                    .OfClass(typeof(ViewPlan)).WhereElementIsNotElementType().FirstOrDefault();

                //墙类型
                wallTypeId = uidoc.Document.GetDefaultElementTypeId(ElementTypeGroup.WallType);

                //创建墙
                if (null == CreateCurtainWall(uidoc, view, wallTypeId, new XYZ(0, 0, 0), new XYZ(5000 / 304.8, 0, 0)))
                {
                    throw new ArgumentException("视图或墙的类型为空");
                }
                return Autodesk.Revit.UI.Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Autodesk.Revit.UI.Result.Failed;
            }
        }

        private Wall CreateCurtainWall(UIDocument uidoc, View currentView, ElementId wallTypeId, XYZ startXYZ, XYZ endXYZ)
        {
            if (null == wallTypeId || null == currentView)
            {
                return null;
            }

            Autodesk.Revit.Creation.Document createDoc = currentView.Document.Create;
            Autodesk.Revit.Creation.Application createApp = currentView.Document.Application.Create;

            Autodesk.Revit.DB.Line baseline = null;
            try
            {
                baseline = Line.CreateBound(startXYZ, endXYZ);
            }
            catch (System.ArgumentException)
            {
                TaskDialog.Show("创建墙", "墙的起始点距离太近,请重新绘制");
            }

            Transaction act = new Transaction(currentView.Document, "创建墙");
            act.Start();
            Wall wall = Wall.Create(currentView.Document, baseline,wallTypeId,currentView.GenLevel.Id, 2000 / 304.8, 0, false, false);
            act.Commit();
            Transaction act2 = new Transaction(currentView.Document);
            act2.Start(Guid.NewGuid().GetHashCode().ToString());
            uidoc.ShowElements(wall);
            act2.Commit();
            return wall;
        }
    }
}

几何计算

public class FirstWallArea : IExternalCommand
{
    public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        try
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            Selection selection = uidoc.Selection;
            string msg = string.Empty;

            ISelectionFilter wallFilter = new WallSelectionFilter();

            //选集
            Reference eleRef = selection.PickObject(ObjectType.Element, wallFilter);
            if (null == eleRef || ElementId.InvalidElementId == eleRef.ElementId)
            {
                return Result.Cancelled;
            }
            //获取墙
            Element element = doc.GetElement(eleRef);
            Wall wall = element as Wall;
            if (wall == null)
                return Result.Failed;

            Enum structuralUsage = wall.StructuralUsage;
            //得到结果
            List<double> areas = GetArea(wall);
            double volumn = GetVolumn(wall);
            int i = 0;
            foreach (double area in areas)
            {
                i++;
                msg += " 面"+i +" :"+ area;
            }
            msg ="是否为结构:"+ structuralUsage.ToString()+" ElementId:"+ element.Id + " 体积:" +volumn +  msg;
            TaskDialog.Show(this.GetType().Name,msg);

            return Result.Succeeded;
        }
        catch (Exception ex)
        {
            message = ex.Message;
            return Result.Failed;
        }
    }

    /// <summary>
    /// 获取几何元素的面积
    /// </summary>
    /// <param name="element"></param>
    /// <returns>平方米</returns>
    public static List<double> GetArea(Element element)
    {
        List<double> area = new List<double>();
        GeometryElement geometryElement = element.get_Geometry(new Options());
        foreach (GeometryObject geometryObject in geometryElement)
        {
            if (geometryObject is Solid)
            {
                Solid solid = geometryObject as Solid;
                foreach (Face face in solid.Faces)
                {
                    double v = UnitUtils.ConvertFromInternalUnits(face.Area, UnitTypeId.SquareMeters);
                    area.Add(v);
                }
            }
        }
        return area;
    }

    /// <summary>
    /// 获取几何元素的体积
    /// </summary>
    /// <param name="element"></param>
    /// <returns>立方米</returns>
    public static double GetVolumn(Element element)
    {
        double volumn = 0;
        GeometryElement geometryElement = element.get_Geometry(new Options());
        foreach (GeometryObject geometryObject in geometryElement)
        {
            if (geometryObject is Solid)
            {
                Solid solid = geometryObject as Solid;
                volumn += solid.Volume;
            }
        }
        volumn = UnitUtils.ConvertFromInternalUnits(volumn, UnitTypeId.CubicMeters);
        return volumn;
    }


}
public class WallSelectionFilter : ISelectionFilter
{
    /// <summary>
    /// Allows an element to be selected
    /// </summary>
    /// <param name="element">A candidate element in the selection operation.</param>
    // <returns>Return true to allow the user to select this candidate element.</returns>
    public bool AllowElement(Element elem)
    {
        return elem is Wall;
    }

   /// <summary>
   // Allows a reference to be selected.
   /// </summary>
   /// <param name="refer"> A candidate reference in the selection operation.</param>
   /// <param name="point">The 3D position of the mouse on the candidate reference.</param>
   /// <returns>Return true to allow the user to select this candidate reference.</returns>
    public bool AllowReference(Reference reference, XYZ position)
    {
        return false;  //设置为不允许
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤影墨客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值