开洞信息数据:

<?xml version="1.0" encoding="utf-8"?>
<Openings>
  <Opening WallId="618644">
    <PT1 X="-46.4673223917839" Y="25.9434404529416" Z="9.8753280839895" />
    <PT2 X="-46.4673223917839" Y="24.2374037075347" Z="8.16929133858268" />
  </Opening>
  <Opening WallId="618699">
    <PT1 X="28.3358272145153" Y="24.2374037075345" Z="9.8753280839895" />
    <PT2 X="28.3358272145153" Y="25.9434404529413" Z="8.16929133858268" />
  </Opening>
  <Opening WallId="618722">
    <PT1 X="-10.7125018356066" Y="57.0806634229704" Z="9.8753280839895" />
    <PT2 X="-12.4185385810134" Y="57.0806634229704" Z="8.16929133858268" />
  </Opening>
  <Opening WallId="618662">
    <PT1 X="-20.8620650191645" Y="-1.31828670826329" Z="9.8753280839895" />
    <PT2 X="-19.1560282737577" Y="-1.31828670826329" Z="8.16929133858268" />
  </Opening>
</Openings>

start

//根据导出开洞信息开洞
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class cmdOpening : IExternalCommand
{
    public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elements)
    {
        UIDocument uiDoc = cmdData.Application.ActiveUIDocument;
        Document doc = uiDoc.Document;
        Selection sel = uiDoc.Selection;         Transaction ts = new Transaction(doc, "http://revit.5d6d.com");
        ts.Start();         string strPath = @"C:\Users\HongYe\Desktop\OpeningInfo.xml";
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(strPath);         foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
        {
            //;
            Wall wall = doc.get_Element(new ElementId(Convert.ToInt32(node.Attributes["WallId"].Value))) as Wall;
            XmlNode nodePt1 = node.SelectSingleNode("PT1");
            XYZ pt1 = new XYZ(double.Parse(nodePt1.Attributes["X"].Value), double.Parse(nodePt1.Attributes["Y"].Value), double.Parse(nodePt1.Attributes["Z"].Value));
            XmlNode nodePt2 = node.SelectSingleNode("PT2");
            XYZ pt2 = new XYZ(double.Parse(nodePt2.Attributes["X"].Value), double.Parse(nodePt2.Attributes["Y"].Value), double.Parse(nodePt2.Attributes["Z"].Value));
            doc.Create.NewOpening(wall, pt1, pt2);
        }         ts.Commit();         return Result.Succeeded;
    }
}
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
//导出开洞信息
public class cmd : IExternalCommand
{
    /// <summary>
    /// 找到链接文档中与风管相交的墙
    /// </summary>
    /// <param name="doc"></param>
    /// <param name="duct"></param>
    /// <returns></returns>
    public static 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;
    }
    public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elements)
    {
        UIDocument uiDoc = cmdData.Application.ActiveUIDocument;
        UIApplication uiApp = cmdData.Application;
        Document doc = uiDoc.Document;
        Selection selection = uiDoc.Selection;         try
        {
            Transaction ts = new Transaction(doc, "http://revit.5d6d.com");
            ts.Start();             string strPath = @"C:\Users\HongYe\Desktop\OpeningInfo.xml";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(strPath);             List<string> listPath = GetLinkFilePaths(doc);
            Document docLink = uiApp.Application.OpenDocumentFile(listPath[]);
            //
            FilteredElementCollector collIns = new FilteredElementCollector(doc);//
            collIns.OfClass(typeof(Instance)).OfCategory(BuiltInCategory.OST_RvtLinks);//RevitLinkType
            Transform transForm = null;
            foreach (Element elDoc in collIns)
            {
                Instance ins = elDoc as Instance;
                if (ins != null)
                {
                    transForm = ins.GetTransform();
                }
            }
            //风管
            FilteredElementCollector collDuct = new FilteredElementCollector(doc);
            collDuct.OfClass(typeof(Duct)).OfCategory(BuiltInCategory.OST_DuctCurves);
            foreach (Element elDuct in collDuct)
            {
                Duct duct = elDuct as Duct;
                List<Wall> listWall = FindDuctWall(docLink, duct);//找到链接文档中与风管相交的墙
                foreach (Wall wall in listWall)
                {
                    //求面和线的交点
                    Face face = FindWallFace(wall);
                    Curve curve = FindDuctCurve(duct);
                    XYZ xyz = FindFaceCurve(face, curve);
                    //墙线的向量
                    XYZ wallVector = FindWallVector(wall);                     //找到风管的宽和高
                    double dWidth = GetDuctWidth(duct);
                    double dHeigh = GetDuctHeight(duct);                     int iUp = ;
                    int iDown = ;
                    int iLeft = ;
                    int iRight = ;
                    //左上角
                    XYZ pt1 = xyz + new XYZ(, , ) * (dHeigh /  + iUp) / 304.8;
                    pt1 = pt1 - wallVector.Normalize() * (dWidth /  + iLeft) / 304.8;
                    pt1 = pt1 - transForm.Origin;
                    //右下角
                    XYZ pt2 = xyz + new XYZ(, , -) * (dHeigh /  + iDown) / 304.8;
                    pt2 = pt2 + wallVector.Normalize() * (dWidth /  + iRight) / 304.8;
                    pt2 = pt2 - transForm.Origin;
                    //开洞
                    //doc.Create.NewOpening(wall, pt1, pt2);
                    //wall.Id.IntegerValue.ToString();
                    //pt1.X;
                    XmlElement xmlEl = xmlDoc.CreateElement("Opening");
                    xmlEl.SetAttribute("WallId", wall.Id.IntegerValue.ToString());
                    XmlElement elPt1 = xmlDoc.CreateElement("PT1");
                    elPt1.SetAttribute("X", pt1.X.ToString());
                    elPt1.SetAttribute("Y", pt1.Y.ToString());
                    elPt1.SetAttribute("Z", pt1.Z.ToString());
                    xmlEl.AppendChild(elPt1);
                    XmlElement elPt2 = xmlDoc.CreateElement("PT2");
                    elPt2.SetAttribute("X", pt2.X.ToString());
                    elPt2.SetAttribute("Y", pt2.Y.ToString());
                    elPt2.SetAttribute("Z", pt2.Z.ToString());
                    xmlEl.AppendChild(elPt2);
                    xmlDoc.DocumentElement.AppendChild(xmlEl);
                }
            }             ts.Commit();
            xmlDoc.Save(strPath);
        }
        catch (Exception ex)
        {
            WinFormTools.MsgBox(ex.ToString());
        }         return Result.Succeeded;
    }
    //找到风管宽度
    public static double GetDuctWidth(Duct duct)
    {
        double dWidth = ;
        foreach (Parameter p in duct.Parameters)
        {
            if (p.Definition.Name == "宽度")
            {
                dWidth = double.Parse(p.AsValueString());
                break;
            }
        }
        return dWidth;
    }
    //找到风管高度
    public static double GetDuctHeight(Duct duct)
    {
        double dHeigh = ;
        foreach (Parameter p in duct.Parameters)
        {
            if (p.Definition.Name == "高度")
            {
                dHeigh = double.Parse(p.AsValueString());
                break;
            }
        }
        return dHeigh;
    }
    /// <summary>
    /// 找到墙线的向量
    /// </summary>
    /// <param name="wall"></param>
    /// <returns></returns>
    public static XYZ FindWallVector(Wall wall)
    {
        LocationCurve lCurve = wall.Location as LocationCurve;
        XYZ xyz = lCurve.Curve.get_EndPoint() - lCurve.Curve.get_EndPoint();
        return xyz;
    }
    /// <summary>
    /// 求线和面的交点
    /// </summary>
    /// <param name="face"></param>
    /// <param name="curve"></param>
    /// <returns></returns>
    public static 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;
    }
    /// <summary>
    /// 找到风管对应的曲线
    /// </summary>
    /// <param name="duct"></param>
    /// <returns></returns>
    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(), list.ElementAt()) as Curve;
        curve.MakeUnbound();
        return curve;
    }
    /// <summary>
    /// 找到墙的正面
    /// </summary>
    /// <param name="wall"></param>
    /// <returns></returns>
    public 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);
        /*下版改进
        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 > )
            {
                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;
    }
    /// <summary>
    /// 取得链接文件路径
    /// </summary>
    /// <param name="doc"></param>
    /// <returns></returns>
    public List<string> GetLinkFilePaths(Document doc)
    {
        List<string> listPath = new List<string>();
        foreach (ElementId elId in ExternalFileUtils.GetAllExternalFileReferences(doc))
        {
            if (doc.get_Element(elId).IsExternalFileReference())
            {
                ExternalFileReference fileRef = doc.get_Element(elId).GetExternalFileReference();
                if (ExternalFileReferenceType.RevitLink == fileRef.ExternalFileReferenceType)
                    listPath.Add(ModelPathUtils.ConvertModelPathToUserVisiblePath(fileRef.GetAbsolutePath()));
            }
        }
        return listPath;
    }
    /// <summary>
    /// 根据链接文件名称找到对应链接路径,模糊匹配,待改进
    /// </summary>
    /// <param name="listPath"></param>
    /// <param name="strKey"></param>
    /// <returns></returns>
    private string GetPath(List<string> listPath, string strKey)
    {
        foreach (string strPath in listPath)
        {
            if (strPath.Contains(strKey))
                return strPath;
        }
        return "";
    }
}

url:http://greatverve.cnblogs.com/p/revit-api-opening-info.html

Revit API根据链接文件开洞的更多相关文章

  1. Revit API修改链接文件房间边界

    start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] );//设置房间边界     ...

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

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

  3. Expo大作战(三十八)--expo sdk api之 FileSystem(文件操作系统)

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  4. Revit API 加载族并生成实例图元

    在Revit API中加载族可以使用Doc.LoadFamily方法,传入要加载的族文件路径名,但是这种方式有一种缺点,就是如果族文件在当前工程中没有加载的话则返回成功,如果已经加载过,则返回失败,也 ...

  5. 纯前端下载pdf链接文件,而不是打开预览的解决方案

    纯前端下载pdf链接文件,而不是打开预览的解决方案 一,介绍与需求 1.1,介绍 XMLHttpRequest 用于在后台与服务器交换数据.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行 ...

  6. 详解:基于WEB API实现批量文件由一个服务器同步快速传输到其它多个服务器功能

    文件同步传输工具比较多,传输的方式也比较多,比如:FTP.共享.HTTP等,我这里要讲的就是基于HTTP协议的WEB API实现批量文件由一个服务器同步快速传输到其它多个服务器这样的一个工具(简称:一 ...

  7. 复制”链接文件“到虚拟机(VirtualBox)的”共享文件夹“时报错:创建符号链接时报错:只读文件系统

    问题描述: 1.Ubuntu 中的 /www/目录,是宿主主机 Windows 7 以“共享文件夹”的形式挂载的: 2./etc/php.ini 是 /opt/software/php/etc/php ...

  8. visual studio 添加链接文件

    本文转载http://blog.163.com/zhongpenghua@yeah/blog/static/87727415201282432345613/   那个有个箭头的文件就是链接文件了,添加 ...

  9. Windows系统创建硬链接文件

    源文件夹:E:\深海 创建新硬链接文件夹:D:\微云同步盘\719179409\4-工作资料\深海   打开命令提示符(管理员) 敲入以下命令:   创建成功后,进入目录 D:\微云同步盘\71917 ...

随机推荐

  1. ajax返回json对象的两种写法

    1. 前言 dataType: 要求为String类型的参数,预期服务器返回的数据类型.如果不指定,JQuery将自动根据http包mime信息返回responseXML或responseText,并 ...

  2. 简单对比 Libevent、libev、libuv

    Libevent.libev.libuv三个网络库,都是c语言实现的异步事件库Asynchronousevent library). 异步事件库本质上是提供异步事件通知(Asynchronous Ev ...

  3. 红包外挂史及AccessibilityService分析与防御

    最近在做一个有趣的外挂的小玩意,前提我们要了解一个重要的类AccessibilityService 转载请注明出处:https://lizhaoxuan.github.io 前言 提起Accessib ...

  4. oracle中REGEXP_SUBSTR方法的使用

    近期在做商旅机票平台,遇到这样一个问题: 有一张tt_ticket表,用来存机票信息.里边有一个字段叫schedule,表示的是行程,存储格式为:北京/虹桥 由于公司位于上海.而上海眼下有两个机场:浦 ...

  5. [工具/PC]计算机中丢失libiconv-2.dll,丢失libintl-8.dll,无法定位程序输入点libiconv于动态链接库libiconv-2.dll上问题解决方法

    CodeBlocks 1. 背景,为了学习C语言,在win系统上下载了codeBlock,先简单介绍下:Code::Blocks 是一个开放源码的全功能的跨平台C/C++集成开发环境. Code::B ...

  6. JQuery 插件一般方法

    如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论下jq ...

  7. MySQL 由 5.7 升级为 8.0 之后,Laravel 的配置改动

    开发机上升级了 MySQL 8.0, 原有的 Laravel 5.5 项目就启动失败了. 报错信息是: [2018-05-30 11:17:37] local.ERROR: SQLSTATE[4200 ...

  8. Laravel框架中Blade模板的用法

    1. 继承.片段.占位.组件.插槽 1.1 继承 1.定义父模板 Laravel/resources/views/base.blade.php 2.子模板继承 @extends('base') 1.2 ...

  9. hdu1540

    怎么会T啊 /* 三种操作:D x:第x个位置1 Q x:查询第x位置所在的0连续块 R :将上次D的位置置0 */ #include<iostream> #include<algo ...

  10. python 全栈开发,Day110(django ModelForm,客户管理之 编辑权限(一))

    昨日内容回顾 1. 简述权限管理的实现原理. 粒度控制到按钮级别的权限控制 - 用户登陆成功之后,将权限和菜单信息放入session - 每次请求时,在中间件中做权限校验 - inclusion_ta ...