Author Topic: Help with intersection  (Read 5704 times)

0 Members and 1 Guest are viewing this topic.

velasquez

  • Newt
  • Posts: 195
Help with intersection
« on: July 15, 2015, 10:09:43 PM »
I have a list with two points that intersect one polyline, how to find the point of intersection?
I can not work with vla-intersectwith because I only have one entity.
« Last Edit: July 15, 2015, 10:49:35 PM by velasquez »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Help with intersection
« Reply #1 on: July 15, 2015, 11:03:39 PM »
Create a temporary entity.
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.

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #2 on: July 16, 2015, 12:18:43 AM »
I have a list with two points that intersect one polyline, how to find the point of intersection?
I can not work with vla-intersectwith because I only have one entity.

Hello Cab,
This is my problem that happens in a while.
I want to avoid the creation of many temporary entities.
Thanks

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Help with intersection
« Reply #3 on: July 16, 2015, 12:58:11 AM »
You can step through the vertices of the polyline, and use the built in Lisp function 'inters' with the four points ( two from the polyline, per segment, and the two you show as yellow in your image ) you have.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Help with intersection
« Reply #4 on: July 16, 2015, 01:38:52 AM »
Or you could do something like this :

Code - Auto/Visual Lisp: [Select]
  1. (defun c:doit (/ catchit intersectionpoint obj1 p1 p2 templine)
  2.    ;; Codehimbelonga kdub 2015/07/16
  3.    ;; Proof of concept code
  4.  
  5.    (if (and
  6.           (setq p1 (getpoint "Select First Point"))
  7.           (setq p2 (getpoint p1 "Select Second Point"))
  8.  
  9.           (setq obj1 (entsel))
  10.           (setq obj1 (car obj1))
  11.  
  12.           (setq tempLine
  13.                   (vla-addline
  14.                      (kdub:activespace)
  15.                      (vlax-3d-point p1)
  16.                      (vlax-3d-point p2)
  17.                   )
  18.           )
  19.        )
  20.       (setq catchit (vl-catch-all-apply
  21.                        'vla-intersectwith
  22.                        (list tempLine
  23.                              (vlax-ename->vla-object obj1)
  24.                              acextendboth
  25.                        )
  26.                     )
  27.       )
  28.    )
  29.    (if tempLine
  30.       (vla-delete tempLine)
  31.    )
  32.    (if (and catchit (not (vl-catch-all-error-p catchit)))
  33.                 (setq IntersectionPoint (kdub:lisp-value catchit))
  34.              )
  35.       )
  36.       (alert "Oooops")
  37.    )
  38.  
  39.  
  40.    (princ)
  41. )
  42.  

