Revit API 楼板开洞

start
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public  class cmdFloorOpening : IExternalCommand
{
     public Result Execute(ExternalCommandData cmdData,  ref  string msg, ElementSet elements)
    {
        UIDocument uiDoc = cmdData.Application.ActiveUIDocument;
        Document doc = uiDoc.Document;
        Selection sel = uiDoc.Selection;
        Autodesk.Revit.Creation.Application aCreate = cmdData.Application.Application.Create;

        Transaction ts =  new Transaction(doc,  " www ");
        ts.Start();
         // 楼板
        Reference refFloor = sel.PickObject(ObjectType.Element,  " floor ");
        Floor floor = doc.GetElement(refFloor)  as Floor;
        Face face = FindFloorFace(floor);
        CurveArray curves = aCreate.NewCurveArray();
         // 风管
        Reference refDuct = sel.PickObject(ObjectType.Element,  " duct ");
        Duct duct = doc.GetElement(refDuct)  as Duct;
        Curve curve = FindDuctCurve(duct);
         // 求交点
        XYZ xyz = FindFaceCurve(face, curve);
         /* 开矩形洞 */
        XYZ xyz1 = xyz +  new XYZ( 110) *  200 /  304.8;
        XYZ xyz2 = xyz +  new XYZ( 1, - 10) *  200 /  304.8;
        XYZ xyz3 = xyz +  new XYZ(- 1, - 10) *  200 /  304.8;
        XYZ xyz4 = xyz +  new XYZ(- 110) *  200 /  304.8;
        Curve c1 = aCreate.NewLine(xyz1, xyz2,  true);
        Curve c2 = aCreate.NewLine(xyz2, xyz3,  true);
        Curve c3 = aCreate.NewLine(xyz3, xyz4,  true);
        Curve c4 = aCreate.NewLine(xyz4, xyz1,  true);
        curves.Append(c1);
        curves.Append(c2);
        curves.Append(c3);
        curves.Append(c4);

         /* 开圆形洞
        double startAngle = 0;
        double midAngle = Math.PI;
        double endAngle = 2 * Math.PI;

        XYZ xAxis = XYZ.BasisX;
        XYZ yAxis = XYZ.BasisY;
        double radius = 180 / 304.8;

        Arc arc1 = aCreate.NewArc(xyz, radius, startAngle, midAngle, xAxis, yAxis);
        Arc arc2 = aCreate.NewArc(xyz, radius, midAngle, endAngle, xAxis, yAxis);

        curves.Append(arc1);
        curves.Append(arc2);
*/

        doc.Create.NewOpening(floor, curves,  true);

        ts.Commit();

         return Result.Succeeded;
    }
     // 求线和面的交点
     public XYZ FindFaceCurve(Face face, Curve curve)
    {
         // 求交点
        IntersectionResultArray intersectionR =  new IntersectionResultArray(); // 交点集合
        SetComparisonResult comparisonR; // Comparison比较
        comparisonR = face.Intersect(curve,  out intersectionR);
        XYZ intersectionResult =  null; // 交点坐标
         if (SetComparisonResult.Disjoint != comparisonR) // Disjoint不交
        {
             if (!intersectionR.IsEmpty)
            {
                intersectionResult = intersectionR.get_Item( 0).XYZPoint;
            }
        }
         return intersectionResult;
    }
     // 找到风管对应的曲线
     public Curve FindDuctCurve(Duct duct)
    {
         // 得到风管曲线
        IList<XYZ> list =  new List<XYZ>();
        ConnectorSetIterator csi = duct.ConnectorManager.Connectors.ForwardIterator();
         while (csi.MoveNext())
        {
            Connector conn = csi.Current  as Connector;
            list.Add(conn.Origin);
        }
        Curve curve = Line.get_Bound(list.ElementAt( 0), list.ElementAt( 1))  as Curve;
        curve.MakeUnbound();
         return curve;
    }
     // 找到楼板的面
     public Face FindFloorFace(Floor floor)
    {
        Face normalFace =  null;
         //
        Options opt =  new Options();
        opt.ComputeReferences =  true;
        opt.DetailLevel = Autodesk.Revit.DB.DetailLevels.Medium;
         //
        GeometryElement e = floor.get_Geometry(opt);
         /* 下版改进
        IEnumerator<GeometryObject> enm = e.GetEnumerator();
        while (enm.MoveNext())
        {
            Solid solid = enm.Current as Solid;
        }
*/
         foreach (GeometryObject obj  in e.Objects) // 待改2013
        {
            Solid solid = obj  as Solid;
             if (solid !=  null && solid.Faces.Size >  0)
            {
                 foreach (Face face  in solid.Faces)
                {
                    PlanarFace pf = face  as PlanarFace;
                     if (pf !=  null)
                    {
                         if (pf.Normal.AngleTo( new XYZ( 00, - 1)) <  0.01) // 数值在0到PI之间
                        {
                            normalFace = face;
                        }
                    }
                }
            }
        }
         return normalFace;
    }
}

url: http://greatverve.cnblogs.com/p/revit-api-FloorOpening.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值