Home > Back-end >  AutoCAD Create And Extrude A Circle Using VB.NET
AutoCAD Create And Extrude A Circle Using VB.NET

Time:07-06

I’m trying to create a circle and then extrude it to create a solid. I know there is a way to create a cylinder but I would prefer to do it this way.

My code below creates the circle just fine but I’m having trouble finding the correct way to extrude it.

Note: I know the line “acRegion1.extrude(Length)” is incorrect but I put it in there to demonstrate what I want.

Hoping someone can help me with this. Thanks in advance.

        <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

CodePudding user response:

You should use Editor.GetPoint and Editor.GetDistance instead of Editor.GetString because they ensure the result type (Point3d and Double) and offer some intersting options. You should check the PromptStatus value of the promptResult. You have to explicitely dispose of the newly created entities (circle and region) that you do not add to the Database.

    [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();
        }
  • Related