Running Batch commands from Lisp

Running Batch commands from Lisp

Anonymous
Not applicable
5,665 Views
9 Replies
Message 1 of 10

Running Batch commands from Lisp

Anonymous
Not applicable

I'm just getting into learning how to write lisp commands. I have found a few programs out there that will let you attach a lisp file to run on multiple drawings like Starbatch. To summarize, the first code I got help with, it runs great on a single file. It doesn't run correctly when I try to run a batch routine with the lisp. 

 

Am I better off to write a lisp command to make this lisp batch, or use some sort of program? I'm used to Bentley where i could just attach a notepad code to a batch process. If it helps, I attached the text for the code I'm running. 

 

Thank you, 

0 Likes
Accepted solutions (1)
5,666 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable

I do not know if you understand, but, You want some AutoLISP routines to load automatically when you start AutoCAD, so that you do not have to load each routine manually every time you want to use it:

 

(defun mystartup ()
  (load "C:\\file location\\LISP.LSP")
  )
(if s::startup
  (setq s::startup (append s::startup (quote ((mystartup)))))
  (defun s::startup () (mystartup))
  )

Response from that >>Link<<.

 

 

 

0 Likes
Message 3 of 10

roland.r71
Collaborator
Collaborator

@Anonymous wrote:

I'm just getting into learning how to write lisp commands. I have found a few programs out there that will let you attach a lisp file to run on multiple drawings like Starbatch. To summarize, the first code I got help with, it runs great on a single file. It doesn't run correctly when I try to run a batch routine with the lisp. 

 

Am I better off to write a lisp command to make this lisp batch, or use some sort of program? I'm used to Bentley where i could just attach a notepad code to a batch process. If it helps, I attached the text for the code I'm running. 

 

Thank you, 


What exactly is the problem?

Do you get error messages? it stops after a few drawings? nothing happens? all dwgs get processed but nothing changed? etc,etc.

As your lisp is a function, did you execute the function after loading the lisp?

You do load the lisp with every drawing?

& about that _Yes ... as i've stated before in the other topic, you should ONLY add it if the file DOES exist.

Only THEN will acad ask if you wish to overwrite. If it does not exist it will not ask and the _Yes will be a "Unknown command" & that will terminate your batch.

Message 4 of 10

Anonymous
Not applicable

Ok, so when I set off a batch, I'm using the batch2 picture attached with the StarBatch program. 

I'm attaching the lisp file with the attached, Purge text. This lisp works individually on drawings great. I have not run into a problem with overwriting. There are no instances of not existing. 

 

Batch1 picture, it appears that the code is setting off, but is bringing up the dialogs instead of running just as commands in the prompt. 

My questions are a matter of does this code run differently in these batch tools or should I make the batch process run inside of my code? I'm just wondering which is the better route to go. Maybe the answer is that this should be running fine with this tool, but I have some of my code off a bit. 

 

Thank you Roland, I am listening to ALL of your advice. It got a bit confusing with all of the different advice chimed in, but I'm following everything you've been saying I believe. I do appreciate it, I've learned a lot so far.  

0 Likes
Message 5 of 10

roland.r71
Collaborator
Collaborator
Accepted solution

There should be no difference in the way the code executes.

There CAN be a difference depending on how a function/lisp routine works and/or how well (or bad) is was written. Like global variables that shouldn't be global & mess up on the 2nd and following runs.

 

