创建几何模型的方法有拉伸、融合、旋转、放样、放样融合等,接下来介绍拉伸和放样两种方法创建一个立方体。
1、拉伸
[Transaction(TransactionMode.Manual)]
public class CreateExtrusionGeometry : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document doc = commandData.Application.ActiveUIDocument.Document;
//创建一个长方体拉伸模型
using (Transaction tran = new Transaction(doc,"拉伸"))
{
tran.Start();
Line line1 = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(10, 0, 0));
Line line2 = Line.CreateBound(new XYZ(10, 0, 0), new XYZ(10, 10, 0));
Line line3 = Line.CreateBound(new XYZ(10, 10, 0), new XYZ(0, 10, 0));
Line line4 = Line.CreateBound(new XYZ(0, 10, 0), new XYZ(0, 0, 0));
CurveLoop loop = new CurveLoop();
loop.Append(line1);
loop.Append(line2);
loop.Append(line3);
loop.Append(line4);
List<CurveLoop> loops = new List<CurveLoop>() { loop };
XYZ direction = new XYZ(0, 1, 1);//拉伸方向
Solid solid = GeometryCreationUtilities.CreateExtrusionGeometry(loops, direction, 100);
DirectShape shape = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_StructuralFoundation));
shape.AppendShape(new List<GeometryObject>() { solid });
tran.Commit();
}
return Result.Succeeded;
}
}
2、放样
[Transaction(TransactionMode.Manual)]
public class CreateSweptGeometry : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document doc = commandData.Application.ActiveUIDocument.Document;
using(Transaction tran = new Transaction(doc,"放样"))
{
tran.Start();
Line line1 = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(10, 0, 0));
Line line2 = Line.CreateBound(new XYZ(10, 0, 0), new XYZ(10, 10, 0));
Line line3 = Line.CreateBound(new XYZ(10, 10, 0), new XYZ(0, 10, 0));
Line line4 = Line.CreateBound(new XYZ(0, 10, 0), new XYZ(0, 0, 0));
CurveLoop loop = new CurveLoop();
loop.Append(line1);
loop.Append(line2);
loop.Append(line3);
loop.Append(line4);
List<CurveLoop> loops = new List<CurveLoop>() { loop };
Line path = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(0, 0, 10));
CurveLoop pathLoop = new CurveLoop();
pathLoop.Append(path);
Solid solid = GeometryCreationUtilities.CreateSweptGeometry(pathLoop, 0, path.GetEndParameter(0), loops);
DirectShape shape = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_StructuralFoundation));
shape.AppendShape(new List<GeometryObject>() { solid });
tran.Commit();
}
return Result.Succeeded;
}
}
效果