Network Your AutoCAD with AutoLISP - Synergis Your AutoCAD with AutoLI… · Network Your AutoCAD...

Preview:

Citation preview

Network Your AutoCAD with AutoLISPRobert Green

Network Your AutoCAD with AutoLISPRobert Green

Robert Green bio …

Mechanical engineer turned computer geekCAD user since 1985CAD manager since 1989LISP hacker since Release 9 CADENCE/Cadalyst author since 1998Still trying to learn everything I can …

Network Your AutoCAD with AutoLISPRobert Green

Some fundamentalsLISP – LISt ProcessorShould you still use LISP?Everything is in lists

Network Your AutoCAD with AutoLISPRobert Green

Key files and why they matter …

LSPLoad in orderWhat to mess withWhat not to!

Network Your AutoCAD with AutoLISPRobert Green

All those wacky file names … ACAD20XX.LSP (system file – XX is version)ACAD.LSP (This is your file)ACAD20XXDOC.LSP (system file - XX is version)ACADDOC.LSP (This is your file)CUINAME.MNL (loads with CUI)So what does it all mean?

Network Your AutoCAD with AutoLISPRobert Green

What do they do, where they live … They load on startup of AutoCADThey load in a certain order (listed on previous slide)Some have code in them and some don’t (ACADDOC.LSP and ACAD.LSP don’t as an example)They reside in the SUPPORT folder …

Network Your AutoCAD with AutoLISPRobert Green

So what should I do … Use ACADDOC.LSP to get startedYou create your own ACADDOC.LSP It loads with every new drawingPut in in the SUPPORT folder and start hacking awayIf you mess up too bad, just delete!

Network Your AutoCAD with AutoLISPRobert Green

Make sure it works …

Create ACADDOC.LSPLoad from network?Verify operation

Network Your AutoCAD with AutoLISPRobert Green

Find the support folder … Use OPTIONS to find the folders …

Network Your AutoCAD with AutoLISPRobert Green

Add a network folder … Use OPTIONS to add it …

Network Your AutoCAD with AutoLISPRobert Green

Create the file … Use Notepad – not Word!Use (prompt “\nACADDOC.LSP loaded.”) as text

Network Your AutoCAD with AutoLISPRobert Green

Save the file … To the SUPPORT folderUse ACADDOC.LSP as the name

Network Your AutoCAD with AutoLISPRobert Green

Alternately … You can use APPLOAD to load filesYou can use STARTUP SUITE to load at each start

Network Your AutoCAD with AutoLISPRobert Green

Syntax Basics Lists and ArgumentsRules of AutoLISPAccessing the command lineSpecial characters

Network Your AutoCAD with AutoLISPRobert Green

Lists and Arguments (+ 20 30)

Here the + is a FUNCTION and the two numbers are ARGUMENTS

(command “line” “0,0” “1,1” “”) Here COMMAND is the function, all others are ARGUMENTS

(getvar “dimscale”)Which is the function? The argument?

Network Your AutoCAD with AutoLISPRobert Green

What’s Going On Here?(command “viewres” “y” “5000”) (command “-color” “BYLAYER”)(command “-linetype” “set” “BYLAYER” “”)(command “menu” “menuname.mnc”) (command “viewres” “y” pause)

That’s not so bad …intuitive actually …

Network Your AutoCAD with AutoLISPRobert Green

User functions Speed for the userLower support for youA win-win scenarioLet’s put everything we’ve learned into action to build some functions.

Network Your AutoCAD with AutoLISPRobert Green

User Function Examples

(defun C:ZA ()(command “.zoom” “a”)(princ)

)

Network Your AutoCAD with AutoLISPRobert Green

User Function Examples

(defun C:ZA ()(command “.zoom” “a”)(princ)

)

Network Your AutoCAD with AutoLISPRobert Green

User Function Examples(defun C:VR ()(command “viewres” “y” “5000”)

)

* Note that I left out PRINC?

(defun C:BL ()(command “-color” “BYLAYER”) (command “-linetype” “set” “BYLAYER” “”) (princ)

)

Network Your AutoCAD with AutoLISPRobert Green

Fillet Zero FunctionFillet Zero(defun c:fz () (setvar “filletrad” 0.0)(command “.fillet” pause pause)(princ)

)

* What have I not done in this function?

Network Your AutoCAD with AutoLISPRobert Green

Improved Fillet Zero (defun c:fz () (setq old_filletrad (getvar “filletrad”))(setvar “filletrad” 0.0) (command “.fillet” pause pause) (setvar “filletrad” old_filletrad)(princ)

)

* Note how we store and recall the FILLETRAD so the function puts things back the way they were!

Network Your AutoCAD with AutoLISPRobert Green

Auto Purge FunctionAuto Purge(defun c:atp () (command “-purge” “a” “*” “n” “.qsave”)(princ)

)

(defun c:atp () (command “-purge” “b” “*” “n” “.qsave”)(princ)

)

Network Your AutoCAD with AutoLISPRobert Green

Take Control of the Command Set …

UndefineDot formRedefine AlertsCMDECHO

Network Your AutoCAD with AutoLISPRobert Green

Undefining … (command “.undefine” “LINE”)(command “.undefine” “TORUS”)

Don’t want them messing with a command? Just undefine it …

Now you can SUBTRACT from the AutoCAD Command set in your ACADDOC.LSP file.

Network Your AutoCAD with AutoLISPRobert Green

The DOT form … Invoke commands like this: .LINENote the dot “.” character?This allows you to invoke a command whether it has been undefined or not!This is our little secret right ...

Network Your AutoCAD with AutoLISPRobert Green

Redefining … (command “.redefine” “LINE”)(command “.redefine” “TORUS”)

Want to be sure that a command is active?Just redefine it …

Now you can UNSUBTRACT from the AutoCAD Command set with ease.

Network Your AutoCAD with AutoLISPRobert Green

Undefining revisited … What if your users find out about REDEFINE and start REDEFINING your UNDEFINES?

Just undefine the redefine like this:

(command “.undefine” “REDEFINE”)

That’ll stop users from redefining …

Network Your AutoCAD with AutoLISPRobert Green

Redefining … You can undefine a command and redefine it like this:

(command “.undefine” “TORUS”)

(defun C:TORUS ()(alert “Don’t use that command!”)(princ)

)

Now you do whatever you want!

Network Your AutoCAD with AutoLISPRobert Green

What Does This Do?(command “.undefine” “QSAVE”)

(defun c:qsave () (command “-purge” “b” “*” “n”)(command “.qsave”)(princ)

)

Network Your AutoCAD with AutoLISPRobert Green

Alerting the user … You can send a message to the user like this:

(alert “Message goes here”)

Network Your AutoCAD with AutoLISPRobert Green

Command echo (CMDECHO) Run in STEALTH mode like this:(defun C:BL ()

(setvar “cmdecho” 0) (command “-color” “BYLAYER”)(command “-linetype” “set” “BYLAYER” “”) (setvar “cmdecho” 1)

(princ))

* SETVAR is on or off so no need to store it’s value

Network Your AutoCAD with AutoLISPRobert Green

Automate the Folders …Registry readsRegistry writesImpose profiles without actually using profilesLet’s make the leap

Network Your AutoCAD with AutoLISPRobert Green

Locate the registry keys

Use REGEDITFind the versionFind the profile

Network Your AutoCAD with AutoLISPRobert Green

Locate the registry keys

Find the key

Network Your AutoCAD with AutoLISPRobert Green

Override with code

(setenv "PrinterConfigDir" "C:\\TEMP\\acad\\Plotters")

Network Your AutoCAD with AutoLISPRobert Green

Synch the Folders …Network filesCopy to local folderConsistency No permissions hassles

Network Your AutoCAD with AutoLISPRobert Green

Add this to ACADDOC.LSP

(command "shell" "robocopy \\\\server\\folder c:\\temp /e")

Where \\\\server is your server locationAnd \\folder is the full path to the files

Let’s see an example:

Network Your AutoCAD with AutoLISPRobert Green

Compile and remote load code …VLIDELoad operations

Network Your AutoCAD with AutoLISPRobert Green

VLIDE environment … You can write code in the VLIDE window like this:

Network Your AutoCAD with AutoLISPRobert Green

Compile your code … Use the VLIDE environment like this:

(vlisp-compile ‘st “c:\\test\\myprog.lsp”)

You’ll get MYPROG.FAS as a result

Network Your AutoCAD with AutoLISPRobert Green

Load compiled code … Use a LOAD statement like this:

(load “c:\\test\\myprog.fas”)

Now your LSP code is secure!Be sure not to lose your LSP file though!

Network Your AutoCAD with AutoLISPRobert Green

Centralized code … (if (findfile "x:\\autolisp\\utils1.fas")

(load "x:\\autolisp\\utils1.fas")))

Network loaded compiled programs are the way to go for security.

Load these files from your ACADDOC.LSP

Network Your AutoCAD with AutoLISPRobert Green

Wrapping up: ResourcesThe course handout (it has extra coverage)CAD-Manager.com/su password = synergisEmail: rgreen@CAD-Manager.comCadalyst Magazine (Hot Tip Harry)Developer’s HelpThe Garden Path example

Network Your AutoCAD with AutoLISPRobert Green

Thank you and keep in touch!

Robert Greenrgreen@CAD-Manager.com

Recommended