As for the dialogs poping up, that too should happen if you execute the code directly on 1 drawing (unless some where turned off by sysvar's like filedia), not just when running a script. (looking at the code, it should indeed)

 

The answer is indeed that it should work fine, but your code is a bit off.

(defun c:test nil
  (command "_.Purge" "_All" "*" "_No"
	   "_.-purge" "_R" "_All" "_No" 
	   "_.Setbylayer" "_All" "" "_Yes" "_Yes"
	   "_.Layer" "_Color" 253 "*" ""
	   "_.Audit" "_Yes")

(command "_.SAVEAS" "2013" (strcat "\\\\universal-5010\\S\\ENGINEERING\\JOBS\\16-006 GM Ramos\\DRAWINGS\\GK\\GK XREF\\" (getvar "dwgname")) "_Yes"))

 

The first line calls _.Purge, this should be like the second line: _.-purge (its the - that indicates you don't want the dialoged version, note: Not all commands come with a - variant)

Same goes for _.Layer -> _.-layer

 

I'm not sure about that defun... it might work as is, but usually people use: () instead of nil

 

& as a tip: Do not call your function "test". Before you know it you got 10 different "test" functions loaded, all in conflict and overwriting eachother and you don't know which is active. Besides not being able to tell what it is, a few months from now. Try to use unique names that are relevant, somewhat. Like you did with the purge.txt (why not .lsp ?) instead of test.txt (of course, you can't name it c:purge as that's an existing command (well, you can, but that would redefine the existing purge command, which you do not want))

0 Likes
Message 6 of 10

Anonymous
Not applicable

Awesome, thanks! I this makes a lot of sense, and i'm seeing it work. Much appreciated. Like I said, I've done a LOT of programming, but I come from the Bentley world, please don't hate me for it. Haha.

0 Likes
Message 7 of 10

roland.r71
Collaborator
Collaborator

@Anonymous wrote:

Awesome, thanks! I this makes a lot of sense, and i'm seeing it work. Much appreciated. Like I said, I've done a LOT of programming, but I come from the Bentley world, please don't hate me for it. Haha.


Why would I?

The more languages you've worked with, the easier a new one becomes, as the basic concepts are always the same. Variables, Functions, If-then-else, Integers, strings etc. i mention nothing new to you here, do I (?)

so you already know LISP for 50% Smiley Wink

0 Likes
Message 8 of 10

Anonymous
Not applicable

Yes, very helpful. Honestly, I've never written lisp until a few days ago. I used AutoCad back in high school and a total of 4 months on the job. 

I've written, SQL, VBE, VBA, and MVBA, and a lot of in between stuff like EC expressions. 

Lisp seems quite simple, but I'm starting to learn some of the Autocad command rules, like the -Layer for instance, the hyphen simply suppresses the dialog. Where like the filedia=0 suppresses dialog, then =1 sets it back. Learning a lot. I really appreciate your help. 

0 Likes
Message 9 of 10

Anonymous
Not applicable

If you want to process a lot of dwg's look into scripts you can use a mixture of commands and load lisps for more complex functions. Sometimes make a lisp as it is shorter than writing multiple lines repeated say after each "open". Lastly Yes a lisp can make lisps, dcl  or scripts.

 

open dwg1 (load "fixup") close Y
open dwg2 (load "fixup") close Y
open dwg3 (load "fixup") close Y
open dwg4 (load "fixup") close Y
0 Likes
Message 10 of 10

adminS5F5J
Community Visitor
Community Visitor

hello i was reading your comment about running complex lisps from scrips, im having ZERO luck getting this lisp to run through a bat file which runs to create dwgs then run the lisp to that dwg, any chance you could take a look and see if anything glaring sticks out to you with what im doing wrong?  the way this is supposed to work -
1 runs the lisp on the original dwg that will be in the folder to  CAC014-PC-HT-1001.dwg
2 closes that dwg, copies only to do a save as the next numerical order CAC014-PC-HT-1002.dwg
3 it the runs the same lisp to the dwg copy saved as the next numerical number
(order of the process is important because the lisp is locating the csv file
and takes the data of the header row and matches it with the tag id to the dwg
file save number in order to populate the attributes) 
here is the content of the batch file

@echo off
setlocal enabledelayedexpansion

rem Define the paths
set "original_dwg=C:New folder\CAC014-PC-HT-1001.dwg"
set "script_file=C:New folder\commands.scr"
set "accoreconsole=C:\Program Files\Autodesk\AutoCAD 2025\accoreconsole.exe"
set "dwg_folder=C:New folder"
set "prefix=CAC014-PC-HT-"

rem Print out the file paths for debugging
echo Original DWG file: %original_dwg%
echo Script file: %script_file%
echo DWG folder: %dwg_folder%

rem Check if the original DWG file exists
if not exist "%original_dwg%" (
echo Original DWG file not found: %original_dwg%
pause
exit /b
)

rem Check if the script file exists
if not exist "%script_file%" (
echo Script file not found: %script_file%
pause
exit /b
)

rem Print the contents of the script file for verification
echo Script file content:
type "%script_file%"
echo.

rem Initialize counter for the new files
set "counter=1002"

rem Loop to create and process new DWG files
for /L %%i in (1,1,3) do (
set "new_dwg=%dwg_folder%\%prefix%!counter!.dwg"

rem Copy the original DWG file to the new file
echo Copying %original_dwg% to !new_dwg!
copy "%original_dwg%" "!new_dwg!" >nul

rem Run the LISP routine on the new DWG file using AcCoreConsole
echo Running AcCoreConsole on !new_dwg! with script %script_file%
"%accoreconsole%" /i "!new_dwg!" /s "%script_file%"

rem Increment the counter for the next file
set /A counter+=1
)

endlocal
pause

0 Likes