.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get Closest Distance from Point to Block By Geometry

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Gepaha
1177 Views, 4 Replies

Get Closest Distance from Point to Block By Geometry

I have a point (P1) and a block and I need to find the point (P2) which belongs to the geometry of the block and whose distance is as short as possible between the known point (P1) and the block. In addition, the line connecting point P1 to point (P2) must pass through the base point (Pb) of the block.
It is practically what the Multileader command does when the attacment point is set to insert point.
It must work in the UCS coordinate system.
I tried to reproduce in the figure below.Get closest Distanfe from point to blockGet closest Distanfe from point to block

Thanks in advance.

4 REPLIES 4
Message 2 of 5
norman.yuan
in reply to: Gepaha

You could write code according to following logic:

 

1. Call BlockReference.Explode() to obtain a set of entities in the place of the block reference;

2. For each known point (P1), you create a temporary Line object, starting at P1, and ending at the block reference's insertion point (or whatever point inside the block reference, for that matter);

3. Call Line.IntersectWith() against each of entities from explosion. Each IntersectWith() call would give you one or more points. Save these points in a collection;

4. After doing IntersectWith() again all entities from the explosion, test each intersection point for the distance to the Line's start point. The point closest to the line's start point (P1) is the point to find (P2).

5. Do not forget to dispose the temporary line and the entities from explosion finally.

 

Actual code should be easy to write.

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 5
Gepaha
in reply to: norman.yuan

Thanks for the help.
I was hoping that there would be a function ready for this since the functionality I want is exactly what happens when I trigger the command to stretch through the grip of a multileader's block. See the figure below:Stretch Block of MultileaderStretch Block of Multileader
For reasons of compatibility of older versions I plan to implement this in a block with several leaders (not Multileaders) attached. This involves jig and I would like to make it as simple as possible since it requires a continuous update of the points.
As far as possible I will try to implement your suggestion.
Best Regards,
gphanauer

Message 4 of 5
norman.yuan
in reply to: Gepaha

I published the code that implements my proposed solution to your question, if you are still interested in:

 

https://drive-cad-with-code.blogspot.com/2020/11/find-lines-intersection-points-with.html 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 5
Gepaha
in reply to: norman.yuan

I ended up implementing this in a Point3dCollection extension method, using BlockReference.Explode following the steps you suggested. It is working, but it requires a series of improvements. I am trying a generic solution that covers most situations, that works with:

  • Nested blocks.

          I implemented it using recursive function, but it is not working properly;

    

  • Empty attributes.

          When I use IntersectWith I end up getting the error "eNullEXtents". I believe that IntersectWith should make use of GeometriExtents and when the attribute is empty or does not have its limits defined, such an error occurs. I ended up implementing an error handling but I don't know if it is the most appropriate way to deal with it. I found some content here:

https://adndevblog.typepad.com/autocad/2012/12/entitygeometricextents-throws-an-exception-enullexten... 

 

  • Text, MTex and Multiline attributes.

     The most extreme points are returning. I'm still trying to find a way to work properly.

Thank you very much for posting a solution, showing the way.

Immensely grateful.

gphanauer.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report