# Tutorial: 3d autocad shortcuts using autolisp



## Inactive

program: autocad 14+
level: advanced (for autocad, easy for programers)
time: quick

this example and others to follow, are designed to cut 3d modeling time in autocad. These files will be short and consist of autolisp instructions and autocad commands. 

here is a colour highlighted view of a LISP file that quickly changes your view to a southwest isometric. essentially all it is doing is using the command "vpoint" with a value of "-1,-1,1" and then zooming 0.9x., but all you'll type is 4 at the command line to run these commands









you can type these out in notepad and save as type txt document, while renaming the file extension as LSP. in this case the name of the file is 4.LSP.

you can also open these in a LISP editor through autocad. type VLISP on the command line and then browse to the file to open, or start a new one. you will get the colour highlights and automatic indents (tabs), which help you visually as you type.

lets start at the top then.

anything to the right of ; will be ignored completely! so use these to put in remarks for yourself or anyone else who might need to look at the file. these can be put anywhere in the file as you will see in the next LSP file i'll upload.

each LISP file starts the same and needs to be contained in parentheses i.e. ( ). hence the ) at the end and if you notice there are only 3 in the first line. also in the first line is
DEFUN C: [letters to run the file, in this case 4] (/ [variables seperated by a space])

there is only one variable is this script and it's 'osm' which will hold the value of the Object Snap Mode. i could have called the variable anything eg 'x' or 'objectsnapmode', but osm is short and to the point.

(defun C:4 (/ osm) explained above...

(setvar "cmdecho" 0) sets the system variable CMDECHO=0. it's not important for the moment.

(setq osm(getvar "osmode")) assigns a numeric value to the variable 'osm' which is the current object snap settings

(command "-osnap" "off") turns off object snaps

(command "vpoint" "-1,-1,1") the diagram shows you the view line trajectory









(command "zoom" ".9x") zooms out a little

actual view you'll get









(setvar "osmode" osm) sets the object snaps back to what it was.

(setvar "cmdecho" 1) sets the CMDECHO=1. again not important at the moment.

(princ) this isn't absolutely needed, but i've always put it in. 

) the last parethesis.

remember the code needs to be enclosed in these and this closes it up. all LISP files will end like this. i turn the object snaps off before i use the vpoint and zoom commands, because in autocad14 the view could snap to anywhere and was unpredictable.

so a quick look over it. 
DEFUN C: defines this function to be a {C} command line entry.
SETVAR changes an AutoCAD system variable. CMDECHO was the only one changed here.
SETQ (autolisp command) assigns a value to the variable after it - 'osm'.
COMMAND activates the AutoCAD commands that you type at the command line.

these will be used over and over in the next autolisp files. finally how do you load this file? type 'ap' or 'appload' on the command line, then browse to this file and click load. now press 4 and enter - does the view change? 

so now you can create the next 3 views, se, ne and nw isometric. you need to copy this file 3 times and rename them 5.LSP 6.LSP and 7.LSP. remember to change the number after DEFUN C:[5, 6 or 7 go here in each LISP file] and also change the vpoints to 
(1,-1,1) (1,1,1) and (-1,1,1) respectively. you can load these the same way as you did 4.LSP. so now all you need to type is 4, 5, 6 or 7 to switch to the sw, se, ne and nw isometric views. 

the next tutorial will show you how to load these automaticly everytime you start autocad and anymore autolisp files you write. one last thing the keystroke/s you assign to run a lisp file will over ride the autocad shortcut (ACAD.PGP file). e.g 's' is normally assigned to 'stretch' command, so if you had used 's' to tun one of these, then 's' will change the view and not 'stretch'.

lastly i have attached the LISP file to download.


----------



## Inactive

program: autocad 14+
level: advanced (for autocad, easy for programers)
time: quick


okay. i have attached the four LISP files for switching the views from southwest throught to northwest. i've also included a lisp file to automatically load these 4 files and you can add a line for each lisp file you want to load everytime you start or open a autocad file. there are a few things to do for this to happen.


1) create a folder (anywhere you want) and copy the lisp files you want to load along with the TOOLS.LSP into that folder.


2) you need to find and edit the acad.mnl file. it will be in the support folder and that will be either in the root of the autocad install folder or

C:\Documents and Settings\Administrator\Application Data\Autodesk\AutoCAD 2004\R16.0\enu\Support

administrator being replaced with whoever you log in as. you might need tell windows to 'show all hidden files' to find it. open it with wordpad and scroll down to the bottom of the file and put in this line

(load "tools")

just before the very last line which is

(princ)

it's always best to make a copy of any of autocad files before you edit them, but this entry is such a simple one.