LIBRARY STUFF
Code - Auto/Visual Lisp: [Select]
  1. ;; (setq *:acapp nil *:activedoc nil *:modelspace nil *:paperspace nil)
  2. ;;;; IAcadApplication Object
  3. (or *:acapp (setq *:acapp (vlax-get-acad-object)))
  4.  
  5. ;;;; IAcadDocument Object
  6. (or *:activedoc
  7.     (setq *:activedoc (vla-get-activedocument *:acapp))
  8. )
  9.  
  10. ;;;; IAcadModelSpace Object
  11. (or *:modelspace
  12.     (setq *:modelspace (vla-get-modelspace *:activedoc))
  13. )
  14.  
  15. ;;;; IAcadPaperSpace Object
  16. (or *:paperspace
  17.     (setq *:paperspace (vla-get-paperspace *:activedoc))
  18. )
  19.  
  20. ;;--------------------------------------------------------------
  21. ;;--------------------------------------------------------------
  22. (defun kdub:activespace ()
  23.    (if (= acmodelspace (vla-get-activespace *:activedoc))
  24.       *:modelspace
  25.       (if (= :vlax-true (vla-get-mspace *:activedoc))
  26.          *:modelspace
  27.          (vla-get-paperspace *:activedoc)
  28.       )
  29.    )
  30. )
  31. ;;--------------------------------------------------------------
  32. ;;; lisp-value : Author : Vladimir Nesterovsky 2002
  33. (defun kdub:lisp-value (val)
  34.    (cond
  35.       ((= (type val) 'variant) (kdub:lisp-value (variant-value val)))
  36.       ((= (type val) 'safearray)
  37.        (mapcar 'kdub:lisp-value (safearray-value val))
  38.       )
  39.       (t val)
  40.    )
  41. )
  42. ;;--------------------------------------------------------------
  43.  
« Last Edit: July 16, 2015, 05:00:29 AM by Kerry »
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.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Help with intersection
« Reply #5 on: July 16, 2015, 04:10:50 AM »
@ Kerry: Can you explain this:
Code - Auto/Visual Lisp: [Select]
  1. ;;;; IAcadApplication Object
Shouldn't there be a setq somewhere?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Help with intersection
« Reply #6 on: July 16, 2015, 04:57:22 AM »
@ Kerry: Can you explain this:
Code - Auto/Visual Lisp: [Select]
  1. ;;;; IAcadApplication Object
Shouldn't there be a setq somewhere?

Yes there should.
I usually do the assignment  to a protected variable ... mucked up the edit for here.

Will fix shortly ...
Thanks for the sharp eye.


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.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Help with intersection
« Reply #7 on: July 16, 2015, 07:48:53 AM »
You can step through the vertices of the polyline, and use the built in Lisp function 'inters' with the four points ( two from the polyline, per segment, and the two you show as yellow in your image ) you have.

1+

Here's an example:
http://www.theswamp.org/index.php?topic=43630.msg489026#msg489026

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #8 on: July 17, 2015, 02:04:40 PM »
You can step through the vertices of the polyline, and use the built in Lisp function 'inters' with the four points ( two from the polyline, per segment, and the two you show as yellow in your image ) you have.
Hello and thank you Lee,
I do not have a straight polyline the points that I have not intersect.
I need to work with (inters p1 p2 p3p4 nil) to extend.
I'll try the suggestion of T.Willey to go through the vertices of the polyline looking for the intersection.
Do you have any suggestion?...

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #9 on: July 17, 2015, 02:08:06 PM »
Code: [Select]
[quote author=Kerry link=topic=49763.msg549263#msg549263 date=1437025132]
Or you could do something like this :

Thank you Kerry,
But I can not create a temporary line.

   

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Help with intersection
« Reply #10 on: July 17, 2015, 04:50:10 PM »
You can not
or you will not ?
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.

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #11 on: July 17, 2015, 05:02:54 PM »
You can not
or you will not ?
Hello Kerry,
I do not informed well, I should not interrupt the work of a function to create a temporary entity and then delete it, as it may happen thousands of times. I already have a polyline selected in beginning the work of a function.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Help with intersection
« Reply #12 on: July 17, 2015, 05:20:25 PM »
You can not
or you will not ?
Hello Kerry,
I do not informed well, I should not interrupt the work of a function to create a temporary entity and then delete it, as it may happen thousands of times. I already have a polyline selected in beginning the work of a function.

You don't need to interrupt anything or do anything manually.

Have you tried the code or looked at how it works ??

The line is drawn and erased when finished by the program ... I doubt you will actually ever see it.

The code is just proof of concept to demonstrate the functionality. I doesn't matter if you select the points and line or if they are provided programmatically ; the methodology still works.

For production code I'd produce a standalone function that takes the pline object and the 2 points as parameters and returns either the intersection point of nil, as applicable. This way it can be called from either an interactive or programmatic situation.

Regards,
 
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.

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #13 on: July 17, 2015, 06:31:30 PM »
You can not
or you will not ?
Hello Kerry,
I do not informed well, I should not interrupt the work of a function to create a temporary entity and then delete it, as it may happen thousands of times. I already have a polyline selected in beginning the work of a function.

You don't need to interrupt anything or do anything manually.

Have you tried the code or looked at how it works ??

The line is drawn and erased when finished by the program ... I doubt you will actually ever see it.

The code is just proof of concept to demonstrate the functionality. I doesn't matter if you select the points and line or if they are provided programmatically ; the methodology still works.

For production code I'd produce a standalone function that takes the pline object and the 2 points as parameters and returns either the intersection point of nil, as applicable. This way it can be called from either an interactive or programmatic situation.

Regards,
 

Many thanks Kerry,
Your code works perfectly.



mianbaoche

  • Guest
Re: Help with intersection
« Reply #14 on: July 18, 2015, 08:57:22 AM »


   (defun c:tt()
   (princ "\nPlease select LWPOLYLINE")(setq en (car(entsel)))
   (princ "\nPlease selectLINE")(setq e1(entget (car(entsel))))
  (foreach a (_polyinters en (cdr (assoc 10 e1)) (cdr (assoc 11 e1)))  (entmakex (list (cons 0 "CIRCLE")(cons 10 a)(cons 40 40))))
 (princ) )

(defun _polyinters ( en p1 p2 / vl l ang)
    (setq en (entget en)
          vl (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) en))
          l (distance (apply 'mapcar (cons 'min vl)) (apply 'mapcar (cons 'max vl)))
     ang (angle p1 p2)
     )
    (vl-remove-if 'null
        (mapcar '(lambda ( p3 p4 ) (inters (polar p1 ang l) (polar p2 (+ ang pi) l) p3 p4))
            vl
            (if (= 1 (logand 1 (cdr (assoc 70 en))))
                (append (cdr vl) (list (car vl)))
                (cdr vl)
            )
        )
    )
)
« Last Edit: July 18, 2015, 11:35:14 PM by mianbaoche »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Help with intersection
« Reply #15 on: July 18, 2015, 09:20:24 AM »
@ mianbaoche:
There is no need to use (Max-distance). The (inters) function has an optional 5th argument:
http://docs.autodesk.com/ACD/2011/ENU/filesALR/WS1a9193826455f5ff1a32d8d10ebc6b7ccc-69d4.htm

Please use code tags: http://www.theswamp.org/index.php?topic=48309.0.

BTW: Welcome to The Swamp.
« Last Edit: July 18, 2015, 09:25:39 AM by roy_043 »

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #16 on: July 21, 2015, 07:56:18 PM »
You can step through the vertices of the polyline, and use the built in Lisp function 'inters' with the four points ( two from the polyline, per segment, and the two you show as yellow in your image ) you have.

1+

Here's an example:
http://www.theswamp.org/index.php?topic=43630.msg489026#msg489026

Hello Lee,
I worked with her suggestion and functions to create the JoyFindInters.lsp code.
The code needs to be improved, but he finds the intersection of two points and a polyline that is not straight.

Code: [Select]
;; Based on Lee Mac codes
;; Args -> <LWPolyline-entity> <Entity name: 7ffffc6cc40> <point1>
;; (setq JoyPT (JoyFindInters en ptbase)) ==> (792.205 374.229)
;;
  (defun JoyFindInters (en JoyPtBase / LM:ProjectPointToLine GetPointsInBoundingBox  _polyinters JoyPointList JoyPtAux)
    ;; Lee Mac
    ;; Projects pt onto the line defined by p1,p2
    (defun LM:ProjectPointToLine (pt p1 p2 / nm)
      (setq nm (mapcar '- p2 p1)
            p1 (trans p1 0 nm)
            pt (trans pt 0 nm)
      ) ;_ fim de setq
      (trans (list (car p1) (cadr p1) (caddr pt)) nm 0)
    ) ;_ fim de defun
;;;
    ;; Bounding Box  -  Lee Mac
    ;; List with top left point top right point
    (defun GetPointsInBoundingBox (/ a b lst)
      (vla-getboundingbox
        (vlax-ename->vla-object en)
        'a
        'b
      ) ;_ fim de vla-getboundingbox
      (setq lst (mapcar 'vlax-safearray->list (list a b)))
      (mapcar '(lambda (a) (mapcar '(lambda (b) ((eval b) lst)) a))
              '((caar cadadr) (caadr cadadr))
      ) ;_ fim de mapcar
    ) ;_ fim de defun
;;;
;;;Lee Mac's suggestion
    (defun _polyinters (/ vl)
      (setq en (entget en)
            vl (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) en))
      ) ;_ fim de setq
      (vl-remove-if
        'null
        (mapcar '(lambda (p3 p4) (inters JoyPtBase JoyPtAux p3 p4))
                vl
                (if (= 1 (logand 1 (cdr (assoc 70 en))))
                  (append (cdr vl) (list (car vl)))
                  (cdr vl)
                ) ;_ fim de if
        ) ;_ fim de mapcar
      ) ;_ fim de vl-remove-if
    ) ;_ fim de defun
    (alert "cheguei")
    (setq JoyPointList (GetPointsInBoundingBox)
          JoyPtAux     (LM:ProjectPointToLine JoyPtBase (car JoyPointList) (cadr JoyPointList))
    ) ;_ fim de setq
;;;
    (car (_polyinters))
  ) ;_ fim de defun


