How to create LISP routines for drawing setup.

BricsCAD Lisp

Migrating from AutoCAD

When you start migrating to BricsCAD, you’ll find that there are virtually no differences in BricsCAD LISP versus OtherLISP. Your code loads and runs, and the functionality is identical. The primary differences when “porting” your apps will be a few minor setup steps, Command line structure, and possibly file locations. To start, let's review the setup and launch of your existing programs in BricsCAD.

If you are auto-launching your programs using Acad.lsp or Acaddoc.lsp, you will need to rename them as on_start.lsp or on_doc_load.lsp respectively. The best solution is to use on_doc_load.lsp, rather than on_start.lsp.

BricsCAD Settings Panel
To enable auto-launching your program set the ACADLSPASDOC system variable to 1 or simply toggle the option in Settings / Program options / System:
It is also recommended that you spend a little time familiarizing yourself with the Options/Setting menu in BricsCAD, as there are settings and options specific to BricsCAD environment.
The next step in the settings dialog would be to add your support folders to the Files search path, which is found within Settings / Program Options / Files:
It is recommended to use the support paths added as in the manner above and/or located one level below the current project folder. It is a good practice to use a findfile function to validate the file exists before proceeding with your commands, as in the method below:
(if (findfile "custom-code.lsp") (load "custom-code")) 
This is one of the methods you can use to demand-load your functions within the on_doc_load.lsp file:

This is one of the methods you may use to demand-load your functions within the on_start.lsp file:

The DEFUN command (DEfine FUNction) is the same name as the function inside your on_doc_load.lsp file and that function redefines the calling function when it’s launched.

Drawing Setup Lisp Routines for BricsCAD

Code sample
The below simple drawing setup routine could be adapted to your needs with minimal changes.
(defun C:MYSETUP ( / )                    ;define the command line function "mysetup"
  (setvar "cmdecho" 0)
  (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
  (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
  (setvar "cmdecho" 1)
Each individual line in this drawing setup routine could have been typed in at the command line in BricsCAD. 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 you don’t include defun, the program will run once, when you load it by its file name. Using defun and loading this routine within your lsp, will make the function available at any time by typing its function name: mysetup.

The commands with preceding dashes (e.g. -layer and -purge) are used to prevent the command’s dialog boxes from launching.

Note: Always use English command and options names to avoid fails on localized (non-English) BricsCAD versions.

A good practice when opening files from others is to execute an audit followed by a purge to remove any unused items.

Purge command could be run multiple times to remove nested items. See the below example:

(repeat 3 (command "-purge" "all" "*" "n"))
Note: 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 it’s easier to read when each line stands alone.

The setvar calls for setting a system variable to a specified value, e.g. style settings for dimensions and text, unit settings, etc.

The maxsort setting was introduced for two reasons.
  1. Maxsort controls the maximum number of dialog box entries that will be sorted.
  2. The default value 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.
A more versatile Layer Maker for your drawing setup routine
The following example could be added to your version of mysetup.lsp. By creating a simple text file “Layers.txt” with an editor like Notepad, you 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. You can place your default layer setup text file in the support path and place alternative versions of it in individual project folders.

(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