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.

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
tiwari1211
367 Views, 12 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

12 REPLIES 12
Message 2 of 13
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 13
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 13
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 13
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 13
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 13
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 13
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 13
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 13
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 13
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 13
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 13
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

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

Post to forums  

Forma Design Contest


AutoCAD Beta