应用程序:GeometryCreation_BooleanOperation
Revit平台:所有
Revit版本:2012.0
首次发布版本:2012.0
编程语言:C#
技能水平:中等
类别:几何
类型:外部命令
主题:几何创建和几何布尔运算
概要:
本示例演示如何使用GeometryCreationUtils创建几何实体并使用BooleanOperationUtils进行几何布尔运算。该项目将创建一个CSG(Constructive Solid Geometry)树(http://en.wikipedia.org/wiki/Constructive_solid_geometry,http://en.wikipedia.org/wiki/File:Csg_tree.png),并通过AVF(Analysis Visualization Framework)在新视图中显示它。
类别:
Autodesk.Revit.UI.IExternalCommand
Autodesk.Revit.Document
Autodesk.Revit.Element
Autodesk.Revit.DB;
Autodesk.Revit.DB.Analysis;
项目文件:
Command.cs
此文件包含一个实现IExternalCommand接口的类Command。该类的功能是创建两个GeometryCreation和AnalysisVisualizationFramework实例,准备并创建CSG树。
GeometryCreation.cs
此文件包含一个GeometryCreation类,它可以创建挤出、旋转、扫描、混合、扫描混合几何实体和特定以中心为基础的盒子、球体和圆柱体。
BooleanOperation.cs
此文件包含一个BooleanOperation类,它实现了两个给定实体之间的几何布尔运算——交集、并集、差异。
AnalysisVisualizationFramework.cs
此文件包含一个AnalysisVisualizationFramework类,它在一个新的命名视图中显示实体。
描述:
本示例实现了IExternalCommand接口,它创建了特定的几何实体并执行了几何布尔运算,以创建一个CSG树。
- 要创建特定的几何实体,请使用GeometryCreation的CreateCenterbasedBox、CreateCenterbasedSphere和CreateCenterbasedCylinder方法。
- 要执行几何布尔运算以创建CSG树,请使用BooleanOperation的BooleanOperation_Intersect、BooleanOperation_Union、BooleanOperation_Union和BooleanOperation_Difference方法。
- 要查看新创建的实体,请使用AnalysisVisualizationFramework的PaintSolid方法。
说明:
1. 配置插件文件并启动Revit以加载此示例。
2. 通过外部命令菜单启动GeometryCreation_BooleanOperation。
3. 该示例将创建一个CSG树,并在命名为“CSG Tree”的视图中显示结果实体。
源代码:
完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码
AnalysisVisualizationFramework .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.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Analysis;
namespace Revit.SDK.Samples.GeometryCreation_BooleanOperation.CS
{
class AnalysisVisualizationFramework
{
/// <summary>
/// The singleton instance of AnalysisVisualizationFramework
/// </summary>
private static AnalysisVisualizationFramework Instance;
/// <summary>
/// revit document
/// </summary>
private Autodesk.Revit.DB.Document m_doc;
/// <summary>
/// The created view list
/// </summary>
private List<String> viewNameList = new List<string>();
/// <summary>
/// The ID of schema which SpatialFieldManager register
/// </summary>
private static int SchemaId = -1;
/// <summary>
/// Constructor
/// </summary>
/// <param name="doc">Revit document</param>
private AnalysisVisualizationFramework(Autodesk.Revit.DB.Document doc)
{
m_doc = doc;
}
/// <summary>
/// Get the singleton instance of AnalysisVisualizationFramework
/// </summary>
/// <param name="doc">Revit document</param>
/// <returns>The singleton instance of AnalysisVisualizationFramework</returns>
public static AnalysisVisualizationFramework getInstance(Autodesk.Revit.DB.Document doc)
{
if (Instance == null)
{
Instance = new AnalysisVisualizationFramework(doc);
}
return Instance;
}
/// <summary>
/// Paint a solid in a new named view
/// </summary>
/// <param name="s">solid</param>
/// <param name="viewName">Given the name of view</param>
public void PaintSolid(Solid s, String viewName)
{
View view;
if (!viewNameList.Contains(viewName))
{
IList<Element> viewFamilyTypes = new FilteredElementCollector(m_doc).OfClass(typeof(ViewFamilyType)).ToElements();
ElementId View3DId = new ElementId(-1);
foreach (Element e in viewFamilyTypes)
{
if (e.Name == "3D View")
{
View3DId = e.Id;
}
}
//view = m_doc.Create.NewView3D(new XYZ(1, 1, 1));
view = View3D.CreateIsometric(m_doc, View3DId);
ViewOrientation3D viewOrientation3D = new ViewOrientation3D(new XYZ(1, -1, -1), new XYZ(1, 1, 1), new XYZ(1, 1, -2));
(view as View3D).SetOrientation(viewOrientation3D);
(view as View3D).SaveOrientation();
view.Name = viewName;
viewNameList.Add(viewName);
}
else
{
view = (((new FilteredElementCollector(m_doc).
OfClass(typeof(View))).Cast<View>()).
Where(e => e.Name == viewName)).First<View>();
}
SpatialFieldManager sfm = SpatialFieldManager.GetSpatialFieldManager(view);
if (sfm == null) sfm = SpatialFieldManager.CreateSpatialFieldManager(view, 1);
if (SchemaId != -1)
{
IList<int> results = sfm.GetRegisteredResults();
if (!results.Contains(SchemaId))
{
SchemaId = -1;
}
}
if (SchemaId == -1)
{
AnalysisResultSchema resultSchema1 = new AnalysisResultSchema("PaintedSolid" + viewName, "Description");
AnalysisDisplayStyle displayStyle = AnalysisDisplayStyle.CreateAnalysisDisplayStyle(
m_doc,
"Real_Color_Surface" + viewName,
new AnalysisDisplayColoredSurfaceSettings(),
new AnalysisDisplayColorSettings(),
new AnalysisDisplayLegendSettings());
resultSchema1.AnalysisDisplayStyleId = displayStyle.Id;
SchemaId = sfm.RegisterResult(resultSchema1);
}
FaceArray faces = s.Faces;
Transform trf = Transform.Identity;
foreach (Face face in faces)
{
int idx = sfm.AddSpatialFieldPrimitive(face, trf);
IList<UV> uvPts = null;
IList<ValueAtPoint> valList = null;
ComputeValueAtPointForFace(face, out uvPts, out valList, 1);
FieldDomainPointsByUV pnts = new FieldDomainPointsByUV(uvPts);
FieldValues vals = new FieldValues(valList);
sfm.UpdateSpatialFieldPrimitive(idx, pnts, vals, SchemaId);
}
}
/// <summary>
/// Compute the value of face on specific point
/// </summary>
/// <param name="face"></param>
/// <param name="uvPts"></param>
/// <param name="valList"></param>
/// <param name="measurementNo"></param>
private static void ComputeValueAtPointForFace(Face face, out IList<UV> uvPts, out IList<ValueAtPoint> valList, int measurementNo)
{
List<double> doubleList = new List<double>();
uvPts = new List<UV>();
valList = new List<ValueAtPoint>();
BoundingBoxUV bb = face.GetBoundingBox();
for (double u = bb.Min.U; u < bb.Max.U + 0.0000001; u = u + (bb.Max.U - bb.Min.U) / 1)
{
for (double v = bb.Min.V; v < bb.Max.V + 0.0000001; v = v + (bb.Max.V - bb.Min.V) / 1)
{
UV uvPnt = new UV(u, v);
uvPts.Add(uvPnt);
XYZ faceXYZ = face.Evaluate(uvPnt);
// Specify three values for each point
for (int ii = 1; ii <= measurementNo; ii++)
doubleList.Add(faceXYZ.DistanceTo(XYZ.Zero) * ii);
valList.Add(new ValueAtPoint(doubleList));
doubleList.Clear();
}
}
}
}
}
BooleanOperation.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.Text;
using Autodesk.Revit.DB;
namespace Revit.SDK.Samples.GeometryCreation_BooleanOperation.CS
{
static class BooleanOperation
{
/// <summary>
/// Boolean intersect geometric operation, return a new solid as the result
/// </summary>
/// <param name="solid1">Operation solid 1</param>
/// <param name="solid2">Operation solid 2</param>
/// <returns>The operation result</returns>
public static Solid BooleanOperation_Intersect(Solid solid1, Solid solid2)
{
return BooleanOperationsUtils.ExecuteBooleanOperation(solid1, solid2, BooleanOperationsType.Intersect);
}
/// <summary>
/// Boolean union geometric operation, return a new solid as the result
/// </summary>
/// <param name="solid1">Operation solid 1</param>
/// <param name="solid2">Operation solid 2</param>
/// <returns>The operation result</returns>
public static Solid BooleanOperation_Union(Solid solid1, Solid solid2)
{
return BooleanOperationsUtils.ExecuteBooleanOperation(solid1, solid2, BooleanOperationsType.Union);
}
/// <summary>
/// Boolean difference geometric operation, return a new solid as the result
/// </summary>
/// <param name="solid1">Operation solid 1</param>
/// <param name="solid2">Operation solid 2</param>
/// <returns>The operation result</returns>
public static Solid BooleanOperation_Difference(Solid solid1, Solid solid2)
{
return BooleanOperationsUtils.ExecuteBooleanOperation(solid1, solid2, BooleanOperationsType.Difference);
}
/// <summary>
/// Boolean intersect geometric operation, modify the original solid as the result
/// </summary>
/// <param name="solid1">Operation solid 1 and operation result</param>
/// <param name="solid2">Operation solid 2</param>
public static void BooleanOperation_Intersect(ref Solid solid1, Solid solid2)
{
BooleanOperationsUtils.ExecuteBooleanOperationModifyingOriginalSolid(solid1, solid2, BooleanOperationsType.Intersect);
}
/// <summary>
/// Boolean union geometric operation, modify the original solid as the result
/// </summary>
/// <param name="solid1">Operation solid 1 and operation result</param>
/// <param name="solid2">Operation solid 2</param>
public static void BooleanOperation_Union(ref Solid solid1, Solid solid2)
{
BooleanOperationsUtils.ExecuteBooleanOperationModifyingOriginalSolid(solid1, solid2, BooleanOperationsType.Union);
}
/// <summary>
/// Boolean difference geometric operation, modify the original solid as the result
/// </summary>
/// <param name="solid1">Operation solid 1 and operation result</param>
/// <param name="solid2">Operation solid 2</param>
public static void BooleanOperation_Difference(ref Solid solid1, Solid solid2)
{
BooleanOperationsUtils.ExecuteBooleanOperationModifyingOriginalSolid(solid1, solid2, BooleanOperationsType.Difference);
}
}
}
GeometryCreation.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.Text;
using Autodesk.Revit.DB;
namespace Revit.SDK.Samples.GeometryCreation_BooleanOperation.CS
{
class GeometryCreation
{
/// <summary>
/// The singleton instance of GeometryCreation
/// </summary>
private static GeometryCreation Instance;
/// <summary>
/// revit application
/// </summary>
private Autodesk.Revit.ApplicationServices.Application m_app;
/// <summary>
/// The direction of cylinder, only on X, Y, Z three axes forward direction
/// </summary>
public enum CylinderDirection
{
BasisX,
BasisY,
BasisZ
};
/// <summary>
/// Constructor
/// </summary>
/// <param name="app">Revit application</param>
private GeometryCreation(Autodesk.Revit.ApplicationServices.Application app)
{
m_app = app;
}
/// <summary>
/// Get the singleton instance of GeometryCreation
/// </summary>
/// <param name="app">Revit application</param>
/// <returns>The singleton instance of GeometryCreation</returns>
public static GeometryCreation getInstance(Autodesk.Revit.ApplicationServices.Application app)
{
if (Instance == null)
{
Instance = new GeometryCreation(app);
}
return Instance;
}
/// <summary>
/// Create an extrusion geometry
/// </summary>
/// <param name="profileLoops">The profile loops to be extruded</param>
/// <param name="extrusionDir">The direction in which to extrude the profile loops</param>
/// <param name="extrusionDist">The positive distance by which the loops are to be extruded</param>
/// <returns>The created solid</returns>
private Solid CreateExtrusion(List<CurveLoop> profileLoops, XYZ extrusionDir, double extrusionDist)
{
return GeometryCreationUtilities.CreateExtrusionGeometry(profileLoops, extrusionDir, extrusionDist);
}
/// <summary>
/// Create a revolved geometry
/// </summary>
/// <param name="coordinateFrame">A right-handed orthonormal frame of vectors</param>
/// <param name="profileLoops">The profile loops to be revolved</param>
/// <param name="startAngle">The start angle for the revolution</param>
/// <param name="endAngle">The end angle for the revolution</param>
/// <returns>The created solid</returns>
private Solid CreateRevolved(Autodesk.Revit.DB.Frame coordinateFrame, List<CurveLoop> profileLoops, double startAngle, double endAngle)
{
return GeometryCreationUtilities.CreateRevolvedGeometry(coordinateFrame, profileLoops, startAngle, endAngle);
}
/// <summary>
/// Create a swept geometry
/// </summary>
/// <param name="sweepPath">The sweep path, consisting of a set of contiguous curves</param>
/// <param name="pathAttachmentCrvIdx">The index of the curve in the sweep path where the profile loops are situated</param>
/// <param name="pathAttachmentParam">Parameter of the path curve specified by pathAttachmentCrvIdx</param>
/// <param name="profileLoops">The curve loops defining the planar domain to be swept along the path</param>
/// <returns>The created solid</returns>
private Solid CreateSwept(CurveLoop sweepPath, int pathAttachmentCrvIdx, double pathAttachmentParam, List<CurveLoop> profileLoops)
{
return GeometryCreationUtilities.CreateSweptGeometry(sweepPath, pathAttachmentCrvIdx, pathAttachmentParam, profileLoops);
}
/// <summary>
/// Create a blend geometry
/// </summary>
/// <param name="firstLoop">The first curve loop</param>
/// <param name="secondLoop">The second curve loop</param>
/// <param name="vertexPairs">This input specifies how the two profile loops should be connected</param>
/// <returns>The created solid</returns>
private Solid CreateBlend(CurveLoop firstLoop, CurveLoop secondLoop, List<VertexPair> vertexPairs)
{
return GeometryCreationUtilities.CreateBlendGeometry(firstLoop, secondLoop, vertexPairs);
}
/// <summary>
/// Create a swept and blend geometry
/// </summary>
/// <param name="pathCurve">The sweep path</param>
/// <param name="pathParams">An increasing sequence of parameters along the path curve</param>
/// <param name="profileLoops">Closed, planar curve loops arrayed along the path</param>
/// <param name="vertexPairs">This input specifies how adjacent profile loops should be connected</param>
/// <returns>The created solid</returns>
private Solid CreateSweptBlend(Curve pathCurve, List<double> pathParams, List<CurveLoop> profileLoops, List<ICollection<VertexPair>> vertexPairs)
{
return GeometryCreationUtilities.CreateSweptBlendGeometry(pathCurve, pathParams, profileLoops, vertexPairs);
}
/// <summary>
/// Create a centerbased box
/// </summary>
/// <param name="center">The given box center</param>
/// <param name="edgelength">The given box's edge length</param>
/// <returns>The created box</returns>
public Solid CreateCenterbasedBox(XYZ center, double edgelength)
{
double halfedgelength = edgelength / 2.0;
List<CurveLoop> profileloops = new List<CurveLoop>();
CurveLoop profileloop = new CurveLoop();
profileloop.Append(Line.CreateBound(
new XYZ(center.X - halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength),
new XYZ(center.X - halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength)));
profileloop.Append(Line.CreateBound(
new XYZ(center.X - halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength),
new XYZ(center.X + halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength)));
profileloop.Append(Line.CreateBound(
new XYZ(center.X + halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength),
new XYZ(center.X + halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength)));
profileloop.Append(Line.CreateBound(
new XYZ(center.X + halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength),
new XYZ(center.X - halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength)));
profileloops.Add(profileloop);
XYZ extrusiondir = new XYZ(0, 0, 1); // orthogonal
double extrusiondist = edgelength;
return GeometryCreationUtilities.CreateExtrusionGeometry(profileloops, extrusiondir, extrusiondist);
}
/// <summary>
/// Create a centerbased sphere
/// </summary>
/// <param name="center">The given sphere center</param>
/// <param name="radius">The given sphere's radius</param>
/// <returns>The created sphere</returns>
public Solid CreateCenterbasedSphere(XYZ center, double radius)
{
Frame frame = new Frame(center,
Autodesk.Revit.DB.XYZ.BasisX,
Autodesk.Revit.DB.XYZ.BasisY,
Autodesk.Revit.DB.XYZ.BasisZ);
List<CurveLoop> profileloops = new List<CurveLoop>();
CurveLoop profileloop = new CurveLoop();
Curve cemiEllipse = Ellipse.CreateCurve(center, radius, radius,
Autodesk.Revit.DB.XYZ.BasisX,
Autodesk.Revit.DB.XYZ.BasisZ,
-Math.PI / 2.0, Math.PI / 2.0);
profileloop.Append(cemiEllipse);
profileloop.Append(Line.CreateBound(
new XYZ(center.X, center.Y, center.Z + radius),
new XYZ(center.X, center.Y, center.Z - radius)));
profileloops.Add(profileloop);
return GeometryCreationUtilities.CreateRevolvedGeometry(frame, profileloops, -Math.PI, Math.PI);
}
/// <summary>
/// Create a centerbased cylinder, only on X, Y, Z three axes forward direction
/// </summary>
/// <param name="center">The given cylinder center</param>
/// <param name="bottomradius">The given cylinder's bottom radius</param>
/// <param name="height">The given cylinder's height</param>
/// <param name="cylinderdirection">Cylinder's extrusion direction</param>
/// <returns>The created cylinder</returns>
public Solid CreateCenterbasedCylinder(XYZ center, double bottomradius, double height, CylinderDirection cylinderdirection)
{
double halfheight = height / 2.0;
XYZ bottomcenter = new XYZ(
cylinderdirection == CylinderDirection.BasisX ? center.X - halfheight : center.X,
cylinderdirection == CylinderDirection.BasisY ? center.Y - halfheight : center.Y,
cylinderdirection == CylinderDirection.BasisZ ? center.Z - halfheight : center.Z);
XYZ topcenter = new XYZ(
cylinderdirection == CylinderDirection.BasisX ? center.X + halfheight : center.X,
cylinderdirection == CylinderDirection.BasisY ? center.Y + halfheight : center.Y,
cylinderdirection == CylinderDirection.BasisZ ? center.Z + halfheight : center.Z);
CurveLoop sweepPath = new CurveLoop();
sweepPath.Append(Line.CreateBound(bottomcenter,
topcenter));
List<CurveLoop> profileloops = new List<CurveLoop>();
CurveLoop profileloop = new CurveLoop();
Curve cemiEllipse1 = Ellipse.CreateCurve(bottomcenter, bottomradius, bottomradius,
cylinderdirection == CylinderDirection.BasisX ? Autodesk.Revit.DB.XYZ.BasisY : Autodesk.Revit.DB.XYZ.BasisX,
cylinderdirection == CylinderDirection.BasisZ ? Autodesk.Revit.DB.XYZ.BasisY : Autodesk.Revit.DB.XYZ.BasisZ,
-Math.PI, 0);
Curve cemiEllipse2 = Ellipse.CreateCurve(bottomcenter, bottomradius, bottomradius,
cylinderdirection == CylinderDirection.BasisX ? Autodesk.Revit.DB.XYZ.BasisY : Autodesk.Revit.DB.XYZ.BasisX,
cylinderdirection == CylinderDirection.BasisZ ? Autodesk.Revit.DB.XYZ.BasisY : Autodesk.Revit.DB.XYZ.BasisZ,
0, Math.PI);
profileloop.Append(cemiEllipse1);
profileloop.Append(cemiEllipse2);
profileloops.Add(profileloop);
return GeometryCreationUtilities.CreateSweptGeometry(sweepPath, 0, 0, profileloops);
}
}
}
版权所有 :无锡模信建筑科技有限公司 苏ICP备2021028830号-1 BIM建模|BIM技术应用|BIM软件开发
联系地址:江苏省无锡市新吴区龙山路4号B座705 手机:18761516598