Script for layers and purging

Script for layers and purging

Anonymous
Not applicable
6,051 Views
23 Replies
Message 1 of 24

Script for layers and purging

Anonymous
Not applicable

I'm trying to write a script that will do the following:

purge, then all, then close

 

-purge then registered apps, all, then "no"

 

setbylayer

all

Yes, Yes, Yes

 

change color to 253 on all those layers selected above, then i want to repeat purge steps again.

then audit

saveas to a directory folder

End

0 Likes
Accepted solutions (2)
6,052 Views
23 Replies
Replies (23)
Message 2 of 24

Kent1Cooper
Consultant
Consultant

What [in actual Script format] have you tried so far?  Post that, with a description of in what way it doesn't work right [what does it do that you don't expect? what does it not  do that you want it to do?], and maybe someone will see what should be different.

 

OR in AutoLisp or command-macro format, if that's really what you mean [it's not uncommon for people to use the word "script" when they mean something more generic, like "routine," but SCRIPT has a specific meaning in AutoCAD, and often that's not what people are really talking about].

Kent Cooper, AIA
0 Likes
Message 3 of 24

Anonymous
Not applicable

I'm fairly new to AutoCad coming from 20 years of programming in Microstation. I'm using script pro and tried the following, but couldn't get through just trying to run the first part:

purge
A
O

-purge
R
N

setbylayer
all

Y

Y

Y

Color

253

purge

A

O

-purge

R

N

Audit

-----The saveas part I am still trying to find a write up on-----

Unless I'm not executing this correctly in the script for initializing it, it seemed pretty straight forward when i found these key in's in other articles. 

For the purge, I'm answering A for "all" and "o" for "close"

-purge answering "r" for registered apps (all) then answering No

For setbylayer, all, then answering "yes" three times. I can do all of these command strings within Autocad, I'm guessing i have the syntax incorrect. Thank you for any advice, i'm researching on my own the most I can. 

0 Likes
Message 4 of 24

deisajavier
Participant
Participant

I thing I get it, try this:

 

(defun c:test ()
  
  (command "_.purge" "_all" "*" "_no"
	   "_.setbylayer" "_all" "" "_yes" "_yes"
	   "_.layer" "_color" "253" "*" ""
	   "_.audit" "_yes")

  (initdia) (command "_.saveas")

  )

Once you purge "all" items there is no need to repeat this command.

0 Likes
Message 5 of 24

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

....

purge
A
O

.... 

For the purge, I'm answering A for "all" and "o" for "close"

..... 


The O for Close is a button choice in the dialog-box version, which I don't think you can use in a Script.  Try another non-dialog-box version [which has no Close option], answering the prompts as they are given:

 

-PURGE

all

*

no

Kent Cooper, AIA
0 Likes
Message 6 of 24

Anonymous
Not applicable

This is what happens in the command line when running a file through script pro with your syntax which appears to me it is not initiating but I'm not sure:

**** System Variable Changed ****
1 of the monitored system variables has changed from the preferred value. Use SYSVARMONITOR command to view changes.


AutoCAD menu utilities loaded.*Cancel*

*Cancel*

Command: *Cancel*
_RIBBON
*Cancel*
COMMANDLINE
PROJECTMANAGER
TOOLPALETTES
*Cancel*
properties
Layer
*Cancel*
EXTERNALREFERENCES

Failed to get PaletteSet!
Failed to get PaletteSet!

Command:

Command:
Command: ._quit

Command: Really want to discard all changes to drawing? <N>

Yes or No, please.
Really want to discard all changes to drawing? <N>

Yes or No, please.

0 Likes
Message 7 of 24

deisajavier
Participant
Participant

You need to load the script and then run it by typing test in the command line.

 

In case you dont know how, do this:

1) Create a new txt file.

2) Copy and paste the code I give to you.

3) Save file with .lsp extension (like test.lsp).

4) In your drawing file type appload in command line.

5) Search and load this file.

6) Type test in command line.

0 Likes
Message 8 of 24

Anonymous
Not applicable

Thank you, that is a big help as I was trying to use an .scr file. 

0 Likes
Message 9 of 24

Anonymous
Not applicable

Thank you. This worked great. However, I'm looking for a way to do this in a batch format. Can I apply this same method using this .lsp to multiple files? This code seemed much simpler than a .scr.

 

0 Likes
Message 10 of 24

Anonymous
Not applicable

One last detail, where can I add a specific file location in your code and have it just save as to that specific location?

(defun c:test ()

(command "_.purge" "_all" "*" "_no"
"_.setbylayer" "_all" "" "_yes" "_yes"
"_.layer" "_color" "253" "*" ""
"_.audit" "_yes")

(initdia) (command "_.saveas")

)

0 Likes
Message 11 of 24

Kent1Cooper
Consultant
Consultant

@deisajavier wrote:

You need to load the script and then run it by typing test in the command line.

....

3) Save file with .lsp extension (like test.lsp).

.....


Careful -- it's not  a script, as the OP discovered by trying to make it one.  (See the end of Message 2.)

Kent Cooper, AIA
0 Likes
Message 12 of 24

Anonymous
Not applicable

I did this all, it works... I'm asking, how to I run a whole directory of files? Do I need to do them one at a time, or can I batch them? Thank you very much, you are being very helpful. I'm looking at batch .lsp commands out there for this answer but there is a lot of bad info out there as well.

0 Likes
Message 13 of 24

