How to draw Perpendicular Lines

How to draw Perpendicular Lines

J-Rocks
Collaborator Collaborator
7,554 Views
12 Replies
Message 1 of 13

How to draw Perpendicular Lines

J-Rocks
Collaborator
Collaborator

Hi,

 

I am trying to learn how to draw perpendicular lines on Polyline with arc segments by dividing the polyline's length into 15 times in C#.

 

The length of each drawn Line is = 10.0 units.

 

Thanks in adnace.

 

Perpendicular lines.JPG

 

I tried coding it this way and I don't know how to draw the perpendicular lines.

 

        public static void PerpendicularLines()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptEntityOptions op = new PromptEntityOptions("\nSelect polyline :");
                op.SetRejectMessage("Must be Polyline");
                op.AddAllowedClass(typeof(Polyline), true);
                op.AllowNone = false;
                PromptEntityResult s = ed.GetEntity(op);
                if (s.Status == PromptStatus.OK)
                {
                    Polyline ent = (Polyline)tr.GetObject(s.ObjectId, OpenMode.ForRead);
                    Line drawLine = new Line();
                    double length = ent.Length;
                    double gap = length / 15.0;
                    double dis = gap;
                    int run = ((int)gap);
                    for (int i = 0; i < run; i++)
                    {
                        Point3d p1 = ent.GetPointAtDist(dis);
                        Vector3d ang = ent.GetFirstDerivative(ent.GetParameterAtPoint(p1));
                        // I lost here and don't know how to create the lines ...
                        dis = dis + gap;
                    }
                }
                tr.Commit();
            }
        }
0 Likes
Accepted solutions (1)
7,555 Views
12 Replies
Replies (12)
Message 2 of 13

_gile
Consultant
Consultant

Hi,

 

The GetfirstDerivative() function return a unit vector which direction is the tangent to the curve at specified point.

Rotating this vector of 90° (PI / 2 radians) will return a unit vector perpendicular to the curve at specified point.

Then, some very basic math/geomety should allow you to scale the vector and use it to get the displaced points.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 13

J-Rocks
Collaborator
Collaborator

Thank you gile,

 

sadly I couldn't solve it because these functions are completely new to me and I don't know how to play with them at the mean time.

0 Likes
Message 4 of 13

_gile
Consultant
Consultant
Accepted solution

IMO, If you want to draw in AutoCAD with code, you should learn at least the geometric operators and functions of the Point3d, Vector3d and Matrix3d types.

 

        public static void PerpendicularLines()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptEntityOptions op = new PromptEntityOptions("\nSelect polyline :");
                op.SetRejectMessage("Must be Polyline");
                op.AddAllowedClass(typeof(Polyline), true);
                op.AllowNone = false;
                PromptEntityResult s = ed.GetEntity(op);
                if (s.Status == PromptStatus.OK)
                {
                    Polyline ent = (Polyline)tr.GetObject(s.ObjectId, OpenMode.ForRead);
                    Line drawLine = new Line();
                    double length = ent.Length;
                    double gap = length / 15.0;
                    double dis = gap;
                    //int run = ((int)gap); <- this is wrong and unusefull
                    var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    for (int i = 0; i < 14; i++)
                    {
                        Point3d p1 = ent.GetPointAtDist(dis);
                        Vector3d ang = ent.GetFirstDerivative(ent.GetParameterAtPoint(p1));
                        // scale the vector by 5.0 (so that it's 5 units length)
                        ang = ang.GetNormal() * 5.0;
                        // rotate the vector
                        ang = ang.TransformBy(Matrix3d.Rotation(Math.PI / 2.0, ent.Normal, Point3d.Origin));
                        // create a line by substracting and adding the vector to the point (displacing the point)
                        Line line = new Line(p1 - ang, p1 + ang);
                        curSpace.AppendEntity(line);
                        tr.AddNewlyCreatedDBObject(line, true);
                        dis = dis + gap;
                    }
                }
                tr.Commit();
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 13

J-Rocks
Collaborator
Collaborator

@_gile wrote:

IMO, If you want to draw in AutoCAD with code, you should learn at least the geometric operators and functions of the Point3d, Vector3d and Matrix3d types.

 


Thank you gile for your help and for writing the codes for me that was wonderful.

 

Yes I should learn about the geometric operations and sadly the AutoCAD.net Help document does not have all funtions , for example the GetParameterAtPoint and GetPointAtDist .... etc are not or I couldn't find any reference for them in the .NET help document to read about them.

 

Do you have any good reference for AutoCAD.net C# ?

 

Mant thanks.

0 Likes
Message 6 of 13

_gile
Consultant
Consultant

By my side, I use the ObjectARX 20XX SDK which contains the required libraries (dll) for .NET developping and the documentation.

The docs folder contains an arxmgd 20XX.chm file for managed code (.NET) only and also all the ObjectARX docs.

You can download them from this page.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 13

J-Rocks
Collaborator
Collaborator
Perfect gile , Many thanks
0 Likes
Message 8 of 13

dgorsman
Consultant
Consultant

Boning up on vector math and trig is also recommended, IMO.  Matrix algebra might be useful in some places, but only so far as to grasp some of the transformation methods.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 9 of 13

J-Rocks
Collaborator
Collaborator

@dgorsman wrote:

Boning up on vector math and trig is also recommended, IMO.  Matrix algebra might be useful in some places, but only so far as to grasp some of the transformation methods.


Thank you for your input dgorsman.

 

Can you post an example please?

0 Likes
Message 10 of 13

_gile
Consultant
Consultant

dgorsman a écrit :

Boning up on vector math and trig is also recommended, IMO.  Matrix algebra might be useful in some places, but only so far as to grasp some of the transformation methods.


I agree, matrix algebra knowledge isn't really required with .NET as the API provides built-in methods for the main operations (linear transformations, inversion, combinations, ...) which is not the case with LISP.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 11 of 13

dgorsman
Consultant
Consultant

That, and lot of the legwork you've posted over the years.

 

Now where's that beer smiley...

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 12 of 13

dgorsman
Consultant
Consultant

For example, vector cross products: https://en.wikipedia.org/wiki/Cross_product

 

Just about everything in AutoCAD 3D space boils down into vector math.

 

From your original problem you have a vector at any given point (direction of line), and a normal vector pointing straight up (0,0,1); the cross product of the two is mutually perpendicular and only needs to be scaled for length.

 

Highly useful when dealing with planes.  I can define an arbitrary plane using a point in space and a normal vector (e.g. a viewing direction); using that, I can determine if a second point is on the plane by creating a vector from the point on the plane to the second point.  If the cross product of the two vectors is 0 they are mutually perpendicular and they are on the same plane.  A variation of the technique is used to project points onto a plane such as a viewport.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 13 of 13

BKSpurgeon
Collaborator
Collaborator

Hi can you post a picture of the final result for completeness of the solution. folks (including me) will be interested to see it.

 

rgds

 

BK

 

0 Likes