Jump to content

Layout Rename - Fail


ILoveMadoka

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

(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
Link to comment
Share on other sites

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
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 2 years later...

Is there a way to reverse this process? I have added a prefix which is incorrect and I'd like to remove. I have done too many things since to use undo.

Link to comment
Share on other sites

4 minutes ago, gregmorris234 said:

Is there a way to reverse this process? I have added a prefix which is incorrect and I'd like to remove. I have done too many things since to use undo.

Cancel that request! Lee Mac's Tabsort does the job exactly with Find and Replace. 

Link to comment
Share on other sites

  • 1 month later...
On 12/14/2018 at 4:39 PM, Lee Mac said:

y


;; 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)

 

 

 

 

 

 

Hi Lee, using your code , as quoted, I made some changes to rename from  old to new names from a CSV holding the new names , as following code 

 

 

;; Design by Gabo CALOS DE VIT from CORDOBA ARGENTINA
;;;    Copyleft 1995-2021 by Gabriel Calos De Vit ; DEVITG@GMAIL.COM    
;;

 ; Hecho por  Gabo CALOS DE VIT de CORDOBA ARGENTINA
;;;    Copyleft 1995-2021 por Gabriel Calos De Vit 
;; DEVITG@GMAIL.COM
;; with LEE MAC help
;;; con ayuda de 
;; Renumber Layouts  -  Lee Mac
;; Sequentially numbers all Paperspace layouts, de viejos a nuevos 


;;-------------------------------------------------------------------------------------------------
(DEFUN LEE-CSV-UNA-SOLA-COLUMNA  (/
                                  AR
                                  DATOS
                                  DIR
                                  LINEA
                                  LINEA-LIST
                                  NOMBRE-ARCHIVO
                                  )
  (SETQ LINEA-LIST NIL)
  (SETQ DIR
         (STRCAT (GETVAR "dwgprefix")
                 (VL-FILENAME-BASE (GETVAR "dwgname"))))
  (SETQ NOMBRE-ARCHIVO (GETFILED "Archivo donde estan  los datos " DIR "csv" 0))
  (SETQ AR (OPEN NOMBRE-ARCHIVO "r"))
  (WHILE
    (SETQ LINEA (READ-LINE AR))
     (SETQ LINEA-LIST (CONS linea LINEA-LIST))
     )
  (CLOSE AR)
  (REVERSE LINEA-LIST)
  
  )

;;************************************************************************************************