Anonymous
Not applicable

The only other thing I need to add to the code is to get this attached picture to auto pick save in the file location listed. 

0 Likes
Message 14 of 24

deisajavier
Participant
Participant

@Kent1Cooper  ha escrito:

@deisajavier wrote:

You need to load the script and then run it by typing test in the command line.

....

3) Save file with .lsp extension (like test.lsp).

.....


Careful -- it's not  a script, as the OP discovered by trying to make it one.  (See the end of Message 2.)


You're right, I had not realized... 

0 Likes
Message 15 of 24

Anonymous
Not applicable

So is it best to use an lsp for single file coding and script (.scr) for batching multiple drawings? Is this what you are telling me? The code I was running from script pro was simpler, but was also limited to basic key in commands. I realize I have much to learn, but there are thousands of codes out there as examples, trying to paste these together to compile what I'm trying to do is getting overwhelming. 

 

0 Likes
Message 16 of 24

Anonymous
Not applicable

I'm pretty close with the lsp. If you want to run an lsp on multiple files, this is a good tool:

https://sourceforge.net/projects/starbatch/

 

Here is my code so far, just try to save from one location to a specific location, I'm on the last key in:

 

(defun c:test ()

(command "_.purge" "_all" "*" "_no"
"_.setbylayer" "_all" "" "_yes" "_yes"
"_.layer" "_color" "253" "*" ""
"_.audit" "_yes"
"_.saveas" "2013" "S:\ENGINEERING\JOBS\16-006 GM Ramos\DRAWINGS\GK\GK XREF\" FILENAME)

)

 

0 Likes
Message 17 of 24

roland.r71
Collaborator
Collaborator

@Anonymous wrote:

So is it best to use an lsp for single file coding and script (.scr) for batching multiple drawings? Is this what you are telling me? The code I was running from script pro was simpler, but was also limited to basic key in commands. I realize I have much to learn, but there are thousands of codes out there as examples, trying to paste these together to compile what I'm trying to do is getting overwhelming. 


Yes and No. By default, you can't open & process other drawings from a LISP routine.

So most people use a script to open the drawings, load the lisp, execute it & open the next drawing. However, this is not absolutely needed. (To set SDI to 1 is!)

 

For what you are doing, it makes no difference.

Whether you create a batch (called "script" *.scr) containing:

-purge
R
N

or a LISP script (*.lsp) containing:

 

(command "-purge" "R" "N")

Makes no difference to the end result.

 

Although using LISP is way more powerfull then just a script (batch), you should just use whatever suits you for a given situation & slowly try to expand your programming knowledge & the complexity of your work.

 

With ACAD there are many roads leading to Rome.

Some are better as others, but there's nothing wrong to stick to the path you know best.

0 Likes
Message 18 of 24

Anonymous
Not applicable

Thank you for the advice, that is very helpful as I've switched over from many years of Microstation programming. To my previous question above that, do you have any quidance? The code I posted one line up? I posted two comments in a row. 

0 Likes
Message 19 of 24

Anonymous
Not applicable

Sorry, let me just summarize my last question so you don't have to read everything. Code is working, just need to know how to save as in the last line. I'm saving from one directory to the specific one below, I want to use the existing drawing number that I will currently be in at all times. There are many different codes to grab this, but I'm not sure how to tie it in. 

 

(defun c:test ()

(command "_.purge" "_all" "*" "_no"
"_.setbylayer" "_all" "" "_yes" "_yes"
"_.layer" "_color" "253" "*" ""
"_.audit" "_yes"
"_.saveas" "2013" "S:\ENGINEERING\JOBS\16-006 GM Ramos\DRAWINGS\GK\GK XREF\" "dwgname")

)

 

 

0 Likes
Message 20 of 24

roland.r71
Collaborator
Collaborator

SAVEAS is a dialog command. Meaning that when invoked a dialog pops up which you need to use. These kind of commands are 'killing' for automation using LISP. (or even *.scr)

 

In some cases this can be circumvented by turning dialogs off (like: filedia 0, for "open")

 

BUT, the guys at AutoDesk where kind enough to add a second version for these commands, using commandline prompts, instead of dialogs.

 

Usually you need to add a - before the command, to get the version without dialogs.

just like you add a . for the oem version (in case the command was redifined by user)

and the _ sign for the non-localized version (original commands are in English, but are translated with various languages)

In this case however its not a - but a + sign.

Enter it at the commandline: +saveas

then it will ask for the format: dwG dwT dwS dxF Other (note the CAPS, those are shortcuts)

After that it asks for a version: 2013 2010 2007 2004 2000 R14

Finally it will ask for a path/filename to save to, with a dialog! (yeah, don't ask me why)

I already mentioned it, but to prevent this file dialog to pop-up, you must set the sysvar FILEDIA to 0 first.

For that use (command "filedia" "0") -or use- (setvar "filedia" 0) <- prefered

To save the file as a ACAD 2013 DWG file:

(command "_.+saveas" "G" "2013" "C:/path/to/file.dwg")

 ; turn back on file dialogs with

(setvar "filedia" 1) -or- (command "filedia" "1")

 

So you need to adjust your current _.saveas line to:

(setvar "filedia" 0)
(command "_.+saveas" "G" "2013" "C:/path/to/file.dwg")
(setvar "filedia" 1)

...with the correct path & filename, of course.

Note: This does mean you need to know what the filename should be, as you need to supply one. 

0 Likes