3) start autocad and goto to tools>options and under the files tab click on the 
'support file search path'
then click on the 'add' button on the right and then browse to the folder that you created and copied the lisp files into. ok this and the next time you open/start a cad file the lisp files specified in the TOOLS.LSP file will be loaded.


so you've created a folder with all your custom lisp files copied in there. you've told the acad.mnl file to load TOOLS.LSP file (which in turn loads the rest of them) and you've added a search path to find the TOOLS.LSP file along with the other lisp files. 


at the moment the TOOLS.LSP is loading only four lisp files the 4, 5, 6 & 7 (note the semi-colon in front of the two lines loading 'q' and 'w'. this is to ignore them for this lesson). if you want to load another lisp file, just copy one of the line entries and replace the previous name with the new name. for example you have a lisp file called spring.lsp. then copy the line

(defun C:7 ()(load "7")(C:7))

and replace 7 with spring.

(defun C:spring ()(load "spring")(C:spring))


----------



## Inactive

assuming everyone (anyone) has gone through the above posts, this is the next one in the sequence. the first one which quickly changed the views but automatically does a zoom extents. that's what happens when you use the command vpoint. so if you are working in a large scene and you need to quickly save/restore a view, that's in close to an object, then this lisp file will do that. i use 2 lisp files using keystrokes q and w to quickly save/restore the views. i never found the need for more than 2.

so when you press q/w it will check to see if the view has already been saved. if it has already a view saved it will ask you to restore it or overwrite it. if you press 'v' it will overwite it with the current view or any other key will restore the one that is already saved. so the commands q and w will save a view called 'view1' and 'view2' respectively. if you have started a new drawing, or have deleted these manually, when you press q or w it will save the view and tell you so at the command prompt. the next time you press q/w it will ask you to restore or overwrite. 










the first 'if' uses the autolisp command 'tblsearch' to check the named views for a save called 'view1'. if this returns empty (nill) then it will carry out the instructions following the first 'progn', otherwise it will continue to the next progn and carry out those instructions. 'command' carries out an autolisp command, as you would type it out on the command line. 'prompt' just prints a line of text and the '\n' starts the text on a new line.

the second 'if' statement asks the user for a response. if the user types any other key than 'v' (= "v" input) then the named view will be restored. if they do type 'v' then it will overwrite the named view.

the lisp file 'w' is very similar to this one, only having 'view2' in place of 'view1' and w following the 'defun C:' statement


----------



## Inactive

*Slice command*

if, like me, you create all of the models in solids you'll find yourself using the slice command alot. i don't like the autocad long version, so i have a lisp file to slice an object(s) on the xy, yz or zx planes. this lisp file will slice on the zx plane, at 0,0,0 or a point of your choice and keep both sides of the sliced object(s).










object is the variable used for the objects picked in the scene. p1 is the point in which to slice them on. osmode is assigned the running object snap setting. again from the release14, i used to turn off the object snap before running the autocad commands. with some commands i found it doing strange things.
the 3rd line i set a mark in the undo command. i never really found it necessary, because the file does so little in the way of commands. essentially if you type 'undo' at the command line and the back - it will undo back to the last found mark.
(setq object (ssget))
the variable object will be set to whatever the ssget instruction returns with. so ssget lets you pick objects in the autocad scene. 

(setq p1 (getpoint "\nPick a point on the cutting plane <0,0,0>: "))
similarly p1 will be assigned a point returned with the instruction getpoint. i have <0,0,0> in the text, so if the user doesn't pick a point it will assume that the objects are to be cut at 0,0,0

(command "-osnap" "off")
here i turn off the running object snaps. i could have used the line
(setvar "osmode" 0)
as well. the latter is the binary way to set the object snaps, not as user friendly. note i turn off the object snaps after you pick a point in the scene. it'd be annoying if you were using object snaps and expected to snap to a point using them.

the if command i've covered above, so the first option will slice the objects at 0,0,0 and the second line will slice the objects on the point you picked. this is the part that saves you typing the options in on the command line yourself.

the last 3 lines are the usual, returning the system variables to the appropriate values. i have also attached 2 other lisp files that will slice on the xy and yz planes. these are virtually the same as above, only xy and yz are used in place of zx.

the next lisp file i will post is to set the running object snaps. a very short one, but i find it handy when you cancel the likes of this file midways and find your settings turned off. i also find it quicker to set some snaps rather that using the autocad dialogue box to set them.


----------



## alansnoog

*Re: Tutorial: AutoLISP*

hi,

Thank for this tutorial, I jusy learn and want to use it on my works,

I find these tips ware usefule.

Thanks.


----------

