Jump to content

Activate Lisp Code On Multiple DWG's


omer111

Recommended Posts

I recently wrote a LISP code with help from this forums that alters the values of a certain block. After I managed to get it to work on a single file, now I need to batch activate it, I want to insert a folder location for both the LISP code, and another folder location containing the DWG's I want altered, and then the 'parent' LISP will open up all DWG's and activate the code one at the time. preferably all in the same session because opening up a new session sometimes asks for user prompts or licenses, and I need it to run smoothly. I couldn't find a LISP code that opens up a new DWG file and I would love to get some examples or references

 

Thank you for your help!

Link to comment
Share on other sites

Yeah, it's doable.  It takes a few steps.

 

The basic idea:

- 2 lisp files; "parent_lisp.lsp" and "batch_execute.lsp"

- Open a drawing, , load parent_lisp.lsp, execute BOF (batch open files)

-  "batch_execute.lsp" should be included in the files that get auto loaded any time a drawing is opened, see:

https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/Automatically-load-AutoLISP-routines.html

now,  "batch_execute.lsp" holds a variable 

(setq specified_folder "C:\\Data\\desktop\\lisp\\CADTUTOR\\batch_execute\\") 

 

If the drawing is saved in that folder, then a function gets executed.  Else it's ignored.

Obviously set this specified_folder to your folder.  don't forget each \ must get doubled to \\, and finish with \\.

 

When you no longer need this batch auto execute, then remove  "batch_execute.lsp" from the list of auto loaded lisp files.

 

I tried it, it works.

You might try it for a limited number of dwg's.  You may get a RAM overload or something.

 

-------

"parent_lisp.lsp"

;; @file this file opens all drawings in a specified folder.

(vl-load-com)

(setq specified_folder "C:\\Data\\desktop\\lisp\\CADTUTOR\\batch_execute\\")  ;; don't forget the \\ at the end

;; substring, like php substr
(defun substring ( str idx len )
  (substr str (if (< idx 0) (+ 1 (strlen str) idx) (1+ idx)) len)
)



(defun open_file (FileName ReadOnly / )
    (vla-Open
     (vla-get-Documents
      (vlax-get-Acad-Object)
     )
     FileName
     (if ReadOnly
      :vlax-true
      :vlax-false
     )
    )
)

;; command BOF, for Batch Open Files
(defun c:bof ( / path allfiles FileName)

  ;; path of the drawings that need to be opened, and the batch function executed on each drawing
  (setq path specified_folder)
  ;; find all the drawings in that folder
		;; @see https://www.cadtutor.net/forum/topic/50837-list-of-drawings-in-a-folder/
  (setq allfiles (vl-directory-files path "*.DWG" 0))

  (princ allfiles)
  
  (foreach FileName  allfiles
    (open_file (strcat path FileName) ReadOnly)
	(Command "DELAY" "2000") ; Pause a couple seconds
  )

)

 

"batch_execute.lsp":

;; @file this file is to be auto loaded when a drawing opens: see (1).  Then check the folder in which the drawings are saved.  
;;   If that folder is the specified foler (setq specified_folder ...), then execute a function.
;;   Then close the drawing 
	;; (1)  https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/Automatically-load-AutoLISP-routines.html

(vl-load-com)

(setq specified_folder "C:\\Data\\desktop\\lisp\\CADTUTOR\\batch_execute\\")  ;; don't forget the \\ at the end


(defun auto_execute ( / dwg_path)
	(setq dwg_path (getvar "DWGPREFIX"))
	(if (= specified_folder dwg_path)
		(do_batch_function)
	)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; as an example, do_batch_function will write the filename of the dwg on the drawing, on x,y = 0,0

;; https://www.cadtutor.net/forum/topic/18257-entmake-functions/
(defun drawText (pt hgt str)
 (entmakex (list (cons 0 "TEXT")
                 (cons 10  pt)
                 (cons 40 hgt)
                 (cons 1  str))))
				 

(defun do_batch_function ( / )
	(drawText (list 0.0 0.0)  2.5 (getvar "dwgname"))
	
	(command "QSAVE") 
	;;(command "CLOSE") 
	
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(auto_execute)

 

  • Like 1
Link to comment
Share on other sites

If you look to Lee Mac Script writer (http://www.lee-mac.com/scriptwriter.html) or search for scriptpro that can help

 

You could put the LISP into your startup suit and include (at the end of the script definition) the code to make it run example

 

(defun c:testthis ( / )
...LISP .....
) ; end defun
(c:testthis)

 

which will run as soon as it is loaded - when the drawing has loaded - and then just open all the drawings you need to - dead simple but remember to remove the (c:testthis) line or take it out of the startup else it will run on every drawing and every time one opens

 

Final if you want to trawl the forums, I am sure I posted a batch LISP on here somewhere - I'd post it again but the weekend has started here, CAD is off, unable to go through the code to take out my company details and check it still works but if you find it you can use that as well.

 

 

Lat thing you could research the Core Console (Big Al seams to know this better than I do), though it is quick it is limited to basic LISP commands and AUtoCAD commands, VLA- commands don't work so well

Edited by Steven P
Link to comment
Share on other sites

One of the simplest way to edit multiple dwg's is a script it tells the CAD what to do like a command line sequence, you can run lisp programs as well. I have used this on like 100 dwgs but they were small dwg's so the open modify and close was like 2-3 seconds per dwg.

 

open dwg1
(load "mylisp")
Close Y
open dwg2
(load "mylisp")
Close Y
open dwg2
(load "mylisp")
Close Y

There are various programs out there that help write the script like pick a directory and get all the dwg names. I have been making scripts for CAD since Acad 1.7.

 

Look at Scriptpro. 

 

In more recent times you can use Accoreconsole which will edit the Autocad dwg without opening it, again you need a working lisp 1st, it uses a script but the script is as simple as (load "mylisp") as the close is done in the lisp. It can be ran as a windows batch program on a directory.

 

https://through-the-interface.typepad.com/through_the_interface/2012/02/the-autocad-2013-core-console.html

a good reference
https://forums.autodesk.com/t5/forums/replypage/board-id/130/message-id/444989

 

  • Like 1
Link to comment
Share on other sites

Here is another option, there will be stuff in there that might not work, going to need both of these files I think and I save them in ....Desktop \\ AutoCAD \\ AutoCAD LISPS .... so you can create that folder or search through the code and change that file path.

 

These are just a fancy DCL option to make a script that is all

 

BATCHLISPS.lsp LISPSHELP.lsp

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...