Jump to content

creating a new list for each object


cletero

Recommended Posts

HI, I will try to make this as clear as possible. I have a lisp that offsets objects multiple times. For further processing, I need to list the objects in the reverse order as they were created by the lisp, which is kind of easy, but I also need to tell or group objects as different lists if the offset creates 2 objects from the original.

In the attachment, you can see the results for a random object (see the one on the top), the different colors represent the different groups I need to separate, the code I'm running will create the red offsets first, then the biggest blue offset and the biggest green offset, the the rest of the blue offsets and lastly the remaining green offsets. Similar way happens on the bottom object

I was thinking about creating a new list each time more than two objects are created, but I can't find a way of giving each list a unique name (ex: list1, list2, list3, etc) since there is no way of telling how many objects will be created.

I have run out of ideas so any help will be appreciated.

offset.dwg

Link to comment
Share on other sites

In case it helps, this is the lisp I'm using to offset:

;Alfredo Rodriguez, 18 May 2014
;This will offset an object multiple times
;It will offset to the inside/outside, up/down, left/right depending on the drawing direction
;So it's better to always set a maximum number of offsets or else it could offset indefinitely
;For this reason, if use default is chosen, it will offset the object 20 times, or you can
;choose how many times to offset. If object is offset to the wrong side, enter a negative distance (ej: -3 instead of 3)
;I'm not a lisp professional, so I can't guaranty any results obtained with this code, so use with care.
(defun c:cntoffset (/ ename dist vobj obj objlist objlist2 how_many count maxcount thisdrawing size listname)
(vl-load-com)

(setq  thisdrawing (vla-get-activedocument (vlax-get-acad-object)))    ;This  makes the Undo command erase only what's drawn in this lisp
(vla-StartUndoMark thisdrawing)    ;This makes the Undo command erase only what's drawn in this lisp

       (null (initget 7))

       (while (setq ent (ssget))
           (setq dist (getdist "\nEnter offset distance: "))
           (setq how_many (sslength ent))
           (setq count 0)
       
           (or
           (setq maxcount (getdist "\nEnter number of offsets (or press Enter to use default): "))
           (setq maxcount 50)
           )
           
           (while (/= count how_many)
               (setq ename (ssname ent count))
               (setq obj (list (vlax-ename->vla-object ename)))
                 (setq objlist (append obj objlist))
               (setq count (+ count 1))
           )

               (setq count 0)
               (while (and (/= count maxcount) (/= objlist nil))
                   (setq count (+ count 1))
                   (setq vobj (car objlist))
                   (if (vlax-method-applicable-p vobj "Offset")
                       ;(progn ;Activate this line during testing to see order in which objects are drawn
                           (if (vl-catch-all-error-p
                               (setq objlist2 (vl-catch-all-apply 'vlax-invoke
                               (list vobj 'Offset (- dist)))))
                               (progn (setq objlist2 nil)
                                   (prompt "\n*** Can not offset that object, try again. ***")
                                     (setq count 0)
                               )
                           (setq size (length objlist2));To see if more than one object was created
                           )
                               
                           ;(prompt "\nDone") ;Activate this line during testing to see order in which objects are drawn
                            ;(alert "\nJust for testing!!!") ;Activate this  line during testing to see order in which objects are drawn
                        ;Activate this line during testing to see order in which objects are drawn
                   )
                   (if (> size 1);To create a different list name for each object, but this seens to be going nowhere
           (progn (setq count2 1)
                   (while (< count2 size)
                   (setq count2 (+ count2 1))
                   (setq listname (strcat "polylist" (itoa count2)))
                   )
           )
           )
                   (setq objlist (append objlist2 (cdr objlist)))
               )

               (setq objlist nil)
               (setq objlist2 nil)
       )

(vla-EndUndoMark thisdrawing)    ;This makes the Undo command erase only what's drawn in this lisp

(princ)

)

Link to comment
Share on other sites

maybe can refer to the handle which is incremental HEX also unique name?

_$ (handent "e23")

each step cons / append to a list

Link to comment
Share on other sites

Like hanhphuc just use (Entlast) as you create each offset and SSADD add obj to a selection set you can then retrive each one. Repeat for as many selections sets as required.

Link to comment
Share on other sites

Hi, guys. Thanks for your answers.

However, I'm not sure if I understand your suggestions... the problem is, at some point more than one entity is created by the offset, so the first of these will be appended to the list, the other ones must be stored to be added to the list after the first one is further offset.

Ex: A creates B, B creates C,D and E, C creates F and G (and then returns "nil"), D creates H and I (and then "nil"), E creates J (and "nil"), so the final lists would have to be (A B C F G) (D H I) (E J). This can repeat multiple times at any level.

 

So what I did was something like this:

store the created objects in a temporarylist, if it's size is bigger than 1, append car of temporarylist to mylist and then set temporarylist to cdr of itself. As soon as "nil" is returned, a marker is inserted to mylist and the car of temporarylist is appended, and again temporary list is set to cdr of itself. This goes on until no more objects are created.

The marker is then used to create a list which is processed by my function, then deleted, create another list with items until next marker and so on.

 

Here's the part of the code I wrote which deals with this, in case it helps anybody or if there are any suggestions on how to improve it.

"polylist" is the list I'm looking for.

 

(setq count 0)
               (while (and (/= count maxcount) (/= objlist nil))
                   (setq count (+ count 1))
                   (setq vobj (car objlist))
                   (if (vlax-method-applicable-p vobj "Offset")
                       ;(progn ;Activate this line during testing to see order in which objects are drawn
                           (if (vl-catch-all-error-p
                               (setq objlist2 (vl-catch-all-apply 'vlax-invoke
                               (list vobj 'Offset (- dist)))))
                               (progn (setq objlist2 nil)
                   (prompt "\n*** Can not offset that object, try again. ***")
                   (setq count 0)
                                 (setq polylist (append polylist (list "p" (car polylist2))))
                               (setq polylist2 (cdr polylist2))
                               )
                               (setq size (length objlist2));To see if more than one object was created
                           )
               ;(prompt "\nDone") ;Activate this line during testing to see order in which objects are drawn
                           ;(alert "\nJust for testing!!!") ;Activate this line during testing to see order in which objects are drawn
                        ;Activate this line during testing to see order in which objects are drawn
                   )
                   (if (> size 1);To separate list if more than one object was created
                       (progn 
                           (setq polylist (append polylist (list (car objlist2))))
                           (setq polylist2 (append polylist2 (cdr objlist2)))
                       )
                             (setq polylist (append polylist objlist2))
                   )
                   (setq objlist (append objlist2 (cdr objlist)))
               )

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...