TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Red Nova on June 17, 2019, 08:42:40 AM

Title: Track recently created objects and put them into a selection set
Post by: Red Nova on June 17, 2019, 08:42:40 AM
Hi.
I am looking for a way to track all recently created objects and put them into a selection set. I know how to do it one by one. But I would like to have a way to track new objects automatically. (I am lazy  :shifty:).
Say I have a lisp and at the start of it I put a "Start New Objects Tracking Mark" and somewhere near the end I put an "End New Objects Tracking Mark". The idea is to be able to track all items created between these 2 marks and have them in a selection set. (Preferably objects that have been deleted during lisp should be also removed from final selection set).
Not sure if this is even doable with lisp but would be grateful for comments :-).
Title: Re: Track recently created objects and put them into a selection set
Post by: MP on June 17, 2019, 08:50:59 AM
One way woukd be to establish a bookmark based on an object’s handle. Easy to then iterate thru objects created after it via entnext, putting them in a selection set, although you may have problems proccesing the selection set (depending if you process them 1 by 1 or via a command) if the objects don’t reside in the same layout. On my phone and in transit so no code sample.
Title: Re: Track recently created objects and put them into a selection set
Post by: ribarm on June 17, 2019, 08:59:05 AM
You could be able to do it very easy in following way :

(defun c:xxx ( / e entlst group ) ;;; start lisp - note that after routine is finished variable "ss" is global - sel.set with newly created entities - that's why is not localized and put among ( / e entlst group )

- start tracking
(setq e (entlast))

- create new entity - appending database
(entmake '((0 . "LINE") (10 0.0 0.0 0.0) (11 1.0 1.0 1.0)))

- collecting new objects and putting them in tracking entity list
(if (not (eq e (setq e (entlast))))
  (setq entlst (cons e entlst))
)

- end tracking
(setq group entlst)
(setq entlst nil)

- end of lisp and selecting grouped entities
(setq ss (ssadd))
(foreach e group
  (ssadd e ss)
)
(sssetfirst nil ss)
(prompt "\nObjects highlighted and stored in sel.set \"ss\"...")
(princ);silent_finish_of_(c:xxx)

); end_defun_(c:xxx)
Title: Re: Track recently created objects and put them into a selection set
Post by: Red Nova on June 17, 2019, 09:28:34 AM
MP, Thank you. Unfortunately I don't understand what this means, never used "object’s handle" and no clue what means "establish a bookmark". Maybe later you could drop a sample when you have a chance.

ribarm. Thank you, in this case I have to every time put the new object in the tracking entity list once it is created? I am trying to avoid this. The problem is I have many lisp codes and on the way I am creating multiple items. Now I need to modify all of these codes. And I need to receive a selection set of all objects created while code was running. I would prefer not check each code one by one to see where an item is created and add it to an entity list. If I could put only "Start Tracking" and "End Tracking" and receive a selection set in the end this would make the modification so much easier.
Title: Re: Track recently created objects and put them into a selection set
Post by: kpblc on June 17, 2019, 09:46:07 AM
I think the better way is to modify your routines and make them return lisp of created objects. And at your 'main' lisp you can collect them and modify (or erase ;))
Title: Re: Track recently created objects and put them into a selection set
Post by: MP on June 17, 2019, 10:23:02 AM
I've only time for a a quick and dirty sample function to illustrate the gathering of entities in a selection set after a specfied entity handle:

Code: [Select]
(defun _Entities-After-Handle ( handle / ss ename data )
    ;;  Return the primary entities after the entity corresponding to arg handle.
    (if (eq 'ename (type (setq ss (ssadd) ename (vl-catch-all-apply 'handent (list handle)))))
        (while ename
            (and
                (setq data (entget ename))
                (null (member (cdr (assoc 0 data)) '("VERTEX" "ATTRIB" "SEQEND")))
                (setq ss (ssadd ename ss))
            )
            (setq ename (entnext ename))
        )
    )   
    (if (< 0 (sslength ss)) ss)
)

If you don't know what handles are you may be trying to run before having learned to walk. This (http://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-AutoLISP/files/GUID-30C33AF6-4BE3-4334-96BD-F929040C31D3-htm.html) may help you in your journey. Cheers.
Title: Re: Track recently created objects and put them into a selection set
Post by: MP on June 17, 2019, 10:26:12 AM
a a

Didn't mean to sound like our pathetic crime minister.
Title: Re: Track recently created objects and put them into a selection set
Post by: Grrr1337 on June 17, 2019, 10:42:37 AM
Heres kinda relevant thread:
http://www.theswamp.org/index.php?topic=52594.msg574992#msg574992
Title: Re: Track recently created objects and put them into a selection set
Post by: tombu on June 17, 2019, 01:42:55 PM
Take a look at irneb's lisp at https://forums.augi.com/showthread.php?81175-select-result-lisp-modification#5
Title: Re: Track recently created objects and put them into a selection set
Post by: Red Nova on June 17, 2019, 03:50:32 PM
Thank you all.

I ended up editing all 31 of my lisps, finding all new entities and adding them to selection sets individually.
Turned to be the easiest way of solving my problem noting my humble lisp knowledge.

Cheers.
Title: Re: Track recently created objects and put them into a selection set
Post by: mailmaverick on June 22, 2019, 10:00:10 AM
Use following code :-
Code: [Select]
(defun c:StartNewObjectsTracking ()
  ;;Start New Objects Tracking Mark
  (setq ssuniversal1 (ssget "_X"))
  (princ "\nNew Objects Tracking Started.")
  (princ)
)
(defun c:EndNewObjectsTracking ()
  ;;End New Objects Tracking Mark
  (setq ssuniversal2 (ssget "_X"))
  (setq ssnew (kdub:sssubtract ssuniversal2 ssuniversal1))
  (sssetfirst nil ssnew)
  (setq ssuniversal1 nil
        ssuniversal2 nil
  )
  (princ (strcat "\nNew Objects Tracking Ended. " (itoa (sslength ssnew)) " New Objects Created."))
  (princ)
)
(defun kdub:sssubtract (ss1 ss2 / ss)
  ;; Subtracts one selection set from another and returns their difference
  (cond ((and ss1 ss2) (vl-cmdf "._Select" ss1 "_Remove" ss2 "") (setq ss (ssget "_P")))
        (ss1 (setq ss ss1))
        (t (setq ss nil))
  )
  ss
)