獲取洞口邊界及在牆上開洞(Revit二次開發)

Code Flying發表於2020-09-24

一、洞口類圖

Opening
element
BoundaryCurves,BoundaryRect,Host,IsRectBoundary,IsTransparentIn3D,IsTransparentInElevation

二、獲取洞口邊界

如果是長方形邊界,使用BoundaryRect屬性,其他形狀邊界使用BoundaryCurves屬性。
方法示例:

private void GetBoundary(Opening opening)  
{  
    if (opening.IsRectBoundary)  
    {  
        XYZ startPoint = opening.BoundaryRect[0];  
        XYZ endPoint = opening.BoundaryRect[1];  
    }  
    else {  
        foreach (Curve curve in opening.BoundaryCurves)  
        {  
            //遍歷Curve   
        }  
    }  
}  

三、其他屬性

  1. Host屬性可以獲得洞口的宿主元素。
  2. IsTransparentIn3D屬性返回是否在三維檢視透明,只能在族文件裡使用。
  3. IsTransparentlnElevation屬性返回是否在裡面檢視透明,只能在族文件裡使用。

四、建立洞口

API方法: NewOpening( Wall wall,XYZ pntStart, XYZ pntEnd),可以在牆上建立洞口,引數意義如下:
• wall:牆物件。
• pntStart:長方形的一個頂點。
• pntEnd:長方形的另一個對角的頂點。

在牆上開洞方法示例:

private void CreateOpening(Document RevitDoc, Wall wall)
        {
            LocationCurve locationCurve = wall.Location as LocationCurve;
            Line location = locationCurve.Curve as Line;
            XYZ startPoint = location.GetEndPoint(0);
            XYZ endPoint = location.GetEndPoint(1);
            Parameter wallHeightParameter = wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM);
            double wallHeight = wallHeightParameter.AsDouble();
            XYZ delta = (endPoint - startPoint + new XYZ(0, 0, wallHeight)) / 3;
            using (Transaction transaction = new Transaction(RevitDoc, "在牆上開洞"))
            {
                transaction.Start();
                Opening opening = RevitDoc.Create.NewOpening(wall, startPoint + delta, startPoint + delta * 2);
                transaction.Commit();
            }
        }
        

主程式呼叫:

Reference pickedRef = null;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;
            UIApplication uiApp = commandData.Application;
            Selection sel = uiApp.ActiveUIDocument.Selection;
            pickedRef = sel.PickObject(ObjectType.Element, "選擇一個族例項");
            Element elem1 = doc.GetElement(pickedRef);
            Wall wall1 = elem1 as Wall;

            try
            {
                CreateOpening(doc, wall1);
            }
            catch
            {
                
            }


            return Result.Succeeded;
        }

建立結果:
在這裡插入圖片描述

相關文章