Author Topic: Intersection of 2 polylines  (Read 12462 times)

0 Members and 1 Guest are viewing this topic.

ozimad

  • Guest
Intersection of 2 polylines
« on: January 19, 2010, 08:20:04 AM »
Hello!  :-)
I am new here. I am from Latvia.
Can you please help me to write a lisp to get the intersection of 2 polylines?
Thanks in advance

nivuahc

  • Guest
Re: Intersection of 2 polylines
« Reply #1 on: January 19, 2010, 08:57:20 AM »
Code: [Select]
(setq PL1 (vlax-ename->vla-object (car (entsel)))
      PL2 (vlax-ename->vla-object (car (entsel)))
      IntPt (vlax-safearray->list
(vlax-variant-value
(vla-IntersectWith PL1 PL2 3)
      )
   )
)

ozimad

  • Guest
Re: Intersection of 2 polylines
« Reply #2 on: January 19, 2010, 09:05:45 AM »
Thanks for you reply I have this in command line:

Select object: ; error: no function definition: INTERSECTWITH


P.S. I am using Autocad Civil 3d 2008
(vla-IntersectWith PL1 PL2 3)
Does 3 mean it will give me 3 dimensions? I want to have x,y only.

nivuahc

  • Guest
Re: Intersection of 2 polylines
« Reply #3 on: January 19, 2010, 09:12:12 AM »
From what I know, this should work in your AutoCAD.

Try running this first:

(vl-load-com)

(vla-IntersectWith PL1 PL2 3) That 3 is the AcExtendOption of IntersectWith which basically means that you're getting the intersection of PL1 and PL2 where they both meet

From the ActiveX help file:
Quote
AcExtendOption enum; input-only
This option specifies if none, one or both, of the objects are to be extended in order to attempt an intersection.

acExtendNone
 Does not extend either object.
 
acExtendThisEntity
 Extends the base object.
 
acExtendOtherEntity
 Extends the object passed as an argument.
 
acExtendBoth
 Extends both objects.
 

BTW, thanks for asking. I was planning to look into this myself for a routine today.

Consequently, there may be a better, more efficient way to do it. I'll leave that up to the squints.

/is forcing himself to learn visual lisp... even if it kills him

ozimad

  • Guest
Re: Intersection of 2 polylines
« Reply #4 on: January 19, 2010, 09:24:26 AM »
Still it gives me the same error.  Even i (vl-load-com) berofe running function. 

nivuahc

  • Guest
Re: Intersection of 2 polylines
« Reply #5 on: January 19, 2010, 09:25:52 AM »
Still it gives me the same error.  Even i (vl-load-com) berofe running function. 

No idea  :|

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Intersection of 2 polylines
« Reply #6 on: January 19, 2010, 09:35:37 AM »
Welcome to The Swamp.
What version of ACAD are running?
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.

ozimad

  • Guest
Re: Intersection of 2 polylines
« Reply #7 on: January 19, 2010, 09:55:17 AM »
AutoCad Civil 3d 2008 with all service packs installed

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Intersection of 2 polylines
« Reply #8 on: January 19, 2010, 10:14:13 AM »
What does this do?

Code: [Select]
;;  return a list of intersect points or nil
(defun get_interpts (obj1 obj2 / iplist)
  (vl-load-com)
  (if (not (vl-catch-all-error-p
             (setq iplist (vl-catch-all-apply
                            'vlax-safearray->list
                            (list
                              (vlax-variant-value
                                (vla-intersectwith obj1 obj2 acextendnone)
                              ))))))
    iplist
  )
)

