Author Topic: Minimum distance between two vlax-curve objects  (Read 21948 times)

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7027
Re: Minimum distance between two vlax-curve objects
« Reply #15 on: May 21, 2008, 12:14:01 PM »
Here is my first try. :-)



And I think setting step to (/ len 1000) is total overkill...

Regards
Joe


Depends on the length of the items tested...if the lines are 10000 ft long then the check is only every 10 feet  :-P

Not correct. The length of the objects involved is not an issue. Study my code.

Joe,

I was referring to my code...I tried your code but got different results depending on where my pick points were (but there's been one or more times where I don't understand what's going on)  :-D

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

VovKa

  • Swamp Rat
  • Posts: 1153
  • Ukraine
Re: Minimum distance between two vlax-curve objects
« Reply #16 on: May 21, 2008, 12:35:17 PM »
it's my try Joe
sometimes 5 repetitions are enough and sometimes 30 are not
let the code itself decide when it is enough :)
Code: [Select]
(defun MD:ObjObj (obj1 obj2 pt / p d fuzz)
  (setq fuzz 1E-8)
  (while (not
   (equal (distance pt (setq p (vlax-curve-GetClosestPointTo obj2 pt)))
  (setq d (distance p (setq pt (vlax-curve-GetClosestPointTo obj1 p))))
  fuzz
   )
)
  )
  (list d pt p)
)
« Last Edit: May 21, 2008, 01:03:57 PM by VovKa »

Joe Burke

  • Guest
Re: Minimum distance between two vlax-curve objects
« Reply #17 on: May 21, 2008, 12:47:39 PM »
Here is an example of a difficult situation.

My code will work if one of the ellipses is selected as indicated. I don't mean to imply this is a good solution. It's just something I'm working on...

Joe Burke

  • Guest
Re: Minimum distance between two vlax-curve objects
« Reply #18 on: May 21, 2008, 12:51:49 PM »
it's my try Joe
sometimes 5 repetitions are enough and sometimes 30 are not
let the code itself decide when it is enough :)
Code: [Select]
(defun MD:ObjObj (obj1 obj2 pt / p d fuzz)
  (setq fuzz 1E-8)
  (while
    (progn (setq pd)
   (not
     (equal (setq d (distance p (setq pt (vlax-curve-GetClosestPointTo obj1 p))))
    (distance pt (setq p (vlax-curve-GetClosestPointTo obj2 pt)))
    fuzz
     )
   )
    )
  )
  (list d pt p)
)

That makes sense :-)

VovKa

  • Swamp Rat
  • Posts: 1153
  • Ukraine
Re: Minimum distance between two vlax-curve objects
« Reply #19 on: May 21, 2008, 01:00:18 PM »
i've made some changes to my code, sorry for stupid errors :)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10365
Re: Minimum distance between two vlax-curve objects
« Reply #20 on: May 21, 2008, 01:11:53 PM »
Joe,
There is a problem with this scenario.
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

VovKa

  • Swamp Rat
  • Posts: 1153
  • Ukraine
Re: Minimum distance between two vlax-curve objects
« Reply #21 on: May 21, 2008, 01:27:56 PM »
one more situation

Arch_Eric

  • Guest
Re: Minimum distance between two vlax-curve objects
« Reply #22 on: May 21, 2008, 02:22:32 PM »
No need to sort since you're only looking for one point, right?

Code: [Select]
(defun c:getshortdist ( / shortdist osm col ent1 ent2 step iter ent1-cp ent2-cp clist)
 (setq osm (getvar "osmode"))
 (setq col (getvar "cecolor"))
 (setvar "osmode" 0)
 (setq ent1 (car (entsel "\nSelect first curve: ")))
 (setq ent2 (car (entsel "\nSelect second curve: ")))
;(setq step (getreal "\nEnter step value: "))
 (setq step 0.1)
 (setq iter step)
 (while (vlax-curve-getPointAtDist ent1 iter)
  (setq ent1-cp (vlax-curve-getPointAtDist ent1 iter))
  (setq ent2-cp (vlax-curve-getClosestPointTo ent2 ent1-cp))
  (setq cp-dist (distance ent1-cp ent2-cp))
  (setq clist (list cp-dist ent1-cp ent2-cp))
  (if (or (= shortdist nil) (< (car clist) (car shortdist))) (setq shortdist clist))
  (setq iter (+ iter step))
 )
 (setvar "cecolor" "210")
 (command "_line" (cadr shortdist) (caddr shortdist) "")
 (command "point" (cadr shortdist))
 (command "point" (caddr shortdist))
 (setvar "cecolor" col)
 (setvar "osmode" osm)
)
« Last Edit: May 21, 2008, 02:37:25 PM by Arch_Eric »

