应用程序:ProximityDetection_WallJoinControl

Revit平台:所有

Revit版本:2011.0

首次发布时间:2011.0

编程语言:C#

难度级别:中等

类别:几何图形

类型:ExternalCommand

主题:接近检测和墙体连接控制

概要:

本示例演示如何通过ElementIntersectsSolidFilterElementIntersectsElementFilterWallUtils来“查找墙中的柱子”、“查找阻塞出口的元素”、“查找(几乎连接到)墙的末端的墙体”和“检查墙体的连接/分离状态”。

相关类:

Autodesk.Revit.UI.IExternalCommand

Autodesk.Revit.Document

Autodesk.Revit.Element

Autodesk.Revit.DB;

Autodesk.Revit.UI;

项目文件:

Command.cs

该文件包含了一个实现了IExternalCommand接口的类Command。该类的功能是创建ProximityDetectionWallJoinControl的两个实例,并弹出对话框以供用户操作和显示结果。

ProximityDetection.cs

该文件包含了一个实现了三个应用程序的类ProximityDetection——“查找墙中的柱子”、“查找阻塞出口的元素”、“查找(几乎连接到)墙的末端的墙体”。

WallJoinControl.cs

该文件包含了一个实现了应用程序——“检查墙体的连接/分离状态”的类WallJoinControl

ProximityDetectionAndWallJoinControlForm.cs

该文件包含了一个窗体,用户可以通过该窗体选择操作并查看结果。

描述:

该示例实现了IExternalCommand接口,允许用户通过窗体选择操作。用户可以运行应用程序,然后在窗体中读取结果。

- 若要显示窗体,请使用ProximityDetectionAndWallJoinControlFormShowDialog方法。

- 若要查找墙中的柱子,请选择第一个单选按钮,使用ProximityDetectionfindBlockingElements方法。

- 若要查找阻塞出口的元素,请选择第二个单选按钮,使用ProximityDetectionfindColumnsInWall方法。

- 若要查找(几乎连接到)墙的末端的墙体,请选择第三个单选按钮,使用ProximityDetectionfindNearbyWalls方法。

- 若要检查墙体的连接/分离状态,请选择第四个单选按钮,使用WallJoinControlcheckJoinedWalls方法。

- 若要查看结果,请检查窗体的树形视图。

说明:

1.设置插件文件并启动revit以加载此示例。

2.通过外部命令菜单启动Proximity DetectionWall Join Control

4.选择操作——“查找墙中的柱子”、“查找阻塞出口的元素”、“查找(几乎连接到)墙的末端的墙体”或“检查墙体的连接/分离状态”。点击确定按钮。

6.根据所选操作,示例将显示操作结果。您可以通过单击树视图来查看信息。

7.点击取消按钮结束。

源代码:

完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

