Jump to content

Rename layers to dwgname-1 etc.


CADkitt

Recommended Posts

I hacked some lisp code into a lisp that renames layouts and add a number to it. Since it gives a "name already exist" error when a name already existed, I made it so it first changed all layouts names to somerandomwords# and then to the correct name which is dwgname(without the .dwg)-1. (probably you can do this faster with some black lisp magic that I don't know about.) But it works good :shock: so no help needed suggestions on how to do it better are always welcome :)

 

I got most of the code from here:

http://forums.augi.com/showthread.php?t=77060&highlight=rename+layout

 


(vl-load-com)
(defun c:RenLay ( / 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 "somerandomwords" (itoa n)))
   )
   (setq n (1+ n))
 )
 (setq n 0)
 (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 (vl-filename-base (getvar 'dwgname)) "-" (itoa n)))
   )
   (setq n (1+ n))
 )
)

Link to comment
Share on other sites

  • 2 months later...

I'm in the need of some code like this and being new to lisp, I'm trying to understand it. I get most of it, but not all of it. Would anyone care to help explain the different sections as I have marked below? Thanks in advance.

 

(setq n 1)

This sets the variable 'n' to the value of 1, easy enough :whistle:

 

(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 "somerandomwords" (itoa n)))
  )
  (setq n (1 + n))
)

Ok, suddenly not so easy. I think this part gets the Layout attributes then uses the argument if the Layout tab name is not equal to "MODEL" it increments the number by 1 until there are no more Layouts to test. Am I close? And what does the "somerandomwords" and the (itoa n) mean in this line of code:

(vla-put-Name x (strcat "somerandomwords" (itoa n)))

:?

(setq n 0)
(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 (vl-filename-base (getvar 'dwgname)) "-" (itoa n)))
   )
   (set q n (1 + n))
 )
)

I think this section takes the incremented number from the first step then renames all the Layout tabs to the drawing name followed by the value of 'n' for each Layout. I still don't get what the (itoa n) means. I tried googling it but I can't really find a definition for it.

 

Any comments are welcomed. Thanks.

 

Lonnie

Link to comment
Share on other sites

LOL, I'm just glad my hair (most of it anyways) doesn't get pulled out every time a piece of my code doesn't work. Otherwise my avatar would be Captain Picard :lol:

Link to comment
Share on other sites

((lambda (name)
  (vlax-for x (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
    (or (eq (vla-get-name x) "Model")
        (vl-catch-all-apply
          (function vla-put-name)
          (list x (strcat name (itoa (vla-get-taborder x))))
        )
    )
  )
)
 (strcat (vl-filename-base (getvar 'dwgname)) "-")
)

Link to comment
Share on other sites

Alan,

 

That works great! One question though: How would I go about adding an IF/THEN relational statement to this code so that the first 3 layouts have the characters "BOM" added to the end of its new name? In our office the first 3 layouts are reserved for the drawings BOM and usually we just manually add "BOM" to the end of the 1st 3 layouts.

 

I know the basic setup of an IF/THEN statement is as such:

(if (condition)(then do this)(else do this))

 

I'm just not sure how to incorporate the statement into your code. Thanks.

 

Lonnie

Link to comment
Share on other sites

((lambda (name / tab)
  (vlax-for x (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
    (cond ((eq (vla-get-name x) "Model"))
          ((< 0 (setq tab (vla-get-taborder x)) 4)
           (vl-catch-all-apply
             (function vla-put-name)
             (list x (strcat name (itoa tab) " BOM"))
           )
          )
          ((vl-catch-all-apply
             (function vla-put-name)
             (list x (strcat name (itoa (vla-get-taborder x))))
           )
          )
    )
  )
)
 (strcat (vl-filename-base (getvar 'dwgname)) "-")
)

Link to comment
Share on other sites

I'm trying to decipher it right now. However since it's almost quitting time here, I'm sure I won't have a handle on it before I leave for the day :whistle:. I will check back in the morning to let you know whether I'm able to get my head around the code. Thanks again for your help.

 

Lonnie

Link to comment
Share on other sites

Alan, I played hooky from work today and took the kids to the movies and to Chuck E. Cheese thus I didn't get a chance to go over this code until just now. However, with the wife working tonight :cry: I have plenty of time to figure it out :).

 

I guess I will start with the first line. I have read a little about using (lambda) but I'm not sure what conditions exist to warrant using it over a regular (defun) statement. I have figured out that the variables listed in front of the backslash are global ones while those that follow the backslash are local only. At least that's the case with a (defun) statement. Does this apply to (lambda)?

((lambda (name / tab)

 

 

Based on the terminology in the 2nd line, I can figure out what the code is doing, I just don't know what terms I could use if I was trying to use this code on something else. Is the 'x' a variable and if so why is it not included in the parenthesis after (lambda)? Could I substitute 'line', 'circle, 'layer' or 'color' for 'layouts'? BTW, I'm assuming this line of code basically pulls the attributes of whatever term is listed in the 'vla-get-layouts'.

(vlax-for x (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))

 

 

In the 3rd line, you've set up a conditional evaluation. I thought I understood this type of evaluation but this example is a little complicated. The basic setup of a conditional evaluation as far as I understand it goes like this:

(cond
   (1st test)(action to take place if true)
   (2nd test)(action to take place if true)
   (else do this if none of the above is true))

 

 

So in the first condition, you've set it up so that the "MODEL" tab is not modified, correct? I say that because I don't see any action to take place if this condition is true so I'm assuming if the action is omitted then nothing happens.

((eq (vla-get-name x) "Model"))

 

 

The next condition returns true if the taborder value is less than 4 and the result is the tab being renamed to the value of the strings "name", "tab" and " BOM".

((< 0 (setq tab (vla-get-taborder x)) 4)
           (vl-catch-all-apply
             (function vla-put-name)
             (list x (strcat name (itoa tab) " BOM"))
           )
          )

 

 

The final condition is returned for all other conditions which is naming the tab the value of the strings "name" and "taborder". Why did you use "(itoa (vla-get-taborder x)) here and (itoa tab) for the previous evaluation? Just wondering...

((vl-catch-all-apply
             (function vla-put-name)
             (list x (strcat name (itoa (vla-get-taborder x))))
           )
          )

 

 

Now I'm guessing this last piece of code is the part that actually does the renaming of the tabs so that would mean the code above it just derives the name and stores it for use in this last piece. I don't know though, it is still a little confusing for me.

(strcat (vl-filename-base (getvar 'dwgname)) "-")

 

 

Now my head hurts :ouch:

 

Thanks again for your help with this code.

 

Lonnie

Link to comment
Share on other sites

you can btw modifie the dwg name in lenth by doing this:

       (setq str1 (strcat (vl-filename-base (getvar 'dwgname))))
	(setq str4(strcat "DRW-" (substr str1 5 ))

The example gets letters 5 to 8 of the dwg name and adds DRW- before it.

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