Lisp For AutoCAD DWG filename?

Lisp For AutoCAD DWG filename?

leonardo.alvarez4SZJL
Advocate Advocate
4,176 Views
22 Replies
Message 1 of 23

Lisp For AutoCAD DWG filename?

leonardo.alvarez4SZJL
Advocate
Advocate

I am running AutoCAD Electrical 2022, Windows 10.

 

Hi Everyone, Is it possible for a LISP to grab attributes and combine them to rename the DWG name itself?. Here is an example.

I have a title block which contains these attributes:

 

DWG#: 5524D85

Rev: B

Dwg Desc: One Line Diagram

 

The Lisp Routine would grab these attributes and rename the DWG as follows: 5524D85 (B) One Line Diagram.

 

Best Regards,

Leo

 

0 Likes
Accepted solutions (2)
4,177 Views
22 Replies
Replies (22)
Message 2 of 23

pendean
Community Legend
Community Legend
You can't rename a file that you have open: an OS limitation.
How about a SAVEAS option?
0 Likes
Message 3 of 23

leonardo.alvarez4SZJL
Advocate
Advocate

@pendean 

 

That is what I was thinking. You can't rename a file that is open. My best option will be save as which I'm okay with. I would like a lisp routine that can "save as" a dwg file with the 3 attributes I have mentioned. I will be nice if it can do it project wide as well.

0 Likes
Message 4 of 23

3wood
Advisor
Advisor

I think it is possible. Can you upload an example drawing contains the title block?

Message 5 of 23

leonardo.alvarez4SZJL
Advocate
Advocate

@3wood 

Absolutely, Try this!

Try to save it as with the following Attributes:

DWG#-SHT (REAL_REV) DESC_2

 

This is how I usually save the drawing as: for Example

994D1111-01 (B) One Line Diagram.

 

Let me know if the DWG doesn't work and I'll upload a template.dwt file

0 Likes
Message 6 of 23

3wood
Advisor
Advisor
Accepted solution

Please try the codes below.

You need close the file to be renamed first.

Then use RENAMEFILE in a new drawing and select the file to be renamed.

It will search the title block in the Paperspace first. If not found, it will then search in the Modelspace.

If there are 2 title blocks in the Paperspace. it will use the latest one (I believe).

You can modify the code to suit your situation.

I guess it will be convenient to add "apply to all other drawings in the same folder" function or even "include subfolders", but that's another challenge. 

RENAMEFILE.gif

;;; RENAMEFILE.LSP by 3wood, 2022.7.19
;;; Rename a dwg file based on the title block attributes in a format of "DWG#-SHT (REAL_REV) DESC_2"
;;; You can change related attribute tag names to suit your preferences.
;;; It will prompt you to enter the title block name and the drawing to be renamed. Then, it will open the drawing temporarily.
;;; It will search the PaperSpace first, then the ModelSpace and then use the attribute information in the first-found title block reference.
;;; Please ensure the drawing that is to be renamed is closed in AutoCAD. Then use the routine in another drawing.

(defun C:RenameFile ( / f1 f2 Input NameData op1)
  
  (setq Input (getstring T (strcat "\nBlock name to search <" (if (not RC_BlockName) "" RC_BlockName) ">: " )))
  (if (and (not RC_BlockName) (equal Input ""))
    (princ "\nInvalid Block Name.")
    (if (not (equal Input ""))
      (setq RC_BlockName Input)
      )
    )
  (if RC_BlockName
    (progn
      (if (not RF_DefaultFile)
	(setq RF_DefaultFile (strcat "C:\\Users\\" (getvar "LOGINNAME") "\\Documents\\"))
	)
      (if
	(setq f1 (getfiled "Select a library drawing:" RF_DefaultFile "dwg" 16)
	      )
	(progn
	  (setq op1 (vla-open (setq dbx (vla-get-documents (vlax-get-acad-object))) f1))
	  (foreach x3 (list 'vla-get-paperspace 'vla-get-modelspace)
	    (if (not NameData)
	      (vlax-for x1 ((eval x3) op1)
		(if
		  (and (equal (vla-get-objectname x1) "AcDbBlockReference")
		       (equal (strcase (vla-get-name x1)) (strcase RC_BlockName))
		       )
		  
		  (setq NameData (mapcar '(lambda (x2) (if (member (vla-get-TagString x2) (list "DWG#" "SHT" "REAL_REV" "DESC_2"))
							 (cons (vla-get-TagString x2) (vla-get-TextString x2))
							 (cons "" "")
							 )
					    )
					 (vlax-safearray->list (vlax-variant-value (vla-GetAttributes x1)))
					 )
			)
		  )
		)
	      )
	    )
	  (vla-close op1 :vlax-false)
	  (vlax-release-object dbx)
	  (if NameData
	    (progn
	      (vl-file-rename f1
		(setq f2
		       (strcat (vl-filename-directory f1)
			       "\\"
			       (cdr (assoc "DWG#" NameData))
			       "-"
			       (cdr (assoc "SHT" NameData))
			       " ("
			       (cdr (assoc "REAL_REV" NameData))
			       ") "
			       (cdr (assoc "DESC_2" NameData))
			       ".dwg"
			       )
		      )
		)
	      (if (not (equal f1 f2))
		(princ (strcat "\nFile renamed to: " f2))
		(princ "\nFile name unchanged.")
		)
	      )
	    )
	  )
	)
      )
    )
  (princ)
  )

 

Message 7 of 23

leonardo.alvarez4SZJL
Advocate
Advocate

@3wood 

 

Thank you so much for your help!

The very next thing will be to add an option to change all the drawings within a folder and then choose where you want to save those files!

Message 8 of 23

leonardo.alvarez4SZJL
Advocate
Advocate

