Need Help using Lisp to Rename Files using their Drawing Numbers

Need Help using Lisp to Rename Files using their Drawing Numbers

dhunt3QYF5
Observer Observer
269 Views
5 Replies
Message 1 of 6

Need Help using Lisp to Rename Files using their Drawing Numbers

dhunt3QYF5
Observer
Observer

Hi I'm new to Lisp programming but I need to tidy up 1,000s of CAD files in my new job.

 

I'm trying to go into each drawing and copy the drawing number then Save the filename as the drawing number

Theres a number of obstacles to this.

First theres no fields or attributes in the title block. Everything is MText.


Second the drawings were done in China so although the Drawing number is in English characters, when you click into the text properties its formatted in Fang Song script/style.

 

I did a simple SSget command, it cannot read the text due to the Fang Song style written in the contents box before the drawing number. If I explode the text to remove the formatting, hidden text behind the drawing number becomes visible and things become a mess.


Finally if I get that to work I still have the issue of saving the file then opening the next file and running the script again. I want to automate that to run for lots of files.


I've attached a drawing I'm working from and a screenshot of the contents box for clarity.
Thanks in advance for any help!

0 Likes
Accepted solutions (1)
270 Views
5 Replies
Replies (5)
Message 2 of 6

paullimapa
Mentor
Mentor
Accepted solution

Let me start you off...assuming the location of the MText object containing the drawing number is always placed in the same window coordinates: 120,0 and 80,10, then you can create a Script file SaveAsDwgNo.scr containing the following:

; Script file name: SaveAsDwgNo.scr
; select the MText object
(setq ss (ssget "_W" '(120 0) '(80 10) '((0 . "MTEXT"))))
; get MText string value
(if ss (setq dwgno (getpropertyvalue (ssname ss 0) "Text")))
; saveas dwgno assumes current folder does NOT contain matching dwg name
(if dwgno (command"_.Saveas" "" (strcat (getvar "dwgprefix") dwgno ".dwg")))
;
_.CLOSE
; script completed

Or you can create a lisp command SaveAsDwgNo.lsp with the following:

; saveasdwgno lisp gets from MText object string value to saveas dwg name
; OP:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/need-help-using-lisp-to-rename-files-using-their-drawing-numbers/td-p/13763209/jump-to/first-unread-message
(defun c:saveasdwgno ; define function
; localize variables
 (/ dwgno ss)
; select the MText object assuming window corrdinates of 120,0 & 80,10
 (setq ss (ssget "_W" '(120 0) '(80 10) '((0 . "MTEXT"))))
; get MText string value
 (if ss (setq dwgno (getpropertyvalue (ssname ss 0) "Text")))
; saveas dwgno assumes current folder does NOT contain matching dwg name
 (if dwgno (command"_.Saveas" "" (strcat (getvar "dwgprefix") dwgno ".dwg")))
 (princ) ; clean exit
) ; defun

You should save lisp file SaveAsDwgNo.lsp in one of the folders specified under Options>Files>Support File Search Path & Trusted Locations. Then run the following command to load & run it:

(load "SaveAsDwgNo")
(c:SaveAsDwgNo)

Then to apply this onto multiple dwgs in the same folder you can use Lee Mac's Script Writer: https://lee-mac.com/scriptwriter.html

 

Note: Make sure all the fonts are found so the following alert window does NOT appear interrupting the opening of each dwg

paullimapa_0-1754956327896.png

paullimapa_1-1754956342376.png

 

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 3 of 6

Sea-Haven
Mentor
Mentor

Something else that may help, if the dwg objects in Model is moved for any reason then the XY of the text will be off. So this was your sample.

 

Block name: PC_TITLE_BLOCK
Insertion point: X= 120 Y= 0 Z= 0

 

I would move the title block and all the dwg objects in model inside or outside of the title block, to reference point 0,0.. The mtext can then be found. If other title blocks are used again move and find the mtext at the correct X&Y.

 

(setq ss (ssget "X" (list (cons 0 "INSERT")(cons 2 "PC_TITLE_BLOCK")(cons 410 "Model")))) ; add more title block names if required to cons 2
(setq obj (vlax-ename->vla-object (ssname ss 0)))
; if need name of titel add here
(setq inspt (vlax-get obj 'Insertionpoint))
(command "move" "All" "" inspt "0,0")
0 Likes
Message 4 of 6

dhunt3QYF5
Observer
Observer

Thats great, thank you!

0 Likes
Message 5 of 6

dhunt3QYF5
Observer
Observer

Thank you! This will be useful too as the drawings really are a mishmash of different types.

0 Likes
Message 6 of 6

paullimapa
Mentor
Mentor

you are welcome...cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes