Get and use name of selected .dwg to rename blocks and layers

Get and use name of selected .dwg to rename blocks and layers

Anonymous
Not applicable
2,057 Views
16 Replies
Message 1 of 17

Get and use name of selected .dwg to rename blocks and layers

Anonymous
Not applicable

Hello folks,

 

Is it possible to get and use the name of selected external .dwg (not the current opened .dwg) to rename blocks and layers inside the current .dwg? For example, I would like to implement my current lisp for renaming 1 block and 2 layers:

 

(if (tblsearch "block" "B-IMP-")
(command ".-rename" "_b" "B-IMP-" "B-IMP-ACE-"
)
)
(if (tblsearch "layer" "0-IMP-")
(command ".-rename" "_la" "0-IMP-" "0-IMP-ACE-"
)
)
(if (tblsearch "layer" "0-IMP-HACHURAS")
(command ".-rename" "_la" "0-IMP-HATCHES" "0-IMP-ACE-HACHURAS"
)
)

 

This lisp just search for the block named "B-IMP" and for the layers named "0-IMP-" (wich constains all drawed geometry, texts, etc.) "0-IMP-HATCHES" (wich contains all drawed hatches) and replaces with "B-IMP-ACE-", "0-IMP-ACE" and "0-IMP-ACE-HATCHES" (where -ACE- is the discipline related to the project like -ARQ-, -TOPO- and so on), so the result of the renaming will be:

 

Block: "B-IMP-ACE-**"

Layer 1 (geometry): "0-IMP-ACE-**"

Layer 2 (hatches): "0-IMP-ACE-**-HATCHES"

 

Afterwards, I need to rename manually again the block and layers, placing the name of the .dwg project file, where is located the "**" above.

 

I would like to use an external .dwg file (selected by the user) to insert its name (without the ".dwg") automatically, resulting:

 

Block: B-IMP-ACE-**NAME OF THE SELECTED FILE**

Layer 1: 0-IMP-ACE-**NAME OF THE SELECTED FILE**

Layer 2: 0-IMP-ACE-**NAME OF THE SELECTED FILE**-HATCHES

 

How complex is to implement a LISP like that? Can't find any related discussion here in forum. Everybody just wants to get and use the current name of the filem wich can be done using (strcat (getvar "dwgprefix")(getvar "dwgname")).

 

Thanks a lot!

Accepted solutions (2)
2,058 Views
16 Replies
Replies (16)
Message 2 of 17

Ranjit_Singh
Advisor
Advisor

Maybe like this?

(vl-filename-base (getfiled "" "" "dwg" 0))
Message 3 of 17

Anonymous
Not applicable

@Ranjit_Singh wrote:

Maybe like this?

(vl-filename-base (getfiled "" "" "dwg" 0))

Thanks for your help man. But my knowledge in programming is almost that "0" inside your code. I would need a help with the rest of the coding aswell to et all the tasks done... 

0 Likes
Message 4 of 17

Ranjit_Singh
Advisor
Advisor
Accepted solution

Start with something like this

(defun c:somefunc  (/ val)
 (if (tblsearch "block" "B-IMP-")
  (command ".-rename"
           "_b"
           "B-IMP-"
           (strcat "B-IMP-ACE-" (setq val (vl-filename-base (getfiled "" "" "dwg" 0))))))
 (if (tblsearch "layer" "0-IMP-")
  (command ".-rename" "_la" "0-IMP-" (strcat "0-IMP-ACE-" val)))
 (if (tblsearch "layer" "0-IMP-HACHURAS")
  (command ".-rename" "_la" "0-IMP-HACHURAS" (strcat "0-IMP-ACE-" val "-HACHURAS"))))

Multi_rename.gif

 

Message 5 of 17

Anonymous
Not applicable

@Ranjit_Singh wrote:

Start with something like this

(defun c:somefunc  (/ val)
 (if (tblsearch "block" "B-IMP-")
  (command ".-rename"
           "_b"
           "B-IMP-"
           (strcat "B-IMP-ACE-" (setq val (vl-filename-base (getfiled "" "" "dwg" 0))))))
 (if (tblsearch "layer" "0-IMP-")
  (command ".-rename" "_la" "0-IMP-" (strcat "0-IMP-ACE-" val)))
 (if (tblsearch "layer" "0-IMP-HACHURAS")
  (command ".-rename" "_la" "0-IMP-HACHURAS" (strcat "0-IMP-ACE-" val "-HACHURAS"))))

 

OUW, perfect! Just like like dream and you just nailed it! Now I can create variations for all other disciplines. Dam, I wish I had your brains! hehehe

I started studying LISP but I'm far away from knowing even the basics. Where did you learn so much? Any specific book? I've already got some websites suggestions like AFRALISP, Autodesk Website and others. When you know how to program, the sky has no limit.

