Creating polyLine Arc using C# for AutoCAD 2014

Creating polyLine Arc using C# for AutoCAD 2014

kite15
Advocate Advocate
8,710 Views
12 Replies
Message 1 of 13

Creating polyLine Arc using C# for AutoCAD 2014

kite15
Advocate
Advocate

Hi,
I am new in C# coding to drive the AutoCAD interface. Can any one help to draw a Polyline using C# Code as like attached snap for AutoCAd 2014 ??.

User can provide the PromptPointOptions !!!.

Thanks.

 

 

0 Likes
Accepted solutions (1)
8,711 Views
12 Replies
Replies (12)
Message 2 of 13

_gile
Consultant
Consultant
Accepted solution

Hi,

 

There is a lack of information to respond to your request.
What exactly are the input data?

AutoCAD manages the arc polyline segments with the bulge concept. The bulge of a segment is equal to the tangent of the quarter of the angle of the arc (IOW, the arc sagitta divided by half of the chord).

Here is an example that reproduces the behavior of the
_PLINE native command by computing the bulge so that the arc is tangent to the previous segment.

 

        [CommandMethod("Test")]
        public void Test()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            // prompt for first point
            var options = new PromptPointOptions("\nFirst point: ");
            var result = ed.GetPoint(options);
            if (result.Status != PromptStatus.OK)
                return;
            var pt1 = result.Value;

            // prompt for second point
            options.Message = "\nSecond point: ";
            options.BasePoint = pt1;
            options.UseBasePoint = true;
            result = ed.GetPoint(options);
            if (result.Status != PromptStatus.OK)
                return;
            var pt2 = result.Value;

            // prompt for third point
            options.Message = "\nThird point: ";
            options.BasePoint = pt2;
            result = ed.GetPoint(options);
            if (result.Status != PromptStatus.OK)
                return;
            var pt3 = result.Value;

            // convert points to 2d points
            var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
            var p1 = pt1.Convert2d(plane);
            var p2 = pt2.Convert2d(plane);
            var p3 = pt3.Convert2d(plane);

            // compute the bulge of the second segment
            var angle1 = p1.GetVectorTo(p2).Angle;
            var angle2 = p2.GetVectorTo(p3).Angle;
            var bulge = Math.Tan((angle2 - angle1) / 2.0);

            // add the polyline to the current space
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                using (var pline = new Polyline())
                {
                    pline.AddVertexAt(0, p1, 0.0, 0.0, 0.0);
                    pline.AddVertexAt(1, p2, bulge, 0.0, 0.0);
                    pline.AddVertexAt(2, p3, 0.0, 0.0, 0.0);
                    pline.TransformBy(ed.CurrentUserCoordinateSystem);
                    curSpace.AppendEntity(pline);
                    tr.AddNewlyCreatedDBObject(pline, true);
                }
                tr.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 13

kite15
Advocate
Advocate

It works, Thank you very much........, Actually, I am new in this (.net) area and trying to create AutoCAD entities through the application (.NET) for the AutoCAD interface.

 

0 Likes
Message 4 of 13

kite15
Advocate
Advocate

Can you give more help Please, to create PolyLine from First point to Second point with fixed length (Current LTSCALE). Actually, After getting the first point the Polyline Automatically drawn a polyline as per current LTSCALE value (Current LTSCALE = Auto drawn polyline) followed the second point angle now the Arc will be drawn after getting the second vertex..Smiley Sad

0 Likes
Message 5 of 13

_gile
Consultant
Consultant

Sorry, I cannot understand what you want to do.

Could you provide a clearer explaination, maybe with some picture, and try to think at all necessary inputs to create the polyline (a length from a point is not enough to define another point).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 13

kite15
Advocate
Advocate

Here Providing a Snap...Please, Help

0 Likes
Message 7 of 13

_gile
Consultant
Consultant

Hi,

 

Just replace:

var pt2 = result.Value;

with:

var pt2 = pt1 + pt1.GetVectorTo(result.Value).GetNormal() * db.Ltscale;

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 13

_gile
Consultant
Consultant