Code: [Select]
(defun c:test()
  (print(get_interpts
          (vlax-ename->vla-object (car(entsel)))
          (vlax-ename->vla-object (car(entsel)))
          ))
  (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.

nivuahc

  • Guest
Re: Intersection of 2 polylines
« Reply #9 on: January 19, 2010, 10:20:51 AM »
See  :roll:, CAB is way better at this stuff than I am  :-)

nivuahc

  • Guest
Re: Intersection of 2 polylines
« Reply #10 on: January 19, 2010, 10:29:41 AM »
BTW, CAB, when I run your test on two lines/plines that are already joined/intersected I get a point list. When I run it on two lines/plines that aren't joined/intersected I get a nil return.
 
 If I modify this line in get_interpts
 (vla-intersectwith obj1 obj2 acextendnone)
 
 to
 (vla-intersectwith obj1 obj2 acextendboth)
 
or
 (vla-intersectwith obj1 obj2 3)

 I get a point list regardless of them being joined/intersected

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Intersection of 2 polylines
« Reply #11 on: January 19, 2010, 10:42:25 AM »
I tend to use :

Code: [Select]
(vlax-invoke <obj> 'IntersectWith <obj> acExtend<something> )

As opposed to vla-intersectwith, as the above will return nil upon no intersection, whereas, when using vla-intersectwith, you either have to check for an error with the safearray->list, (as in CAB's Example):

Code: [Select]
(vl-catch-all-apply (function vlax-safearray->list)
  (list (vlax-variant-value
          (vla-IntersectWith obj1 obj2 acExtendNone))))

(As you can have an empty variant, but you cannot convert it to a list).

Or, you have to check the dimension of the safearray:

Code: [Select]
(if (< 0 (vlax-safearray-get-u-bound
           (vlax-variant-value
             (vla-Intersectwith obj1 obj2 acExtendNone)) 1))

  ...

As a negative upper bound indicates an empty variant.

As an example, this will get all intersections between objects in a SelSet:

Code: [Select]
(defun Get_Inters (ss / list->3D-point i j obj1 obj2 iLst)
  ;; Lee Mac  ~  19.01.10

  (defun list->3D-point (lst)
    (if lst
      (cons (list (car lst) (cadr lst) (caddr lst))
            (list->3D-point (cdddr lst)))))

  (setq i (sslength ss))

  (while (not (minusp (setq j (1- i) i (1- i))))
    (setq obj1 (vlax-ename->vla-object (ssname ss i)))

    (while (not (minusp (setq j (1- j))))
      (setq obj2 (vlax-ename->vla-object (ssname ss j)))

      (setq iLst (append iLst
                   (list->3D-point
                     (vlax-invoke obj1 'IntersectWith obj2 acExtendNone))))))

  iLst)


(defun c:test (/ ss x)
  (vl-load-com)

  (if (setq ss (ssget))
    (foreach x (get_Inters ss)
      (command "_.point" "_non" x)))

  (princ))

    

      

Hope this helps!

Lee


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Intersection of 2 polylines
« Reply #12 on: January 19, 2010, 10:46:36 AM »
Thanks for you reply I have this in command line:

Select object: ; error: no function definition: INTERSECTWITH


P.S. I am using Autocad Civil 3d 2008
(vla-IntersectWith PL1 PL2 3)
Does 3 mean it will give me 3 dimensions? I want to have x,y only.

The OP's problem is with availability not usage.
Can anyone confirm the availability of the Method in Autocad Civil 3d 2008 ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ozimad

  • Guest
Re: Intersection of 2 polylines
« Reply #13 on: January 19, 2010, 11:06:18 AM »
Realy nice it WORKS!
Using test function, but now i need to call test function from my function.
I have 4 polylines, 2 crossing + 2 crossing. And i need to build a polyline between crossings.
I want to call test giving it 2 polyline and getting back intersection point, as example
(setq ipointslist (test(pl1 pl2))

how does it need to look, i am realy novice in lisp so sorry for mistakes  :ugly:

(defun c:test(___pl1___pl2___)
  (print(get_interpts
          (vlax-ename->vla-object (car(__pl1____)))
          (vlax-ename->vla-object (car(___pl2___)))
          ))
  (princ)
 )


;;  return a list of intersect points or nil
(defun get_interpts (obj1 obj2 / iplist)
  (vl-load-com)
  (if (not (vl-catch-all-error-p
             (setq iplist (vl-catch-all-apply
                            'vlax-safearray->list
                            (list
                              (vlax-variant-value
                                (vla-intersectwith obj1 obj2 acextendnone)
                              ))))))
    iplist
  )
)

Krushert

  • Seagull
  • Posts: 13679
  • FREE BEER Tomorrow!!
Re: Intersection of 2 polylines
« Reply #14 on: January 19, 2010, 12:38:59 PM »


how does it need to look, i am realy novice in lisp so sorry for mistakes  :ugly:


We all been there and like me still there on some days.
Welcome to TheSwamp. 
I + XI = X is true ...  ... if you change your perspective.

I no longer CAD or Model, I just hang out here picking up the empties beer cans