How to Automate DraftSight Commands with AutoLISP

By Mike Walloch on

DraftSight provides us with many commands chock full of flexible options. These options allow us to create objects in lots of different ways for different situations. For example, the Circle command defaults to drawing a circle by picking a center point, followed by a point to set the radius of the circle. Other options include Center Diameter, 3 Point, 2 Point, Tangent Tangent Radius, and Tangent Tangent Tangent.

DraftSight Circle Command
DraftSight Circle Command Window

There are buttons in the Circle flyout on the Home ribbon for each of these options, but in reality there is only one Circle command. All the other buttons on the flyout do is automate entering command options at the keyboard. So keyboard entry is always an alternative to picking commands from menus, and is often faster as it involves less mouse travel and fewer clicks. Especially if you use aliases and other customization options to reduce the number of keystrokes needed to get the job done. Personally, I like keyboard shortcuts of one or two characters in length, and no more than three characters.

DraftSight makes available to us a wide array of customization options through the Application Programming Interface (API). These tools make it possible to customize the DraftSight user interface, create new commands, automate tasks, etc.  The API includes the following supported application languages:

  • C++
  • C++ COM
  • C#
  • VB.NET
  • JavaScript
  • AutoLISP
  • Visual LISP

In this article we’re going to look at options to automate DraftSight for existing commands using the AutoLISP programming language. Don’t worry, you don’t need to be a programmer to create and use some simple code. You can copy and paste directly from this article to use the commands I’m going to show you on your own machine, if you wish to do so. You can also copy, paste, and alter AutoLISP routines to create similar new ones to meet your specific needs.

The first thing you’ll need is a word processor or text editor application, like Notepad. Any application which will write files in the venerable .TXT format will do, since an AutoLISP file is just a text format file with a .LSP extension.

Any number of AutoLISP routines can be included in the same file. Once the file is loaded in an open DWG, all the new commands defined in the .LSP file will be available in the current drawing session. To load a .LSP file, type LOADAPPLICATION or click ‘Load Application…’ on the Manage tab of the ribbon menu. Then browse to the file you want to load and click ‘Open.’ That’s all there is to it.

Load Application
Load Application

Now let’s take a look at a simple AutoLISP routine.  The six lines of code below use DraftSight’s RICHLINE command to draw two parallel lines at a user specified distance from each other, offset to either side of a start and end point. It then uses the EXPLODE command to devolve the Richline object into two Line objects. This is really useful for drawing the profile of a shaft or hole along an existing centerline. (An AutoCAD compatible version of this routine would use the MLINE command instead of RICHLINE.)

;Parallel Lines – Richline Zero

(defun c:rz ()

  (setvar “cmdecho” 1)

  (command “richline” “j” “z” “s” pause pause pause “”)

  (command “explode” “l”)

)

The first line ‘;Parallel Lines – Richline Zero’ starts with a semicolon. This ‘remarks out’ all the remaining text on that line. Remark lines are useful for creating notes for other users, and your future self, about what the code is for.

All code for a single routine appears inside nested sets of parentheses. So, for every ‘(‘ you must have a corresponding ‘)’. The first one begins the routine, and the last one ends it. So the second line ‘(defun c:rz ()’ is where the code actually begins. The DEFUN function defines a new command name, which is whatever text follows the ‘c:’, and it’s not case sensitive. So, the new command will be activated by typing ‘RZ’ or ‘rz’ at the command prompt in DraftSight.

The set of parentheses ‘()’ following the new command name is for defining any new variables which might be needed by the routine. This one does not need any variables so we’re leaving it empty.

The third line of code ‘(setvar “cmdecho” 1)’ uses the SETVAR function to change the CMDECHO system variable to 1, or in other words to turn on command echoing within AutoLISP routines.  This will ensure command prompts and options are displayed correctly in the Command Window while the routine is running. If CMDECHO was set to 0 the routine would still work correctly, but the text in the Command Window would not look right.

The fourth line ‘(command “richline” “j” “z” “s” pause pause pause “”)’ is the main show in this routine. It issues the DraftSight command RICHLINE, followed by a set of keystrokes to set options, and pauses for user input. Spaces separate one command line entry or selection in the graphics area from the next. Here are the explanations of what each entry in this line of code means:

RICHLINE – Command Name

J – Justification option.

Z – Zero (or centered) position option.

S – Scale (distance between lines) option.

PAUSE – Waits for the user to enter a number for Scale and hit ENTER.

PAUSE – Waits for the user to pick a start point.

PAUSE – Waits for the user to pick an end point.

“” – Acts as if the user hit the ENTER key instead of picking a third point, in this case ending the RICHLINE command.

The fifth line of code ‘(command “explode” “l”)’ issues the EXPLODE command and uses the ‘L’ option to select the last entity or entities which were selected. In this case that was the Richline object created by the fourth line of code, resulting in the complex object becoming two basic Line objects.

The sixth line of code is simply the end parenthesis which closes this routine. In truth, breaking this routine into multiple lines is not necessary. The system would process it just the same if there were no line breaks from the DEFUN function all the way to the final closing parenthesis. Breaking it into multiple lines just makes it easier for us humans to work with and read. Same with the spaces at the start of the third, fourth, and fifth lines.

Here is what the Command Window would look like, along with the resulting pair of parallel lines, if you did the same thing as the above AutoLISP routine just by using keyboard entry in DraftSight:

Command Window
Command Window

Here is the exact sequence of entries to finish the process using the RICHLINE and EXPLODE commands, with a spacing of 1 unit between the lines:

RICHLINE

J

Z

S

1

<Pick Point>

<Pick Point>

<Enter>

EXPLODE

L

<Enter>

Here is the sequence of entries to do the same job using the new RZ command:

RZ

1

<Pick Point>

<Pick Point>

That’s 20 typed characters, including two command names and five options with out-of-the-box DraftSight, vs three typed characters using the RZ AutoLISP routine! Sure, it doesn’t save you a ton of time from a single use. But once the program is written you can use it over and over again, day after day, year after year. The compound time savings you can get by using customizations like this are truly immense.

Now, let’s return to the CIRCLE command and its options mentioned at the beginning of this article. Below are a few AutoLISP routines to create Command Line shortcuts for the different ways of drawing a circle, other than the default Center Radius option. Loading the code will give you five new commands: CD, C2, C3, TTT, and TTR. Notice each of these is much simpler than the RZ command.

;Circle Diameter

(defun c:cd () (command “circle” pause “d”))

;Circle 2 Points

(defun c:c2 () (command “circle” “2p”))

;Circle 3 Points

(defun c:c3 () (command “circle” “3p”))

;Circle TTT

(defun c:ttt () (command “circle” “ttt”))

;Circle TTR

(defun c:ttr () (command “circle” “ttr”))

Automate DraftSight Conclusion

Getting comfortable with keyboard entry in DraftSight can save you a lot of time over the course of your day, and a ton of time over the course of your career. Not only does it reduce mouse travel and number of clicks, but it also makes it easy to create your own shortcuts using several different methods. AutoLISP routines are a robust and flexible tool for doing just that. We’ll explore other options in future articles.