squirreldip

  • Newt
  • Posts: 114
Re: Help with intersection
« Reply #17 on: July 21, 2015, 08:22:04 PM »
You can step through the vertices of the polyline, and use the built in Lisp function 'inters' with the four points ( two from the polyline, per segment, and the two you show as yellow in your image ) you have.

1+

Here's an example:
http://www.theswamp.org/index.php?topic=43630.msg489026#msg489026

2+

Complication will be if the polyline has curves (or splines).  If not, I would cycle through the vertices and use the inters command - each time an intersection is found I would check to see that the point was between the polyline pair.

Another complication is that there may be more than one intersection - you'd then need add some logic to which one you want to return.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Help with intersection
« Reply #18 on: July 22, 2015, 04:12:45 AM »
@ mianbaoche:
... The (inters) function has an optional 5th argument:
http://docs.autodesk.com/ACD/2011/ENU/filesALR/WS1a9193826455f5ff1a32d8d10ebc6b7ccc-69d4.htm
Velasquez what you are doing is similar to what mianbaoche proposed. There is no need for your two nested functions.

If the onseg argument is present and is nil, inters returns the point where the lines intersect, even if that point is off the end of one or both of the lines. If the onseg argument is omitted or is not nil, the intersection point must lie on both lines or inters returns nil. The inters function returns nil if the two lines do not intersect.
« Last Edit: July 22, 2015, 04:16:54 AM by roy_043 »

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #19 on: July 22, 2015, 08:27:56 AM »
@ mianbaoche:
... The (inters) function has an optional 5th argument:
http://docs.autodesk.com/ACD/2011/ENU/filesALR/WS1a9193826455f5ff1a32d8d10ebc6b7ccc-69d4.htm
Velasquez what you are doing is similar to what mianbaoche proposed. There is no need for your two nested functions.

