Jump to content
ILoveMadoka

Layout Rename - Fail

Recommended Posts

ILoveMadoka

I tried to modify this routine (to rename all Layout Tabs)

 

(vl-load-com)
(defun RenLay ( kword / n)
  (setq n 1)
  (vlax-for x (vla-get-Layouts (vla-get-ActiveDocument (vlax-get-acad-object)))
    (if (not (eq (strcase (vla-get-name x)) "MODEL"))
      (vla-put-Name x (strcat kword (itoa n)))
    )
    (setq n (1+ n))
  )
)

Which was found here:

http://forums.augi.com/showthread.php?77060-Rename-Layout-tabs-on-multiple-dwg&highlight=rename layout

 

To allow me to enter the desired name as part of the routine but I obviously don't know jack..

 

(vl-load-com)
(defun C:RT5 (S1 / n)
(setq S1 (getstring T "\nEnter New Layout Prefix:"))
  (setq n 1)
  (vlax-for x (vla-get-Layouts (vla-get-ActiveDocument (vlax-get-acad-object)))
    (if (not (eq (strcase (vla-get-name x)) "MODEL"))
      (vla-put-Name x (strcat S1 (itoa n)))
    )
    (setq n (1+ n))
  )
)

When I run the routine I just get *Cancel*

 

I want to change the number(s) to start with a zero (01) but I'll "attempt" that next... 

 

If someone could explain "WHY" this doesn't work, I'd appreciate it..

 

 

Share this post


Link to post
Share on other sites
steven-g

Try