You can also prompt the user for the first segment angle instead of a second point and compute the second point according to the angle and the LTSCALE value:

 

        [CommandMethod("Test")]
        public void Test()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            // prompt for first point
            var options = new PromptPointOptions("\nSpecify the first point: ");
            var result = ed.GetPoint(options);
            if (result.Status != PromptStatus.OK)
                return;
            var pt1 = result.Value;

            // prompt for the first segment angle
            var angleOptions = new PromptAngleOptions("\nSpecify the first segment angle: ");
            angleOptions.BasePoint = pt1;
            angleOptions.UseBasePoint = true;
            var angleResult = ed.GetAngle(angleOptions);
            if (angleResult.Status != PromptStatus.OK)
                return;

// compute the second point var pt2 = new Point3d( pt1.X + Math.Cos(angleResult.Value) * db.Ltscale, pt1.Y + Math.Sin(angleResult.Value) * db.Ltscale, pt1.Z); // prompt for third point options.Message = "\nSpecify the arc end point: "; options.BasePoint = pt2; options.UseBasePoint = true; result = ed.GetPoint(options); if (result.Status != PromptStatus.OK) return; var pt3 = result.Value; // convert points to 2d points var plane = new Plane(); var p1 = pt1.Convert2d(plane); var p2 = pt2.Convert2d(plane); var p3 = pt3.Convert2d(plane); // compute the bulge of the second segment var angle1 = p1.GetVectorTo(p2).Angle; var angle2 = p2.GetVectorTo(p3).Angle; var bulge = Math.Tan((angle2 - angle1) / 2.0); // add the polyline to the current space using (Transaction tr = db.TransactionManager.StartTransaction()) { var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); using (var pline = new Polyline()) { pline.AddVertexAt(0, p1, 0.0, 0.0, 0.0); pline.AddVertexAt(1, p2, bulge, 0.0, 0.0); pline.AddVertexAt(2, p3, 0.0, 0.0, 0.0); pline.TransformBy(ed.CurrentUserCoordinateSystem); curSpace.AppendEntity(pline); tr.AddNewlyCreatedDBObject(pline, true); } tr.Commit(); } }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 9 of 13

Anonymous
Not applicable

Hi Gilles,

 

We have an option in autocad, when we drawing Polyline with ARc option. Here is my need is to draw multiline with arc option, when user given command to draw a acr in a multiline.

Screen shots provided below. Request you to Please provide the code to accomplish the requirement.

 

Draw multiline with Arc Options.PNG

 

Note :  I want to built it in Autocad 2016

 

Regards

 

Alex Andrews.

0 Likes
Message 10 of 13

_gile
Consultant
Consultant

Hi,

 

The multiline command does not have any Arc option because mulitiline entities cannot contain arcs (only straight segments).

 

You may use polylines with arcs and offset them.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 11 of 13

Anonymous
Not applicable

Hi Gilles,

 

Could you please help me to draw polyline with Arc options in AutoCAD using C#.net. And the same Polyline with the combination of Arc to placed with some Offset distance.

 

The Screen shot which I shown below.

 

Capture.PNG

0 Likes
Message 12 of 13

_gile
Consultant
Consultant

@Anonymous

 

In view of the screenshots you send, it seems that you are ignoring some AutoCAD basics (like the _OFFSET command).
In my opinion, it is not possible to pretend to customize AutoCAD with code without mastering these databases.

 

You should start by learning how to use the _OFFSET command, then you can better understand how the GetOffsetCurves() method works (if the offset distance is positive the curve is offseted to the right, if it is negative, to the left).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 13 of 13

ActivistInvestor
Mentor
Mentor

@Anonymous wrote:

Hi Gilles,

 

Could you please help me to draw polyline with Arc options in AutoCAD using C#.net. And the same Polyline with the combination of Arc to placed with some Offset distance.

 

The Screen shot which I shown below.

 

 


Your screen shot does not show two polylines that are offsets of each other. Try the OFFSET command one one of them and see if you get anything like the other.

 

Also, if you want help, you need to start out by attempting to solve the problem yourself, and then posting the resulting code. You seem to be just asking others here to write code for you. 

0 Likes