ProximityDetection.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace Revit.SDK.Samples.ProximityDetection_WallJoinControl.CS
{
/// <summary>
/// The class that is responsible for proximity detection
/// </summary>
public class ProximityDetection
{
/// <summary>
/// The singleton instance of ProximityDetection
/// </summary>
private static ProximityDetection Instance;

/// <summary>
/// revit application
/// </summary>
private Autodesk.Revit.ApplicationServices.Application m_app;
/// <summary>
/// revit document
/// </summary>
private Autodesk.Revit.DB.Document m_doc;

/// <summary>
/// Constructor
/// </summary>
/// <param name="app">Revit application</param>
/// <param name="doc">Revit document</param>
private ProximityDetection(
Autodesk.Revit.ApplicationServices.Application app,
Autodesk.Revit.DB.Document doc)
{
m_app = app;
m_doc = doc;
}

/// <summary>
/// Get the singleton instance of ProximityDetection
/// </summary>
/// <param name="app">Revit application</param>
/// <param name="doc">Revit document</param>
/// <returns>The singleton instance of ProximityDetection</returns>
public static ProximityDetection getInstance(
Autodesk.Revit.ApplicationServices.Application app,
Autodesk.Revit.DB.Document doc)
{
if (Instance == null)
{
Instance = new ProximityDetection(app, doc);
}
return Instance;
}

/// <summary>
/// Find columns in wall
/// </summary>
/// <param name="walls">The walls to be detected</param>
/// <returns>The detection result</returns>
public XElement findColumnsInWall(IEnumerable<Wall> walls)
{
// create a node that place all walls.
XElement wallsNode = new XElement("Walls", new XAttribute("Name", "Walls"));

try
{
foreach (Wall wall in walls)
{
XElement wallNode = new XElement("Wall", new XAttribute("Name", wall.Name));

// Iterate to find columns and structural columns
FilteredElementCollector collector = new FilteredElementCollector(m_doc);
List<BuiltInCategory> columnCategories = new List<BuiltInCategory>();
columnCategories.Add(BuiltInCategory.OST_Columns);
columnCategories.Add(BuiltInCategory.OST_StructuralColumns);
collector.WherePasses(new ElementMulticategoryFilter(columnCategories));

// Apply element intersection filter
ElementIntersectsElementFilter testElementIntersectsElementFilter =
new ElementIntersectsElementFilter(wall);

collector.WherePasses(testElementIntersectsElementFilter);

XElement columnsNode = new XElement("columns",
new XAttribute("Count", collector.Count().ToString()));

foreach (Element column in collector)
{
columnsNode.Add(new XElement("column", new XAttribute("Name", column.Name)));
}

wallNode.Add(columnsNode);
wallsNode.Add(wallNode);
}
}
catch (Exception ex)
{
wallsNode.Add(new XElement("Error", new XAttribute("Exception", ex.ToString())));
}

// return the whole walls Node
return wallsNode;

}

/// <summary>
/// Find elements blocking egress
/// </summary>
/// <param name="egresses">The egresses to be detected</param>
/// <returns>The detection result</returns>
public XElement findBlockingElements(ICollection<Element> egresses)
{
// create a node that place all egresses.
XElement egressesNode = new XElement("Egresses", new XAttribute("Name", "Egresses"));

try
{
// find the elements blocking egress
foreach (Element egressElement in egresses)
{
XElement egressNode = new XElement("Egress",
new XAttribute("Name", egressElement.Name));

int count = 1;
IEnumerator<GeometryObject> Objects = egressElement.get_Geometry(new Autodesk.Revit.DB.Options()).GetEnumerator();
Objects.MoveNext();
GeometryInstance gi = Objects.Current as GeometryInstance;
IEnumerator<GeometryObject> Objects1 = gi.GetInstanceGeometry().GetEnumerator();

//foreach (GeometryObject egressGObj in
// (egressElement.get_Geometry(new Autodesk.Revit.DB.Options()).Objects.get_Item(0) as GeometryInstance).GetInstanceGeometry().Objects)
while (Objects1.MoveNext())
{
GeometryObject egressGObj = Objects1.Current;

if (egressGObj is Solid)
{
Solid egressVolume = egressGObj as Solid; //calculated from shape and location of a given door

XElement solidNode = new XElement("ElementSolid" + count.ToString());
// Iterate to find all instance types
FilteredElementCollector blockingcollector = new FilteredElementCollector(m_doc);
blockingcollector.WhereElementIsNotElementType();

// Apply geometric filter
ElementIntersectsSolidFilter testElementIntersectsSolidFilter =
new ElementIntersectsSolidFilter(egressVolume);
blockingcollector.WherePasses(testElementIntersectsSolidFilter);

IEnumerable<Element> blockingElement = blockingcollector;

// Exclude the door itself
List<ElementId> exclusions = new List<ElementId>();
exclusions.Add(egressElement.Id);
blockingcollector.Excluding(exclusions);

XElement blockingegressNode = new XElement("blocking_egress_elements",
new XAttribute("Count", blockingElement.Count().ToString()));

foreach (Element blockingelement in blockingElement)
{
blockingegressNode.Add(new XElement("blocking_egress_element",
new XAttribute("Name", blockingelement.Name)));
}

solidNode.Add(blockingegressNode);
egressNode.Add(solidNode);

count++;
}
}
egressesNode.Add(egressNode);
}
}
catch (Exception ex)
{
egressesNode.Add(new XElement("Error", new XAttribute("Exception", ex.ToString())));
}

// return the whole Egresses Node
return egressesNode;
}

/// <summary>
/// Find walls (nearly joined to) end of walls
/// </summary>
/// <param name="walls">The walls to be detected</param>
/// <returns>The detection result</returns>
public XElement findNearbyWalls(IEnumerable<Wall> walls)
{
// create a node that place all walls.
XElement wallsNode = new XElement("Walls", new XAttribute("Name", "Walls"));

try
{
foreach (Wall wall in walls)
{
XElement wallNode = new XElement("Wall", new XAttribute("Name", wall.Name));

// Start
XElement endNode = new XElement("Start", new XAttribute("Name", "Start"));

XYZ wallEndPoint = (wall.Location as LocationCurve).Curve.GetEndPoint(0);
double wallHeight = wall.get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM).AsDouble();

FilteredElementCollector collector = nearbyWallsFilter(wallEndPoint, wallHeight, 10.0); // 10 ft

// Exclude the wall itself
List<ElementId> exclusions = new List<ElementId>();
exclusions.Add(wall.Id);
collector.Excluding(exclusions);

IEnumerable<Wall> nearbyWalls = collector.OfType<Wall>();

XElement nearbyWallsNode = new XElement("near_by_walls",
new XAttribute("Count", nearbyWalls.Count().ToString()));

foreach (Wall nearbywall in nearbyWalls)
{
nearbyWallsNode.Add(new XElement("near_by_wall",
new XAttribute("Name", nearbywall.Name)));
}

endNode.Add(nearbyWallsNode);
wallNode.Add(endNode);

// End
endNode = new XElement("End", new XAttribute("Name", "End"));

wallEndPoint = (wall.Location as LocationCurve).Curve.GetEndPoint(1);

collector = nearbyWallsFilter(wallEndPoint, wallHeight, 10.0);

// Exclude the wall itself
exclusions = new List<ElementId>();
exclusions.Add(wall.Id);
collector.Excluding(exclusions);

nearbyWalls = collector.OfType<Wall>();

nearbyWallsNode = new XElement("near_by_walls",
new XAttribute("Count", nearbyWalls.Count().ToString()));

foreach (Wall nearbywall in nearbyWalls)
{
nearbyWallsNode.Add(new XElement("near_by_wall",
new XAttribute("Name", nearbywall.Name)));
}

endNode.Add(nearbyWallsNode);
wallNode.Add(endNode);

wallsNode.Add(wallNode);
}
}
catch (Exception ex)
{
wallsNode.Add(new XElement("Error", new XAttribute("Exception", ex.ToString())));
}

// return the whole walls Node
return wallsNode;
}

/// <summary>
/// Find the nearby walls on specific point and in specific height
/// </summary>
/// <param name="point">The given point</param>
/// <param name="height">The given height</param>
/// <param name="radius">The radius in which walls can be detected</param>
/// <returns>The detection result</returns>
private FilteredElementCollector nearbyWallsFilter(XYZ point, double height, double radius)
{
// build cylindrical shape around wall endpoint
List<CurveLoop> curveloops = new List<CurveLoop>();
CurveLoop circle = new CurveLoop();
circle.Append(Arc.Create(point, radius
, 0, Math.PI,
XYZ.BasisX, XYZ.BasisY));
circle.Append(Arc.Create(point, radius
, Math.PI, 2 * Math.PI,
XYZ.BasisX, XYZ.BasisY));
curveloops.Add(circle);

Solid wallEndCylinder =
GeometryCreationUtilities.CreateExtrusionGeometry(curveloops, XYZ.BasisZ, height);

// Iterate document to find walls
FilteredElementCollector collector = new FilteredElementCollector(m_doc);
collector.OfCategory(BuiltInCategory.OST_Walls);

// Apply geometric filter
ElementIntersectsSolidFilter testElementIntersectsSolidFilter =
new ElementIntersectsSolidFilter(wallEndCylinder);
collector.WherePasses(testElementIntersectsSolidFilter);

return collector;
}
}
}

