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

Autolisp Renaming Tabs

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
Anonymous
1499 Views, 5 Replies

Autolisp Renaming Tabs

Hey everyone,

 

I've searched and searched and can't find anything to really help me. Keep in mind I'm very new to AutoLISP. 

 

I've been using this code to attempt to renumber my layout tab names. Unfortunately everytime I specify a new starting number (say because we added a new tab) it stacks the new number with the complete old tab name. 

(defun c:NUMLAYOUT (/ *error*)
  (princ "\rNUMLAYLOUT ")

  (defun *error* (msg)
    (if acDoc
      (vla-endundomark acDoc)
    )
    (cond ((not msg))                                                   ; Normal exit
          ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
          ((princ (strcat "\n** Error: " msg " ** ")))
    )                                                                   ; Fatal error, display it
    (princ)
  )

  ((lambda (acDoc / i suffix delin)
     (vla-startundomark acDoc)
     (if
       (and         
         (setq i (1- (getint "\nEnter starting number: ")))
         (if
           (/= ""
               (setq suffix (getvar "ctab"))
           )
            (setq suffix (strcat "  " suffix))
            
         )
       )
        (progn
          (foreach x (layoutlist)
            (vla-put-name
              (vla-item (vla-get-layouts acDoc) x)
              (strcat (itoa (setq i (1+ i))) suffix)
            )
          )
          (*error* nil)
        )
        (*error* "Must enter start of sequence")
     )
   )
    (vla-get-activedocument (vlax-get-acad-object))
  )
)

I'm looking for help with having (getvar "ctab") to ignore the first 2 or 3 variables when it grabs the name. 

 

Thanks,

 

Jason (NEWBIE)

 

 

5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: Anonymous

That's a bummer, ran out of time to edit my post. 

 

This is the code I really wanted to add. I'd still like a way to replace the old 2-3 variables on the tab name with the newly imputed "page number".

 

(defun c:NumberingPages (/ i) (vl-load-com)
  (setq i (1- (getint "\nEnter starting number: ")))
  (foreach x (layoutlist)
    (vla-put-name
      (vla-item	(vla-get-layouts
		  (vla-get-activedocument (vlax-get-acad-object))
		)
		x
      )
      (strcat (itoa (setq i (1+ i))) "  " x)
    )
  )
  (princ)
)

Thanks and sorry about the confusion.

 

 

Message 3 of 6
ВeekeeCZ
in reply to: Anonymous

Try... (without seeing your last "edit")

 

Spoiler
(vl-load-com)

(defun c:NUMLAYOUT (/ *error* Layorder)
  (princ "\rNUMLAYLOUT ")

  (defun *error* (msg)
    (if acDoc
      (vla-endundomark acDoc)
    )
    (cond ((not msg))                                                   ; Normal exit
          ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
          ((princ (strcat "\n** Error: " msg " ** ")))
    )                                                                   ; Fatal error, display it
    (princ)
  )


  
  (defun Layorder (/ order)  ; pbejse https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/layout-order/td-p/3749964
  (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)))))))

  

  
  ((lambda (acDoc / i suffix delin)
     (vla-startundomark acDoc)
     (if
       (and
         (setq i (1- (getint "\nEnter starting number: ")))
         (setq suffix (getvar "ctab"))
	 )
       (progn
	 ;(if (wcmatch suffix "#* *") (setq suffix (strcat "  " (vl-string-left-trim " " (substr suffix (+ 2 (vl-string-position 32 suffix)))))))
	 (if (wcmatch suffix "#*  *") (setq suffix (substr suffix (+ 1 (vl-string-position 32 suffix)))))
          (foreach x (layorder)
            (vla-put-name
              (vla-item (vla-get-layouts acDoc) x)
              (strcat (itoa (setq i (1+ i))) suffix)
            )
          )
          (*error* nil)
        )
        (*error* "Must enter start of sequence")
     )
   )
    (vla-get-activedocument (vlax-get-acad-object))
  )
)

 

I used pbe's sub for sorting layouts... because (layoutlist) is in the order layouts were created. 

 

Edit: if you're sure that there are always two spaces behind a number.... (see modified version) 

Message 4 of 6
Anonymous
in reply to: ВeekeeCZ

pbe's is awesome! Thanks so much for that. 

 

I'm still having an issue with keeping the old tab name and just replacing the numbers. 

 

I've done this now

(defun c:NumberingPages (/ i) (vl-load-com)

  (defun Layorder (/ order)  ; pbejse https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/layout-order/td-p/3749964
  (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)))))))

  (setq i (1- (getint "\nEnter starting number: ")))
  (foreach x (layorder)
    (vla-put-name
      (vla-item	(vla-get-layouts
		  (vla-get-activedocument (vlax-get-acad-object))
		)
		x
      )
      (strcat (itoa (setq i (1+ i))) "  " x)
    )
  )
  (princ)
)

This pbejse worked amazingly on an old drawing I was having trouble with.

Message 5 of 6
ВeekeeCZ
in reply to: Anonymous

hmm... the algorithm is the same as I used in my first post........ marked in red. HTH

 

Spoiler
(vl-load-com)

(defun c:NumberingPages (/ i Layorder) 

  (defun Layorder (/ order)  ; pbejse https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/layout-order/td-p/3749964
  (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)))))))

  (setq i (1- (getint "\nEnter starting number: ")))
  (foreach x (layorder)
    (vla-put-name
      (vla-item	(vla-get-layouts
		  (vla-get-activedocument (vlax-get-acad-object))
		)
		x
      )
      (strcat (itoa (setq i (1+ i))) (if (wcmatch x "#*  *")
				       (substr x (+ 1 (vl-string-position 32 x)))
				       (strcat "  " x)))
    )
  )
  (princ)
)

Edit: Sorry, fixed. I bet you have done the same, right? 🙂

 

or 

(strcat (itoa (setq i (1+ i))) "  "
	      (if (wcmatch x "#*  *")
		(substr x (+ 3 (vl-string-position 32 x)))
		x))
Message 6 of 6
Anonymous
in reply to: ВeekeeCZ

You are a champion. Works like a charm! 

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

Post to forums  

Autodesk Design & Make Report

”Boost