Jump to content

Rename Layout Lisp


Shablab

Recommended Posts

I have the following Lisp routine below. My problem is that for sheets after 10 the layout that would be named 'SH 02' gets put behind 'SH 10' instead of the initial "SH 01". I'm wondering what I can change to stop this from happening.  I know it's the way autocad/ most programs reads numbers.

 

(defun c:renamelayouts (/ laylist c)
(setq laylist (vl-sort (layoutlist) '<) c 1)
(foreach x laylist
(command "layout" "R" x (strcat "SH " (if (< (strlen (itoa c)) 2) (strcat "0" (itoa c)) (itoa c)) ""));pad layout names
(setq c (1+ c))
)
(princ)
)

 

Link to comment
Share on other sites

The issue is caused by your initial sort:

(vl-sort (layoutlist) '<)

Since your layout names are not prefixed with leading zeroes at this stage, the layouts are sorted alphabetically, causing the issue you have described.

 

Link to comment
Share on other sites

A hint to add 0 at front


 (if ( > num 9)  
      (setq newnum (strcat "D" (rtos num 2 0)))
      (setq newnum (strcat "D0" (rtos num 2 0)))
      )
      (vla-put-name lay newnum)
 

Link to comment
Share on other sites

11 hours ago, BIGAL said:

A hint to add 0 at front

 


 (if ( > num 9)  
      (setq newnum (strcat "D" (rtos num 2 0)))
      (setq newnum (strcat "D0" (rtos num 2 0)))
      )
      (vla-put-name lay newnum)
 

 

 

The code is already doing this as part of the rename operation - the issue occurs due to the sort performed before renaming.

Edited by Lee Mac
Link to comment
Share on other sites

  • 2 weeks later...

After all these observations and comments could someone please post what the final code should look like?

 

I thought that I could cob the code pieces together but I was wrong...

 

Please?

Link to comment
Share on other sites

1 hour ago, ILoveMadoka said:

After all these observations and comments could someone please post what the final code should look like?

 

I thought that I could cob the code pieces together but I was wrong...

 

Please?

Here is a quick example with some comments. Perhaps you can learn from it :)

(defun c:foo (/ a i l p x)
  ;; Get layouts
  (vlax-for x (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) (setq l (cons x l)))
  ;; Sort by taborder and remove model tab
  (setq l (cdr (vl-sort l '(lambda (a b) (< (vla-get-taborder a) (vla-get-taborder b))))))
  ;; Set counter
  (setq i 0)
  ;; Temporarily number them so we don't get duplicates
  (foreach x l (setq i (1+ i)) (vla-put-name x (strcat "FooTempBazoom_" (itoa i))))
  ;; Set counter
  (setq i 0)
  ;; Get the layout count string length so we can add 0's if needed
  (setq a (strlen (itoa (length l))))
  (foreach x l
    ;; Increment counter
    (setq i (1+ i))
    ;; Reset prefix
    (setq p "")
    ;; Add zeroes to prefix if any
    (repeat (- a (strlen (itoa i))) (setq p (strcat p "0")))
    ;; Change the tab name
    (vla-put-name x (strcat p (itoa i)))
  )
  (princ)
)
(vl-load-com)

 

Link to comment
Share on other sites

  • 2 weeks later...
On 3/26/2019 at 9:04 AM, ronjonp said:

Here is a quick example with some comments. Perhaps you can learn from it

 

Just seeing this...

 

Thank you Sir..

 

and to you Lee as well..

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...