Batch Processing on all open drawings with LISP

Batch Processing on all open drawings with LISP

Anonymous
Not applicable
3,817 Views
11 Replies
Message 1 of 12

Batch Processing on all open drawings with LISP

Anonymous
Not applicable

Hi,

 

I have a LISP routine that I am trying to implement batch processing on. It takes a job and applies company standards to a drawing, then prints it with a specific naming format, and saves it in the related place. The naming changes according to drawing-specific attributes.

 

I have it set-up in an OOP format. The command must be executed on each drawing, and there could be 100+. Using a foreach loop would surely be my best approach at this point in time, but how can I iterate my process through every drawing the user has open? (In other words, foreach drawing open, perform the main routine)

 

I've considered converting my code to publish instead of plot for efficiency and did a pro/cons list. I don't think its the best decision right now.

 

Also, how could I retrieve all of the open drawing names with another foreach loop, and have them listed in a dialog box with a Confirm / Cancel option before processing my script?

 

 

0 Likes
3,818 Views
11 Replies
Replies (11)
Message 2 of 12

Moshe-A
Mentor
Mentor

@Anonymous  hi,

 

the best way to run lisp through list of dwg is ScriptPro 2.0 

>> download ScriptPro 2.0 from here <<

if you can not get ScriptPro then create a script (scr) file. you can write a small lisp to create a script for specific folder.

 

to retrieve the opened documents (files) you need to use ActiveX:

 

; get the pointer to opened documents

(setq docs (vla-get-documents (vlax-get-acad-object)))

 

enjoy

moshe

 

0 Likes
Message 3 of 12

Sea-Haven
Mentor
Mentor

This little snippit may help you can get via vl what dwgs are currently open.

 

[code]

(defun openblk (blkname / adocs)
(setq acDocs (vla-get-documents (vlax-get-acad-object)))
(vla-open acDocs blkname)
(vla-activate (vla-item acdocs 1))
;(vla-put-activedocument (vlax-get-acad-object) (vla-item acdocs 1))
)

[/code]

0 Likes
Message 4 of 12

maratovich
Advisor
Advisor

For batch printing use this (no problem):  Revers

For batch Lisp use this: AutoViewport

 

 

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
0 Likes
Message 5 of 12

Jonathan3891
Advisor
Advisor

You could give this a try. It doesn't work on all open drawings, but all drawings in a folder.

 

