In Bootstrap AutoCAD Deployments for Customizations Part 3 I told you the bootstrap process adds only one file, acad.lsp, to the deployment. You won’t ever edit it, and any files that may need to be edited will be located on the network and edited outside of the deployment. This will make the IT department happy.
A brief explanation of AutoLISP file
AutoLISP® files are text-based files (.lsp instead of .txt). They can be edited in Notepad or your favorite text editor, but I recommend using the Visual LISP® editor built into AutoCAD® software and accessed with the command VLIDE. One reason I like the Visual LISP editor: It color-codes the file syntax. See Figure 1.
The code includes a lot of parentheses (I’ve heard LISP referred to, affectionately, as standing for Lost In Stupid Parentheses) plus AutoLISP functions (those odd bits of text that aren’t normal words) and normal words. (I’ve written so much AutoLISP code that I overuse parentheses in my normal writing, much to the despair of my editors.)
Never written AutoLISP code? Don’t worry. I will explain everything clearly and you can always reach out to me if you have issues.
The only acad.lsp file you need for bootstrapping AutoCAD
I advocate placing customizations on the network when possible. The locally deployed acad.lsp file placed in %ProgramFiles%AutodeskAutoCAD 2016Support should do no more than locate another .lsp file on the network and execute it.
This, in essence, is bootstrapping: Our acad.lsp file will be deployed with AutoCAD—but its sole purpose is to find, and execute, another .lsp file, bootstrap.lsp, that is located on the network.
Trusted locations, recently introduced to AutoCAD, complicate things a bit. The acad.lsp file not only has to load the required code; it also adds the code’s location to trusted locations.
All this is easily accomplished and does not violate the AutoCAD security model because it takes place in a trusted location.
Ready? OK, deep breath. Below is the code for the acad.lsp file. Simply copy it into your text editor and save it as “acad.lsp” to your My Documents folder for now.
(princ "nLoading bootstrap version of acad.lsp... ")
(setq myFile "p:autocad_2016bootstrap.lsp")
(cond ((findfile myFile)
(setq oldPaths (getvar "TrustedPaths"))
(setq newPaths (strcat (vl-string-right-trim ";" oldPaths)
(cond ((> (strlen oldPaths) 0) ";") (""))
(vl-filename-directory myFile)
""))
(setvar "TrustedPaths" newPaths)
(load myFile)))
(princ "done.")
(princ)
That wasn’t so scary, was it?
Note: If you are using Notepad, enclose the filename in double quotes so Notepad does not add the .txt extension. However, if you are using the Visual LISP editor, .lsp is automatically used. See Figure 2.
Note: AutoLISP usually doesn’t care whether text is lower case or in caps except when you display something at the AutoCAD command prompt. I use capitalizations to help make the code clearer.
OK, now let’s talk through each line in detail.
A detailed look at the acad.lsp code for bootstrapping AutoCAD
(princ "nLoading bootstrap version of acad.lsp... ")
This line prints “Loading bootstrap version of acad.lsp… ” to the AutoCAD command prompt. I intentionally added a blank space to the end of the text so that the cursor does not blink right after the ellipsis. This is how most AutoCAD commands do their prompts. The “n” places the text on a new line on the AutoCAD command prompt.
Note: In one of the few AutoLISP cases where capitalization matters, the “n” must be lower case.
(setq myFile "p:autocad_2016bootstrap.lsp")
This line sets the location and filename for the bootstrap code—the one that will be available outside of the deployment created by the IT department.
Note: This is the only line of code in this file that you need to edit for your own use!
I named the bootstrap code file bootstrap.lsp and I suggest you do the same. In the above example, the network location for the file is p:autocad_2016. Your actual location will vary.
“Wait a minute!” you may be saying. “The network location is p:autocad_2016 but there are two backslashes in the code.” Is that a mistake? No.
For every normal backslash in your path, the AutoLISP code requires two backslashes. This is because the backslash is a special character to AutoLISP code. For example, if the bootstrap.lsp file is located on your network at m:autocadprograms, then the code needs to use “m:autocadprogramsbootstrap.lsp” in the double quotes. Paths can have spaces, so “s:autocad 2016codebootstrap.lsp” is valid, too.
(cond ((findfile myFile)
This line checks to see if the bootstrap.lsp file can be found in the location you provided. If not, no problem: AutoCAD will not attempt to alter the environment. This is part of the beauty of bootstrapping. If a special AutoCAD environment cannot be supported because the bootstrap code is missing, out-of-the-box AutoCAD continues to load without issue or errors.
Note: The opening parentheses in (cond ((findfile myFile) won’t have their matching closing parentheses until later in the code.
(setq oldPaths (getvar "TrustedPaths"))
This gets the original setting for the AutoCAD system variable TrustedPaths and saves it to a variable called “oldPaths.”
(setq newPaths (strcat (vl-string-right-trim ";" oldPaths)
(cond ((> (strlen oldPaths) 0) ";") (""))
(vl-filename-directory myFile)
""))
These four lines work together to add the bootstrap.lsp file path to the end of any current trusted locations. The first line strips out any semicolons (;) at the end of the old paths. The second line checks to see if there are any old paths, and, if there are, puts a semicolon on the end. The third line adds the path to the bootstrap.lsp file. The last line adds a trailing backslash to the end of our new path because that’s how AutoCAD stores them.
You may be asking, “Why strip out any trailing semicolons in the first line and then just add one back in the second line?” Answer: To avoid multiple semicolons at the end of the old paths before adding our new path. Does it harm anything to have multiple semicolons? Not at this time. But it is sloppy to let semicolons proliferate.
(setvar "TrustedPaths" newPaths)
This sets the AutoCAD system variable to the new paths, which we set based on the old paths plus our new path.
(load myFile)))
This line loads the found bootstrap.lsp file we specified at the beginning of the code. (Again, if the file cannot be found, this loading attempt will never occur, so there will be no error for the user to see.) The two closing parentheses (load myFile))) complete the opening parentheses earlier in the code.
(princ "done.")
This line will print “done.” at the AutoCAD command prompt, whether the bootstrap.lsp file is found or not. Also note there is no new-line character “n” at the beginning. This is intentional, so the “done.” prompt goes at the end of the “Loading bootstrap version of acad.lsp… ” prompt. Here’s what you’ll see:
Command: Loading bootstrap version of acad.lsp… done.
(princ)
This provides a “clean exit” to the code at the AutoCAD prompt.
Bootstrapped AutoCAD deployments are easy—no?
Even though the acad.lsp file contains 12 lines of code, there is only one line you need to edit on your own. That’s not so bad, is it? Just remember to use two backslashes for every single backslash in the path you provide.
Stay tuned! Bootstrap AutoCAD Deployments for Customizations Part 5
My next post (Bootstrap AutoCAD Deployments for Customizations Part 5) looks at how we use the bootstrap.lsp file to add needed profiles to the user’s AutoCAD environment.
Here’re all the previous installments:
Bootstrap AutoCAD Deployments for Customizations Part 1