How to get the distance between two points with C#

How to get the distance between two points with C#

sonny3g
Collaborator Collaborator
4,926 Views
4 Replies
Message 1 of 5

How to get the distance between two points with C#

sonny3g
Collaborator
Collaborator

The problem is that a user needs to select a block the select a point on that block where he wants a leader to be placed.  I need to be able to tell how far from the block insertion point is the point where he wants the leader to be placed regardless of the rotation of the block or the orientation of the UCS. 

 

I have been chasing my tail on this for two days and I just don't know how to do this.  Anyone got a suggestion?  All help is appreciated.

 

Sonny

 

Scott G. Sawdy
scott.sawdy@bluecoyotecad.com
0 Likes
Accepted solutions (1)
4,927 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You simply need to get the BlockReference.Position which is always WCS defined and transform the point returned by PromptPointResult.Value to WCS coordinates because it's always UCS defined.

 

            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            var peo = new PromptEntityOptions("\nSelect block: ");
            peo.SetRejectMessage("\nMust be a block.");
            peo.AddAllowedClass(typeof(BlockReference), true);
            var per = ed.GetEntity(peo);
            if (per.Status == PromptStatus.OK)
            {
                var ppr = ed.GetPoint("\nSpecify a point: ");
                if (ppr.Status == PromptStatus.OK)
                {
                    dynamic br = per.ObjectId;
                    var position = br.Position;
                    var point = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
                    var distance = point.DistanceTo(position);
                    ed.WriteMessage("\nDistance = {0}", distance);
                }
            }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

kerry_w_brown
Advisor
Advisor

Nice example Gilles.

 

Sonny,

Because the example uses dynamic ;

Important: When working with DLR and C#, you will need to reference the Microsoft.CSharp library.

 


https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-NET/files/GUI...

 

 

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 4 of 5

sonny3g
Collaborator
Collaborator

Thanks _kdub,

I will take some time out to read through the article in the link you provided.

 

sonny

Scott G. Sawdy
scott.sawdy@bluecoyotecad.com
0 Likes
Message 5 of 5

sonny3g
Collaborator
Collaborator

Thanks Giles, as always you come through with a solution!

 

Sonny

Scott G. Sawdy
scott.sawdy@bluecoyotecad.com
0 Likes