@3wood 

 

Thank you so much for your code! Is it possible it If you can help me modify this code!

Since it renames the dwg file to "f2" symbol. The project doesn't recognize it anymore so I have to manually add the drawing in the project manager tab. This usually happens because the project.wdp file holds the drawing name values.

wdp file.PNG

  • Can the code be modifed to access the project.wdp file and do a search and replace to say the new drawing? Then that way I don't have to manually add the drawing each time.
  • Second thing if you can make it to select a folder containing the project.

Best Regards,

Leo

 

0 Likes
Message 9 of 23

3wood
Advisor
Advisor

I think both tasks can be done.

I don't have AutoCAD Electrical installed. What is rule of the .wdp file? Where does it store? 

Message 10 of 23

leonardo.alvarez4SZJL
Advocate
Advocate

@3wood 

Copied from the AutoCAD Electrical Support learning.

 

A .wdp file is a text file that lists the drawing files that make up the set of interrelated drawings. Project Manager updates this file as you add drawings, remove drawings, change the drawing order, change the folder structure, update the project properties, and more.

 

Where does it store?

 

This file will be in the Project folder. Here is an example. My project is 9999 HECS Template. And the wdp is in it.

pic 1.PNG

 

Inside of .wdp file. You will see this.

wdp file.PNG

This files will shows you each drawing that the project have.

 

And this makes total sense because when you look at the project manager tab and I have 9999 project open I should see those dwg files in order.

 

pm.PNG

Now when I used the renamefile.lsp. The lisp routine made the adjustment to rename the file. After it renames the file. The project manager does not recognize the drawing anymore because the .wdp file is not updated. So to fix this you have to go inside the .wdp file and rename it yourself so that the project manager recognizes it. Here is how it should look.

 

it should look like this.PNG

I have attached the .wdp file so you can take a look at it. I had to upload it as a text file.

 

This routine is good But it needs one more step. 

 

  • It would be nice that it also access the .wdp file and also changes the name of its old drawing with the new drawings.

Thank you for your help!

Leo

0 Likes
Message 11 of 23

3wood
Advisor
Advisor

Can you try updated lisp file first? Now it can rename multiple files in a folder (with an option to include all subfolders).

https://drive.google.com/file/d/1yCumhnuVXeIZE41wDKPnnI7pdL9QQpbW/view?usp=sharing

 

Then we can work together to resolve the .wdp file issue.

Does .wdp file saved in the same folder as the dwg file to be renamed? 

0 Likes
Message 12 of 23

leonardo.alvarez4SZJL
Advocate
Advocate

@3wood 

Does .wdp file saved in the same folder as the dwg file to be renamed? 

 

Yes. This is true. It will always be where the dwg file is renamed which is also inside the Project. I just made an example project called file rename and this is how it look like!

 

pic 1.PNG

 The WDP file is in there. I also created a Drawing 1.dwg file which should be inside the .wdp file.

 

pic 2.PNG

Which it is. Now if I keep creating new drawings for the project. The .wdp file will automatically update adding those drawings. Let me show you. I have created 4 more drawings inside the project and this what the .wdp file looks like now. 

pic 3.PNG

What you see is.

===DESC1 attribute ----> Cabinet 1

===DESC2 attribute ----> AC/DC Circuit

===DESC3 attribute ----> 

and Drawing

 

 

Now when I use the renamefile.lsp routine this happens!

pic 5.PNG

The Drawings are renamed and but the .wdp file say different. Which now I wont be able to open them in AutoCAD Electrical.

 

I hope this was very clear to you on how the .wdp file works. And by the way Drawing 2 does not rename at all and I don't know why. When I run the routine for single drawing it stills does not rename it. I also deleted Drawing 2 and created it a new Drawing 2 and it still doesn't work. This is what it shows.

 

PIC 4.PNG

It prompts that it renamed it but it actually didn't.

 

Furthermore, thank you so much for your help!

Best Regards

0 Likes
Message 13 of 23

3wood
Advisor
Advisor

Please ensure closing the drawing to be renamed in AutoCAD before running the routine.

I'll help you on the .wdp file updating tomorrow.

Message 14 of 23

leonardo.alvarez4SZJL
Advocate
Advocate

Thank you very much @3wood 

0 Likes
Message 15 of 23

3wood
Advisor
Advisor
Message 16 of 23

leonardo.alvarez4SZJL
Advocate
Advocate

Hi @3wood 

 

It works but it gives some errors. Idk why but it never works with Drawing 2. I do not having drawing 2 opened when I run the lisp routine. And drawing 4 was renamed but not in the project manager. Here is a snip of how it looks.

 

snip.PNG

 

Thank you for your time and help.

0 Likes
Message 17 of 23

3wood
Advisor
Advisor

Can you upload Drawing 2.dwg, Drawing 4.dwg and the .wdp file for testing?

 

Message 18 of 23

leonardo.alvarez4SZJL
Advocate
Advocate

Here is the whole project. Just in case! This is right after I ran the lisp routine so I have not do anything to it after.

0 Likes
Message 19 of 23

3wood
Advisor
Advisor

The value of DESC_2 in Drawing 2.dwg is "AC/DC DISTRIBUTION".

But character "/" is not valid for a file name. That's why Drawing 2.dwg is not able to be renamed.

We can replace invalid characters in attribute with another character such as "_" in the new file name. What character do you prefer?

 

3wood_0-1658840794861.png

I'll fix the Drawing 4 issue for you tomorrow.

Message 20 of 23

leonardo.alvarez4SZJL
Advocate
Advocate

@3wood 

 

The "_" is completely fine. I would prefer to have it removed after AC DC Distribution. But I can change it in the Desc_2 Attribute to say AC & DC Distribution.

0 Likes