getting distance between two lines

getting distance between two lines

a.kouchakzadeh
Advocate Advocate
2,868 Views
5 Replies
Message 1 of 6

getting distance between two lines

a.kouchakzadeh
Advocate
Advocate

 

how can I get the distance between two parallel lines?

I cant use the end or start point because the might not be parallel to X axes or Y axes and they might not even be the same length.

 

akouchakzadeh_0-1682592861653.png

 

 

0 Likes
Accepted solutions (1)
2,869 Views
5 Replies
Replies (5)
Message 2 of 6

Norman_Yuan
Mentor
Mentor

You can call Curve.GetClosestPointTo(Point3d, bool) from either line to the other line's Start/EndPoint with the second parameter as "true", meaning to extend the line when finding the closest point.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 6

vkpunique
Advocate
Advocate

never used this code by myself but you can use GetCloseestPointTo Method

line.GetClosestPointTo (pickedPt.TransformBy (ed.CurrentUserCoordinateSystem), false);

 Here general logic, i am assuming that you already have two lines on your active drawing. User will select two lines one by one. You'll have two line object plus selected points, you can just calculate closes point using  GetCloseestPointTo method. here is sample code, I've tested it it's working fine

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

            var entOpts = new PromptEntityOptions("\nSelect the first line: ");
            entOpts.SetRejectMessage("\nSelected object is not a line.");
            entOpts.AddAllowedClass(typeof(Line), true);
            var entRes = ed.GetEntity(entOpts);
            if (entRes.Status != PromptStatus.OK)
                return;
            var line1Id = entRes.ObjectId;
            var pickedPt = entRes.PickedPoint;

            entOpts.Message = "\nSelect the second line: ";
            entRes = ed.GetEntity(entOpts);
            if (entRes.Status != PromptStatus.OK)
                return;
            var line2Id = entRes.ObjectId;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var line1 = (Line)tr.GetObject(line1Id, OpenMode.ForRead);
                var line2 = (Line)tr.GetObject(line2Id, OpenMode.ForRead);
                var pt1 = line1.GetClosestPointTo(pickedPt.TransformBy(ed.CurrentUserCoordinateSystem), false);
                var pt2 = line2.GetClosestPointTo(pt1, true);
                var dim = new AlignedDimension(pt1, pt2, pt1, null, db.Dimstyle);
                var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                space.AppendEntity(dim);
                tr.AddNewlyCreatedDBObject(dim, true);
                tr.Commit();
            }
        }

 YOu'll have 2 points, you can add align dim or Just calculate distance between pt1 and pt2 and display it wherever you want

0 Likes
Message 4 of 6

a.kouchakzadeh
Advocate
Advocate

thanks for the reply to both

this method does the work however my question was not clear enough.

is there any way to calculate the distance between two parallel lines without passing any point?
because I dont have any points.
like, can I first check if the lines are parallel and than find the distance between them?

0 Likes
Message 5 of 6

Norman_Yuan
Mentor
Mentor
Accepted solution

You DO HAVE points for the calculation with GetClosestPointTo(), when you say you have 2 lines, as in my previous reply suggested: you can use either the StartPoint or EndPoint of the other line as the first parameter of the call of GetClosestPointTo() method. You just need to make sure to pass "true" as the second parameter. There is no need to pick point on the other line. 

 

You can try for yourself by using StartPoint and EndPoint to calculate the distance of the 2 parallel line, regardless where they starts/ends, as long as the second parameter in the GetClosestPointTo() is true, the distance will be the same. Of course you can use this approach to verify if the 2 lines is in parallel.

 

Alternatively, you can use LinearEntity3d.IsParallelTo() to test before calculating distance.

 

 

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 6

a.kouchakzadeh
Advocate
Advocate

Awesome, thanks Norman

0 Likes