我试图创建一个圆,然后挤出它来创建一个实体。我知道有一种方法可以创建一个圆柱体,但我更喜欢这样做。
我下面的代码创建的圆圈很好,但我有困难找到正确的方式来挤压它。
注意:我知道行"acRegion1.extrude(Length)"是不正确的,但我把它放在那里来演示我想要的。
希望有人能帮我这个。提前谢谢。
<CommandMethod("DRAW_EXTRUDE_CIRCLE")>
Public Shared Sub DRAW_EXTRUDE_CIRCLE()
Dim doc As Document = AutoCADApp.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim PositionX As Single = ed.GetString("X").StringResult
Dim PositionY As Single = ed.GetString("Y").StringResult
Dim PositionZ As Single = ed.GetString("Z").StringResult
Dim Radius As Single = ed.GetString("Radius").StringResult
Dim Length As Single = ed.GetString("Length").StringResult
Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Dim Obj As Circle = New Circle()
With Obj
.SetDatabaseDefaults()
.Center = New Point3d(PositionX, PositionY, PositionZ)
.Radius = Radius
End With
'' Adds the circle to an object array
Dim acDBObjColl As DBObjectCollection = New DBObjectCollection()
acDBObjColl.Add(Obj)
'' Calculate the regions based on each closed loop
Dim myRegionColl As DBObjectCollection = New DBObjectCollection()
myRegionColl = Region.CreateFromCurves(acDBObjColl)
Dim acRegion1 As Region = myRegionColl(0)
acRegion1.extrude(Length)'this line is incorrect
btr.AppendEntity(Obj)
tr.AddNewlyCreatedDBObject(Obj, True)
tr.Commit()
End Using
End Sub
您应该使用编辑器。GetPoint和编辑器。GetDistance而不是Editor。GetString,因为它们确保了结果类型(Point3d和Double),并提供了一些有趣的选项。您应该检查prompresult的PromptStatus值。您必须显式地处理未添加到数据库中的新创建的实体(圆和区域)。
[CommandMethod("DRAW_EXTRUDE_CIRCLE")]
public static void DrawExtrudeCircle()
{
var doc = AcAp.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var promptCenter = ed.GetPoint("nCenter: ");
if (promptCenter.Status != PromptStatus.OK) return;
var promptRadius = ed.GetDistance("nRadius: ");
if (promptRadius.Status != PromptStatus.OK) return;
var promptLength = ed.GetDistance("nLength: ");
if (promptLength.Status != PromptStatus.OK) return;
using (var tr = db.TransactionManager.StartTransaction())
{
using (var circle = new Circle(promptCenter.Value, Vector3d.ZAxis, promptRadius.Value))
{
var curves = new DBObjectCollection();
curves.Add(circle);
var regions = Region.CreateFromCurves(curves);
using (var region = (Region)regions[0])
{
var solid = new Solid3d();
solid.Extrude(region, promptLength.Value, 0.0);
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
curSpace.AppendEntity(solid);
tr.AddNewlyCreatedDBObject(solid, true);
}
}
tr.Commit();
}