Joe Burke

  • Guest
Re: Minimum distance between two vlax-curve objects
« Reply #23 on: May 22, 2008, 05:51:32 AM »
Thanks to all who replied.

I'll study the posted code and see what can be done to fix the problems.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10365
Re: Minimum distance between two vlax-curve objects
« Reply #24 on: May 22, 2008, 08:40:57 AM »
And I think setting step to (/ len 1000) is total overkill...

Regards
Joe

Depends on the length of the items tested...if the lines are 10000 ft long then the check is only every 10 feet  :-P
I work in inch units so my typical worst case would be site work with a 250' lot line. In this
case the 3000 inch plines would mean a check every 3 inches. The 1000 is reasonably fast
on my machine in ACAD2000. So that is what I used as well. The alternative is more code.
In fact it would need some intelligent code. For plines the check would need to be at and
midway between each vertex. That would catch a bulge. For circles, ellipses & arcs you
could check using a "Bracket" algorithm. So I think you get my drift.
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10365
Re: Minimum distance between two vlax-curve objects
« Reply #25 on: May 22, 2008, 08:42:24 AM »
Joe,
I really like your shortest distance feed back code.
« Last Edit: May 22, 2008, 10:44:26 AM by CAB »
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Joe Burke

  • Guest
Re: Minimum distance between two vlax-curve objects
« Reply #26 on: May 22, 2008, 10:39:43 AM »
To all who replied.

The problems VovKa pointed out were essentially the ones I was aware of as noted in my first code post. At first glance they seemed hard to solve. But after some thinking, it seems not.

Attached is beta version 2. I hope it solves those problems without much additional code/overhead.

Please let me know if you find a case where it does not work as expected.

BTW, there are some bells and whistes I intend to add. The main one is report when two lines or rays or xlines are parallel. There should also be testing for coplanar objects.

Regards and thanks...

Joe Burke

  • Guest
Re: Minimum distance between two vlax-curve objects
« Reply #27 on: May 22, 2008, 10:57:06 AM »
Joe,
I really like your shortest distance feed back code.

Thanks, Alan. The idea behind it is you don't have to check for points along the entire length of each object.

VovKa

  • Swamp Rat
  • Posts: 1153
  • Ukraine
Re: Minimum distance between two vlax-curve objects
« Reply #28 on: May 22, 2008, 12:21:38 PM »
more AI should be employed

CAB

  • Global Moderator
  • Seagull
  • Posts: 10365
Re: Minimum distance between two vlax-curve objects
« Reply #29 on: May 22, 2008, 01:55:23 PM »
This is my version 2. Added grdraw
Code: [Select]
;; CAB @ TheSwamp.org - 5/22/2008
;; Version 2
;; Returns the minimum distance between two objects.
;; Supported object types: any vlax-curve object
;;
(defun c:MD() (c:MinDist))
(defun c:MinDist (/ step e1 e2 p1 p2 len idx dis dlst
               curveOK getent)
  (vl-load-com)
  ;;  CAB test to see if vlax-curve can be used on an object
  (defun curveOK (ent) ; returns nil if not allowed
    (not (vl-catch-all-error-p
           (vl-catch-all-apply 'vlax-curve-getendparam (list ent))
         )
    )
  )
  ;; returns an entity which can be used with vlax-curve
  (defun getent (pmt / ent)
    (while (not (and (setq ent (car (entsel pmt)))
                (curveOK ent)
           ))
      (prompt "\nMissed or not a Curve object. Try again.")
    )
    ent
  )


  ;;=========================================================
  (setq e1 (getent "\nFirst entity."))
  (setq e2 (getent "\nSecond entity."))
  (setq len  (vlax-curve-getdistatparam e1 (vlax-curve-getendparam e1)))
  (setq idx  0.0
        step (/ len 1000)
  )
  (cond
    ((vlax-invoke
       (vlax-ename->vla-object e1) 'IntersectWith
                  (vlax-ename->vla-object e2) acExtendNone)
     (setq dis 0.0)
    )
    (t
     (while (and (<= idx len)
                 (setq p1 (vlax-curve-getpointatdist e1 idx))
                 (setq p2 (vlax-curve-getclosestpointto e2 p1))
            )
       (setq dis (distance p1 p2)
             idx  (+ idx step)
       )
       (if (or (null dlst) (< dis (car dlst)))
         (setq dlst (list dis p1 p2))
       )             
     )
     (if (and dlst (listp dlst))
       (grdraw (trans (cadr dlst)0 1)(trans (caddr dlst)0 1) 6 1)
     )
    )
  )
  (print dis)
  (princ)
)
(prompt "\nMinimum Distance Loaded, Enter MD to run.")
(princ)
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.