I've accepted your awesome solution for this. MANY thanks great man!

 

0 Likes
Message 6 of 17

Ranjit_Singh
Advisor
Advisor

You did have all the code. So kudos to you. I just added the file selection part (which was suggested in Post #2). I have learnt most of the LISP from AutoCAD help and this forum. The authors on these forums, @ВeekeeCZ, @john.uhden, @Kent1Cooper @dbroad, to name a few, post really amazing solutions every single day. Look at their codes and study them. That's what I do. Good luck.

Message 7 of 17

Anonymous
Not applicable

Still about renaming, is it possible to apply the same block name, like for example "B-IMP-ACE-NAME OF FILE" to the .dwg itself via LISP with the file opened? Can you help me too with this very last task?

 

 

0 Likes
Message 8 of 17

Ranjit_Singh
Advisor
Advisor

The dwg file that was selected? (In my example video detail.dwg?) If so, try below

(defun c:somefunc  (/ val filep dwgname)
 (if (tblsearch "block" "B-IMP-")
  (command ".-rename"
           "_b"
           "B-IMP-"
           (setq dwgname (strcat "B-IMP-ACE-" (setq val (vl-filename-base (setq filep (getfiled "" "" "dwg" 0))))))))
 (if (tblsearch "layer" "0-IMP-")
  (command ".-rename" "_la" "0-IMP-" (strcat "0-IMP-ACE-" val)))
 (if (tblsearch "layer" "0-IMP-HACHURAS")
  (command ".-rename" "_la" "0-IMP-HACHURAS" (strcat "0-IMP-ACE-" val "-HACHURAS")))
 (vl-file-rename filep (strcat (vl-filename-directory filep) "\\" dwgname ".dwg")))

If you are talking about the file that is already open, then you cannot rename it. You have to do a saveas

 

(defun c:somefunc  (/ val dwgname)
 (if (tblsearch "block" "B-IMP-")
  (command ".-rename"
           "_b"
           "B-IMP-"
           (setq dwgname (strcat "B-IMP-ACE-" (setq val (vl-filename-base (getfiled "" "" "dwg" 0)))))))
 (if (tblsearch "layer" "0-IMP-")
  (command ".-rename" "_la" "0-IMP-" (strcat "0-IMP-ACE-" val)))
 (if (tblsearch "layer" "0-IMP-HACHURAS")
  (command ".-rename" "_la" "0-IMP-HACHURAS" (strcat "0-IMP-ACE-" val "-HACHURAS")))
 (command "._saveas" "" (strcat (getvar 'dwgprefix) dwgname ".dwg")))
Message 9 of 17

Anonymous
Not applicable

@Ranjit_Singh wrote:

The dwg file that was selected? (In my example video detail.dwg?) If so, try below

(defun c:somefunc  (/ val filep dwgname)
 (if (tblsearch "block" "B-IMP-")
  (command ".-rename"
           "_b"
           "B-IMP-"
           (setq dwgname (strcat "B-IMP-ACE-" (setq val (vl-filename-base (setq filep (getfiled "" "" "dwg" 0))))))))
 (if (tblsearch "layer" "0-IMP-")
  (command ".-rename" "_la" "0-IMP-" (strcat "0-IMP-ACE-" val)))
 (if (tblsearch "layer" "0-IMP-HACHURAS")
  (command ".-rename" "_la" "0-IMP-HACHURAS" (strcat "0-IMP-ACE-" val "-HACHURAS")))
 (vl-file-rename filep (strcat (vl-filename-directory filep) "\\" dwgname ".dwg")))

If you are talking about the file that is already open, then you cannot rename it. You have to do a saveas

 

(defun c:somefunc  (/ val dwgname)
 (if (tblsearch "block" "B-IMP-")
  (command ".-rename"
           "_b"
           "B-IMP-"
           (setq dwgname (strcat "B-IMP-ACE-" (setq val (vl-filename-base (getfiled "" "" "dwg" 0)))))))
 (if (tblsearch "layer" "0-IMP-")
  (command ".-rename" "_la" "0-IMP-" (strcat "0-IMP-ACE-" val)))
 (if (tblsearch "layer" "0-IMP-HACHURAS")
  (command ".-rename" "_la" "0-IMP-HACHURAS" (strcat "0-IMP-ACE-" val "-HACHURAS")))
 (command "._saveas" "" (strcat (getvar 'dwgprefix) dwgname ".dwg")))

No, I guess I didn't expressed myself really well! As I can't rename a file that is already open, after renaming the block to the selected file and your LISP, how to get the name of the block, like for example B-IMP-ACE-* or any variation like B-IMP-ARQ-*, B-IMP-FUN-* etc, whatever comes in front of B-IMP-* and do a "save as" toa new .dwg using the B-IMP-* block name? I guess it's better explaneid now.

0 Likes
Message 10 of 17

Ranjit_Singh
Advisor
Advisor

I will use my video in Post 4 as a reference. Your block was initially called "B-IMP-". Using the LISP it got renamed to "B-IMP-ACE-detail" Now you want to save the open dwg with a new name. What's the  name of this new drawing? "B-IMP-ACE"? or B-IMP-ACE-detail"?

Message 11 of 17

Anonymous
Not applicable

@Ranjit_Singh wrote:

I will use my video in Post 4 as a reference. Your block was initially called "B-IMP-". Using the LISP it got renamed to "B-IMP-ACE-detail" Now you want to save the open dwg with a new name. What's the  name of this new drawing? "B-IMP-ACE"? or B-IMP-ACE-detail"?


The name will be "B-IMP-ACE-detail", that is the result of renaming of the block "B-IMP-" using the LISP and a selected .dwg.. 🙂

But is there any function that gets whatever comes with "B-IMP-*", save this text and prints it to a new file using save as?

Because I will have like B-IMP-ACE-detail (Acessibility), B-IMP-ARQ-detail (Arquitetonic), B-IMP-FUN-detail (Fundation and Strucutres) and many Civil engineering disciplines variations. So the ideia is to print to a new file everything that recognizes and comes with B-IMP-* block.

So it's basic more like a generic "Save As" that prints out to a new file using save as the name of B-IMP-* blocks.

 

OBS.: In the attachments, you can see a .rar file wich contains all variations that I've created based on your LISP to rename all most common disciplines. Everything is being loaded from toolpalette and working great for renaming the B-IMP- blocks to B-IMP-ACE-detail, B-IMP-ARQ-detail, B-IMP-FUN-detail and so on... 🙂

 

Depending on the discipline, you just choose what discipline is and then select the file that you want to get the name and you are done now. Many thanks for that until now mate:

Capturar.PNG

 

 

0 Likes
Message 12 of 17

Ranjit_Singh
Advisor
Advisor

@Anonymous wrote:
...The name will be "B-IMP-ACE-detail", that is the result of renaming of the block "B-IMP-" using the LISP and a selected .dwg......

That is what the second code in Post 9 did. Did it not?

 

0 Likes
Message 13 of 17

Anonymous
Not applicable

No I tried the LISP from the Post 9 and this was the result:

Capturar.PNG

 

A new file name "REN-ACE" and saved in documents folder. Plus, just by adding that last save as command line, I loose the pop box to select the file to rename the block B-IMP and the layers... 

0 Likes
Message 14 of 17

Ranjit_Singh
Advisor
Advisor
Accepted solution

Try now.

(defun c:somefunc  (/ val filep dwgname)
 (if (tblsearch "block" "B-IMP-")
  (command ".-rename"
           "_b"
           "B-IMP-"
           (setq dwgname (strcat "B-IMP-ACE-" (setq val (vl-filename-base (setq filep (getfiled "" "" "dwg" 0))))))))
 (if (tblsearch "layer" "0-IMP-")
  (command ".-rename" "_la" "0-IMP-" (strcat "0-IMP-ACE-" val)))
 (if (tblsearch "layer" "0-IMP-HACHURAS")
  (command ".-rename" "_la" "0-IMP-HACHURAS" (strcat "0-IMP-ACE-" val "-HACHURAS")))
 (command "._saveas" "" (strcat (vl-filename-directory filep) "\\" dwgname ".dwg")))

This is an example for ACE.

 

Message 15 of 17

Anonymous
Not applicable
Now it worked perfectly mate. Thanks for ALL your help and time to help me with these tasks. They are going to save me a lot of time in daily jobs.
Have a great new year my friend.
0 Likes
Message 16 of 17

Anonymous
Not applicable

Everything is working great and smooth now, but as I used "Save as..." to save a new file with the name of the block (for exemple B-IMP-ACE-detail.dwg, B-IMP-ARQ-detail.dwg....) it's always remaining the old "B-IMP-.dwg" file in the folder. Is there any way to add one more line of code to delete this remaining file named "B-IMP-.dwg" that is always left behind after using the "Renaming" and "Save As" LISP you wrote? After that it will be even more perfect.

I promise this is the last thing I ask you to help me sir Smiley Frustrated

 

I searched and tried this function: (vl-file-delete "B-IMP-.dwg") but it didn't work at all, so I guess I don't know how to use properly...

0 Likes
Message 17 of 17

Anonymous
Not applicable

@Ranjit_Singh, can you help me with this very last task on how to implement a LISP to delete the remaining "B-IMP-.dwg" file, after renaming and saving as the previous file?

 

Thanks a zillion!

 

0 Likes