Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Insert text in DXF file at origin from its file name for engraving.

19 REPLIES 19
SOLVED
Reply
Message 1 of 20
tiwari1211
892 Views, 19 Replies

Insert text in DXF file at origin from its file name for engraving.

Hello Experts, I have pleanty of Dxf files in project folder for production. Every DXF file has file name as part name_MOC.  I want to insert "part name" from file name inside sketch at origin 0,0,0. So that i can engrave the part name on the part while laser cutting. Is there any way to program any Lisp/Excel Macro which after selecting folder, read all its DXF and update them with part name ?

 

Part name for Example is -  12345-001_1.4404/316L

from this i like to copy only upto 12345-001 and insert inside DXF for engraving. 

 

I appriciate any help in this regard. Thank you

19 REPLIES 19
Message 2 of 20
ec-cad
in reply to: tiwari1211

Are the 'part-names' always 9 characters long ? e.g. 12345-001 without exceptions ?

If so, you could dxfin each file, and since you will already (have) the filename.dxf string,

just do: (setq str (substr filename 1 9))

And (command "_Text" (list 0.0 0.0 0.0) "0" str)

Do you have a Lisp started ?

You could modify attached to do the job.

 

ECCAD

 

Message 3 of 20
tiwari1211
in reply to: ec-cad

Many thanks for your reply, The part name can be more than 9 characters but limited till underscore "_" 

 

unfortunately I have no experience in doing the programming. 

Can you please tell me in detail or help in modifying the Lisp file ?

Thaks for your support

Message 4 of 20
Sea-Haven
in reply to: tiwari1211

I can see one issue and I am not sure, the original request was to edit a dxf for engraving, so the question is that output need to be a dxf rather than a dwg ? (command "_saveas" "2013" dwg). If a dxf is required then should leave original as is and save a dxf copy to say another directory, no need for saving a dwg. Or even save both dxf & dwg.

 

@tiwari1211 can you clarify please.

 

 

 

Message 5 of 20
tiwari1211
in reply to: tiwari1211

Thanks for your reply,

Yes, my requirement is DXF. 

so, already i have several DXF files with file name for example 12345-015a_MOC. No i am looking to have part name "12345-015a" from file name & put it as Text inside DXF sketch for engraving. 

 

Is there any way to get all the DXF files in a folder updated for above changes.. like in batch mode ?

So that program ask for folder selection and consider all DXF files inside the folder and perfrom the changes ?

 

Thanks a lot .

Message 6 of 20
ec-cad
in reply to: tiwari1211

So, If I'm reading this right, you want to:

Foreach .dxf (in a selected Folder),

Dxfin filename

Make string for text, of characters of filename until you hit a "_" character,

Place the Text at 0,0,0  with 0 rotation, existing TextHeight - string above.

And, Dxfout (overwriting existing file in same folder)

Erase All (to prepare for next Dxfin),

Dxfin the 'next' filename.dxf ...

When 'done' echo DXF Updated in 'Foldername.

 

Is that correct ? OR do you want to leave the originals intact, and dxfout to a 'Finished' Folder, under the

Folder chozen ?

Either way can be done, I would prefer saving into a different folder, with 'same' original filename.dxf

 

Awaiting your reply.

ECCAD

 

Message 7 of 20
ec-cad
in reply to: ec-cad

Waited soooooooolong. 🙂

Here's what I came up with. You just load the program and it will run. No 'Function C:xx. calls.

Once loaded, it prompts to select a folder with the .dxf files.

Foreach of the .dxf, it adds the text (you may want to adjust the textsize variable), then it

saves it into a "Finished" folder, just one level down from the Folder you selected in the beginning.

e.g. IF your .dxf files are in a folder 'C:\Project12345\',   then it will save the finished

into a folder C:\Project12345\Finished\' - AND that folder (may not be visible, depending on how

your windows is set up.) If it's not visible, right click on the main folder, do 'Properties', and set

the folders below that to (NOT) read-only. Or, go to the original folder in Explorer, and it will be

shown on the Right Pane, click on that. Here's the code.

;; Add_dxf_name.lsp
;; New: 08-24-2024 ECCAD for the AutoLisp Forum
;;
 (vl-load-com)
