Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SSGET not taking into account newly-created blocks ?

6 REPLIES 6
Reply
Message 1 of 7
lara2070
674 Views, 6 Replies

SSGET not taking into account newly-created blocks ?

Hi,

 

does the SSGet function not take into account the blocks that have just been created ? is there a temporary list stored somewhere else ?

here is my question:i'm trying to make it easy for users to create labels (blocks called Pline_block) for polylines. One feature that ScottBolton helped me with last month was to give a default 3-digit code value for these labels, that was basicelly the first available 3-digit number on the layout. this code works great for the first block that I create and gives me the next available 3-digit number (for instance 306). However if i run the lisp again for another polyline, the 3-digit number that it gives me is the same as before (306); which means it does not seem to read the block that had just been created...

any idea or suggestion is greatly appreciated thanks !

 

code abstract:

 

;;///là on regarde quel numéro est dispo
(setq ss (ssget "X" '(2 . "Pline_block"))
      count 0
      numlist nil
      )
(repeat (sslength ss)
  (setq ent (ssname ss count)
 cont (entget ent)
 obj2 (vlax-ename->vla-object ent)
 )
  (foreach a (vlax-invoke obj2 'getattributes)
    (if (= (vlax-get a 'TagString) "PIECE")
      (setq value (vlax-get a 'textString))
      )
    )
  (if (and
 (not (vl-string-search "room" (strcase value T)))
 (not (vl-string-search "chambre" (strcase value T)))
 )
    (progn
      (setq numlist (cons (substr value (- (strlen value) 2)) numlist))
      )
    )
  (setq count (1+ count))
  )
(setq numlist (acad_strlsort numlist)
      biggest (1+ (atoi (last numlist)))
      )
;;;//
(setq longueur (strlen(itoa biggest)))
(if (< longueur 3) (setq lenum(strcat "0" (itoa biggest))))
(if (null lenum) (setq lenum (itoa biggest)))
(setq longueur2 (strlen lenum))
(if (< longueur2 3) (setq lenum2 (strcat "0" lenum)));deux fois pour être sur d'avoir 3 chiffres
(if (null lenum2) (setq lenum2 lenum ))
;;;//

 

;;///là on regarde quel numéro est dispo(setq ss (ssget "X" '(2 . "Pline_block"))      

count 0      

numlist nil    

 )

(repeat (sslength ss) 

 (setq ent (ssname ss count) 

cont (entget ent) 

obj2 (vlax-ename->vla-object ent)

 ) 

 (foreach a (vlax-invoke obj2 'getattributes)  

 (if (= (vlax-get a 'TagString) "PIECE")    

 (setq value (vlax-get a 'textString))      

)  

 ) 

 (if (and 

(not (vl-string-search "room" (strcase value T)))

 (not (vl-string-search "chambre" (strcase value T))) 

)    

(progn      

(setq numlist (cons (substr value (- (strlen value) 2)) numlist))    

 )  

 ) 

 (setq count (1+ count)) 

 )

(setq numlist (acad_strlsort numlist)      

biggest (1+ (atoi (last numlist)))    

 )


;;;//

(setq longueur (strlen(itoa biggest)))

(if (< longueur 3) (setq lenum(strcat "0" (itoa biggest))))

(if (null lenum) (setq lenum (itoa biggest)))

(setq longueur2 (strlen lenum))(if (< longueur2 3)

(setq lenum2 (strcat "0" lenum)));deux fois pour être sur d'avoir 3 chiffres

(if (null lenum2)

(setq lenum2 lenum ))

;;;//

6 REPLIES 6
Message 2 of 7
SomeBuddy
in reply to: lara2070

Hi,

 

Maybe you could post the whole function and a drawing sample with the right stuff in it, so that some tests could be conducted. Just by reading some chunk  of code is not always obvious what the problem might be,

 

 

Message 3 of 7
honkinberry
in reply to: lara2070

The problem is with these two lines:

 

(setq numlist (cons (substr value (- (strlen value) 2)) numlist))

 

   biggest (1+ (atoi (last numlist)))

 

 

your numlist is storing the string values, but after trimming the last two characters.

But then that aside, you are then setting biggest to 1+ the last item in the list.

 

My guess is the logic should be more like:

(if (> (atoi valuex) biggest) (setq biggest (atoi value)))

Not sure why you even need numlist.

 

--J

 

 

Message 4 of 7
lara2070
in reply to: lara2070

It's true that I should have given an example drawing and the whole code (see attached): i thought i needed the numlist to only use the numerical value of the code (which is composed of string and number), that is why i truncated with numlist.

I tried using your hint honkinberry but no luck (I am a loooooooong way away from understanding lisp haha but i'll get there eventually!)

anyway, attached is a sample file, with a few polylines and a couple of labels, the code atatched works fine to create the next label (gives me "103" as a next number, which is correct), but if i try again, it gives me "103" again...

NB: if all the numerical values are following each other (i.e. the 3 rooms were labelled "001", "002", and "003"), there seems to be no problem...

 

Thank you all for your help!

 

 

 

Message 5 of 7
SomeBuddy
in reply to: lara2070

Hi,

 

I've tried to load your lisp file and I got the alert message saying that the block Pline_block est introuvable (as you can see in the attached screen shot). After checking your code it seems that the brackets are not ballanced.

 

Here is a function which seems to return the next available number correctly. I couldn't make your code work for me so I've just multiplied the blocks using the  copy-paste alternative and manually changing the value. The function seems to return the correct value each and every time, no matter the combination and the number of blocks. The code is a little longer, but this will work with any numeric value and not only with three digits numbers.

 

You can plug this function in your code if it works for you. Just pay attention to the fact that the next available value is returned as string, so the only thing you have to do is to concatenate it.

 

BTW, if you need more help, you can write me in french if it's easier for you, at the following adress:

 

cyberiq_at_mailcity_dot_com

 

HTH

 

 

(defun getnextnum ( / blksset attval numlist stl strnextval)
(vl-load-com)
(if (setq blksset (ssget "X" '((0 . "INSERT")(2 . "Pline_block"))))
(progn
(foreach
item
(mapcar
'vlax-ename->vla-object
(vl-remove-if 'listp (mapcar 'cadr (ssnamex blksset)))
)
(foreach att (vlax-invoke item 'getattributes)
(if (= (vlax-get att 'tagstring) "PIECE")
(setq attval (vlax-get att 'textstring))
)
)
(if
(and
(not (vl-string-search "room" (strcase attval T)))
(not (vl-string-search "chambre" (strcase attval T)))
)
(setq numlist
(cons
(read
(apply
'strcat
(member
(nth
(vl-position
T
(mapcar
'numberp
(mapcar
'read
(setq stl
(mapcar 'chr (vl-string->list attval))
)
)
)
)
stl
)
stl
)
)
)
numlist
)
)
)
)
(setq strnextval (itoa (setq nextval (1+ (apply 'max numlist)))))
(if (< (strlen strnextval) 3)
(repeat (- 3 (strlen strnextval))
(setq strnextval (strcat "0" strnextval))
)
strnextval
)
)
(alert "\nPLINE_BLOCK not found.")
)
)

 

 

Message 6 of 7
lara2070
in reply to: lara2070

Thanks ! I can't really get my head around the series of mapcar/read/etc functions but it works perfectly for what I need, so I'll take it as it is and try and figure it out slowly 🙂

 

Thank you again !

Message 7 of 7
SomeBuddy
in reply to: lara2070

You're welcome. If you want to make it easier for you, try to understand what these functions do from the most inner one to the exterior, because this is how the LISP works, each function passes a return value to the surrounding function, which in turn passes a return value to the surrounding function and so on.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost