Need a lisp file to combine files and give it the name of the current drawing

Need a lisp file to combine files and give it the name of the current drawing

Hans_Knol
Advocate Advocate
871 Views
7 Replies
Message 1 of 8

Need a lisp file to combine files and give it the name of the current drawing

Hans_Knol
Advocate
Advocate

Hello all,

I'm looking for a lisp routine that can put the contents of 4 different files into one file and give the name of the file the name of the current drawing.

so the names of the exported files (from AutoCAD Electrical) are,

SheetData.tmp

WireToData.tmp

WireFromData.tmp

EquipmentData.tmp

All these file will be stored into the directory "C:\Data\AppData\ACAD\process", after generate the new file it must be saves in the same directory with the filename of the current drawing for example "Drawing1.txt"

 

Hopy somebody can help me with this.

Regards,

Hans

Hans Knol
0 Likes
872 Views
7 Replies
Replies (7)
Message 2 of 8

ronjonp
Mentor
Mentor

Sure:

(defun c:foo (/ _writefile _readfile A B D R)
  ;; RJP » 2020-01-10
  (defun _writefile (fn lst / file r)
    (cond ((and (eq 'str (type fn)) (setq file (open fn "w")))
	   (foreach x lst (write-line x file))
	   (close file)
	   fn
	  )
    )
  )
  (defun _readfile (fn / f r l)
    (cond ((and (eq 'str (type fn)) (setq fn (findfile fn)) (setq f (open fn "r")))
	   (while (setq l (read-line f)) (setq r (cons l r)))
	   (close f)
	   (reverse r)
	  )
    )
  )
  (setq d "C:\\Data\\AppData\\ACAD\\process\\")
  (foreach f '("EquipmentData.tmp" "WireFromData.tmp" "SheetData.tmp" "WireToData.tmp")
    (and (setq a (findfile (strcat d f))) (setq b (_readfile a)) (setq r (cons b r)))
  )
  ;; Check that there are 4 entries to write
  (if (= (length r) 4)
    (_writefile
      (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) ".txt")
      (apply 'append r)
    )
    (alert "All files not found!")
  )
  (princ)
)
0 Likes
Message 3 of 8

hak_vz
Advisor
Advisor
(defun join_files ( / path filelist f joined)
    (setq 
        path 
            "C:\Data\AppData\ACAD\process"
        filelist
            (list 
                (strcat path "\\" "SheetData.tmp")
                (strcat path "\\" "WireToData.tmp")
                (strcat path "\\" "WireFromData.tmp")
                (strcat path "\\" "EquipmentData.tmp")
            )
        f
            (strcat path "\\" "Drawing1.txt")
        joined 
            (fopen f "W")
    )
    (foreach g filelist
        (cond ((findfile g)
            (setq file (open g "r"))
            (while (setq line (read-line file))(write-line line joined))
            (write-line  " " joined)
            (close file)
            )
        )
    )
    (close joined)
    (princ)
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 4 of 8

Sea-Haven
Mentor
Mentor

Very very easy using old fashioned DOS the copy command supports joining files to a new file. So for your request the DOS command is 

 

copy SheetData.tmp+WireToData.tmp+WireFromData.tmp+EquipmentData.tmp dwgfilename.tmp

 

You just use strcat to make the correct string as above then use the "shell" command

 

(command "shell" copystr) 

 

Using prior code there should be enough info to work it out.

 

 

 

 

0 Likes
Message 5 of 8

Sea-Haven
Mentor
Mentor

Found a few minutes. need to replace path etc and filenames.

 

If hit maximum characters due to paths just do file1+file2 -> tmp tmp+file3 etc till done.

 

(defun join_tmp ( / path filelist )
(setq dwgname (cadr (fnsplitl (getvar 'dwgname))))
    (setq path "D:\\Acadtemp")
    (setq  filelist
            (Strcat 
                 "copy " 
		 path "\\" "File1.txt+"
                 path "\\" "File2.txt+"
                 path "\\" "File3.tx+"
                 path "\\" "File4.txt" " "
				 path "\\" dwgname ".txt"
            )
	)
    (command "shell" filelist)
    (princ)
)
Message 6 of 8

hak_vz
Advisor
Advisor

Correction to take into account name of current dwg

 

(defun join_files ( / path filelist f joined)
    (setq 
        path 
            "C:\Data\AppData\ACAD\process"
        filelist
            (list 
                (strcat path "\\" "SheetData.tmp")
                (strcat path "\\" "WireToData.tmp")
                (strcat path "\\" "WireFromData.tmp")
                (strcat path "\\" "EquipmentData.tmp")
            )
        f
            (strcat path "\\" (cadr (fnsplitl (getvar 'dwgname))) ".txt")
        joined 
            (fopen f "W")
    )
    (foreach g filelist
        (cond ((findfile g)
            (setq file (open g "r"))
            (while (setq line (read-line file))(write-line line joined))
            (write-line  " " joined)
            (close file)
            )
        )
    )
    (close joined)
    (princ)
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 7 of 8

devitg
Advisor
Advisor

Hi SeaHaven ,  as I can see the 

fnsplitl

is a defun , similar to 

 

vl-filename-base

Is it?

0 Likes
Message 8 of 8

Sea-Haven
Mentor
Mentor

Yes a pre VL version, can also do strip last 4 characters so remove .dwg

0 Likes