;;
;; Old Version of 'BrowseForFolder' by: Tony Tanzillo (AutoLisp Forum)
;;
(defun BrowseForFolder ( Message / sh folder parentfolder folderobject result) 
 (vl-load-com) 
  (setq sh 
   (vla-getInterfaceObject 
     (vlax-get-acad-object) 
       "Shell.Application" 
     ) 
   ) 
   (setq folder 
      (vlax-invoke-method 
          sh 
          'BrowseForFolder 
          0 
          Message 
          0 
       ) 
   ) 
   (vlax-release-object sh) 

    (if folder 
      (progn 
         (setq parentfolder 
           (vlax-get-property folder 'ParentFolder) 
         ) 
        (setq FolderObject 
           (vlax-invoke-method 
              ParentFolder 
               'ParseName 
              (vlax-get-property Folder 'Title) 
           ) 
        ) 
       (setq result 
          (vlax-get-property FolderObject 'Path) 
       ) 
       (mapcar 'vlax-release-object 
         (list folder parentfolder folderobject) 
       ) 
     (if (/= (substr result (strlen result)) "\\")
       (setq result (strcat result "\\"))
       result
     )
   ) 
 ) 
); defun BrowseForFolder

 (defun get_dxf_file_lst ()
  (if (setq DXF_Path_Name (BrowseForFolder "Select Folder of DXF Files."))
   (progn
    (setq fil_lst (vl-directory-files DXF_Path_Name "*.dxf" 1))
     (if (/= fil_lst nil)
      (setq fil_lst (acad_strlsort fil_lst))
      (setq fil_lst (list ""))
     ); end if
   ); progn
  ); if
  (princ)
 ); end function get_dxf_file_list
;;
;; Local Functions to process each DXF, add text, save in Finished folder
;;
 (defun save_the_dxf ( fname )
   (setq FOLDER (strcat DXF_Path_Name "Finished"))
   (vl-mkdir FOLDER); Nil if folder exists 
   (setq OUTPATH (strcat FOLDER "\\"))
   (setq dxfname (strcat OUTPATH out_fil))
   (command "_DXFOUT" dxfname 16)
 ); end function save_the_dxf

 (defun get_project_string ( fname )
  (setq str ""); blanked string
  (if fname
   (progn
    (setq N 1)
    (repeat (strlen fname)
     (setq chk (substr fname N 1))
      (if (= chk "_")
       (setq str (substr fname 1 (- N 1)))
      ); if
     (setq N (+ N 1))
    );repeat
   ); progn
  ); if
  str
 ); end function get_project_string

 (defun add_text_to_dxf ( DXFList )
  (setq L 0)
   (repeat (length DXFList)
    (setq dx_fil (nth L DXFList)); get a file at a time
    (setq out_fil dx_fil); Save existing File Name
    (setq dx_fil (strcat DXF_Path_Name dx_fil))
    (command "_erase" "all" "")
    (command "_purge" "ALL" "" "N")
    (command "_DXFIN" dx_fil); load the DXF
    (command "_zoom" "E"); zoom
    (setq txt (get_project_string out_fil))
    (if (/= txt "")
     (progn
      (setvar "TEXTSIZE" 10.0); or whatever textheight you need.
      (command "_Text" (list 0.0 0.0 0.0) (getvar "textsize") "0" txt)
     ); progn
    ); if
    (save_the_dxf dx_fil)
    (setq L (+ L 1))
   ); repeat
   (princ "\n")
   (alert (strcat "DXF Files Updated in " OUTPATH))
   (princ)
 ); end function add_text_to_dxf

;;
;; Begin
;;
 (setq fil_lst nil)
 (get_dxf_file_lst)
 (if fil_lst
   (add_text_to_dxf fil_lst)
 ); if
 (if (= fil_lst nil)
  (alert "Did Not Find any .dxf Files in that Folder ?")
 ); if
 (princ)

ECCAD

Message 8 of 20
tiwari1211
in reply to: tiwari1211

Hello Eccad, I am very sorry for not being quick & many many thanks for helping me out. 

Below are my answers as per my requirement if you still can support -

 

1) You are right here-- Make string for text, of characters of filename until you hit a "_" character

2) Place the Text at 0,0,0  with 0 rotation, existing TextHeight - string above. -- Height 10, Layer "Yellow" .. rest you are correct.

3) This meets my requirement - 

And, Dxfout (overwriting existing file in same folder)

Erase All (to prepare for next Dxfin),

Dxfin the 'next' filename.dxf ...

When 'done' echo DXF Updated in 'Foldername.

 

I attached a Text.dxf to give you a reference of Text Layer i am looking for. 

 

Once again Thank you very much for all your help. 

 

Best Wishes 

Message 9 of 20
tiwari1211
in reply to: tiwari1211

Hi, I tried the lisp file you provided. It insert the complete file name and not only the part name as needed. I tried with the attached DXF file and below is the output. 

I am looking for only test "12345-3.4" to be inserted. 

 

tiwari1211_0-1724530458591.png

Can you please check this as well. 

 

Thank you

Message 10 of 20
ec-cad
in reply to: tiwari1211

Changed the get_project_string function to just find the 1st "_" character.

Added Layer Make "YELLOW", so text will go there.

 

Updated version attached.

 

ECCAD

 

Message 11 of 20
ec-cad
in reply to: tiwari1211

Looks like it read the line, found the "_" characters, but it didn't stop

at the first one, but caught the 'last' one, hence setting the str to what

was (left) of that. Easy fix. Update coming shortly.

 

ECCAD

Message 12 of 20
tiwari1211
in reply to: tiwari1211

Awesome!! The lisp file works perfectly as it should be, only it creates one additional folder will be better if it overwrites existing files in the same folder. 

Many thanks and i wish you a good day ahead 🙂

Message 13 of 20
ec-cad
in reply to: tiwari1211

You are very Welcome.

As for saving the .dxf in the same folder... what if something were to go wrong with the process ?

That (could) destroy the original .dxf, better be safe than sorry. If you review the final .dxf's

in \Finished  folder, and like what you see, just copy/paste back to original folder, overwriting

the originals, then 'Delete' the Finished folder. Only takes a couple of seconds.

 

ECCAD

Message 14 of 20
tiwari1211
in reply to: tiwari1211

I agree with you on this. Its beter to have a origional copy. 

Many thanks again for all your help.  I wish you all success & happiness in life 🙂

 

Message 15 of 20
tiwari1211
in reply to: tiwari1211

.

Message 16 of 20
tiwari1211
in reply to: ec-cad

Hi, I hope my msg finds you in good health. 

I have one more question to you, if you can help. 

I want to have material number as well behind the part number. for example 12345-3.4/316L. (the material will change as per project requirement). Is there any possibility that user can give material input to program and program adds it behind the part nos. for all the parts in folder ?

Thanks a lot for looking into my request.

Message 17 of 20
ec-cad
in reply to: tiwari1211

Question: To add the Material Code (assuming it's a string), do you want to 'edit' the existing .dxf's

and 'append' the Material Code to the original Filename string, OR do you want to process 'new' dxf's,

get the Filename and 'append' the Material Code. Big difference. Here's the Lisp to just 'add' that to

the end of the Filename, and output the .dxf with the text as appended.

You will type in e.g.:  /316L. for example for the Material Code. Include the "/".

 

ECCAD

Message 18 of 20
tiwari1211
in reply to: ec-cad

Thank you very much, You are veryy good.. 🙂

The code works exactly as per my need & even provide flexibility.

 

You made my evening.. Many many thanks again 🙂

Message 19 of 20
ec-cad
in reply to: tiwari1211

Thanks for the feedback.

Have a Good Day.

 

ECCAD

Message 20 of 20
tiwari1211
in reply to: tiwari1211

Hello Ec-cad, I hope you are doing well. Will it be possible for you to help me again in below questions ?

 

1) In current script, instead of browsing for the dxf folder location. Is it possible that the script takes the folder path of currently active dxf file and update all dxf's available in this folder (including the active one). And replace all the existing DXF in the same location instead of adding any new folder.  

 

2) Is it possible to keep adding material code as optional ? so that command ask for -

Do you want to add material code (Y/N) ? 
if answer is no, that update dxf without material code and if Yes then ask for material code to enter. 

 

3) also is it possible to Include the "/" in script instead of considering this in the material code ? because its very often forgotten & then have to regenerate the output again.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report