应用程序:AutoJoin
Revit平台:所有
Revit版本:2011.0
首次发布用于:2010.0
编程语言:C#
技能等级:中等
类别:几何体
类型:外部命令
主题:自动连接可组合元素的几何体。
摘要:此示例演示如何在多个常规形状之间自动连接几何图形,以便在族建模和体量中使用。
相关类:
Autodesk.Revit.UI.IExternalCommand
Autodesk.Revit.DB.Document
Autodesk.Revit.DB.CombinableElement
项目文件:
命令.cs
将此方法作为Revit的外部命令来实现。
自动联接.cs
连接此文档中所有重叠的通用表单。
交叉口.cs
判断两个几何体对象是否重叠。
功能:
此示例使用Document::CombineElements方法在重叠的常规形状之间连接几何图形,以便在族建模和体量中使用。
此示例提供了一种实用方法,根据API方法Face::Intersect(Curve)来判断两个几何体对象是否重叠。
实施:
此外部命令可以在Revit族文档上运行。
1.打开一个Revit族,该族包括常规形式(“扫掠”、“混合”、“拉伸”和“旋转”)。
2.运行此外部命令,所有重叠的通用窗体都会连接起来。
可选:
1.打开一个Revit族,该族包括常规形式(“扫掠”、“混合”、“拉伸”和“旋转”)。
2.选择多个通用表单。
3.运行此外部命令,所选的通用表单将被连接,无论它们是否重叠。
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
AutoJoin.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 Autodesk.Revit;
using Autodesk.Revit.DB;
namespace Revit.SDK.Samples.AutoJoin.CS
{
/// <summary>
/// Join all the overlapping generic forms in this document.
/// </summary>
public class AutoJoin
{
/// <summary>
/// Constructor
/// </summary>
public AutoJoin()
{
m_elements = new List<CombinableElement>();
}
/// <summary>
/// Join geometry between overlapping solids.
/// </summary>
/// <param name="document">The active document</param>
/// <returns>The number of geometry combination be joined in this document.</returns>
public int Join(Document document)
{
int combinated = 0;
// CombinableElement is of an element type that exists in the API, but not in Revit's native object model.
// We use a combination of GenericForm and GeomCombination elements instead to find all CombinableElement.
LogicalOrFilter filter = new LogicalOrFilter(
new ElementClassFilter(typeof(GenericForm)),
new ElementClassFilter(typeof(GeomCombination)));
FilteredElementIterator itor = (new FilteredElementCollector(document)).WherePasses(filter).GetElementIterator();
itor.Reset();
while (itor.MoveNext())
{
GenericForm gf = itor.Current as GenericForm;
if (null != gf && !gf.IsSolid)
continue;
CombinableElement ce = itor.Current as CombinableElement;
if (null == ce)
continue;
else
m_elements.Add(ce);
}
// Added all solid forms in this document.
while (1 < m_elements.Count)
{
GeomCombination geomCombination = JoinOverlapping(m_elements, document);
if (null == geomCombination)
{
return combinated;//No overlapping.
}
combinated++;
}
return combinated;
}
/// <summary>
/// Join the overlapped elements in the list.
/// </summary>
/// <param name="elements">the element list may includes overlapping.</param>
/// <param name="document">the active document</param>
/// <returns>the joined geometry combination, the joined elements is removed from the list.</returns>
public GeomCombination JoinOverlapping(List<CombinableElement> elements, Document document)
{
CombinableElementArray joinedElements = new CombinableElementArray();
// try to find the first overlapping.
foreach (CombinableElement aElement in elements)
{
foreach (CombinableElement xElement in elements)
{
if (IsOverlapped(aElement, xElement))
{
joinedElements.Append(aElement);
break;
}
}
if (1 == joinedElements.Size)
break;
}
if (0 == joinedElements.Size)
{
return null;//Can not find any overlapping.
}
// try to find all elements overlapped the first element.
foreach (CombinableElement aElement in elements)
{
if (IsOverlapped(aElement, joinedElements.get_Item(0)))
{
joinedElements.Append(aElement);
}
}
List<CombinableElement> allCanJoin = new List<CombinableElement>();
bool isNew = false;
do
{
allCanJoin.Clear();
isNew = false;
// try to find all elements overlapped joinedElements
foreach (CombinableElement aElement in joinedElements)
{
foreach (CombinableElement xElement in elements)
{
if (IsOverlapped(aElement, xElement))
{
allCanJoin.Add(xElement);
}
}
}
foreach (CombinableElement aElement in allCanJoin)
{
bool isContained = false;
for (int ii = 0; ii < joinedElements.Size; ii++)
{
if (aElement.Id.IntegerValue == joinedElements.get_Item(ii).Id.IntegerValue)
{
isContained = true;
break;
}
}
if (!isContained)
{
isNew = true;
joinedElements.Append(aElement);
}
}
} while (isNew);// find all elements which overlapped with joined geometry combination.
// removed the joined elements from the input list.
foreach (CombinableElement aElement in joinedElements)
{
elements.Remove(aElement);
}
return document.CombineElements(joinedElements);
}
/// <summary>
/// Tell if the element A and B are overlapped.
/// </summary>
/// <param name="elementA">element A</param>
/// <param name="elementB">element B</param>
/// <returns>return true if A and B are overlapped, or else return false.</returns>
public bool IsOverlapped(CombinableElement elementA, CombinableElement elementB)
{
if (elementA.Id.IntegerValue == elementB.Id.IntegerValue)
{
return false;
}
Options geOptions = Command.s_appCreation.NewGeometryOptions();
return Intersection.IsOverlapped(elementA.get_Geometry(geOptions), elementB.get_Geometry(geOptions));
}
private List<CombinableElement> m_elements;// this element list to combine geometry.
}
}
Intersection.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 Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using GeometryElement = Autodesk.Revit.DB.GeometryElement;
using AppCreation = Autodesk.Revit.Creation.Application;
namespace Revit.SDK.Samples.AutoJoin.CS
{
/// <summary>
/// Tell if two geometry objects are overlapping or not.
/// </summary>
class Intersection
{
/// <summary>
/// Tell if the geometry object A and B are overlapped.
/// </summary>
/// <param name="geometryA">geometry object A</param>
/// <param name="geometryB">geometry object B</param>
/// <returns>return true if A and B are overlapped, or else return false.</returns>
public static bool IsOverlapped(GeometryObject geometryA, GeometryObject geometryB)
{
List<Face> facesOfA = new List<Face>();
List<Curve> curvesOfB = new List<Curve>();
GetAllFaces(geometryA, facesOfA);
GetAllCurves(geometryB, curvesOfB);
foreach (Face face in facesOfA)
{
foreach (Curve curve in curvesOfB)
{
if (face.Intersect(curve) == Autodesk.Revit.DB.SetComparisonResult.Overlap)
{
return true;
}
}
}
return false;
}
/// <summary>
/// Get all faces of the geometry object and insert them to the list
/// </summary>
/// <param name="geometry">the geometry object</param>
/// <param name="faces">the face list</param>
private static void GetAllFaces(GeometryObject geometry, List<Face> faces)
{
if (geometry is GeometryElement)
{
GetAllFaces(geometry as GeometryElement, faces);
return;
}
if (geometry is Solid)
{
GetAllFaces(geometry as Solid, faces);
return;
}
}
private static void GetAllFaces(GeometryElement geoElement, List<Face> faces)
{
//foreach (GeometryObject geObject in geoElement.Objects)
IEnumerator<GeometryObject> Objects = geoElement.GetEnumerator();
while (Objects.MoveNext())
{
GeometryObject geObject = Objects.Current;
GetAllFaces(geObject, faces);
}
}
private static void GetAllFaces(Solid solid, List<Face> faces)
{
foreach (Face face in solid.Faces)
{
faces.Add(face);
}
}
private static void GetAllCurves(GeometryObject geometry, List<Curve> curves)
{
if (geometry is GeometryElement)
{
GetAllCurves(geometry as GeometryElement, curves);
return;
}
if (geometry is Solid)
{
GetAllCurves(geometry as Solid, curves);
return;
}
}
private static void GetAllCurves(GeometryElement geoElement, List<Curve> curves)
{
//foreach (GeometryObject geObject in geoElement.Objects)
IEnumerator<GeometryObject> Objects = geoElement.GetEnumerator();
while (Objects.MoveNext())
{
GeometryObject geObject = Objects.Current;
GetAllCurves(geObject, curves);
}
}
private static void GetAllCurves(Solid solid, List<Curve> curves)
{
foreach (Face face in solid.Faces)
{
GetAllCurves(face, curves);
}
}
private static void GetAllCurves(Face face, List<Curve> curves)
{
foreach (EdgeArray loop in face.EdgeLoops)
{
foreach (Edge edge in loop)
{
List<Autodesk.Revit.DB.XYZ> points = edge.Tessellate() as List<Autodesk.Revit.DB.XYZ>;
for (int ii = 0; ii + 1 < points.Count; ii++)
{
Line line = Line.CreateBound(points[ii], points[ii + 1]);
curves.Add(line);
}
}
}
}
}
}
版权所有 :无锡模信建筑科技有限公司 苏ICP备2021028830号-1 BIM建模|BIM技术应用|BIM软件开发
联系地址:江苏省无锡市新吴区龙山路4号B座705 手机:18761516598