Problem with LISP - Renaming & renumbering tabs

Problem with LISP - Renaming & renumbering tabs

Anonymous
Not applicable
1,095 Views
4 Replies
Message 1 of 5

Problem with LISP - Renaming & renumbering tabs

Anonymous
Not applicable

Hi, I'm a total noob with this. I've searched what i'm looking for but I can't find exactly what I want.

My goal is a lisp that ask you for a prefix, and what number should start renumbering the layouts.

So, you introduce for example "C-2-" and "5", and all the layouts should be like "C-2-05, C-2-06...." (with the zero when its <10).

I've tried modifying some scripts I've found in this forum and merging them. So my script is this:

 

 

(defun c:renombrapres (/ acad-object active-document layouts lout i prefijo)
	(setq prefijo 
		(getstring "\by HCP - Inserta prefijo:"))
	(setq acad-object (vlax-get-acad-object))
	(setq active-document (vla-get-activedocument acad-object))
	(setq layouts (vla-get-layouts active-document))
	(setq i 1)
	(vlax-for lout layouts
		(if (/= (vla-get-name lout) "Model")
			(if( > i 9)
			(vla-put-name lout (strcat prefijo (rtos i 2 0)))
			(vla-put-name lout (strcat prefijo "0" (rtos i 2 0))))
		)		
		(setq i (1+ i))
		(vlax-release-object lout)
	)
	(vlax-release-object layouts)
	(vlax-release-object active-document)
	(vlax-release-object acad-object)
	(setq i 1)
	(prompt "by HCP - Listo!")
	(princ)	
)

But it doesnt work properly, becuase

- It wont ask for the starting number

- The first time runs fine, but the next, sometimes it will start numbering at 02, dont know why.

 

I have tried this variation but It gives me an error with the number I enter, so It don't work at all.

 

(defun c:renombrapres (/ acad-object active-document layouts lout i prefijo num)
	(setq prefijo 
		(getstring "\byy HCP - Inserta prefijo:"))
	(setq num 
		(getint "\by HCP - Inserta primer número (sin 0):"))
	(setq acad-object (vlax-get-acad-object))
	(setq active-document (vla-get-activedocument acad-object))
	(setq layouts (vla-get-layouts active-document))
	(setq i num)
	(vlax-for lout layouts
		(if (/= (vla-get-name lout) "Model")
			(if( > i 9)
			(vla-put-name lout (strcat prefijo (rtos i 2 0)))
			(vla-put-name lout (strcat prefijo "0" (rtos i 2 0))))
		)		
		(setq i (num + i))
		(vlax-release-object lout)
	)
	(vlax-release-object layouts)
	(vlax-release-object active-document)
	(vlax-release-object acad-object)
	(prompt "by HCP - Listo!")
	(princ)	
)

Please help me!!

Thank you very much!!!

 

PS:Sorry my bad english

0 Likes
Accepted solutions (1)
1,096 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

Try this one. The issue might be that your routine does not sort tabs by its current order but takes the order of creation.. just guessing.

 

Try this one. Special thanks to @pbejse for his sub.

 

(vl-load-com)

(defun c:LayoutsNumbering (/ i Layorder pre layouts)
  
  ; pbejse https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/layout-order/td-p/3749964
  (defun Layorder (/ order)
    (vlax-for lay (vla-get-layouts (vla-get-ActiveDocument (vlax-get-acad-object)))
      (setq order (cons (list (vla-get-name lay)
                              (vla-get-taborder lay))
                        order)))
    (mapcar 'car (Cdr (vl-sort order '(lambda (j k) (< (cadr j) (cadr k)))))))
  
  ; --------------------------------------------------------------------
  
  (if (and (setq pre (getstring T "\nPrefix: "))
           (setq i (1- (cond ((getint "\nEnter starting number <0>: "))
                             (0))))
           (setq j i)
           (setq layouts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))))
           )
    (progn
      (foreach x (layorder) 
        (vla-put-name
          (vla-item  layouts x)
          (strcat "tmp" (if (< i 9) "0" "") (itoa (setq i (1+ i)))))) ; dummy name to avoiding using name already in use.
      (foreach x (layorder)
        (vla-put-name
          (vla-item  layouts x)
          (strcat pre (if (< j 9) "0" "") (itoa (setq j (1+ j))))))))
  (princ)
  )

Edit: use this one. Previous code failed when some possibly new name was already in use. (try to run twice with same prefix)

Message 3 of 5

Anonymous
Not applicable

Oh my god it works perfect!!

Thank you very much!!!

Regards!!

0 Likes
Message 4 of 5

Anonymous
Not applicable

Just a little correction,

(strcat pre (if (< i 9) "0" "") (itoa (setq i (1+ i)))))))

If not, it will rename as "blabla010" instead of "blabla10".

But the rest is just perfect.

Thank you again!!

 

EDIT: Ok, you already solved it!! Thanks man!!

 

Message 5 of 5

Sea-Haven
Mentor
Mentor

Re less than 10 layouts, but be aware "Model" is included so with 1 layout will get count of 2.

 

(setq lays (vla-get-layouts (vla-get-ActiveDocument (vlax-get-acad-object))))
(vla-get-count lays)
0 Likes