2D Drafting 3D Modelling BIM Mechanical Applications

7 Tips for LISP Programming - Customizing BricsCAD® – P25

By Ralph Grabowski 7 min 9 Ağustos 2020
7 Tips for LISP Programming - Customizing BricsCAD® – P25

In the last few posts we've looked at most powerful method available to "non-programmers" for customizing BricsCAD --- the LISP programming language. In this post, I'm going to conclude our LISP coding with 7 tips for writing LISP code.

Tip #1. Use an ASCII Text Editor

BricsCAD has it's own, built-in LISP editor: BLADE. To access it simply enter BLADE into the Command line.

LISP code must be written in plain ASCII text --- no special characters and no formatting (like bolface or color) of the sort that word processors add to the file. When you programming  LISP with, say, Word, then save as a .doc-format file (the default), BricsCAD will simply refuse to load the LISP file, even when the file extension is .lsp.

In an increasingly Window-ized world, it is harder to find a true ASCII text editor. There is one, however, supplied free by Microsoft with Windows called Notepad, which you'll find in the \windows folder. Do not use Write or WordPad supplied with Windows. While both of these have an option to save in ASCII, you're bound to forget sometimes and end up frustrated. Linux provides the excellent Text Edit (aka gedit) text editor, while Mac has TextEdit.

Almost any other word processor has an option to save text in plain ASCII, but not by default. Word processors have a number of different terms for what I mean by "pure ASCII format." Word calls it "Text Only"; WordPerfect calls it "DOS Text"; WordPad calls it "Text Document"; and Atlantis calls it "Text Files." You get the idea.

Tip #2: Loading LSP Code into BricsCAD

To load the LISP code into BricsCAD, you use the load function. Here's an example where points.

lsp is the name of the LISP routine:

(load "points")

You don't need to type the .lsp extension.

When BricsCAD cannot find points.lsp, you need to specify the folder name by using either a forward slash or double backslashes --- your choice:

(load "\\BricsCAD\\points")

After you've typed this a few times, you'll find it gets tedious. To solve the problem, write a one-line LISP routine that reduces the keystrokes, like this:

(defun c:x () (load "points"))

Now anytime you need to load the points.lsp routine, you just type X and press Enter, as follows:

x

Under Windows, you could also just drag the .lsp file from the File Manager into BricsCAD. Note that the code moves one way: from the text editor to BricsCAD; you cannot drag the code from BricsCAD back to the text editor.

Tip #3: Toggling System Variables

One problem in programming is: How to change a value when you don't know what the value is? In BricsCAD, you come across this problem with system variables, many of which are toggles. A toggle system variable has a value of 0 or 1, indicating that the value is either off (0) or on (1). For example, system variable SplFrame is by default 0: when turned off, splined polylines do not display their frame.

No programmer ever assumes that the value of SplFrame is going to be zero just because that's its default value. In the case of toggle system variables, there two solutions:

  1. Employ the if function to see if the value is 0 or 1.
  2. Subtract 1 and take the absolute value.

Tip #4: Be Neat and Tidy

Remember, your mother told you to always pick up your things. This problem of setting system variables applies universally. When your LISP routine changes the values of system variables, it must always set them back to the way they were before the routine began running.

Many programmers write a set of generic functions that save the current settings at the beginning of the routine, carries out the changes and then restores the saved values at the end of the routine. Here's a code fragment that shows this, where the original value of SplFrame is stored in variable SplVar using getvar and then restored with setvar:

(setq splvar (getvar "splframe"))
...
(setvar "splframe" splvar)

Tip #5: UPPER vs. lowercase

In (almost) all cases, the LISP programming language doesn't care if you use UPPERCASE or lowercase for writing the code.

For legibility, there are some conventions:

  • LISP function names in all lowercase.
  • Your function names in Mixed Case.
  • BricsCAD variables and command names in all UPPERCASE.