(defun C:RT5 ( / S1 n)

If the S1 is before the forward slash it is expected to be supplied as a value (argument) when you call the function, after the forward slash and it is localizing the variable.

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-AutoLISP/files/GUID-31B647C5-61F3-4B06-BC88-4CE83EB981C5-htm.html

Share this post


Link to post
Share on other sites
ILoveMadoka

Thank you.

 

First time thru works just fine.

If I run the command again <just testing> the number of the first tab is no longer 1.

The first tab number increments.

 

Next question. 

How do I ensure that the first tab is always numbered 1?

 

 

Share this post


Link to post
Share on other sites
Tharwat
(defun c:rt5 ( / y s n l)
;; Tharwat - 14.Dec.2018    ;;
  (setq n 0 l (layoutlist))
  (if (and (/= (setq s (getstring t "\nSpecify New Layout Prefix :")) "")
           (/= (setq s (vl-string-left-trim " " s)) "")
           )
    (vlax-for x (vla-get-Layouts (vla-get-ActiveDocument (vlax-get-acad-object)))
      (or (eq (strcase (vla-get-name x)) "MODEL")
          (member (setq y (strcat s (itoa (setq n (1+ n))))) l)
          (vla-put-Name x y)
          )
      )
    (princ "\nNil or invalid input.")
    )
  (princ)
  ) (vl-load-com)

 

  • Thanks 1

Share this post


Link to post
Share on other sites
ILoveMadoka

Tharwat...

 

That works perfectly.

Thank you so very much!!

 

 

Share this post


Link to post
Share on other sites
Tharwat
12 minutes ago, ILoveMadoka said:

Tharwat...

 

That works perfectly.

Thank you so very much!!

 

You are welcome.

Share this post


Link to post
Share on other sites
Lee Mac

If you want to reliably rename layouts (or indeed any collection in which the item name constitutes a primary key and therefore must be unique), you will need to first rename the items to a temporary placeholder which is guaranteed to be unique within the current set and also unique within the target set.

 

For example, with the code posted so far in this thread, there is no test as to whether the target name is already used by another item in the collection. Hence, if you were to run the program, reorder the layouts or delete & add a new layout with the same name, then the next run of the program would fail.

 

This is because each layout is individually renamed as the code iterates over a set, and therefore every renaming operation must ensure that the target name does not conflict with another item in the set.

 

Furthermore, you cannot guarantee that the order in which the layouts appear in the layout collection will match the order of layouts in a drawing (hence the purpose of the taborder property).

 

One way to achieve a reliable rename is as follows:

;; Renumber Layouts  -  Lee Mac
;; Sequentially numbers all Paperspace layouts, with an optional prefix & suffix.

(defun c:rl ( / int lst lyn ord pre sed suf )

    ;; Obtain a valid (optional) prefix & suffix
    (setq pre (validstring "\nSpecify prefix <none>: ")
          suf (validstring "\nSpecify suffix <none>: ")
          lyn (list (strcase pre))
    )

    ;; Obtain list of layout objects, current names, and sort index
    (vlax-for lyt (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
        (if (= :vlax-false (vla-get-modeltype lyt))
            (setq lst (cons lyt lst)
                  lyn (cons (strcase (vla-get-name lyt)) lyn)
                  ord (cons (vla-get-taborder lyt) ord)
            )
        )
    )

    ;; Construct a unique seed for temporary renaming
    (setq sed "%")
    (while (vl-some '(lambda ( x ) (wcmatch x (strcat "*" sed "*"))) lyn)
        (setq sed (strcat sed "%"))
    )

    ;; Temporarily rename layouts to ensure no duplicate keys when renumbering
    (setq int 0)
    (foreach lyt lst
        (vla-put-name lyt (strcat sed (itoa (setq int (1+ int)))))
    )

    ;; Rename layouts in tab order, with prefix & suffix
    (setq int 0)
    (foreach idx (vl-sort-i ord '<)
        (vla-put-name (nth idx lst) (strcat pre (padzeros (itoa (setq int (1+ int))) 2) suf))
    )
    (princ)
)

(defun padzeros ( str len )
    (if (< (strlen str) len) (padzeros (strcat "0" str) len) str)
)

(defun validstring ( msg / rtn )
    (while
        (not
            (or
                (= "" (setq rtn (getstring t msg)))
                (snvalid rtn)
            )
        )
        (princ (strcat "\nThe name cannot contain the characters \\<>/?\":;*|,=`"))
    )
    rtn
)

(vl-load-com) (princ)

 

Edited by Lee Mac
  • Thanks 1

Share this post


Link to post
Share on other sites
ronjonp
2 hours ago, Lee Mac said:

;; Construct a unique seed for temporary renaming (setq sed "%") (while (vl-some '(lambda ( x ) (wcmatch x (strcat "*" sed "*"))) lyn) (setq sed (strcat sed "%")) )

@Lee Mac

Just curious, why are you using this naming rather than using the layout handle like we talked about in this thread?

Edited by ronjonp

Share this post


Link to post
Share on other sites
Lee Mac
48 minutes ago, ronjonp said:

@Lee Mac

Just curious, why are you using this naming rather than using the layout handle like we talked about in this thread?

 

Because the layout handle is not absolutely bulletproof given that there is the possibility that an existing layout may share the same name; whereas, constructing a unique seed for the temporary layout name will, by definition, never coincide with an existing layout name.

 

The choice of the symbol for the temporary name is essentially arbitrary, but I used the percent symbol since it is less likely to already exist as part of a layout name (whilst still being a valid layout name character).

Edited by Lee Mac

Share this post


Link to post
Share on other sites
ronjonp

Cool. I know you have your reasons ... but aren't handles considered to be unique? That was my duh moment when you mentioned it before. 🙂

Share this post


Link to post
Share on other sites
Lee Mac
1 hour ago, ronjonp said:

... but aren't handles considered to be unique?

 

Indeed, but that only guarantees uniqueness within the set of temporary names, not uniqueness within the set of current names & target names - hence, if one of the layouts has handle "A1" and another layout is already named "A1" then the temporary renaming step will fail.

Edited by Lee Mac

Share this post


Link to post
Share on other sites
ronjonp

Gotcha 👍

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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