(defun c:BatchJob (/ dwgs file dwgName scrFile folderName)
  (setq folderName
  (browsefolder "Select folder to perform batch job: ")
  )
  (setq dwgs (vl-directory-files folderName "*.dwg"))
  (setq scrFile (open (strcat folderName "\\batchJob.scr") "w"))
  (foreach file dwgs
	(setq dwgName (strcat "\"" folderName "\\" file "\""))
	(write-line ".Open" scrFile) 
	(write-line dwgName scrFile)
	(write-line "SomeCommand" scrFile)
	(write-line ".Qsave" scrFile)
	(write-line ".close" scrFile)

  )	 
  (close scrFile)  
  (command ".script" (strcat folderName "\\batchJob.scr"))
  (princ)
)	
;;; Function to browse folder
;;; This code was originally posted by kpblc2000 in AUGI AutoLisp Forum
;;;
(defun browsefolder (title / shlobj folder fldobj)
  (vl-load-com)
  (setq
	shlobj (vla-getinterfaceobject
	  (vlax-get-acad-object)
	  "Shell.Application"
	)
	folder (vlax-invoke-method shlobj 'browseforfolder 0 title 0)
  )
  (vlax-release-object shlobj)
  (if folder
	(progn
	  (setq
 fldobj	(vlax-get-property folder 'self)
 folderName (vlax-get-property fldobj 'path)
	  )
	  (vlax-release-object folder)
	  (vlax-release-object fldobj)
	  folderName
	)
  )
)
(princ)

 


Jonathan Norton
Blog | Linkedin
0 Likes
Message 6 of 12

Anonymous
Not applicable

My LISP script applies certain styles to a set of open drawings, and plots them with company standard naming, while saving a copy in two locations relative to some attributes in the block of a drawing.

 

Currently our process is to open each drawing, and type the command which executes these functions. The issue is we must perform the command on each drawing individually.

 

So, if there are 100 drawings, we must type the command 100 times. My hopes is to have all 100 drawings open, and have the lisp function be able to iterate through every open drawing.

 

Here is my current main function:

 

(defun batchissue_main ()
(textpage)
(princ "\n ")
(princ)
(princ "\n ")
(princ)
    (progn (setting)
	    (border)
	    (gather)
	    (type_issue)
	    (pset)
	    (checkexistissue)
	    (deleteoldpdf)
      	    (pdfissue)
    )
  (princ)
)					;defun
(princ)

 

 

I'm hoping to turn it into something like this:

 

(defun C:batchissue_main ()
(textpage)
(princ "\n ")
(princ)
(princ "\n ")
(princ)
~FOR EACH DRAWING OPEN~ (progn (setting) (border) (gather) (type_issue) (pset) (checkexistissue) (deleteoldpdf) (pdfissue) ) (princ)
~~END FOR EACH DRAWING OPEN ) ;defun (princ)

Any ideas related to approaching this situation? 

 

 

0 Likes
Message 7 of 12

dbhunia
Advisor
Advisor

If so Try this in SDI Mode....... (only one drawing will be in opened condition in that AutoCAD session)

Run the LISP and Select the Folder of Drawings you want to Process.

 

This program is a sample which will place a text "SAMPLE TEXT" at "0,0,0" in Model space of each drawing of the target folder...

 

(defun C:Batch_Run ( / *error* sh folder SDI_Val LISPI_Val folderobject result Files_Folder n)
	(vl-load-com)
	(defun *error* ( msg )
		(setvar 'SDI SDI_Val)(setvar 'LISPINIT LISPI_Val)
		(if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*"))
			(princ (strcat "\nOops an Error : " msg " occurred."))
		)
		(princ)
	)  
	(setq SDI_Val (getvar 'SDI))
	(setq LISPI_Val (getvar 'LISPINIT))
	(setq sh (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application" ))
	(setq folder (vlax-invoke-method sh 'BrowseForFolder 0 "Select Folder of Drawings to Process" 512 ))
	(vlax-release-object sh)
	(setvar 'cmdecho 0)(setvar 'SDI 1)(setvar 'LISPINIT 0)
	(if folder
		(progn
			(setq folderobject (vlax-get-property folder 'Self))
			(setq result (vlax-get-property FolderObject 'Path))
			(vlax-release-object folder)
			(vlax-release-object FolderObject)
			(setq Files_Folder (vl-directory-files result "*.dwg"))
			(command "save" (strcat (getvar "dwgprefix") (getvar "dwgname")) "Y")
			(setq n 0)
			(while (< n (length Files_Folder))
				(command "fileopen" (strcat result "\\" (nth n Files_Folder)));;;Open Each Drawing
				;;;;;;; commands to run on each drawig starts here ;;;;;;;
				
				(setvar 'TILEMODE 1);For texting - Changing to Model Space ;;; Remove this line
				(command "_.text" "0,0,0" "10" "" "SAMPLE TEXT");For texting - Placing Text at 0,0,0 ;;; Remove this line
				
				;;;;;;; commands to run on each drawig ends here ;;;;;;;
				(vl-cmdf "save" (strcat result "\\" (nth n Files_Folder)));;;Save & Close opened Drawing
				(setq n (+ 1 n))
			)
		)
	)
	(setvar 'SDI SDI_Val)(setvar 'LISPINIT LISPI_Val)(setvar 'cmdecho 1)
	(princ)
)

You remove the Blue lines and place your commands there, those will run in each Drawing of the target folder... (still every thing depends on your functions behavior).

 

For more talk in morning.

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 8 of 12

Sea-Haven
Mentor
Mentor

You want to process a 100 dwgs I would look at using scripts they can run lisp on a open dwg. So as posted else where you could do stuff like what layouts to plot in a dwg using say excel to pick the layouts printer etc. A tick boxes approach you will be surprised how fast a dwg can open plot and then close with no user interaction. 

0 Likes
Message 9 of 12

Crstiano
Collaborator
Collaborator

Hi @Jonathan3891 and guys,

 

Lisp is not my best language 🙂 , So:

 

using this code:

I just try change the line 

(write-line "SomeCommand" scrFile)

For some like this:

"(sssetfirst nil (ssget "_x" '((0 . "point"))))"

(write-line "Erase" scrFile)

 

My idea is open all files, select all points and delete all.

 

Can you help me ?

 

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.


Cristiano Oliveira
EESignature
ConsultCAD.com

0 Likes
Message 10 of 12

Jonathan3891
Advisor
Advisor

In order for that to work in the lisp I provided, the delete point code will need to be added to the lisp somewhere and then call the command from the script. 

 

I've tested and this works.

 

 

(defun c:BatchJob (/ dwgs file dwgName scrFile folderName)
  (setq folderName
  (browsefolder "Select folder to perform batch job: ")
  )
  (setq dwgs (vl-directory-files folderName "*.dwg"))
  (setq scrFile (open (strcat folderName "\\batchJob.scr") "w"))
  (foreach file dwgs
	(setq dwgName (strcat "\"" folderName "\\" file "\""))
	(write-line ".Open" scrFile) 
	(write-line dwgName scrFile)
	(write-line "DPNT" scrFile)
	(write-line ".Qsave" scrFile)
	(write-line ".close" scrFile)
	

  )	 
  (close scrFile)  
  (command ".script" (strcat folderName "\\batchJob.scr"))
  (princ)
)	
;;; Function to browse folder
;;; This code was originally posted by kpblc2000 in AUGI AutoLisp Forum
;;;
(defun browsefolder (title / shlobj folder fldobj)
  (vl-load-com)
  (setq
	shlobj (vla-getinterfaceobject
	  (vlax-get-acad-object)
	  "Shell.Application"
	)
	folder (vlax-invoke-method shlobj 'browseforfolder 0 title 0)
  )
  (vlax-release-object shlobj)
  (if folder
	(progn
	  (setq
 fldobj	(vlax-get-property folder 'self)
 folderName (vlax-get-property fldobj 'path)
	  )
	  (vlax-release-object folder)
	  (vlax-release-object fldobj)
	  folderName
	)
  )
  (princ)
)

;; Delete Point
(defun C:DPNT (/ SS1)
  (setq SS1 (ssget "x" '((0 . "point"))))
  (if SS1
    (progn
      (command "erase" SS1 "")
      (princ "\nAll Points Erased!")
      )
    (princ "\nNo Points Found!")
    )
  (princ)
  )

 

 


Jonathan Norton
Blog | Linkedin
Message 11 of 12

Crstiano
Collaborator
Collaborator

Your code is similar I tried yestarday. But found the same problem.

 

Explain:

After LoadApplication. It´s load successful. Commands Batchjob and NPNT loaded.

Executing Bachtjob:

It asked folder, create the script, but when try execute the  NPNT .. it don't recognition the command..

There's something wrong ??

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.


Cristiano Oliveira
EESignature
ConsultCAD.com

0 Likes
Message 12 of 12

Sea-Haven
Mentor
Mentor

Its a 2 step process make the lisp that does all the work and save it, 2nd step is using a lisp make the script file, you would then just load the pre-saved lisp, it can have a defun so call that as well.

 

Open dwg1
(load "mylisp")
(mydefun)
Close
Y
open dwg2
................
0 Likes