As I said, LISP doesn't care and converts everything into uppercase in any case. It also strips out all comments, excess white space, tabs, and return characters. The exception is text in quote marks, such as prompts, which are left as is.

There are two exception where LISP does care: when you are working with escape codes and the letter T.

Escape codes are used in text strings and must remain lowercase. For example, \e is the escape character (equivalent to ASCII 27) and \t is the tab character.

Note: they use backslashes; it is for this reason that you cannot use the backslash for separating folders names back in Tip #2. LISP would think you were typing an escape code.

And some functions use the letter T as a flag. It must remain UPPERCASE.

Tip # 6: Quotation Marks as Quotation Marks

As we have seen, the LISP programming language uses quotation marks ( " ) for strings. Thus, you cannot use a quotation markto display quotation marks and inches, such as displaying 25 inches as 25".

The workaround is to use the escape codes mentioned above in Tip #5, specifically the octal code equivalent for the ASCII character for the quotation mark. Sound complicated? It is. But all you need to know is 042. Here's how it works:

First, assign the strings to variables, as follows:

(setq disttxt "The length is ")
(setq distval 25)
(setq qumark "\042")

Notice how I assigned octal 042 to variable qumark. The backslash tells LISP the numbers following are in octal. Octal, by the way, is half of hexadecimal: 0 1 2 3 4 5 6 7 10 11 12 ... 16 17 20 21 ...

Then concatenate the three strings together with the strcat function:

(strcat distxt distval qumark)

To produce the prompt:

The length is 25"

Tip #7: Tabs and Quotation Marks

Vijay Katkar is writing code for a dialog box with a list box. He told me, "I want to display strings in it --- just like the dialog box displayed by the Layer command. I am able to concatenate the values and print the strings but there is no vertical alignment, since the strings are of different lengths. I tried using the tab metacharacter (\t) in the string but it prints the literal '\t' in the list box. Is there any way I can get around this problem?"

I recall a similar problem: How to display quotation marks or the inches symbol within a text string?

For example, I have a line of LISP code that I want to print out as:

The diameter is 2.54"

Normally, I cannot use the quotation (") character in a string. LISP uses the quotation as its string delimiter to mark the beginning and ending of the string. In the following line of code:

(prompt "The diameter is 2.54"")

LISP sees the first quotation mark as the start of the string, the second quotation as the end of the string, and the third quotation mark as an error. The solution is the \nnn metacharacter. This lets me insert any ASCII character, including special characters, such as tab, escape, and quotation marks.

The workaround here is to use the ASCII code for the quotation mark, \042, like this:

prompt "The diameter is 2.54\042")

Similarly, Vijay needs to use the \009 metacharacter to space the text in his dialog box. And, in fact, that worked: "According to what you had told me, I used the same and it worked."

What's next?

That's all for LISP programming! Join me next time when I'll show you how to create custom dialog boxes in BricsCAD.

Start LISP Programming Today

Permanent or subscription licenses that work in all languages, in all regions.

19 Mart 2024 8 min

What's New in BricsCAD® V24.2

BricsCAD® V24.2 is chock full of new and enhanced features for 2D drafting, 3D modeling, civil, survey, BIM, and mechanical workflows. With this release, the Bricsys team included several new features and improvements to help you accelerate your time to deliverable and provide you with a familiar CAD experience. So, let's dive in and explore what's new in BricsCAD V24.2!

10 Mart 2024 6 min

The path to Scan to BIM automation with BricsCAD® BIM V24

The new automation tools included in BricsCAD® BIM V24 make the Scan-to-BIM process easier and more efficient! If you want fast and accurate reality capture of existing objects, these tools will be a great addition to your arsenal. We've added tools like the Point Cloud Classifier, Point Cloud Detect Rooms, Point Cloud Fit Rooms, and Normal Calculation for structured point clouds to help you create digital twins for quantity takeoff, create accurate building plans, and with facility management.

Follow us on social media