Home > Back-end >  AUTOCAD Create A Polyline With An Arc .NET
AUTOCAD Create A Polyline With An Arc .NET

Time:07-15

I'm trying to create a polyline as shown in the image below (without the dimensions). My code does work with creating 3 segments, however it's the part where I'm trying to create a concave radius is where I'm having problems.

My code is using a bulge to try and create it which I believe is incorrect, however I'm not sure to convert the polyline segment to an arc.

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

enter image description here

Here is my code.

        <CommandMethod("CORNERRADIUS2")>
    Public Shared Sub CORNERRADIUS2()

        Dim doc = AutoCADApp.DocumentManager.MdiActiveDocument
        Dim db = doc.Database
        Dim ed = doc.Editor

        Dim PositionX As Double = ed.GetString("X").StringResult
        Dim PositionY As Double = ed.GetString("Y").StringResult
        Dim PositionZ As Double = ed.GetString("Z").StringResult

        Dim Radius As Double = ed.GetString("Radius").StringResult

        Using tr = db.TransactionManager.StartTransaction()

            Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
            Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

            Dim acPoly As Polyline = New Polyline()

            With acPoly
                .SetDatabaseDefaults()
                .AddVertexAt(0, New Point2d(PositionX   Radius, PositionY), 0, 0, 0)
                .AddVertexAt(1, New Point2d(PositionX, PositionY), 0, 0, 0)
                .AddVertexAt(2, New Point2d(PositionX, PositionY   Radius), 0, 0, 0)
                .SetBulgeAt(2, Radius) 'This line is incorrect
                .Closed = True
            End With

            btr.AppendEntity(acPoly)
            tr.AddNewlyCreatedDBObject(acPoly, True)
            tr.Commit()

        End Using

    End Sub

CodePudding user response:

The 'bulge' of a polyline segment is equal to the tangent of the quarter o the arc angle, in other words, the ratio between the arc sagitta and the the half of the arc chord. The bulge is negative if the arc is clockwise and positive if the arc is counter-clockwise.

One more time, using Editor.GetString to prompt for anything else than a string is a bad practice. enter image description here

    [CommandMethod("CORNERRADIUS2")]
    public static void CornerRadius2()
    {
        var doc = Application.DocumentManager.MdiActiveDocument;
        var db = doc.Database;
        var ed = doc.Editor;

        var promptPointResult = ed.GetPoint("\nSpecify point: ");
        if (promptPointResult.Status != PromptStatus.OK)
            return;
        var point = promptPointResult.Value;

        var promptDistanceOptions = new PromptDistanceOptions("\nRadius: ");
        promptDistanceOptions.BasePoint = point;
        promptDistanceOptions.UseBasePoint = true;
        var promptDistanceResult = ed.GetDistance(promptDistanceOptions);
        if (promptDistanceResult.Status != PromptStatus.OK)
            return;
        double radius = promptDistanceResult.Value;

        using (var tr = db.TransactionManager.StartTransaction())
        {
            var pline = new Polyline();
            double bulge = Math.Tan(Math.PI / 8.0);
            pline.AddVertexAt(0, new Point2d(point.X   radius, point.Y), 0.0, 0.0, 0.0);
            pline.AddVertexAt(1, new Point2d(point.X, point.Y), 0.0, 0.0, 0.0);
            pline.AddVertexAt(2, new Point2d(point.X, point.Y   radius), bulge, 0.0, 0.0);
            pline.Closed = true;
            pline.TransformBy(ed.CurrentUserCoordinateSystem);
            var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
            curSpace.AppendEntity(pline);
            tr.AddNewlyCreatedDBObject(pline, true);
            tr.Commit();
        }
    }
  • Related