Welcome back! I’m hope that after reading my previous blog posts, you’ve decided to start writing some Lisp routines to help simplify your CAD work. But, if you haven’t yet, where is the best place to start? I think that the best routine to begin with is one that automates your drawing setup. If you’re like me, you probably change a bunch of settings every time you start a new drawing. Most of these settings: layers, styles, and system variables, can be saved in your template drawing (.dwt). However, when you open a drawing that was started by someone else, you’re never sure exactly how it was set up.
Because of this, files from an outside source often force you to spend a few minutes adjusting your preferences for each individual drawing file. On the other side of the fence: if you’re sending files to a client, you want to work “your way” while creating the drawing, but then adjust the settings to conform to your client’s standards. The first Lisp routine example below is a framework for automating the drawing setup process.
A Simple Solution
This simple drawing setup routine might work for you, with just minimal changes. Here’s the code with comments and detailed notes in the comments below:
(defun c:mysetup () ;define the command line function “mysetup” (command “audit” “y”) ;audit current drawing (command "-purge" "all" "*" "n") ;purge all unused styles, etc (command "-layer" "m" "1" "c" "1" "" "") ; create layer “1” color “1” (command "-layer" "m" "2" "c" "2" "" "") (command "-layer" "m" "3" "c" "3" "" "") (command "-layer" "m" "4" "c" "4" "" "") (command "viewres" "y" "20000") ;set view resolution to maximum (command "facetres" "10") ;set facet resolution to max (setvar "filedia" 1) ;turn file dialogs on (setvar "attdia" 1) ;turn attribute dialogs on (setvar "attreq" 1) ;enable attribute editing on placement (setvar "cecolor" "bylayer") ;set current color bylayer (setvar "clayer" "0") ;set current layer to “0” (setvar "maxsort" 5000) ;set maximum layer sort to 5,000 (command "zoom" "e" "zoom" "0.95x") ;zoom extents, then out a little ) ;end the function
How does this drawing setup routine work?
First, you’ll see that each individual line in this drawing setup routine could have been typed in at the command line in BricsCAD – and you probably do that today. By wrapping them into a Lisp routine, the defun (define function) makes the drawing setup routine “callable” from a menu or by typing its name on the BricsCAD command line.
- If we don’t include the defun, the program will run once, when you load it by its file name. Using the defun, and loading this routine within your lsp will make the function available at any time by typing its function name: mysetup.
Next, look at the command strings themselves. The commands with the preceding dashes (e.g. -layer and -purge) are used to prevent the command’s dialog boxes from launching. As a first step when opening files from others, I usually execute an audit followed by a purge to remove any unused items.
In the case of the purge command, I often run it multiple times to remove nested items. Here’s a bit of added Lisp logic that can make that three-peat happen:
(repeat 3 (command "-purge" "all" "*" "n"))
Notice that purge is done before adding the new, empty layers… as they would be removed if we did it after!
The command calls for setting layers could be combined into a single command statement, but I think that it’s easier to read when each line stands alone. Filedia is a global setting (a.k.a. “system variable”), so it is not drawing dependent. I add these types of settings to routines like this to ensure that I get the behavior I expect from the new drawing.
I included the maxsort setting for two reasons. First, to show that some settings require a numeric value instead of a text string (notice the missing quotes in maxsort’s command line). Maxsort controls the maximum number of dialog box entries that will be sorted. The second reason is that the “factory default” for maxsort is 1,000. With today’s computers and often found larger project files, that number can be too low. If you open drawings where the layer names won’t sort into alphabetical order, the default maxsort value could be the reason!
Additional setvar “calls” include style settings for dimensions and text, unit settings, etc…
A more versatile Layer Maker for your drawing setup routine
I’ll leave you for now with the following example, which could be added to your version of mysetup.lsp. By creating a simple text file with an editor like Notepad, we can add a list of layer names to be created when the routine is run. This is an alternative to the hard-coded process shown above. By using the findfile function, BricsCAD will first search the current directory, then sequentially through your defined support folders to find the layers.txt file. This being known, you can place your default layer setup text file in the support path and place alternative versions of the in individual project folders. This way, you can support multiple layer layouts easily.
(if (not (findfile "Layers.txt")) ;“not” = looking for no result from findfile (alert "Can't find Layers.txt!") ;No file? Yell at the user and exit! (progn ;Otherwise, do the stuff in the progn group (setq layerfile (open (findfile "Layers.txt") "r")) ;open layers.txt to "read" (while (setq layername (read-line layerfile)) ;while reading each line, store the text to layername (command "-layer" "m" layername "") ;make layer with the layername ) ;end the while loop (close layerfile) ;close the Layers.txt file ) ;end the progn ) ;end the if
Next time, we’ll take this setup program process a bit further, by adding some additional information to your external settings file and moving existing entities from one layer to another. Until then, happy coding!