WallJoinControl.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.ProximityDetection_WallJoinControl.CS
{
   /// <summary>
   /// The class that is responsible for controlling the joint of walls
   /// </summary>
   public class WallJoinControl
   {
      /// <summary>
      /// The singleton instance of WallJoinControl
      /// </summary>
      private static WallJoinControl Instance;
      /// <summary>
      /// revit application
      /// </summary>
      private Autodesk.Revit.ApplicationServices.Application m_app;
      /// <summary>
      /// revit document
      /// </summary>
      private Autodesk.Revit.DB.Document m_doc;
      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="app">Revit application</param>
      /// <param name="doc">Revit document</param>
      private WallJoinControl(
         Autodesk.Revit.ApplicationServices.Application app, 
         Autodesk.Revit.DB.Document doc)
      {
         m_app = app;
         m_doc = doc;
      }
      /// <summary>
      /// Get the singleton instance of WallJoinControl
      /// </summary>
      /// <param name="app">Revit application</param>
      /// <param name="doc">Revit document</param>
      /// <returns>The singleton instance of WallJoinControl</returns>
      public static WallJoinControl getInstance(
         Autodesk.Revit.ApplicationServices.Application app, 
         Autodesk.Revit.DB.Document doc)
      {
         if (Instance == null)
         {
            Instance = new WallJoinControl(app, doc);
         }
         return Instance;
      }
      /// <summary>
      /// Check every wall's joined walls by WallUtils method in initial, after disallow join, then after allow join states
      /// </summary>
      /// <param name="walls">The walls to be checked</param>
      /// <returns>The check result</returns>
      public XElement checkJoinedWalls(IEnumerable<Wall> walls)
      {
         // create a node that place all walls.
         XElement wallsNode = new XElement("Walls", new XAttribute("Name", "Walls"));
         try
         {
            foreach (Wall wall in walls)
            {
               // create a wall node
               XElement wallNode = new XElement("Wall", 
                  new XAttribute("name", wall.Name), 
                  new XAttribute("Type", wall.WallType.Kind.ToString()));
               LocationCurve locationCurve = wall.Location as LocationCurve;
               if (null == locationCurve)
               {
                  wallNode.Add(new XElement("Error", 
                     new XAttribute("Exception", "This wall has not a LocationCurve!")));
               }
               else
               {
                  // start
                  XElement endNode = new XElement("Start", new XAttribute("Name", "Start"));
                  wallNode.Add(checkWallEnd(wall, locationCurve, 0, endNode));
                  // end
                  endNode = new XElement("End", new XAttribute("Name", "End"));
                  wallNode.Add(checkWallEnd(wall, locationCurve, 1, endNode));
               }
               wallsNode.Add(wallNode);
            }
         }
         catch (Exception ex)
         {
            wallsNode.Add(new XElement("Error", new XAttribute("Exception", ex.ToString())));
         }
         // return the whole walls Node
         return wallsNode;
      }
      /// <summary>
      /// Check wall's two ends in three states
      /// </summary>
      /// <param name="wall">The wall to be checked</param>
      /// <param name="locationCurve">The wall's location curve</param>
      /// <param name="end">The index indicates the start or end of this wall</param>
      /// <param name="endnode">Result XML node</param>
      /// <returns>The check result</returns>
      private XElement checkWallEnd(Wall wall, LocationCurve locationCurve, int end, XElement endnode)
      {
         // Initial state
         XElement stateNode = new XElement("Initial", new XAttribute("Name", "Initial"));
         endnode.Add(GetState(wall, locationCurve, end, stateNode));
         // Disallow join
         WallUtils.DisallowWallJoinAtEnd(wall, end);
         // After DisallowWallJoinAtEnd Evoked state
         stateNode = new XElement("After_DisallowWallJoinAtEnd_Evoked", 
            new XAttribute("Name", "After_DisallowWallJoinAtEnd_Evoked"));
         endnode.Add(GetState(wall, locationCurve, end, stateNode));
         // Allow join
         WallUtils.AllowWallJoinAtEnd(wall, end);
         // After AllowWallJoinAtEnd Evoked state
         stateNode = new XElement("After_DisallowWallJoinAtEnd_Evoked", 
            new XAttribute("Name", "After_DisallowWallJoinAtEnd_Evoked"));
         endnode.Add(GetState(wall, locationCurve, end, stateNode));
         return endnode;
      }
      /// <summary>
      /// Get wall's one end state
      /// </summary>
      /// <param name="wall">The wall to be checked</param>
      /// <param name="locationCurve">The wall's location curve</param>
      /// <param name="end">The index indicates the start or end of this wall</param>
      /// <param name="statenode">Result XML node</param>
      /// <returns>The check result</returns>
      private XElement GetState(Wall wall, LocationCurve locationCurve, int end, XElement statenode)
      {
         // Get geometry information
         statenode.Add(GetGeometryInfo(locationCurve, end));
         // Get IsWallJoinAllowedAtEnd API method's result
         statenode.Add(GetIsWallJoinAllowedAtEnd(wall, end));
         // Get all joined walls
         statenode.Add(GetJoinedWalls(locationCurve, end));
         return statenode;
      }
      /// <summary>
      /// Get wall's one end geometry information
      /// </summary>
      /// <param name="locationCurve">The wall's location curve</param>
      /// <param name="end">The index indicates the start or end of this wall</param>
      /// <returns>The check result</returns>
      private XElement GetGeometryInfo(LocationCurve locationCurve, int end)
      {
         XElement geometryinfoNode = new XElement("GeometryInfo");
         // Output LocationCurve end point coordinate
         XYZ endpoint = locationCurve.Curve.GetEndPoint(end);
         geometryinfoNode.Add(new XElement("PointCoordinate", 
            new XAttribute("X", Math.Round(endpoint.X, 9).ToString()),
            new XAttribute("Y", Math.Round(endpoint.Y, 9).ToString()),
            new XAttribute("Z", Math.Round(endpoint.Z, 9).ToString())));
         return geometryinfoNode;
      }
      /// <summary>
      /// Get wall's one end IsWallJoinAllowedAtEnd API method's result
      /// </summary>
      /// <param name="wall">The wall to be checked</param>
      /// <param name="end">The index indicates the start or end of this wall</param>
      /// <returns>The check result</returns>
      private XElement GetIsWallJoinAllowedAtEnd(Wall wall, int end)
      {
         return new XElement("IsWallJoinAllowedAtEnd", 
            new XAttribute("Value", WallUtils.IsWallJoinAllowedAtEnd(wall, end)));
      }
      /// <summary>
      /// Get wall's one end all joined walls
      /// </summary>
      /// <param name="locationCurve">The wall's location curve</param>
      /// <param name="end">The index indicates the start or end of this wall</param>
      /// <returns>The check result</returns>
      private XElement GetJoinedWalls(LocationCurve locationCurve, int end)
      {
         // retrieve joined elements 
         ElementArray array = locationCurve.get_ElementsAtJoin(end);
         XElement joinedwallsNode = new XElement("JoinedWalls", 
            new XAttribute("Count", array.Size.ToString()));
         // output array
         foreach (Element ele in array)
         {
            if (ele is Wall)
            {
               joinedwallsNode.Add(new XElement("JoinedWall", 
                  new XAttribute("Name", ele.Name)));
            }
         }
         return joinedwallsNode;
      }
   }
}