If the onseg argument is present and is nil, inters returns the point where the lines intersect, even if that point is off the end of one or both of the lines. If the onseg argument is omitted or is not nil, the intersection point must lie on both lines or inters returns nil. The inters function returns nil if the two lines do not intersect.

The image shows the amount points of intersection with the polyline, points can not be extended need to be exact. I could not do it otherwise.
« Last Edit: July 22, 2015, 08:35:46 AM by velasquez »

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #20 on: July 22, 2015, 08:33:15 AM »
@ mianbaoche:
... The (inters) function has an optional 5th argument:
http://docs.autodesk.com/ACD/2011/ENU/filesALR/WS1a9193826455f5ff1a32d8d10ebc6b7ccc-69d4.htm
Velasquez what you are doing is similar to what mianbaoche proposed. There is no need for your two nested functions.

If the onseg argument is present and is nil, inters returns the point where the lines intersect, even if that point is off the end of one or both of the lines. If the onseg argument is omitted or is not nil, the intersection point must lie on both lines or inters returns nil. The inters function returns nil if the two lines do not intersect.

Hello Roy,
Working this way the point can occur outside the polyline for the search loop. This should not happen to me.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Help with intersection
« Reply #21 on: July 22, 2015, 09:24:38 AM »
Hello Roy,
Working this way the point can occur outside the polyline for the search loop. This should not happen to me.
The solution that I would use:
Call the (inters) function with nil as the 5th argument.
If an intersection is found check the intersection with:
Code: [Select]
(equal ptFound (vlax-curve-getclosestpointto polylineObj ptFound T) 1e-8)

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #22 on: July 22, 2015, 12:33:40 PM »
Hello Roy,
Working this way the point can occur outside the polyline for the search loop. This should not happen to me.
The solution that I would use:
Call the (inters) function with nil as the 5th argument.
If an intersection is found check the intersection with:
Code: [Select]
(equal ptFound (vlax-curve-getclosestpointto polylineObj ptFound T) 1e-8)
Thank you Roy,
I will try your suggestion.

velasquez

  • Newt
  • Posts: 195
Re: Help with intersection
« Reply #23 on: July 24, 2015, 06:48:15 PM »
Hello Roy,
Working this way the point can occur outside the polyline for the search loop. This should not happen to me.
The solution that I would use:
Call the (inters) function with nil as the 5th argument.
If an intersection is found check the intersection with:
Code: [Select]
(equal ptFound (vlax-curve-getclosestpointto polylineObj ptFound T) 1e-8)

Hello Roy
I tested your suggestion and it worked well in my case.
Thanks

Code: [Select]
;;;List of polyline segments in 3D points
;;;Args <LWPolyline-entity> Index of the first vertex
;;;(f en 0))

(defun f (en i)
  (if (<= i (vlax-curve-getendparam en))
    (cons
      (vlax-curve-getpointatparam en i)
      (f en (1+ i))
    ) ;_ fim de cons
  ) ;_ fim de if
) ;_ fim de defun

;;;Args -> <LWPolyline-entity> <Entity name: 7ffffc6cc40> <point1> <point2>
;;; (setq IntersPt (JoyFindIntersec en pt1 pt2)) ==> (792.205 374.229)
(defun JoyFindIntersec (JoyEnt p1 p2 / JoyPlSegments)
;;;
  (vl-load-com)
;;; 
;;;List of segments
  (setq JoySegments (f JoyEnt 0))
;;;Creates a list of possible intersections
  (setq JoyListPts
         (vl-remove-if
           'null
           (mapcar '(lambda (p3 p4) (inters p1 p2 p3 p4 nil))
                   lista
                   (cdr lista)
           ) ;_ fim de mapcar
         ) ;_ fim de vl-remove-if
  ) ;_ fim de setq
;;;
;;;Checks the position of the point in polyline
  (car (vl-remove-if
         'null
         (mapcar '(lambda (x)
                    (if (equal x (vlax-curve-getclosestpointto JoyEnt x t) 1e-8)
                      x
                    ) ;_ fim de if
                  ) ;_ fim de lambda
                 JoyListPts
         ) ;_ fim de mapcar
       ) ;_ fim de vl-remove-if
  ) ;_ fim de car"