涉及向量计算,求相交等相关技术。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using WinForm = System.Windows.Forms; using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes; using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Architecture; using System.Xml;
using SelSet = HongYe.Revit.Public.SelectSet; namespace RevitCodes
{
    //找洞口
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class cmdWallOpening : IExternalCommand
    {
        public Result Execute(ExternalCommandData cmdData, ref string messages, ElementSet elements)
        {
            UIApplication uiApp = cmdData.Application;
            Document doc = uiApp.ActiveUIDocument.Document;
            Selection sel = uiApp.ActiveUIDocument.Selection;             Transaction ts = new Transaction(doc, "revit.5d6d.com");
            ts.Start();
            /*
            //选择一面墙
            WallSelectionFilter fWall = new WallSelectionFilter();
            Reference ref1 = uiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element, fWall, "选择一面墙:");
            Element elem1 = doc.GetElement(ref1);
            Wall wall = elem1 as Wall;
            //选择一个风管
            DuctSelectionFilter fDuct = new DuctSelectionFilter();
            Reference ref2 = uiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element, fDuct, "选择一个风管:");
            Element elem2 = doc.GetElement(ref2);
            Duct duct = elem2 as Duct;
             */ 
            //开同心洞
            //CenterOpen(doc, wall, duct, 640, 640);
            List<Duct> listDuct = FindAllDuct(doc);
            foreach (Duct duct in listDuct)
            {
                List<Wall> listWall = FindDuctWall(doc, duct);
                foreach (Wall wall in listWall)
                {
                    CenterOpen(doc, wall, duct, , );
                }
            }             ts.Commit();             return Result.Succeeded;
        }
        //找到所有风管
        List<Duct> FindAllDuct(Document doc)
        {
            List<Duct> listDuct = new List<Duct>();
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            collector.OfClass(typeof(Duct)).OfCategory(BuiltInCategory.OST_DuctCurves);             foreach (Element el in collector)
            {
                Duct duct = el as Duct;
                if (duct != null)
                    listDuct.Add(duct);
            }
            return listDuct;
        }
        //找到与风管相交的墙
        List<Wall> FindDuctWall(Document doc, Duct duct)
        {
            List<Wall> listWall = new List<Wall>();
            //找到outLine
            BoundingBoxXYZ bb = duct.get_BoundingBox(doc.ActiveView);
            Outline outline = new Outline(bb.Min, bb.Max);
            //
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            BoundingBoxIntersectsFilter invertFilter = new BoundingBoxIntersectsFilter(outline, false);
            IList<Element> noIntersectWalls = collector.OfClass(typeof(Wall)).WherePasses(invertFilter).ToElements();
            foreach (Element el in noIntersectWalls)
            {
                Wall wall = el as Wall;
                if (wall != null)
                    listWall.Add(wall);
            }
            return listWall;
        }
        //开同心洞
        Result CenterOpen(Document doc, Wall wall, Duct duct, double dWidth, double dHeigh)
        {
            SubTransaction subTs = new SubTransaction(doc);
            subTs.Start();
            try
            {
                //求面和线的交点
                Face face = FindWallFace(wall);
                Curve curve = FindDuctCurve(duct);
                XYZ xyz = FindFaceCurve(face, curve);
                //墙线的向量
                XYZ wallVector = FindWallVector(wall);
                //交点向上向墙线正方向移动160(风管宽高320)
                XYZ pt1 = xyz + new XYZ(, , ) * dHeigh /  / 304.8;
                pt1 = pt1 + wallVector.Normalize() * dWidth /  / 304.8;
                //交点向下向墙线反方向移动160(风管宽高320)
                XYZ pt2 = xyz + new XYZ(, , -) * dHeigh /  / 304.8;
                pt2 = pt2 - wallVector.Normalize() * dWidth /  / 304.8;
                //开洞
                doc.Create.NewOpening(wall, pt1, pt2);                 subTs.Commit();
                return Result.Succeeded;
            }
            catch
            {
                subTs.RollBack();
                return Result.Failed;
            }
        }
        //找到墙线的向量
        XYZ FindWallVector(Wall wall)
        {
            LocationCurve lCurve = wall.Location as LocationCurve;
            XYZ xyz = lCurve.Curve.get_EndPoint() - lCurve.Curve.get_EndPoint();
            return xyz;
        }
        //求线和面的交点
        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().XYZPoint;
                }
            }
            return intersectionResult;
        }
        //找到风管对应的曲线
        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(), list.ElementAt()) as Curve;
            return curve;
        }
        //找到墙的正面
        Face FindWallFace(Wall wall)
        {
            Face normalFace = null;
            //
            Options opt = new Options();
            opt.ComputeReferences = true;
            opt.DetailLevel = Autodesk.Revit.DB.DetailLevels.Medium;
            //
            GeometryElement e = wall.get_Geometry(opt);
            foreach (GeometryObject obj in e.Objects)
            {
                Solid solid = obj as Solid;
                if (solid != null && solid.Faces.Size > )
                {
                    foreach (Face face in solid.Faces)
                    {
                        PlanarFace pf = face as PlanarFace;
                        if (pf != null)
                        {
                            if (pf.Normal.AngleTo(wall.Orientation) < 0.01)//数值在0到PI之间
                            {
                                normalFace = face;
                            }
                        }
                    }
                }
            }
            return normalFace;
        }
    }
    //墙的过滤条件
    class WallSelectionFilter : ISelectionFilter
    {
        public bool AllowElement(Element e)
        {
            return e is Wall;
        }         public bool AllowReference(Reference r, XYZ p)
        {
            return true;
        }
    }
    //风管的过滤条件
    class DuctSelectionFilter : ISelectionFilter
    {
        public bool AllowElement(Element e)
        {
            return e is Duct;
        }         public bool AllowReference(Reference r, XYZ p)
        {
            return true;
        }
    }
}

