• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    Visual LISP, AutoLISP and General Customization

    Reply
    Distinguished Contributor
    Posts: 136
    Registered: ‎02-10-2004
    Accepted Solution

    select lines/plines that intersect a given pline..

    324 Views, 8 Replies
    09-13-2012 08:03 AM

    Hi,

    I need to change the color of all lines/plines that intersect a given pline. How would I go about making the selection set of all lines that intersect that given pline.

    Thanks for any help

    regards

    Sean

    Please use plain text.
    Valued Mentor
    _Tharwat
    Posts: 459
    Registered: ‎07-02-2010

    Re: select lines/plines that intersect a given pline..

    09-13-2012 09:00 AM in reply to: sean.keohane

    Here it goes ...

     

    (defun c:Test (/ s prm i lst ss)
      (if (setq s (ssget "_+.:S" '((0 . "*POLYLINE"))))
        (progn
          (setq prm (fix (vlax-curve-getendparam (ssname s 0))))
          (setq i 0)
          (repeat (1+ prm)
            (setq
              lst (cons (vlax-curve-getpointatparam (ssname s 0) i) lst)
            )
            (setq i (1+ i))
          )
          (if (setq ss (ssget "_F" lst '((0 . "LINE,*POLYLINE"))))
            (sssetfirst nil ss)
          )
        )
        (princ)
      )
    )

     Tharwat

    Please use plain text.
    *Expert Elite*
    Kent1Cooper
    Posts: 4,057
    Registered: ‎09-13-2004

    Re: select lines/plines that intersect a given pline..

    09-13-2012 11:51 AM in reply to: sean.keohane

    sean.keohane wrote:

    .... I need to change the color of all lines/plines that intersect a given pline. How would I go about making the selection set of all lines that intersect that given pline.

    ....


    I find that _Tharwat's suggestion can, if the Polyline has any arc segments, miss some Lines that do intersect the Polyline, and/or "find" some that don't intersect, because the Fence line through the vertices will go straight across the chords of the arcs, rather than follow their curvature.
     

    If that is a possibilty that you need to account for, you probably need to collect a selection set of all Lines in the drawing [or in a selected area, or something], and do a series of (vla-intersectwith) tests, pairing each of them with the Polyline, and if that doesn't find any intersection, remove that Line from the selection set.  In simplest terms, without the usual controls, etc., something like this [minimally tested]:

     

    (defun C:LIP (/ pobj lss lint inc lent); = LinesIntersectingPolyline
      (setq
        pobj (vlax-ename->vla-object (car (entsel "\nSelect Polyline: "))); = Polyline OBJect
        lss (ssget "_X" '((0 . "LINE"))); = Line Selection Set
        lint (ssadd); = Lines that INTersect -- start empty
        inc -1
      ); setq
      (repeat (sslength lss)
        (setq lent (ssname lss (setq inc (1+ inc)))); = Line ENTity
        (if
          (vlax-invoke
            pobj
            'IntersectWith
            (vlax-ename->vla-object lent)
            acExtendNone
          ); vlax-invoke
          (ssadd lent lint)
        ); if
      ); repeat
    ); defun

     

    Or, if the drawing may have a very large number of Lines in it [as most of ours do], that might be a tedious effort.  You could get closer to overcoming the arc-segment problem by making the selection Fence in _Tharwat's suggestion with a series of more closely-spaced points along the Polyline, using multiples of some comparatively short distance, rather than using the vertices.  If you're really tricky, you could probably use just the vertices for all line segments, and only along arc segments, add points close enough together to approximate the curve to whatever degree of accuracy you need.

    Kent Cooper
    Please use plain text.
    *Expert Elite*
    Kent1Cooper
    Posts: 4,057
    Registered: ‎09-13-2004

    Re: select lines/plines that intersect a given pline..

    09-13-2012 02:32 PM in reply to: Kent1Cooper

    Kent1Cooper wrote:
    ....
    lss (ssget "_X" '((0 . "LINE"))); = Line Selection Set
    ....

    Oops -- I paid attention to the one sentence in the original post that mentioned Lines but not Polylines.  Try this instead:

    (defun C:LPIP (/ pobj ss int inc ent); = LinesPolylinesIntersectingPolyline
      (setq
        pobj (vlax-ename->vla-object (car (entsel "\nSelect Polyline: "))); = Polyline OBJect
        ss (ssget "_X" '((0 . "LINE,*POLYLINE"))); = line/polyline Selection Set
        int (ssadd); = things that INTersect -- start empty
        inc -1
      ); setq
      (repeat (sslength ss)
        (setq ent (ssname ss (setq inc (1+ inc)))); = line/polyline ENTity
        (if
          (vlax-invoke
            pobj
            'IntersectWith
            (vlax-ename->vla-object ent)
            acExtendNone
          ); vlax-invoke
          (ssadd ent int)
        ); if
      ); repeat
    ); defun

    Kent Cooper
    Please use plain text.
    Distinguished Contributor
    Posts: 136
    Registered: ‎02-10-2004

    Re: select lines/plines that intersect a given pline..

    09-13-2012 04:44 PM in reply to: Kent1Cooper

    Kent/Tharwat,

    Thank you for your replies. I've tried your codes and I played around with it but I can't get it to give me a selection set of lines that intersect. I keep getting  - Select Polyline: ; error: An error has occurred inside the *error*
    functionAutoCAD variable setting rejected: "osmode" nil

    I'd appreciate it if you could take another look at it if you have time.

    thanks

    Sean

    Please use plain text.
    *Expert Elite*
    Kent1Cooper
    Posts: 4,057
    Registered: ‎09-13-2004

    Re: select lines/plines that intersect a given pline..

    09-13-2012 05:13 PM in reply to: sean.keohane

    sean.keohane wrote:

    .... I played around with it but I can't get it to give me a selection set of lines that intersect. I keep getting  - Select Polyline: ; error: An error has occurred inside the *error*
    functionAutoCAD variable setting rejected: "osmode" nil

    ....


    There must be something else going on.  _Tharwat's routine worked for me when I tried it earlier [except for the arc-segment question], and mine works once I remove the 'int' variable name from the localized variables list at the top [rather silly of me to include it there], so you can actually use what's stored in it afterwards.  [It still "works" if you don't fix that, but you can't use the selection set it built.]

     

    Neither includes anything about Osmode, and neither contains an *error* function, so there's probably something related to another routine sticking around.  Are you sure nothing else is active when you try it?  Have you tried opening a new drawing, drawing some of those things, and trying it, without having run any other routines yet?  Are you getting the same error with both routines?

    Kent Cooper
    Please use plain text.
    Distinguished Contributor
    Posts: 136
    Registered: ‎02-10-2004

    Re: select lines/plines that intersect a given pline..

    09-14-2012 01:13 AM in reply to: Kent1Cooper

    Hi Kent,

    I tried again this morning and both your code and Tharwat's code return a selection.(don't know what happened last night) Your code however returns all lines in the dwg in the selection and not just the lines that intersect my selected pline. Tharwat's code returns the selection sought. I like the added intention for arc segment in your code and I'll continue to work on it.

    Thanking you and Tharwat again for sharing your expertise.

    Regards

    Sean

     

    Please use plain text.
    *Expert Elite*
    Kent1Cooper
    Posts: 4,057
    Registered: ‎09-13-2004

    Re: select lines/plines that intersect a given pline..

    09-14-2012 05:19 AM in reply to: sean.keohane

    sean.keohane wrote:

    .... Your code however returns all lines in the dwg in the selection and not just the lines that intersect my selected pline. .... 


    Odd again....  Afer removing that one variable name from the localized list in each, both the LIP [Lines only] and LPIP [Lines & Polylines] are working for me, without including all Lines [or all Lines & Polylines] in the final set.

    Kent Cooper
    Please use plain text.
    Distinguished Contributor
    Posts: 136
    Registered: ‎02-10-2004

    Re: select lines/plines that intersect a given pline..

    09-14-2012 09:54 AM in reply to: Kent1Cooper

    Hi Kent,

    Thanks again for helping me out. I unloaded a menu (for contour generating from a points file) I use it once in a blue moon. Restarted Autocad and your lisp is working. It was the only non standard feature permanently loaded, so perhaps some feature of that program was interfering with your lisp. Anyhow, its working at my end now.

    Have a good weekend,

     

    no -  have a great weekend!

    Regards

    Sean Keohane

    Please use plain text.