热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

RevitAPI遍历全部风管,找到与风管相关的墙开洞

涉及向量计算,求相交等相关技术。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingS
涉及向量计算,求相交等相关技术。
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 &#61; FindAllDuct(doc);
            
foreach (Duct duct in listDuct)
            {
                List
<Wall> listWall &#61; FindDuctWall(doc, duct);
                
foreach (Wall wall in listWall)
                {
                    CenterOpen(doc, wall, duct, 
640640);
                }
            }

            ts.Commit();

            
return Result.Succeeded;
        }
        
//找到所有风管
        List<Duct> FindAllDuct(Document doc)
        {
            List
<Duct> listDuct &#61; new List<Duct>();
            FilteredElementCollector collector 
&#61; new FilteredElementCollector(doc);
            collector.OfClass(
typeof(Duct)).OfCategory(BuiltInCategory.OST_DuctCurves);

            
foreach (Element el in collector)
            {
                Duct duct 
&#61; el as Duct;
                
if (duct !&#61; null)
                    listDuct.Add(duct);
            }
            
return listDuct;
        }
        
//找到与风管相交的墙
        List<Wall> FindDuctWall(Document doc, Duct duct)
        {
            List
<Wall> listWall &#61; new List<Wall>();
            
//找到outLine
            BoundingBoxXYZ bb &#61; duct.get_BoundingBox(doc.ActiveView);
            Outline outline 
&#61; new Outline(bb.Min, bb.Max);
            
//
            FilteredElementCollector collector &#61; new FilteredElementCollector(doc);
            BoundingBoxIntersectsFilter invertFilter 
&#61; new BoundingBoxIntersectsFilter(outline, false);
            IList
<Element> noIntersectWalls &#61; collector.OfClass(typeof(Wall)).WherePasses(invertFilter).ToElements();
            
foreach (Element el in noIntersectWalls)
            {
                Wall wall 
&#61; el as Wall;
                
if (wall !&#61; null)
                    listWall.Add(wall);
            }
            
return listWall;
        }
        
//开同心洞
        Result CenterOpen(Document doc, Wall wall, Duct duct, double dWidth, double dHeigh)
        {
            SubTransaction subTs 
&#61; new SubTransaction(doc);
            subTs.Start();
            
try
            {
                
//求面和线的交点
                Face face &#61; FindWallFace(wall);
                Curve curve 
&#61; FindDuctCurve(duct);
                XYZ xyz 
&#61; FindFaceCurve(face, curve);
                
//墙线的向量
                XYZ wallVector &#61; FindWallVector(wall);
                
//交点向上向墙线正方向移动160(风管宽高320)
                XYZ pt1 &#61; xyz &#43; new XYZ(001* dHeigh / 2 / 304.8;
                pt1 
&#61; pt1 &#43; wallVector.Normalize() * dWidth / 2 / 304.8;
                
//交点向下向墙线反方向移动160(风管宽高320)
                XYZ pt2 &#61; xyz &#43; new XYZ(00-1* dHeigh / 2 / 304.8;
                pt2 
&#61; pt2 - wallVector.Normalize() * dWidth / 2 / 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 
&#61; wall.Location as LocationCurve;
            XYZ xyz 
&#61; lCurve.Curve.get_EndPoint(1- lCurve.Curve.get_EndPoint(0);
            
return xyz;
        }
        
//求线和面的交点
        XYZ FindFaceCurve(Face face, Curve curve)
        {
            
//求交点
            IntersectionResultArray intersectionR &#61; new IntersectionResultArray();//交点集合
            SetComparisonResult comparisonR;//Comparison比较
            comparisonR &#61; face.Intersect(curve, out intersectionR);
            XYZ intersectionResult 
&#61; null;//交点坐标
            if (SetComparisonResult.Disjoint !&#61; comparisonR)//Disjoint不交
            {
                
if (!intersectionR.IsEmpty)
                {
                    intersectionResult 
&#61; intersectionR.get_Item(0).XYZPoint;
                }
            }
            
return intersectionResult;
        }
        
//找到风管对应的曲线
        Curve FindDuctCurve(Duct duct)
        {
            
//得到风管曲线
            IList<XYZ> list &#61; new List<XYZ>();
            ConnectorSetIterator csi 
&#61; duct.ConnectorManager.Connectors.ForwardIterator();
            
while (csi.MoveNext())
            {
                Connector conn 
&#61; csi.Current as Connector;
                list.Add(conn.Origin);
            }
            Curve curve 
&#61; Line.get_Bound(list.ElementAt(0), list.ElementAt(1)) as Curve;
            
return curve;
        }
        
//找到墙的正面
        Face FindWallFace(Wall wall)
        {
            Face normalFace 
&#61; null;
            
//
            Options opt &#61; new Options();
            opt.ComputeReferences 
&#61; true;
            opt.DetailLevel 
&#61; Autodesk.Revit.DB.DetailLevels.Medium;
            
//
            GeometryElement e &#61; wall.get_Geometry(opt);
            
foreach (GeometryObject obj in e.Objects)
            {
                Solid solid 
&#61; obj as Solid;
                
if (solid !&#61; null && solid.Faces.Size > 0)
                {
                    
foreach (Face face in solid.Faces)
                    {
                        PlanarFace pf 
&#61; face as PlanarFace;
                        
if (pf !&#61; null)
                        {
                            
if (pf.Normal.AngleTo(wall.Orientation) < 0.01)//数值在0到PI之间
                            {
                                normalFace 
&#61; 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

转:https://www.cnblogs.com/greatverve/p/api-duct-wall-opening.html



推荐阅读
  • ASP.NETCoreZero笔记(PowerTools)
    安装ASP.NETCoreZeroPowerTool根据官方的介绍,使用该工具,可以快速得创建具备单个表结构及父子表结构的服务以及前端页面。服务:指的是生产对应基础功能webApi ... [详细]
  • 一、概述ceph为k8s提供存储服务主要有两种方式,cephfs和cephrdb;cephfs方式支持k8s的pv的3种访问模式ReadWriteOnce,ReadOnlyMany ... [详细]
  • Adapter相当于C(Controller,控制器),listView相当于V(View,视图)用于显示数据为ListView提供数据的List,数组或数据库相当于MVC模式中的 ... [详细]
  • IIS6.0提供一个重新设计的万维网发布服务(WorldWideWebPublishingService)架构,可以帮助你为你的网站构建更好的性能、可靠、可扩展性(scalabil ... [详细]
  • POI编程
    POI编程1简介在我们实际的开发中,表现层的解决方案虽然有多样,但是IE浏览器已成为最多人使用的浏览器,因为大家都用Windows。在企业办公系统中 ... [详细]
  • 本文目录一览:1、java中几种解析html的工具 ... [详细]
  • Jenkins自动部署SpringBoot项目实践教程
    Jenkins自动部署SpringBoot项目实践教程-目录1、Jenkins安装2、Jenkins插件安装3、点击添加凭据4、Jenkins环境配置4.1、全局配置4.2、系统配 ... [详细]
  • 1,Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合。它没有用户界面,并且也不会产生任何可视输出。任何用户 ... [详细]
  • 缓冲区溢出实例(一)–Windows
    一、基本概念缓冲区溢出:当缓冲区边界限制不严格时,由于变量传入畸形数据或程序运行错误,导致缓冲区被填满从而覆盖了相邻内存区域的数据。可以修改内存数据,造成进程劫持,执行恶意代码,获 ... [详细]
  • PICT初探索
    pict用于测试用例的生成,非常高效。也非常方便。安装完成后主要程序就一个exe文件。需要通过cmd命令行进入pict文件夹。建立测试用例文件然后使用简单的命令行生成测试用例也可以 ... [详细]
  • C#制作TextBox水印提示
    前言在使用C#的TextBox控件时,有时候会有以下需求:在用户没有输入文字时,TextBox有提示文字,如下图所示 ... [详细]
  • 人生的旅途,前途很远,也很暗。然而不要怕,不怕的人的面前才有路。——鲁迅自从上一篇博客发布后,已经有很长时间没有更新博客了,一直忙着支付通的事情,在此给大家道个歉。先贴个图:你不要惊讶 ... [详细]
  • delFile:删除文件rmdirFolder:删除文件夹【貌似只能删除空目录】rm-rfFolder:删除非空文件夹【用windows自带的cmd提示 ... [详细]
  • 这里是一些专栏的废话,你可以从下一个分界线开始:)---------------------------------------------------------- ... [详细]
  • 代码实例:<?php$file$_GET[file];if(isset($file)){include(pages$file);}else{i ... [详细]
author-avatar
撒谎滴土豆
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有