url:http://greatverve.cnblogs.com/p/api-duct-wall-opening.html

Revit API遍历全部风管,找到与风管相关的墙开洞的更多相关文章

  1. Revit API通过相交过滤器找到与风管相交的对象。

    相交过滤器的应用,比几何相交法简便.Excluding剔除 //找到与风管相交的对象,通过相交过滤器. [TransactionAttribute(Autodesk.Revit.Attributes. ...

  2. Revit API判断直线相交关系移动风管

    start )             );         )) )) );         XYZ xyz12 = lCurve1.Curve.get_EndPoint();         XY ...

  3. Revit API遍历系统族布置喷头

    系统族可以通过内参遍历,遍历出来是个FamilySymbol喷头属于系统族,但不能通过NewDuct();类似这样的方法布置.必须使用 NewFamilyInstance() );           ...

  4. Revit API找到风管穿过的墙(当前文档和链接文档)

    start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class c ...

  5. Revit API画垂直于风管的风管

    start /// <summary> /// 选择风管与风管外一点,画与风管垂直的风管. /// </summary> [Transaction(TransactionMod ...

  6. 【Revit API】梁构件支座检查算法

    一.前言         应该是第二次写关于Revit API的博文了.虽然在BIM企业中工作,从事桌面BIM软件开发,但是我是不怎么喜欢写Revit API相关的代码.平时更多的是在写界面展示,架构 ...

  7. Revit API 判断一个构件在某个视图中的可见性

    查看 Revit API.发现有Element::IsHidden这个方法.通过UI创建一个element,注意要使得这个element在某些视图可见,但是在另一些视图不可见.运行下面的方法,你会发现 ...

  8. Revit API 操作共享参数和项目参数

    1.获取共享参数 private string GetSharInfo(Autodesk.Revit.ApplicationServices.Application revitApp) { Strin ...

  9. revit API 生成墙图元

    由于Revit的版本问题,在网上找的生成墙图元的代码,在我机器上的Revit 2016中编译不能通过,通过多次调试,终于找到在revit 2016中使用API生成墙图元的代码,现在贴出来. 下面的代码 ...

随机推荐

  1. 升级openssh到最新版本

    首先,下载最新版本,传到服务器:http://mirror.aarnet.edu.au/pub/OpenBSD/OpenSSH/portable/ 安装 cd /root/ mkdir ssh_upg ...

  2. android休眠唤醒驱动流程分析【转】

    转自:http://blog.csdn.net/hanmengaidudu/article/details/11777501 标准linux休眠过程: l        power managemen ...

  3. LINUX下 USB转串口 【转】

    转自:http://blog.163.com/smilexiao_11015461/blog/static/2122052182012102410399459/ 1.将设备u口插入pc2.输入#lsm ...

  4. Long-Polling, Websockets, SSE(Server-Sent Event) 之间有什么区别?

    链接:http://www.mamicode.com/info-detail-1327667.html https://www.jianshu.com/p/d3f66b1eb748?from=time ...

  5. 织梦任意页面调用{dede:field.content/}的方法

    过滤掉所有的html代码,只显示文字,具体的ID自己更改. 代码如下: {dede:sql sql='Select content from dede_arctype where id=1'} [fi ...

  6. Tomcat启动默认访问项目

    一般有两种可以实现:推荐使用这一种.更灵活 一般项目的编译项目都在Tomcat的webapps下,项目的访问路径一般为:http://localhost:8080/项目虚拟路径.但是Tomcat的默认 ...

  7. react 的JSX语法需要注意哪些点?

    注释方式 ReactDOM.render( <div> {/*JSX 中的注释方式*/} </div>, document.getElementById('root') ) j ...

  8. 【BZOJ】1294: [SCOI2009]围豆豆Bean

    题解 随机跳题真好玩 这个就是考虑我们怎么判断点在多边形内,就是点做一条射线,穿过了奇数条边 我们只需要记录一个二进制状态表示每个点的射线穿过路径的次数的奇偶性 枚举起点,然后用BFS的方式更新dp状 ...

  9. Codeforces 1088E Ehab and a component choosing problem

    Ehab and a component choosing problem 如果有多个连接件那么这几个连接件一定是一样大的, 所以我们先找到值最大的连通块这个肯定是分数的答案. dp[ i ]表示对于 ...

  10. 少走弯路,一个老程序猿谈PHP职业发展规划

    PHP是一个使用者非常多的开发语言,但在每个领域里的开发侧重点有所不同互联网方面,在稳定的基础上,更注重性能.高并发,高负载的处理. PHP职业发展规划一般有三条路线,一条技能专精发展路线.另两条,是 ...