Results 1 to 8 of 8

Thread: Select the last N entities created in a lisp routine.

  1. #1
    I could stop if I wanted to
    Join Date
    2011-09
    Posts
    308
    Login to Give a bone
    0

    Default Select the last N entities created in a lisp routine.

    OK gang, here's today's conundrum.

    I've got this lisp that draws a ramp tag and it works great:

    Code:
    (defun c:ramp (/ arrow slopetext slopeorigin layorig usersnaps p1 p2 x1 x2 dist ang p3 angdeg)
     
    (setq arrow "slopearrow.dwg")
    (setq slopetext "slopevalue.dwg")
    (setq slopeorigin "slopeorigin.dwg")
    
    (setq layorig (getvar "clayer"))
    
    (setq p1 (getpoint "\npick high point of ramp: "))
    (setq p2 (getpoint "\npick low point of ramp: "))
    (setq x1 (car p1))
    (setq x2 (car p2))
    (setq dist (distance P1 P2))
    (setq ang (angle P1 P2))
    (setq p3 (POLAR P1 ANG (/ DIST 2)))
    (setq angdeg (* (/ ang pi) 180.0))
    
    (command "-layer" "Make" "L-GR-TEXT" "Color" "3" "L-GR-TEXT" "Ltype" "CONTINUOUS" "" "")(princ)
    (command "line" p1 p2 "")
    (command "-insert" slopeorigin P1 "1" "1" angdeg)
    (command "-insert" arrow p2 "1" "1" angdeg)
    (command "-insert" slopetext p3 "1" "1" angdeg)
    
    (if (> x1 x2) (command "rotate" "L" "" p3 "180")
    )
    
    (setvar "clayer" layorig)
    
    (princ)
    )
    Now I've been asked to have the routine to make an unnamed group from the objects it creates. So I have cobbled this together (added code in red):

    Code:
    (defun c:ramp ( / arrow slopetext slopeorigin layorig usersnaps p1 p2 x1 x2 dist ang p3 angdeg ss lastEnt en)
    
    (setq arrow "slopearrow.dwg")
    (setq slopetext "slopevalue.dwg")
    (setq slopeorigin "slopeorigin.dwg")
    (setq layorig (getvar "clayer"))
    (setq lastEnt (entlast))
    
    (setq p1 (getpoint "\npick high point of ramp: "))
    (setq p2 (getpoint "\npick low point of ramp: "))
    (setq x1 (car p1))
    (setq x2 (car p2))
    (setq dist (distance P1 P2))
    (setq ang (angle P1 P2))
    (setq p3 (POLAR P1 ANG (/ DIST 2)))
    (setq angdeg (* (/ ang pi) 180.0))
    
    (command "-layer" "Make" "L-GR-TEXT" "Color" "3" "L-GR-TEXT" "Ltype" "CONTINUOUS" "" "")(princ)
    
    (command "line" p1 p2 "")
    (command "-insert" slopeorigin P1 "1" "1" angdeg)
    (command "-insert" arrow p2 "1" "1" angdeg)
    (command "-insert" slopetext p3 "1" "1" angdeg)
    
    (if (> x1 x2) (command "rotate" "L" "" p3 "180")
    )
    
    
    (setq ss (ssadd))
       (if (setq en (entnext LastEnt)) ;Check if there's a new entity created sinse the last one
         (while en ;Step through all new entities
           (ssadd en ss) ;Add it to the selection set
           (setq en (entnext en)) ;Get the next entity
         )
       )
    
    (command "_.-group" "c" "*" "*" ss "")
    
    (setvar "clayer" layorig)
    (princ)
    )
    It does what it should the first time, it makes a group form the objects created in the routine.

    However, during subsequent runs, it makes a group of the objects created in the routine AND the last group created in the previous run. Obviously it should EXCLUDE the previous group and make the new group from only objects created SINCE last object was created.

    I'm missing something. Thanks for your help.

    I can't seem to upload files at the moment but Slopeorigin.dwg is just a circle, slopearrow.dwg is just an arrow made from 2 lines and slopevalue.dwg is just an atribute. You should be able substitue your own blocks easily. I'll keep trying.

  2. #2
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,685
    Login to Give a bone
    0

    Default Re: Select the last N entities created in a lisp routine.

    Try moving
    Code:
    (setq lastEnt (entlast))
    down to just before
    Code:
    (command "line" p1 p2 "")
    to make sure (entnext LastEnt) is that line.

    Worked for me, but I don't have those drawings to test it completely.

  3. #3
    I could stop if I wanted to
    Join Date
    2011-09
    Posts
    308
    Login to Give a bone
    0

    Default Re: Select the last N entities created in a lisp routine.

    Thanks Tom, I'll keep you posted!!

  4. #4
    I could stop if I wanted to
    Join Date
    2011-09
    Posts
    308
    Login to Give a bone
    0

    Default Re: Select the last N entities created in a lisp routine.

    Quote Originally Posted by Tom Beauford View Post
    Try moving
    Code:
    (setq lastEnt (entlast))
    down to just before
    Code:
    (command "line" p1 p2 "")
    to make sure (entnext LastEnt) is that line.

    Worked for me, but I don't have those drawings to test it completely.

    Well, it does the same thing. I think it has to do with this section later on in the routine:

    Code:
    (if (> x1 x2) (command "rotate" "L" "" p3 "180")
    )
    That line rotates the slope text depending on how you pick the points and it uses the "Last" object selection.

  5. #5
    I could stop if I wanted to
    Join Date
    2011-09
    Posts
    308
    Login to Give a bone
    0

    Default Re: Select the last N entities created in a lisp routine.

    Eh... having second thoughts about it being that line. I now think its in the section of the code below. I think this for 2 reasons:

    1. This is the section that collects objects created SINCE Entlast
    2. I copied this from some other routine and I don't REALLY understand how it's working.



    Code:
    (setq ss (ssadd))
      (if (setq en (entnext LastEnt)) ;Check if there's a new entity created since the last one
        (while en ;Step through all new entities
          (ssadd en ss) ;Add it to the selection set
          (setq en (entnext en)) ;Get the next entity
        )
      )

  6. #6
    I could stop if I wanted to
    Join Date
    2011-09
    Posts
    308
    Login to Give a bone
    0

    Default Re: Select the last N entities created in a lisp routine.

    Well, I've got it working... but I'm not proud of how I did it. It seems clunky to me and I'm sure there is a more elegant way to accomplish it.

    The first thing it does is draw a point at 0,0 (which becomes the last entity)
    Then at the end of the command, it erases the point.

    It works, but you know... not a great solution. Plus if it fails for some reason, it's going to leave a point at 0,0. Meh. I give it a C-.

    Code:
    (defun c:ramp ( / arrow slopetext slopeorigin layorig usersnaps p1 p2 x1 x2 dist ang p3 angdeg ss lastEnt en)
    
    (command "point" "0,0")
    
    (setq lastEnt (entlast))
    (setq arrow "slopearrow.dwg")
    (setq slopetext "slopevalue.dwg")
    (setq slopeorigin "slopeorigin.dwg")
    (setq layorig (getvar "clayer"))
    
    (setq p1 (getpoint "\npick high point of ramp: "))
    (setq p2 (getpoint "\npick low point of ramp: "))
    (setq x1 (car p1))
    (setq x2 (car p2))
    (setq dist (distance P1 P2))
    (setq ang (angle P1 P2))
    (setq p3 (POLAR P1 ANG (/ DIST 2)))
    (setq angdeg (* (/ ang pi) 180.0))
    
    (command "-layer" "Make" "L-GR-TEXT" "Color" "3" "L-GR-TEXT" "Ltype" "CONTINUOUS" "" "")(princ)
    
    (command "line" p1 p2 "")
    (command "-insert" slopeorigin P1 "1" "1" angdeg)
    (command "-insert" arrow p2 "1" "1" angdeg)
    (command "-insert" slopetext p3 "1" "1" angdeg)
    
    (if (> x1 x2) (command "rotate" "L" "" p3 "180")
    (command "rotate" "L" "" p3 "0")
    )
    
    (setq ss (ssadd))
      (if (setq en (entnext LastEnt)) ;Check if there's a new entity created since the last one
        (while en ;Step through all new entities
          (ssadd en ss) ;Add it to the selection set
          (setq en (entnext en)) ;Get the next entity
        )
      )
    
    (command "_.-group" "c" "*" "*" ss "")
    
    (command "erase" lastent "")
    
    (setvar "clayer" layorig)
    (princ)
    )

  7. #7
    All AUGI, all the time
    Join Date
    2015-10
    Location
    Belgrade, Serbia, Europe
    Posts
    564
    Login to Give a bone
    0

    Default Re: Select the last N entities created in a lisp routine.

    If it works fine, than this should work too (added error handler in case routine fails - it sets back sysvar clayer to original state and automatically removes dummy point)... You can study error handlers from www.lee-mac.com ... HTH, M.R.

    Code:
    (defun c:ramp ( / *error* pt arrow slopetext slopeorigin layorig usersnaps p1 p2 x1 x2 dist ang p3 angdeg ss lastEnt en )
    
    
    (defun *error* ( msg )
    (if layorig (setvar 'clayer layorig))
    (if (and pt (entget pt)) (entdel pt))
    (if msg (prompt msg))
    (princ)
    )
    
    ;;;(command "point" "0,0")
    (setq pt (entmakex (list '(0 . "POINT") (cons 10 (list 0.0 0.0 0.0)))))
    
    (setq lastEnt (entlast))
    (setq arrow "slopearrow.dwg")
    (setq slopetext "slopevalue.dwg")
    (setq slopeorigin "slopeorigin.dwg")
    (setq layorig (getvar "clayer"))
    
    (setq p1 (getpoint "\npick high point of ramp: "))
    (setq p2 (getpoint "\npick low point of ramp: "))
    (setq x1 (car p1))
    (setq x2 (car p2))
    (setq dist (distance P1 P2))
    (setq ang (angle P1 P2))
    (setq p3 (POLAR P1 ANG (/ DIST 2)))
    (setq angdeg (* (/ ang pi) 180.0))
    
    (command "-layer" "Make" "L-GR-TEXT" "Color" "3" "L-GR-TEXT" "Ltype" "CONTINUOUS" "" "")(princ)
    
    (command "line" p1 p2 "")
    (command "-insert" slopeorigin P1 "1" "1" angdeg)
    (command "-insert" arrow p2 "1" "1" angdeg)
    (command "-insert" slopetext p3 "1" "1" angdeg)
    
    (if (> x1 x2) (command "rotate" "L" "" p3 "180")
    (command "rotate" "L" "" p3 "0")
    )
    
    (setq ss (ssadd))
      (if (setq en (entnext LastEnt)) ;Check if there's a new entity created since the last one
        (while en ;Step through all new entities
          (ssadd en ss) ;Add it to the selection set
          (setq en (entnext en)) ;Get the next entity
        )
      )
    
    (command "_.-group" "c" "*" "*" ss "")
    
    ;;;(command "erase" lastent "")
    
    ;;;(setvar "clayer" layorig)
    ;;;(princ)
    
    
    (*error* nil)
    
    )

  8. #8
    I could stop if I wanted to
    Join Date
    2011-09
    Posts
    308
    Login to Give a bone
    0

    Default Re: Select the last N entities created in a lisp routine.

    Works great Marko!! Thanks for your hard work.

    Now I just need to stare at it for an hour and figure out WHY it works.

    And yeah... Lee Mac is the man.

Similar Threads

  1. Looking for lisp routine to select objects inside polyline
    By vlad_tzok133583 in forum AutoLISP
    Replies: 22
    Last Post: 2022-04-29, 05:44 AM
  2. Replies: 8
    Last Post: 2016-01-22, 01:38 PM
  3. LISP CHALLENGE - symbols created within routine
    By bowlingbrad in forum AutoLISP
    Replies: 3
    Last Post: 2008-12-23, 07:40 PM
  4. Create a Selection Set of Entities as They are Created
    By autocad.wishlist1734 in forum AutoCAD Wish List
    Replies: 0
    Last Post: 2008-10-12, 06:37 PM
  5. Can't select entities
    By jpaulsen in forum AutoCAD General
    Replies: 6
    Last Post: 2007-04-19, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •