应用程序:BREPBuilder示例
Revit平台:所有
Revit版本:2018.0
首次发布用于:2017.0
编程语言:C#
技能等级:中等
类别:几何
类型:外部应用
主题:BRepBuilder使用示例
主题:使用BRepBuilder构建Revit几何图形的API中的几个案例。
相关类:
Autodesk.Revit.UI.IExternalApplication
Autodesk.Revit.UI.IExternalCommand
Autodesk.Revit.DB.BRepBuilder
Autodesk.Revit.DB.DirectShape
项目文件:
Application.cs它包含从接口IExternalApplication继承并实现OnStartup和OnShutdown方法的类Application。
CreateCube.cs它包含从接口IExternalCommand继承并实现Execute方法的类CreateCube。
CreateNURBS.cs它包含从接口IExternalCommand继承并实现Execute方法的类CreateNURBS。
CreatePeriodic.cs它包含从接口IExternalCommand继承并实现Execute方法的类CreatePeriodic。
功能:
此示例提供以下功能:
让用户使用BRepBuilder创建简单的立方体实体。
让用户使用BRepBuilder创建Nurbs曲面(打开外壳)。
允许用户使用BRepBuilder创建两个具有周期性面曲面的实体(截锥和圆柱体)。
然后在DirectShape元素中设置由BRepBuilder创建的几何图形,使其在Revit视图中可见。
实施:
打开Revit应用程序。打开空的建筑模板,然后创建三维视图。
请注意,额外的功能区面板名为“创建几何体”。它有三个按钮:
1.“CreateCube”。按下它可以使用BRepBuilder创建简单的立方体实体。
2.“创建Nurbs”。按下它可以使用BRepBuilder创建Nurbs曲面(开放外壳)。
3.“CreatePeriodic”。按此键可使用BRepBuilder创建具有周期性表面的两个实体(圆锥体和圆柱体)。
CreateCube.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;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.BRepBuilderExample.CS
{
/// <summary>
/// Implement method Execute of this class as an external command for Revit.
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class CreateCube : IExternalCommand
{
/// <summary>
/// Implement this method as an external command for Revit.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.</param>
/// <param name="message">A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.</param>
/// <param name="elements">A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.</param>
/// <returns>Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user canceled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.</returns>
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
_dbdocument = commandData.Application.ActiveUIDocument.Document;
try
{
Solid mySolid = CreateCubeImpl().GetResult();
if(null == mySolid)
return Result.Failed;
using (Autodesk.Revit.DB.Transaction tran = new Autodesk.Revit.DB.Transaction(_dbdocument, "CreateCube"))
{
tran.Start();
DirectShape dsCubed = DirectShape.CreateElement(_dbdocument, new ElementId(BuiltInCategory.OST_Walls));
if (null == dsCubed)
return Result.Failed;
dsCubed.ApplicationId = "TestCreateCube";
dsCubed.ApplicationDataId = "Cube";
List<GeometryObject> shapes = new List<GeometryObject>();
shapes.Add(mySolid);
dsCubed.SetShape(shapes, DirectShapeTargetViewType.Default);
tran.Commit();
}
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
return Result.Succeeded;
}
private BRepBuilder CreateCubeImpl()
{
// create a BRepBuilder; add faces to build a cube
BRepBuilder brepBuilder = new BRepBuilder(BRepType.Solid);
// a cube 100x100x100, from (0,0,0) to (100, 100, 100)
// 1. Planes.
// naming convention for faces and planes:
// We are looking at this cube in an isometric view. X is down and to the left of us, Y is horizontal and points to the right, Z is up.
// front and back faces are along the X axis, left and right are along the Y axis, top and bottom are along the Z axis.
Plane bottom = Plane.CreateByOriginAndBasis(new XYZ(50, 50, 0), new XYZ(1, 0, 0), new XYZ(0, 1, 0)); // bottom. XY plane, Z = 0, normal pointing inside the cube.
Plane top = Plane.CreateByOriginAndBasis(new XYZ(50, 50, 100), new XYZ(1, 0, 0), new XYZ(0, 1, 0)); // top. XY plane, Z = 100, normal pointing outside the cube.
Plane front = Plane.CreateByOriginAndBasis(new XYZ(100, 50, 50), new XYZ(0, 0, 1), new XYZ(0, 1, 0)); // front side. ZY plane, X = 0, normal pointing inside the cube.
Plane back = Plane.CreateByOriginAndBasis(new XYZ(0, 50, 50), new XYZ(0, 0, 1), new XYZ(0, 1, 0)); // back side. ZY plane, X = 0, normal pointing outside the cube.
Plane left = Plane.CreateByOriginAndBasis(new XYZ(50, 0, 50), new XYZ(0, 0, 1), new XYZ(1, 0, 0)); // left side. ZX plane, Y = 0, normal pointing inside the cube
Plane right = Plane.CreateByOriginAndBasis(new XYZ(50, 100, 50), new XYZ(0, 0, 1), new XYZ(1, 0, 0)); // right side. ZX plane, Y = 100, normal pointing outside the cube
//Note that the alternating of "inside/outside" matches the alternating of "true/false" in the next block that defines faces.
//There must be a correspondence to ensure that all faces are correctly oriented to point out of the solid.
// 2. Faces.
BRepBuilderGeometryId faceId_Bottom = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(bottom, null), true);
BRepBuilderGeometryId faceId_Top = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(top, null), false);
BRepBuilderGeometryId faceId_Front = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(front, null), true);
BRepBuilderGeometryId faceId_Back = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(back, null), false);
BRepBuilderGeometryId faceId_Left = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(left, null), true);
BRepBuilderGeometryId faceId_Right = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(right, null), false);
// 3. Edges.
// 3.a (define edge geometry)
// walk around bottom face
BRepBuilderEdgeGeometry edgeBottomFront = BRepBuilderEdgeGeometry.Create(new XYZ(100, 0, 0), new XYZ(100, 100, 0));
BRepBuilderEdgeGeometry edgeBottomRight = BRepBuilderEdgeGeometry.Create(new XYZ(100, 100, 0), new XYZ(0, 100, 0));
BRepBuilderEdgeGeometry edgeBottomBack = BRepBuilderEdgeGeometry.Create(new XYZ(0, 100, 0), new XYZ(0, 0, 0));
BRepBuilderEdgeGeometry edgeBottomLeft = BRepBuilderEdgeGeometry.Create(new XYZ(0, 0, 0), new XYZ(100, 0, 0));
// now walk around top face
BRepBuilderEdgeGeometry edgeTopFront = BRepBuilderEdgeGeometry.Create(new XYZ(100, 0, 100), new XYZ(100, 100, 100));
BRepBuilderEdgeGeometry edgeTopRight = BRepBuilderEdgeGeometry.Create(new XYZ(100, 100, 100), new XYZ(0, 100, 100));
BRepBuilderEdgeGeometry edgeTopBack = BRepBuilderEdgeGeometry.Create(new XYZ(0, 100, 100), new XYZ(0, 0, 100));
BRepBuilderEdgeGeometry edgeTopLeft = BRepBuilderEdgeGeometry.Create(new XYZ(0, 0, 100), new XYZ(100, 0, 100));
// sides
BRepBuilderEdgeGeometry edgeFrontRight = BRepBuilderEdgeGeometry.Create(new XYZ(100, 100, 0), new XYZ(100, 100, 100));
BRepBuilderEdgeGeometry edgeRightBack = BRepBuilderEdgeGeometry.Create(new XYZ(0, 100, 0), new XYZ(0, 100, 100));
BRepBuilderEdgeGeometry edgeBackLeft = BRepBuilderEdgeGeometry.Create(new XYZ(0, 0, 0), new XYZ(0, 0, 100));
BRepBuilderEdgeGeometry edgeLeftFront = BRepBuilderEdgeGeometry.Create(new XYZ(100, 0, 0), new XYZ(100, 0, 100));
// 3.b (define the edges themselves)
BRepBuilderGeometryId edgeId_BottomFront = brepBuilder.AddEdge(edgeBottomFront);
BRepBuilderGeometryId edgeId_BottomRight = brepBuilder.AddEdge(edgeBottomRight);
BRepBuilderGeometryId edgeId_BottomBack = brepBuilder.AddEdge(edgeBottomBack);
BRepBuilderGeometryId edgeId_BottomLeft = brepBuilder.AddEdge(edgeBottomLeft);
BRepBuilderGeometryId edgeId_TopFront = brepBuilder.AddEdge(edgeTopFront);
BRepBuilderGeometryId edgeId_TopRight = brepBuilder.AddEdge(edgeTopRight);
BRepBuilderGeometryId edgeId_TopBack = brepBuilder.AddEdge(edgeTopBack);
BRepBuilderGeometryId edgeId_TopLeft = brepBuilder.AddEdge(edgeTopLeft);
BRepBuilderGeometryId edgeId_FrontRight = brepBuilder.AddEdge(edgeFrontRight);
BRepBuilderGeometryId edgeId_RightBack = brepBuilder.AddEdge(edgeRightBack);
BRepBuilderGeometryId edgeId_BackLeft = brepBuilder.AddEdge(edgeBackLeft);
BRepBuilderGeometryId edgeId_LeftFront = brepBuilder.AddEdge(edgeLeftFront);
// 4. Loops.
BRepBuilderGeometryId loopId_Bottom = brepBuilder.AddLoop(faceId_Bottom);
BRepBuilderGeometryId loopId_Top = brepBuilder.AddLoop(faceId_Top);
BRepBuilderGeometryId loopId_Front = brepBuilder.AddLoop(faceId_Front);
BRepBuilderGeometryId loopId_Back = brepBuilder.AddLoop(faceId_Back);
BRepBuilderGeometryId loopId_Right = brepBuilder.AddLoop(faceId_Right);
BRepBuilderGeometryId loopId_Left = brepBuilder.AddLoop(faceId_Left);
// 5. Co-edges.
// Bottom face. All edges reversed
brepBuilder.AddCoEdge(loopId_Bottom, edgeId_BottomFront, true); // other direction in front loop
brepBuilder.AddCoEdge(loopId_Bottom, edgeId_BottomLeft, true); // other direction in left loop
brepBuilder.AddCoEdge(loopId_Bottom, edgeId_BottomBack, true); // other direction in back loop
brepBuilder.AddCoEdge(loopId_Bottom, edgeId_BottomRight, true); // other direction in right loop
brepBuilder.FinishLoop(loopId_Bottom);
brepBuilder.FinishFace(faceId_Bottom);
// Top face. All edges NOT reversed.
brepBuilder.AddCoEdge(loopId_Top, edgeId_TopFront, false); // other direction in front loop.
brepBuilder.AddCoEdge(loopId_Top, edgeId_TopRight, false); // other direction in right loop
brepBuilder.AddCoEdge(loopId_Top, edgeId_TopBack, false); // other direction in back loop
brepBuilder.AddCoEdge(loopId_Top, edgeId_TopLeft, false); // other direction in left loop
brepBuilder.FinishLoop(loopId_Top);
brepBuilder.FinishFace(faceId_Top);
// Front face.
brepBuilder.AddCoEdge(loopId_Front, edgeId_BottomFront, false); // other direction in bottom loop
brepBuilder.AddCoEdge(loopId_Front, edgeId_FrontRight, false); // other direction in right loop
brepBuilder.AddCoEdge(loopId_Front, edgeId_TopFront, true); // other direction in top loop.
brepBuilder.AddCoEdge(loopId_Front, edgeId_LeftFront, true); // other direction in left loop.
brepBuilder.FinishLoop(loopId_Front);
brepBuilder.FinishFace(faceId_Front);
// Back face
brepBuilder.AddCoEdge(loopId_Back, edgeId_BottomBack, false); // other direction in bottom loop
brepBuilder.AddCoEdge(loopId_Back, edgeId_BackLeft, false); // other direction in left loop.
brepBuilder.AddCoEdge(loopId_Back, edgeId_TopBack, true); // other direction in top loop
brepBuilder.AddCoEdge(loopId_Back, edgeId_RightBack, true); // other direction in right loop.
brepBuilder.FinishLoop(loopId_Back);
brepBuilder.FinishFace(faceId_Back);
// Right face
brepBuilder.AddCoEdge(loopId_Right, edgeId_BottomRight, false); // other direction in bottom loop
brepBuilder.AddCoEdge(loopId_Right, edgeId_RightBack, false); // other direction in back loop
brepBuilder.AddCoEdge(loopId_Right, edgeId_TopRight, true); // other direction in top loop
brepBuilder.AddCoEdge(loopId_Right, edgeId_FrontRight, true); // other direction in front loop
brepBuilder.FinishLoop(loopId_Right);
brepBuilder.FinishFace(faceId_Right);
// Left face
brepBuilder.AddCoEdge(loopId_Left, edgeId_BottomLeft, false); // other direction in bottom loop
brepBuilder.AddCoEdge(loopId_Left, edgeId_LeftFront, false); // other direction in front loop
brepBuilder.AddCoEdge(loopId_Left, edgeId_TopLeft, true); // other direction in top loop
brepBuilder.AddCoEdge(loopId_Left, edgeId_BackLeft, true); // other direction in back loop
brepBuilder.FinishLoop(loopId_Left);
brepBuilder.FinishFace(faceId_Left);
brepBuilder.Finish();
return brepBuilder;
}
private Document _dbdocument = null;
}
}
CreateNURBS.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;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.BRepBuilderExample.CS
{
/// <summary>
/// Implement method Execute of this class as an external command for Revit.
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class CreateNURBS : IExternalCommand
{
/// <summary>
/// Implement this method as an external command for Revit.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.</param>
/// <param name="message">A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.</param>
/// <param name="elements">A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.</param>
/// <returns>Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user canceled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.</returns>
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
_dbdocument = commandData.Application.ActiveUIDocument.Document;
try
{
using (Autodesk.Revit.DB.Transaction tr = new Autodesk.Revit.DB.Transaction(_dbdocument, "CreateNURBS"))
{
tr.Start();
DirectShape myDirectShape = DirectShape.CreateElement(_dbdocument, new ElementId(BuiltInCategory.OST_GenericModel));
myDirectShape.ApplicationId = "TestCreateNURBS";
myDirectShape.ApplicationDataId = "NURBS";
if(null != myDirectShape)
myDirectShape.SetShape(CreateNurbsSurface());
tr.Commit();
}
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
return Result.Succeeded;
}
private BRepBuilder CreateNurbsSurface()
{
//Note that we are not creating a closed solid here. It is an open shell.
BRepBuilder brepBuilder = new BRepBuilder(BRepType.OpenShell);
// Create NURBS surface
IList<double> knotsU = new List<double> { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 };
IList<double> knotsV = new List<double> { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 };
int degreeU = 4;
int degreeV = 4;
IList<XYZ> controlPoints = new List<XYZ>
{
new XYZ(0, 0, 0), new XYZ(0, 20, 0), new XYZ(0, 40, 0), new XYZ(0, 60, 0), new XYZ(0, 80, 0),
new XYZ(20, 0, 100), new XYZ(20, 20, 200), new XYZ(20, 40, 300), new XYZ(20, 60, 200), new XYZ(20, 80, 100),
new XYZ(40, 0, -100), new XYZ(40, 20, -250), new XYZ(40, 40, -300), new XYZ(40, 60, -250), new XYZ(40, 80, -100),
new XYZ(60, 0, 100), new XYZ(60, 20, 200), new XYZ(60, 40, 300), new XYZ(60, 60, 200), new XYZ(60, 80, 100),
new XYZ(80, 0, 0), new XYZ(80, 20, 0), new XYZ(80, 40, 0), new XYZ(80, 60, 0), new XYZ(80, 80, 0)
};
BRepBuilderSurfaceGeometry nurbsSurface = BRepBuilderSurfaceGeometry.CreateNURBSSurface(degreeU, degreeV, knotsU, knotsV, controlPoints, false/*bReverseOrientation*/, null /*pSurfaceEnvelope*/);
// Create 4 NURBS curves defining the boundary of the NURBS surface that has just been created
IList<double> weights = new List<double> { 1, 1, 1, 1, 1 };
IList<XYZ> backEdgeControlPoints = new List<XYZ> { new XYZ(0, 0, 0), new XYZ(0, 20, 0), new XYZ(0, 40, 0), new XYZ(0, 60, 0), new XYZ(0, 80, 0) };
Curve backNurbs = NurbSpline.CreateCurve(4, knotsU, backEdgeControlPoints, weights);
BRepBuilderEdgeGeometry backEdge = BRepBuilderEdgeGeometry.Create(backNurbs);
IList<XYZ> frontEdgeControlPoints = new List<XYZ> { new XYZ(80, 0, 0), new XYZ(80, 20, 0), new XYZ(80, 40, 0), new XYZ(80, 60, 0), new XYZ(80, 80, 0) };
Curve frontNurbs = NurbSpline.CreateCurve(4, knotsU, frontEdgeControlPoints, weights);
BRepBuilderEdgeGeometry frontEdge = BRepBuilderEdgeGeometry.Create(frontNurbs);
IList<XYZ> leftEdgeControlPoints = new List<XYZ> { new XYZ(0, 0, 0), new XYZ(20, 0, 100), new XYZ(40, 0, -100), new XYZ(60, 0, 100), new XYZ(80, 0, 0) };
Curve leftNurbs = NurbSpline.CreateCurve(4, knotsU, leftEdgeControlPoints, weights);
BRepBuilderEdgeGeometry leftEdge = BRepBuilderEdgeGeometry.Create(leftNurbs);
IList<XYZ> rightEdgeControlPoints = new List<XYZ> { new XYZ(0, 80, 0), new XYZ(20, 80, 100), new XYZ(40, 80, -100), new XYZ(60, 80, 100), new XYZ(80, 80, 0) };
Curve rightNurbs = NurbSpline.CreateCurve(4, knotsU, rightEdgeControlPoints, weights);
BRepBuilderEdgeGeometry rightEdge = BRepBuilderEdgeGeometry.Create(rightNurbs);
// Add the geometries to the brepBuilder
BRepBuilderGeometryId nurbSplineFaceId = brepBuilder.AddFace(nurbsSurface, false);
BRepBuilderGeometryId loopId = brepBuilder.AddLoop(nurbSplineFaceId);
BRepBuilderGeometryId backEdgeId = brepBuilder.AddEdge(backEdge);
BRepBuilderGeometryId frontEdgeId = brepBuilder.AddEdge(frontEdge);
BRepBuilderGeometryId leftEdgeId = brepBuilder.AddEdge(leftEdge);
BRepBuilderGeometryId rightEdgeId = brepBuilder.AddEdge(rightEdge);
// Add each edge to the loop
brepBuilder.AddCoEdge(loopId, backEdgeId, true);
brepBuilder.AddCoEdge(loopId, leftEdgeId, false);
brepBuilder.AddCoEdge(loopId, frontEdgeId, false);
brepBuilder.AddCoEdge(loopId, rightEdgeId, true);
brepBuilder.FinishLoop(loopId);
brepBuilder.FinishFace(nurbSplineFaceId);
brepBuilder.Finish();
return brepBuilder;
}
private Document _dbdocument = null;
}
}
CreatePeriodic.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;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.BRepBuilderExample.CS
{
/// <summary>
/// Implement method Execute of this class as an external command for Revit.
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class CreatePeriodic : IExternalCommand
{
/// <summary>
/// Implement this method as an external command for Revit.
/// </summary>
/// <param name="commandData">An object that is passed to the external application
/// which contains data related to the command,
/// such as the application object and active view.</param>
/// <param name="message">A message that can be set by the external application
/// which will be displayed if a failure or cancellation is returned by
/// the external command.</param>
/// <param name="elements">A set of elements to which the external application
/// can add elements that are to be highlighted in case of failure or cancellation.</param>
/// <returns>Return the status of the external command.
/// A result of Succeeded means that the API external method functioned as expected.
/// Cancelled can be used to signify that the user canceled the external operation
/// at some point. Failure should be returned if the application is unable to proceed with
/// the operation.</returns>
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
_dbdocument = commandData.Application.ActiveUIDocument.Document;
try
{
CreateCylinder();
CreateTruncatedCone();
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
return Result.Succeeded;
}
/// <summary>
/// Create a DirectShape element from a BRepBuilder object and keep it in the _dbdocument.
/// The main purpose is to display the BRepBuilder objects created.
/// In this function, the BrepBuilder is directly set to a DirectShape.
/// </summary>
/// <param name="myBRepBuilder"> The BRepBuilder object.</param>
/// <param name="name"> Name of the BRepBuilder object, which will be passed on to the DirectShape creation method.</param>
private void createDirectShapeElementFromBrepBuilderObject(BRepBuilder myBRepBuilder, String name)
{
if (!myBRepBuilder.IsResultAvailable())
return;
using (Transaction tr = new Transaction(_dbdocument, "Create a DirectShape"))
{
tr.Start();
DirectShape myDirectShape = DirectShape.CreateElement(_dbdocument, new ElementId(BuiltInCategory.OST_GenericModel));
myDirectShape.ApplicationId = "TestBRepBuilder";
myDirectShape.ApplicationDataId = name;
if(null != myDirectShape)
myDirectShape.SetShape(myBRepBuilder);
tr.Commit();
}
}
private void CreateCylinder()
{
// Naming convention for faces and edges: we assume that x is to the left and pointing down, y is horizontal and pointing to the right, z is up
BRepBuilder brepBuilder = new BRepBuilder(BRepType.Solid);
// The surfaces of the four faces.
Frame basis = new Frame(new XYZ(50, -100, 0), new XYZ(0, 1, 0), new XYZ(-1, 0, 0), new XYZ(0, 0, 1));
// Note that we do not have to create two identical surfaces here. The same surface can be used for multiple faces,
// since BRepBuilderSurfaceGeometry::Create() copies the input surface.
// Thus, potentially we could have only one surface here,
// but we must create at least two faces below to account for periodicity.
CylindricalSurface frontCylSurf = CylindricalSurface.Create(basis, 50);
CylindricalSurface backCylSurf = CylindricalSurface.Create(basis, 50);
Plane top = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), new XYZ(0, 0, 100)); // normal points outside the cylinder
Plane bottom = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), new XYZ(0, 0, 0)); // normal points inside the cylinder
// Note that the alternating of "inside/outside" matches the alternating of "true/false" in the next block that defines faces.
// There must be a correspondence to ensure that all faces are correctly oriented to point out of the solid.
// Add the four faces
BRepBuilderGeometryId frontCylFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(frontCylSurf, null), false);
BRepBuilderGeometryId backCylFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(backCylSurf, null), false);
BRepBuilderGeometryId topFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(top, null), false);
BRepBuilderGeometryId bottomFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(bottom, null), true);
// Geometry for the four semi-circular edges and two vertical linear edges
BRepBuilderEdgeGeometry frontEdgeBottom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(0, -100, 0), new XYZ(100, -100, 0), new XYZ(50, -50, 0)));
BRepBuilderEdgeGeometry backEdgeBottom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(100, -100, 0), new XYZ(0, -100, 0), new XYZ(50, -150, 0)));
BRepBuilderEdgeGeometry frontEdgeTop = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(0, -100, 100), new XYZ(100, -100, 100), new XYZ(50, -50, 100)));
BRepBuilderEdgeGeometry backEdgeTop = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(0, -100, 100), new XYZ(100, -100, 100), new XYZ(50, -150, 100)));
BRepBuilderEdgeGeometry linearEdgeFront = BRepBuilderEdgeGeometry.Create(new XYZ(100, -100, 0), new XYZ(100, -100, 100));
BRepBuilderEdgeGeometry linearEdgeBack = BRepBuilderEdgeGeometry.Create(new XYZ(0, -100, 0), new XYZ(0, -100, 100));
// Add the six edges
BRepBuilderGeometryId frontEdgeBottomId = brepBuilder.AddEdge(frontEdgeBottom);
BRepBuilderGeometryId frontEdgeTopId = brepBuilder.AddEdge(frontEdgeTop);
BRepBuilderGeometryId linearEdgeFrontId = brepBuilder.AddEdge(linearEdgeFront);
BRepBuilderGeometryId linearEdgeBackId = brepBuilder.AddEdge(linearEdgeBack);
BRepBuilderGeometryId backEdgeBottomId = brepBuilder.AddEdge(backEdgeBottom);
BRepBuilderGeometryId backEdgeTopId = brepBuilder.AddEdge(backEdgeTop);
// Loops of the four faces
BRepBuilderGeometryId loopId_Top = brepBuilder.AddLoop(topFaceId);
BRepBuilderGeometryId loopId_Bottom = brepBuilder.AddLoop(bottomFaceId);
BRepBuilderGeometryId loopId_Front = brepBuilder.AddLoop(frontCylFaceId);
BRepBuilderGeometryId loopId_Back = brepBuilder.AddLoop(backCylFaceId);
// Add coedges for the loop of the front face
brepBuilder.AddCoEdge(loopId_Front, linearEdgeBackId, false);
brepBuilder.AddCoEdge(loopId_Front, frontEdgeTopId, false);
brepBuilder.AddCoEdge(loopId_Front, linearEdgeFrontId, true);
brepBuilder.AddCoEdge(loopId_Front, frontEdgeBottomId, true);
brepBuilder.FinishLoop(loopId_Front);
brepBuilder.FinishFace(frontCylFaceId);
// Add coedges for the loop of the back face
brepBuilder.AddCoEdge(loopId_Back, linearEdgeBackId, true);
brepBuilder.AddCoEdge(loopId_Back, backEdgeBottomId, true);
brepBuilder.AddCoEdge(loopId_Back, linearEdgeFrontId, false);
brepBuilder.AddCoEdge(loopId_Back, backEdgeTopId, true);
brepBuilder.FinishLoop(loopId_Back);
brepBuilder.FinishFace(backCylFaceId);
// Add coedges for the loop of the top face
brepBuilder.AddCoEdge(loopId_Top, backEdgeTopId, false);
brepBuilder.AddCoEdge(loopId_Top, frontEdgeTopId, true);
brepBuilder.FinishLoop(loopId_Top);
brepBuilder.FinishFace(topFaceId);
// Add coedges for the loop of the bottom face
brepBuilder.AddCoEdge(loopId_Bottom, frontEdgeBottomId, false);
brepBuilder.AddCoEdge(loopId_Bottom, backEdgeBottomId, false);
brepBuilder.FinishLoop(loopId_Bottom);
brepBuilder.FinishFace(bottomFaceId);
brepBuilder.Finish();
createDirectShapeElementFromBrepBuilderObject(brepBuilder, "Full cylinder");
}
private void CreateTruncatedCone()
{
BRepBuilder brepBuilder = new BRepBuilder(BRepType.Solid);
Plane bottom = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, -1), new XYZ(0, 0, 0));
Plane top = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), new XYZ(0, 0, 50));
Frame basis = new Frame(new XYZ(0, 0, 100), new XYZ(0, 1, 0), new XYZ(1, 0, 0), new XYZ(0, 0, -1));
// Note that we do not have to create two identical surfaces here. The same surface can be used for multiple faces,
// since BRepBuilderSurfaceGeometry::Create() copies the input surface.
// Thus, potentially we could have only one surface here,
// but we must create at least two faces below to account for periodicity.
ConicalSurface rightConicalSurface = ConicalSurface.Create(basis, Math.Atan(0.5));
ConicalSurface leftConicalSurface = ConicalSurface.Create(basis, Math.Atan(0.5));
// Create 4 faces of the cone
BRepBuilderGeometryId topFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(top, null), false);
BRepBuilderGeometryId bottomFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(bottom, null), false);
BRepBuilderGeometryId rightSideFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(rightConicalSurface, null), false);
BRepBuilderGeometryId leftSideFaceId = brepBuilder.AddFace(BRepBuilderSurfaceGeometry.Create(leftConicalSurface, null), false);
// Create 2 edges at the bottom of the cone
BRepBuilderEdgeGeometry bottomRightEdgeGeom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(-50, 0, 0), new XYZ(50, 0, 0), new XYZ(0, 50, 0)));
BRepBuilderEdgeGeometry bottomLeftEdgeGeom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(50, 0, 0), new XYZ(-50, 0, 0), new XYZ(0, -50, 0)));
// Create 2 edges at the top of the cone
BRepBuilderEdgeGeometry topLeftEdgeGeom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(-25, 0, 50), new XYZ(25, 0, 50), new XYZ(0, -25, 50)));
BRepBuilderEdgeGeometry topRightEdgeGeom = BRepBuilderEdgeGeometry.Create(Arc.Create(new XYZ(25, 0, 50), new XYZ(-25, 0, 50), new XYZ(0, 25, 50)));
// Create 2 side edges of the cone
BRepBuilderEdgeGeometry sideFrontEdgeGeom = BRepBuilderEdgeGeometry.Create(new XYZ(25, 0, 50), new XYZ(50, 0, 0));
BRepBuilderEdgeGeometry sideBackEdgeGeom = BRepBuilderEdgeGeometry.Create(new XYZ(-25, 0, 50), new XYZ(-50, 0, 0));
BRepBuilderGeometryId bottomRightId = brepBuilder.AddEdge(bottomRightEdgeGeom);
BRepBuilderGeometryId bottomLeftId = brepBuilder.AddEdge(bottomLeftEdgeGeom);
BRepBuilderGeometryId topRightEdgeId = brepBuilder.AddEdge(topRightEdgeGeom);
BRepBuilderGeometryId topLeftEdgeId = brepBuilder.AddEdge(topLeftEdgeGeom);
BRepBuilderGeometryId sideFrontEdgeid = brepBuilder.AddEdge(sideFrontEdgeGeom);
BRepBuilderGeometryId sideBackEdgeId = brepBuilder.AddEdge(sideBackEdgeGeom);
// Create bottom face
BRepBuilderGeometryId bottomLoopId = brepBuilder.AddLoop(bottomFaceId);
brepBuilder.AddCoEdge(bottomLoopId, bottomRightId, false);
brepBuilder.AddCoEdge(bottomLoopId, bottomLeftId, false);
brepBuilder.FinishLoop(bottomLoopId);
brepBuilder.FinishFace(bottomFaceId);
// Create top face
BRepBuilderGeometryId topLoopId = brepBuilder.AddLoop(topFaceId);
brepBuilder.AddCoEdge(topLoopId, topLeftEdgeId, false);
brepBuilder.AddCoEdge(topLoopId, topRightEdgeId, false);
brepBuilder.FinishLoop(topLoopId);
brepBuilder.FinishFace(topFaceId);
// Create right face
BRepBuilderGeometryId rightLoopId = brepBuilder.AddLoop(rightSideFaceId);
brepBuilder.AddCoEdge(rightLoopId, topRightEdgeId, true);
brepBuilder.AddCoEdge(rightLoopId, sideFrontEdgeid, false);
brepBuilder.AddCoEdge(rightLoopId, bottomRightId, true);
brepBuilder.AddCoEdge(rightLoopId, sideBackEdgeId, true);
brepBuilder.FinishLoop(rightLoopId);
brepBuilder.FinishFace(rightSideFaceId);
// Create left face
BRepBuilderGeometryId leftLoopId = brepBuilder.AddLoop(leftSideFaceId);
brepBuilder.AddCoEdge(leftLoopId, topLeftEdgeId, true);
brepBuilder.AddCoEdge(leftLoopId, sideBackEdgeId, false);
brepBuilder.AddCoEdge(leftLoopId, bottomLeftId, true);
brepBuilder.AddCoEdge(leftLoopId, sideFrontEdgeid, true);
brepBuilder.FinishLoop(leftLoopId);
brepBuilder.FinishFace(leftSideFaceId);
brepBuilder.Finish();
createDirectShapeElementFromBrepBuilderObject(brepBuilder, "Cone surface");
}
private Document _dbdocument = null;
}
}
版权所有 :无锡模信建筑科技有限公司 苏ICP备2021028830号-1 BIM建模|BIM技术应用|BIM软件开发
联系地址:江苏省无锡市新吴区龙山路4号B座705 手机:18761516598