start

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;
using Autodesk.Revit.DB.Electrical;
using System.Diagnostics;
using System.Security.Cryptography;
using System.IO; namespace RevitCodes
{
    /// <summary>
    /// 工具类
    /// </summary>
    public class RevitTool
    {
        /// <summary>
        /// 管件是否连接风管,如果什么也没连接也返回true
        /// </summary>
        /// <returns></returns>
        public static bool IsConnectToDuct(FamilyInstance fi)
        {
            bool bResult = true;
            ConnectorSetIterator csi = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
            while (csi.MoveNext())
            {
                Connector conn = csi.Current as Connector;
                if (conn.IsConnected == true)//是否有连接
                {
                    ConnectorSet connectorSet = conn.AllRefs;//找到所有连接器连接的连接器
                    ConnectorSetIterator csiChild = connectorSet.ForwardIterator();
                    while (csiChild.MoveNext())
                    {
                        Connector connected = csiChild.Current as Connector;
                        if (null != connected && connected.Owner.UniqueId != conn.Owner.UniqueId)
                        {
                            //TaskDialog.Show("conn", conn.Width + "|" + connected.Width + ",");
                            // look for physical connections 
                            if (connected.ConnectorType == ConnectorType.End ||
                                connected.ConnectorType == ConnectorType.Curve ||
                                connected.ConnectorType == ConnectorType.Physical)
                            {
                                //判断是不是管件,只要有一头连接的不是风管就返回false
                                if (connected.Owner is Duct)
                                {
                                    Duct duct = connected.Owner as Duct;
                                    Parameter pWidth = duct.get_Parameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
                                    Parameter pHeight = duct.get_Parameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
                                    //TaskDialog.Show("width", pWidth.AsValueString());
                                }
                                else
                                {
                                    bResult = false;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            return bResult;
        }
        /// <summary>
        /// 改变风管尺寸
        /// </summary>
        /// <param name="duct"></param>
        /// <param name="sWidth"></param>
        /// <param name="sHeight"></param>
        public static void ChangeDuctSize(Duct duct, string sWidth, string sHeight)
        {
            Parameter pWidth = duct.get_Parameter(BuiltInParameter.RBS_CURVE_WIDTH_PARAM);
            Parameter pHeight = duct.get_Parameter(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM);
            pWidth.SetValueString(sWidth);
            pHeight.SetValueString(sHeight);
        }
        /// <summary>
        /// 改变管件尺寸
        /// </summary>
        /// <param name="fi"></param>
        /// <param name="dWidth"></param>
        /// <param name="dHeight"></param>
        public static void ChangeFittingSize(FamilyInstance fi, double dWidth, double dHeight)
        {
            ConnectorSetIterator csi = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
            while (csi.MoveNext())
            {
                Connector conn = csi.Current as Connector;
                conn.Width = dWidth / 304.8;
                conn.Height = dHeight / 304.8;
            }
        }         /// <summary>
        /// 是否需要变换
        /// </summary>
        /// <param name="fi"></param>
        /// <returns></returns>
        public static bool isChange(FamilyInstance fi)
        {
            bool bChange = true;//是否需要变换宽高
            XYZ xyzFace = GetFittingSolid(fi).Normal;
            //正面朝上或者朝下,不需要变换
            if (xyzFace.AngleTo(new XYZ(, , )) < 0.001 || xyzFace.AngleTo(new XYZ(, , -)) < 0.001)
            {
                bChange = false;
            }
            return bChange;
        }
        /// <summary>
        /// 取得管件的几何体的方向面,管件族的几何体,有三个。
        /// </summary>
        /// <param name="fi"></param>
        /// <returns></returns>
        public static PlanarFace GetFittingSolid(FamilyInstance fi)
        {
            //得到管件尺寸
            double dWidth = , dHeight = ;
            ConnectorSetIterator csi = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
            while (csi.MoveNext())
            {
                Connector conn = csi.Current as Connector;
                dWidth = conn.Width;
                dHeight = conn.Height;
            }
            //
            PlanarFace resultFace = null;
            Solid resultSolid = null;
            //
            Options opt = new Options();
            opt.ComputeReferences = true;
            opt.DetailLevel = Autodesk.Revit.DB.DetailLevels.Medium;
            //
            GeometryElement e = fi.get_Geometry(opt);
            foreach (GeometryObject obj in e.Objects)
            {
                GeometryInstance gi = obj as GeometryInstance;
                if (gi != null)
                {
                    GeometryElement ge = gi.GetInstanceGeometry();
                    foreach (GeometryObject go in ge.Objects)
                    {
                        Solid solid = go as Solid;
                        if (solid != null && solid.Faces.Size > )
                        {
                            bool isSolid = true;
                            foreach (Face face in solid.Faces)
                            {
                                PlanarFace pf = face as PlanarFace;
                                if (pf != null)
                                {
                                    if (pf.Area < 0.02)//如果有一个面积特别小,这个截面体。角度为1度也大于0.02
                                    {
                                        isSolid = false;
                                    }
                                }
                            }
                            if (isSolid)
                            {
                                resultSolid = solid;
                            }
                        }
                    }
                }
            }
            //找到截面
            double dArea = dWidth * dHeight;
            PlanarFace sectionFace = null;
            foreach (Face face in resultSolid.Faces)
            {
                if (Math.Abs(face.Area - dArea) < 0.001)//面积近似
                {
                    sectionFace = face as PlanarFace;
                    break;
                }
            }
            //找到与截面垂直的管件正面
            foreach (Face face in resultSolid.Faces)
            {
                PlanarFace pFace = face as PlanarFace;
                if (pFace != null)
                {
                    //面积相同的排除
                    if (Math.Abs(face.Area - dArea) < 0.001)//面积近似
                    {
                        continue;
                    }
                    else
                    {
                        double dAngle = pFace.Normal.AngleTo(sectionFace.Normal);
                        if (Math.Abs(dAngle - Math.PI / ) < 0.001)//与截面垂直的面
                        {
                            resultFace = pFace;
                        }
                    }
                }
            }
            //
            return resultFace;
        }         /// <summary>
        /// 从框选元素中过滤出风管
        /// </summary>
        /// <param name="listElement"></param>
        /// <returns></returns>
        public static List<Duct> GetDuctsFromElements(IList<Element> listElement)
        {
            List<Duct> listDuct = new List<Duct>();
            foreach (Element el in listElement)
            {
                if (el is Duct)
                {
                    Duct duct = el as Duct;
                    listDuct.Add(duct);
                }
            }
            return listDuct;
        }
        /// <summary>
        /// 从框选元素中过滤出管件
        /// </summary>
        /// <param name="listElement"></param>
        /// <returns></returns>
        public static List<FamilyInstance> GetFittingsFromElements(IList<Element> listElement)
        {
            List<FamilyInstance> listFitting = new List<FamilyInstance>();
            foreach (Element el in listElement)
            {
                if (el is FamilyInstance)
                {
                    FamilyInstance fi = el as FamilyInstance;
                    listFitting.Add(fi);
                }
            }
            return listFitting;
        }
    }
    //改变尺寸方法一
    [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class cmdChangeSize1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
        {
            UIApplication app = commandData.Application;
            Document doc = app.ActiveUIDocument.Document;
            Selection sel = app.ActiveUIDocument.Selection;             Transaction ts = new Transaction(doc, "hongye");
            //
            double dWidth = ;
            double dHeight = ;
            IList<Element> listElement = sel.PickElementsByRectangle("框选一个弯头及相连的两根风管");
            List<Duct> listDuct = RevitTool.GetDuctsFromElements(listElement);
            List<FamilyInstance> listFi = RevitTool.GetFittingsFromElements(listElement);
            //第一步,改变管件尺寸,改变后,肯定会增加变径。
            ts.Start();
            foreach (FamilyInstance fi in listFi)
            {
                RevitTool.ChangeFittingSize(fi, dWidth, dHeight);
            }
            ts.Commit();
            //第二步,改变风管尺寸,如果不需要变换,变径会自动消失。
            ts.Start();
            foreach (Duct duct in listDuct)
            {
                RevitTool.ChangeDuctSize(duct, dWidth.ToString(), dHeight.ToString());
            }
            ts.Commit();
            //第三步,判断风管与管件之间是否仍然存在变径,如果存在则宽度互换,再次改变风管尺寸。
            ts.Start();
            foreach (FamilyInstance fi in listFi)
            {
                if (!RevitTool.IsConnectToDuct(fi))//如果连接的是变径
                {
                    foreach (Duct duct in listDuct)
                    {
                        RevitTool.ChangeDuctSize(duct, dHeight.ToString(), dWidth.ToString());
                    }
                }
            }
            ts.Commit();
            //////pf.Normal.AngleTo             return Result.Succeeded;
        }
    }     //改变尺寸方法二
    [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class cmdChangeSize2 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
        {
            UIApplication app = commandData.Application;
            Document doc = app.ActiveUIDocument.Document;
            Selection sel = app.ActiveUIDocument.Selection;             Transaction ts = new Transaction(doc, "hongye");
            ts.Start();
            //
            double dWidth = ;
            double dHeight = ;
            IList<Element> listElement = sel.PickElementsByRectangle("框选一个弯头及相连的两根风管");
            List<Duct> listDuct = RevitTool.GetDuctsFromElements(listElement);
            List<FamilyInstance> listFi = RevitTool.GetFittingsFromElements(listElement);
            //
            foreach (FamilyInstance fi in listFi)
            {
                RevitTool.ChangeFittingSize(fi, dWidth, dHeight);
                if (RevitTool.isChange(fi))
                {
                    foreach (Duct duct in listDuct)
                    {
                        RevitTool.ChangeDuctSize(duct, dHeight.ToString(), dWidth.ToString());
                    }
                }
                else
                {
                    foreach (Duct duct in listDuct)
                    {
                        RevitTool.ChangeDuctSize(duct, dWidth.ToString(), dHeight.ToString());
                    }
                }
            }
            ts.Commit();             return Result.Succeeded;
        }     }
}

url:http://greatverve.cnblogs.com/p/duct-fitting-change-size.html

Revit API改变风管及管件尺寸的更多相关文章

  1. Revit API移动风管

    移动风管曲线就可以移动风管 , , ));//向上移动3         ts.Commit();         return Result.Succeeded;     } } url:http: ...

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

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

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

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

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

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

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

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

  6. Revit API射线法读取空间中相交的元素

    Revit API提供根据射线来寻找经过的元素.方法是固定模式,没什么好说.关键代码:doc.FindReferencesWithContextByDirection(ptStart, (ptEnd  ...

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

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

  8. Revit API风管对齐

    start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] ].Origin, list ...

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

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

随机推荐

  1. C++产生固定范围内的固定数量的随机数

    #include<iostream> #include<ctime> #include<random> using namespace std; void knut ...

  2. gitminer

    https://github.com/UnkL4b/GitMiner + Autor: UnK + Blog: https://unkl4b.github.io + Github: https://g ...

  3. mysql-8.0.11-winx64 免安装版配置方法

    mysql-8.0.11-winx64.zip  下载地址:https://dev.mysql.com/downloads/file/?id=476233 mysql-8.0.11-winx64.zi ...

  4. HttpClient使用之下载远程服务器中的文件(注意目录遍历漏洞)

    参考文献: http://bbs.csdn.net/topics/390952011 http://blog.csdn.net/ljj_9/article/details/53306468 1.下载地 ...

  5. Dhaka2011

    Dhaka2011 A - Binary Matrix 题目描述:有一个\(n \times m\)的\(01\)矩阵,这一矩阵第一行和最后一行是相邻的,第一列和最后一列是相邻的,现在每次可以交换相邻 ...

  6. poj1221

    dp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ...

  7. vs2017 Remote Debugger远程调试目录

    默认目录:C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Remote Debugger

  8. Ibatis.Net 数据库操作学习(四)

    一.查询select 还记得第一篇示例中是如何读出数据库里3条数据的吗?就是调用了一个QueryForList方法,从方法名就知道,查询返回列表. 1.QueryForList  返回List< ...

  9. LeetCode(13):罗马数字转整数

    Easy! 题目描述: 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写 ...

  10. mysql 索引理解

    数据的查询,都需要将数据从磁盘中加载到内存中进行运算加载,索引的出现,让原来每个数据块做一次IO减少为区间范围的快速定位,来减少块的io次数. 如上图,是一颗b+树,关于b+树的定义可以参见B+树,这 ...