How to expand .lsp file function from currently open drawing to whole project?

How to expand .lsp file function from currently open drawing to whole project?

iedwards17
Explorer Explorer
653 Views
4 Replies
Message 1 of 5

How to expand .lsp file function from currently open drawing to whole project?

iedwards17
Explorer
Explorer

Hello,

 

I'm super new to all of this, so I'm sorry if it seems like a stupid question. I am writing a LISP file that exports all blocks' x and y position data in the currently open AutoCAD Electrical drawing. Is there a way to change it from the currently open drawing to iterating or looping through all of the drawings in a specific folder or .wdp project file? 

 

Thanks

 

Here is my current code:

 

(defun c:ExportBlockPositionData ()
(setq file (getfiled "Select File to Save" "" "csv" 1))
(setq blk_list '())
(setq ss (ssget "_X" '((0 . "INSERT"))))

(if ss
(progn
(setq count (sslength ss))
(setq output_string "Block Name,X Position,Y Position\n")

(setq i 0)
(while (< i count)
(setq blk (ssname ss i))
(setq blk_name (cdr (assoc 2 (entget blk))))
(setq blk_pos (cdr (assoc 10 (entget blk))))

(setq output_string (strcat output_string blk_name "," (rtos (car blk_pos) 2 2) "," (rtos (cadr blk_pos) 2 2) "\n"))

(setq i (1+ i))
)

(setq file_id (open file "w"))
(write-char (ascii "sep=,\n") file_id) ; Excel separator
(write-line output_string file_id)
(close file_id)

(princ "\nBlock position data exported successfully.")
)
(princ "\nNo blocks found in the drawing.")
)
(princ)
)

0 Likes
Accepted solutions (1)
654 Views
4 Replies
Replies (4)
Message 2 of 5

paullimapa
Mentor
Mentor
Accepted solution

Excellent question. You'll just have to Script this on all of drawings in a folder.

First of all for this code to run in a Script you just need to change this line which brings up a selection window:

 

(setq file (getfiled "Select File to Save" "" "csv" 1))

 

To this line which just uses the drawing folder & file name as the csv file:

 

(vl-load-com)(setq file (strcat(getvar "dwgprefix")(vl-filename-base (getvar"dwgname")) ".csv") ; saves csv file matching folder & drawing name

 

Next install something like Script Writer which allows you to run a series of commands on all drawings in a selected folder:

paullimapa_0-1707435267119.png

In your case the Script Line in the above window would look something like this:

 

_.Open *file* (load"ExportBlockPositionData") ExportBlockPositionData _.Close

 

 


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

iedwards17
Explorer
Explorer

@paullimapa Thank you!

0 Likes
Message 4 of 5

paullimapa
Mentor
Mentor

Glad to have helped…cheers!!!


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

Sea-Haven
Mentor
Mentor

If you want 1 file across multi dwg's, add dwg name to file output you can use "A" rather than "W" for file, "A" is append, but you will need to probably for simplicity hard code an output filename. A side note if to Excel is required could write direct to Excel.

0 Likes