LISP routines for drawing setup

About

This article describes how to create LISP routines for drawing setup.

Migrating from AutoCAD®

When you start migrating to BricsCAD, 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 programs are a few minor setup steps, Command line structure, and possibly file locations.

For more information, see the Migrating LISP files article.

Reviewing the setup and launch of existing LISP programs in BricsCAD

If you are auto-launching (load and run) your LISP programs using acad.lsp or acaddoc.lsp, you need to rename them as on_start.lsp or on_doc_load.lsp respectively.

Note: The best solution is to use on_doc_load.lsp, rather than on_start.lsp.

To load your on_start.lsp or on_doc_load.lsp files for every new drawing, set the ACADLSPASDOC system variable to 1.

Note: For other files, you can use the APPLOAD or LSP (Express Tools) commands.

It is also recommended that you spend a little time familiarizing yourself with the Settings dialog box in BricsCAD, as there are settings and options specific to BricsCAD environment.

The next step would be to add the paths of your support folders to the Support files search path in the Settings dialog box (SRCHPATH system variable).

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 that 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:

(autoload "custom-code" '("MyCommand" "MyOtherCommand"))

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

(defun c:MyCommand ()
  (load "custom-code")
  (c:MyCommand)
)

The function defined with defun (DEfine FUNction) has the same name as the function inside your custom-code.lsp file. When the command is launched for the first time, loading custom-code.lsp replaces this placeholder definition with the actual one, which is then executed.

Creating drawing setup LISP routines for BricsCAD

Note: The LSP (Express Tools) command lists all BricsCAD LISP commands, functions and variables.
Code sample
The below simple drawing setup routine could be adapted to your needs with minimal changes.
(defun C:MYSETUP ( / oldecho)                   ;define the command line function "mysetup"
  (setq oldecho (getvar "cmdecho"))             ;save the current cmdecho value
  (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" oldecho)                   ;restore the original cmdecho value
  (princ)
)
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 at the BricsCAD Command line.

If you do not include defun, the program will run once, when you load it by its file name. Using defun and loading this routine within your on_doc_load.lsp file makes the function available at any time by typing its function name: mysetup.

The commands with preceding dashes (for example, -layer and -purge) are used to prevent the command dialog boxes from launching.

Note: Always prefix command and option names with an underscore "_", for example, "_-purge", so that they are interpreted as English names. This keeps your routines working on localized (non-English) BricsCAD versions.

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

The PURGE command could be run with the BAtch all option, to remove nested items:

(command "_-purge" "_BA")
Note: The purge operation is performed before adding the new empty layers because if it were carried out afterward, the new layers would be removed.

The calls to the command function that create the layers could be combined into a single command statement, but it is easier to read when each line stands alone.

The setvar function sets a system variable to a specified value, for example, style settings for dimensions and text, unit settings, etc.

The MAXSORT system variable controls the maximum number of entries that are sorted in dialog boxes and lists. Its default value is 1,000, which can be too low for today's larger project files.

If you open drawings where the layer names will not 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 inside the C:MYSETUP function of 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 folder 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 found: warn the user and exit
  (progn                                                ;otherwise, run the statements in the progn group
    (setq layerfile (open (findfile "Layers.txt") "r")) ;open Layers.txt for reading
    (while (setq layername (read-line layerfile))       ;read each line and store the text in layername
      (if (/= layername "")                             ;skip empty lines
        (command "_-layer" "_m" layername "")           ;make a layer named layername
      )
    )                                                   ;end the while loop
    (close layerfile)                                   ;close the Layers.txt file
  )                                                     ;end the progn
)                                                       ;end the if

For more information, see the BricsCAD V27 Developer Reference web page: Developer documentation.