(DEFUN CAMBIA-NOMBRE  (/

                       IGUALES
                       INT
                       LST
                       LYN
                       NOMBRES-NO-VALIDOS-LIST
                       NOMBRES-NVOS-LIST
                       NOMBRES-NVOS-QTY
                       NOMBRES-VIEJOS-LIST
                       NOMBRES-VIEJOS-QTY
                       ORD
                       PRE
                       SED
                       SUF)



  (SETQ NOMBRES-VIEJOS-LIST (LAYOUTLIST))
  (SETQ NOMBRES-VIEJOS-QTY (LENGTH NOMBRES-VIEJOS-LIST))


  (SETQ NOMBRES-NVOS-LIST (LEE-CSV-UNA-SOLA-COLUMNA))
  (SETQ NOMBRES-NVOS-QTY (LENGTH NOMBRES-NVOS-LIST))

  (IF (= NOMBRES-NVOS-QTY NOMBRES-VIEJOS-QTY)
    (SETQ IGUALES T)

    (ALERT (STRCAT "las cantidades de layout , originales  "
                   (ITOA NOMBRES-VIEJOS-QTY)
                   "   "
                   "\n   son distintos que los nuevos "
                   (ITOA NOMBRES-NVOS-QTY)))

    )

  
  (SETQ NOMBRES-NO-VALIDOS-LIST ())
  (FOREACH NOMBRES-NVOS  NOMBRES-NVOS-LIST

    (IF (NOT (SNVALID NOMBRES-NVOS))
      (PROGN
        (ALERT (STRCAT "el nombre "
                       NOMBRES-NVOS
                       " NO ES VÁLIDO  \n porque tiene alguno de estos \n caracteres  \n \\<>/?\":;*|,=`"))
        (SETQ NOMBRES-NO-VALIDOS-LIST (CONS NOMBRES-NVOS NOMBRES-NO-VALIDOS-LIST)))
      ); end if 
    ) ; FOREACH NOMBRES-NVOS 


  ;; 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 NOMBRES-NVOS
  (IF IGUALES 
    (PROGN
      (FOREACH IDX  (VL-SORT-I ORD '<)
        (VLA-PUT-NAME (NTH IDX LST) (NTH IDX NOMBRES-NVOS-LIST))
        ) ;ewnd foreach
      ) ;end progn for true


    ) ; end if de qty


  (VL-LOAD-COM)
  (PRINC)

  ) ; end CAMBIA-NOMBRE



(DEFUN C:CBIA-LAYOUT  ()


  (CAMBIA-NOMBRE)
  (alert "comand es \n CBIA-LAYOUT")
  )


















;|«Visual LISP© Format Options»
(200 2 1 0 nil "end of " 100 20 2 2 nil nil T nil T)
;*** DO NOT add text below the comment! ***|;

 

It work ok , but it show the layout tabs in reverse order from the original , the new correspon to the old. 

 

My need is to reorder the layout tabs , 

 

 thanks in advance.

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Looking at the VLIDE help  I solve it 

 

;; Design by Gabo CALOS DE VIT from CORDOBA ARGENTINA
;;;    Copyleft 1995-2021 by Gabriel Calos De Vit ; DEVITG@GMAIL.COM    
;;

 ; Hecho por  Gabo CALOS DE VIT de CORDOBA ARGENTINA
;;;    Copyleft 1995-2021 por Gabriel Calos De Vit 
;; DEVITG@GMAIL.COM
;; with LEE MAC help
;;; con ayuda de 
;; Renumber Layouts  -  Lee Mac
;; Sequentially numbers all Paperspace layouts, de viejos a nuevos 


;;-------------------------------------------------------------------------------------------------
(DEFUN LEE-CSV-UNA-SOLA-COLUMNA  (/
                                  AR
                                  DATOS
                                  DIR
                                  LINEA
                                  LINEA-LIST
                                  NOMBRE-ARCHIVO
                                  )
  (SETQ LINEA-LIST NIL)
  (SETQ DIR
         (STRCAT (GETVAR "dwgprefix")
                 (VL-FILENAME-BASE (GETVAR "dwgname"))))
  (SETQ NOMBRE-ARCHIVO (GETFILED "Archivo donde estan  los datos " DIR "csv" 0))
  (SETQ AR (OPEN NOMBRE-ARCHIVO "r"))
  (WHILE
    (SETQ LINEA (READ-LINE AR))
     (SETQ LINEA-LIST (CONS linea LINEA-LIST))
     )
  (CLOSE AR)
  (REVERSE LINEA-LIST)
  
  )

;;************************************************************************************************


(DEFUN CAMBIA-NOMBRE  (/

                       IGUALES
                       INT
                       LST
                       LYN
                       NOMBRES-NO-VALIDOS-LIST
                       NOMBRES-NVOS-LIST
                       NOMBRES-NVOS-QTY
                       NOMBRES-VIEJOS-LIST
                       NOMBRES-VIEJOS-QTY
                       ORD
                       PRE
                       SED
                       SUF)



  (SETQ NOMBRES-VIEJOS-LIST (LAYOUTLIST))
  (SETQ NOMBRES-VIEJOS-QTY (LENGTH NOMBRES-VIEJOS-LIST))


  (SETQ NOMBRES-NVOS-LIST (LEE-CSV-UNA-SOLA-COLUMNA))
  (SETQ NOMBRES-NVOS-QTY (LENGTH NOMBRES-NVOS-LIST))

  (IF (= NOMBRES-NVOS-QTY NOMBRES-VIEJOS-QTY)
    (SETQ IGUALES T)

    (ALERT (STRCAT "las cantidades de layout , originales  "
                   (ITOA NOMBRES-VIEJOS-QTY)
                   "   "
                   "\n   son distintos que los nuevos "
                   (ITOA NOMBRES-NVOS-QTY)))

    )

  
  (SETQ NOMBRES-NO-VALIDOS-LIST ())
  (FOREACH NOMBRES-NVOS  NOMBRES-NVOS-LIST

    (IF (NOT (SNVALID NOMBRES-NVOS))
      (PROGN
        (ALERT (STRCAT "el nombre "
                       NOMBRES-NVOS
                       " NO ES VÁLIDO  \n porque tiene alguno de estos \n caracteres  \n \\<>/?\":;*|,=`"))
        (SETQ NOMBRES-NO-VALIDOS-LIST (CONS NOMBRES-NVOS NOMBRES-NO-VALIDOS-LIST)))
      ); end if 
    ) ; FOREACH NOMBRES-NVOS 


  ;; 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 NOMBRES-NVOS
  (IF IGUALES 
    (PROGN
      (FOREACH IDX  (VL-SORT-I ORD '<)
        (VLA-PUT-NAME (NTH IDX LST) (NTH IDX NOMBRES-NVOS-LIST))
        ) ;ewnd foreach
      ) ;end progn for true


    ) ; end if de qty


  (VL-LOAD-COM)
  (PRINC)

  ) ; end CAMBIA-NOMBRE



(DEFUN C:CBIA-LAYOUT  ()


  (CAMBIA-NOMBRE)
  (TABORDER)
  (alert "comand es \n CBIA-LAYOUT")
  )


(vl-load-com)
(DEFUN TABORDER  ()
  ;; sets the TabOrder of the Layouts to be
  ;; in alphabetic order, and displays a list of Layouts in the order they appear
  ;; in the tabs.
  (SETQ ACADOBJ (VLAX-GET-ACAD-OBJECT))
  (SETQ DOC (VLA-GET-ACTIVEDOCUMENT ACADOBJ))

  (SETQ SORTIT (LIST)
        TABCOUNT 0)

  ;; Alphabetize internally
  (WHILE (>= (1- (VLA-GET-COUNT (VLA-GET-LAYOUTS DOC))) TABCOUNT)
    (SETQ ADDEDTAB :VLAX-FALSE)

    (SETQ TABNAME (VLA-GET-NAME (VLA-ITEM (VLA-GET-LAYOUTS DOC) TABCOUNT)))

    (IF (/= (STRCASE TABNAME) "MODEL")
      ;; Skip modelspace
      (PROGN
        (SETQ SORTIT (APPEND SORTIT (LIST TABNAME)))
        ;; Add to beginning of list
        )
      )

    (SETQ TABCOUNT (1+ TABCOUNT))
    )

  ;; Sort layout names
  (SETQ SORTIT    (ACAD_STRLSORT SORTIT)
        SORTCOUNT 1)

  ;; Update tab order
  (FOREACH NAME  SORTIT
    (PROGN
      (SETQ TEMPLAYOUT (VLA-ITEM (VLA-GET-LAYOUTS DOC) NAME))
      (VLA-PUT-TABORDER TEMPLAYOUT SORTCOUNT)
      (SETQ SORTCOUNT (1+ SORTCOUNT))
      )
    )

  ;;-------------------------------
  ;; Read and display New Tab Order
  ;;-------------------------------
  (SETQ MSG      "The tab order is now set to: \n"
        TABCOUNT 0)

  (WHILE (>= (1- (VLA-GET-COUNT (VLA-GET-LAYOUTS DOC))) TABCOUNT)
    (SETQ TABNAME (VLA-GET-NAME (VLA-ITEM (VLA-GET-LAYOUTS DOC) TABCOUNT)))

    (IF (/= (STRCASE TABNAME) "MODEL")
      ;; Skip modelspace
      (PROGN
        (SETQ TABORDER (VLA-GET-TABORDER (VLA-ITEM (VLA-GET-LAYOUTS DOC) TABCOUNT)))
        (SETQ MSG (STRCAT MSG "(" (ITOA TABORDER) ")    " TABNAME "\n"))
        )
      )
    (SETQ TABCOUNT (1+ TABCOUNT))
    )

  (ALERT MSG)
  )






























;|«Visual LISP© Format Options»
(200 2 1 0 nil "end of " 100 20 2 2 nil nil T nil T)
;*** DO NOT add text below the comment! ***|;

 

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