AutoLISP (4 of 5)
- getcorner
- getdist
- getangle
- getorient
- getreal
- getint
- getstring
- getkword
- entsel
- osnap
- grread
- princ
- prin1
- terpri
- prompt
- alert
- textscr
- graphscr
- command
- command-s
- vl-cmdf
- entlast
- entnext
- entmod
- entdel
- entupd
- ssget
- ssname
- sslength
- ssmemb
- tblsearch
- getvar
- setvar
- redraw
- regen
- open
- close
- read-line
- write-line
- findfile
- getenv
- load
- vl-filename-base
- vl-filename-directory
- vl-filename-extension
- vlax-get-acad-object
- vlax-ename->vla-object
- vlax-vla-object->ename
getcorner
(getcorner base-point [prompt])
Asks for the opposite corner of a rectangle starting at the base point.
(getcorner p "\nOpposite corner: ")
While the cursor moves, the rectangle from the base point is shown. The corner can be picked or typed (20,10) and it is returned as a 3D point. With initget empty answers can be forbidden.
Example
Ask for the two corners of a window:
(setq p1 (getpoint "\nFirst corner: ")) (setq p2 (getcorner p1 "\nOpposite corner: "))
If 20,10 is typed for the second one, p2 is (20.0 10.0 0.0).
See also
getdist
(getdist [base-point] [prompt])
Asks for a distance: typed (in any length format) or with two points. Returns a real.
With a base point it only asks for the second point and shows a rubber band. The result is a real in drawing units. Use initget before it to forbid zero or negative values, or to allow Enter, which then returns nil.
As in AutoCAD, a typed point (3,4, @3,4, 10<45) counts as the first point: it asks for the second one and returns the distance between them, measured in 3D unless bit 64 of initget is set. getangle and getorient accept two typed points the same way; getreal does not (it answers that a numeric value is required).
(setq d (getdist "\nDistance: "))
getangle
(getangle [base-point] [prompt])
Asks for an angle: typed (in any angle format) or with two points. Returns radians.
(setq a (getangle "\nAngle: "))
Differences with AutoCAD
It does not take ANGBASE or ANGDIR into account (with the initial values the result is the same).
getorient
(getorient [base-point] [prompt])
Same as getangle.
Differences with AutoCAD
In AutoCAD it always measures from east, even when ANGBASE is not 0; in LinceCAD getangle does not use ANGBASE either, so both give the same result.
getreal
(getreal [prompt])
Asks for a real number (with a decimal point; the comma separates coordinates).
(setq k (getreal "\nFactor: "))
A typed integer is returned as a real (3 gives 3.0). The comma is not a decimal separator because it separates coordinates: to 2,5 the program answers with this warning and asks again:
Requires numeric value.
Enter with nothing returns nil, unless initget forbids it.
Example
Ask for a factor and use it:
(setq k (getreal "\nFactor: ")) ; with 2.5, 2.5 (* k 10) ; 25.0
See also
getint
(getint [prompt])
Asks for an integer between -32768 and 32767.
(initget 7) (setq n (getint "\nNumber of copies: "))
If a real, a text or a number out of range is typed, the program warns and asks again:
Requires an integer value.
Enter with nothing returns nil, unless initget forbids it; initget bits 2 and 4 reject 0 and negative values.
Example
A command that asks for a positive number of sides:
(defun c:sides (/ n) (initget 7) (setq n (getint "\nNumber of sides: ")) (princ (strcat "\nSides: " (itoa n))) (princ))
Typing 0 gives Value must be nonzero.; -3, Value must be positive.; with 6 it writes Sides: 6.
See also
getstring
(getstring [spaces] [prompt])
Asks for a text. If the first argument is not nil, Space is part of the text and only Enter ends it. Returns "" on Enter.
(setq name (getstring T "\nLayer name: "))
getkword
(getkword [prompt])
Asks for one of the initget keywords. Returns the whole keyword (or its global name) and nil on Enter.
Call initget first with the allowed keywords; without it getkword accepts nothing and returns nil. Capital letters in the keyword mark the abbreviation the user can type. Add the bit 1 to forbid Enter.
(initget "Line Arc") (getkword "\n[Line/Arc]: ")
entsel
(entsel [prompt])
Asks to select one object with a click. Returns a list with the entity name and the picked point, or nil.
Pressing Enter or Escape returns nil, so always test the result before using it. For several objects use ssget. The second item of the list is the point where the click was made, useful to know which part of the object was picked.
(setq s (entsel "\nSelect a line: ")) (entget (car s))
osnap
(osnap point modes)
Applies the object snaps in modes to a point, a string with the abbreviations separated by commas ("_end", "_mid,_cen", "_int"…), and returns the nearest snap point within the aperture, or nil if there is none. The point, both the given and the returned one, is in the current UCS. With "_non" it returns the point unchanged.
(setq p (osnap (getpoint "\nNear an endpoint: ") "_end"))
Differences with AutoCAD
The aperture is that of object snap in the current view; AutoCAD in console mode (with no view) returns nil for any mode except "_non".
grread
(grread [track [allkeys [curtype]]])
Reads one input from the user with no prompt: a click returns (3 point) (in the current UCS) and typed text, (2 code) with the code of its first character; Enter alone gives (2 13). Escape cancels the routine.
(setq g (grread)) (if (= (car g) 3) (cadr g))
Differences with AutoCAD
This is the basic form. It does not track the mouse: with track it never returns (5 point), so a loop that draws a preview while the cursor moves only sees the final click. Nor does it read single keys: the command line hands over the typed text when Enter is pressed and grread returns its first character. The allkeys and curtype arguments do nothing.
princ
(princ [expression [file]])
Writes the expression on the command line (or to the file), strings without quotes. Returns the expression. (princ) with no arguments writes nothing: at the end of a routine it hides the last value.
(princ "\nDone.") (princ)
prin1
(prin1 [expression [file]])
Like princ, but strings are written in quotes and with their escape characters.
(prin1 "a") ; writes "a"
It returns the value it received, so on the command line it is shown twice. With a file opened with open, it writes to it in the same format, which read can read back.
Example
(prin1 "a\nb") ; writes "a\nb" with the quotes and the \n (princ "a\nb") ; writes a and b on two lines
See also
(print [expression [file]])
Like prin1, with a line break before and a space after.
(print 42)
It returns the value it received. With a file, the line break and the space go to the file too: (print 42 f) writes a line with 42 .
Example
(print 42) ; writes 42 on a new line and returns 42
See also
terpri
(terpri)
Writes a line break. Returns nil.
It is the same as (princ "\n"). Use it to separate blocks of information the routine writes.
Example
(princ "One") (terpri) (princ "Two") ; writes One and Two on two lines
See also
prompt
(prompt string)
Writes the string on the command line. Returns nil.
(prompt "\nSelect the walls: ")
It only accepts strings. Strings can include \n to start on a new line. Use it to give instructions before a question or to report what the routine has done.
Example
(prompt "\nSelect the walls: ") (setq ss (ssget))
See also
alert
(alert string)
Shows the string in a message box with an OK button (and writes it on the command line). In scripts, it only writes it.
Use it for messages the user must not miss, such as a missing layer or a failed check. For ordinary progress text prefer princ or prompt, which do not interrupt the routine.
(alert "There is no WALLS layer.")
textscr
(textscr)
In AutoCAD, switches to the text window. In LinceCAD it does nothing. Returns nil.
To see more lines of the history, open the text window with F2 or expand the command line. It is kept so that routines that use it do not fail.
Example
(textscr) ; nil
See also
graphscr
(graphscr)
In AutoCAD, goes back to the graphics window. In LinceCAD it does nothing. Returns nil.
The LinceCAD window always shows the drawing and the command line, so there is nothing to switch to. It is kept so that routines that use it do not fail.
Example
(graphscr) ; nil
See also
command
(command [argument]...)
Types the arguments on the command line, like a script: strings as they are ("" is Enter), numbers and points as text, entity names and selection sets as a selection, and pause for the user to give that input. nil is skipped. (command) with no arguments cancels the active command. Returns nil.
(command "_line" '(0 0) '(100 0) "") (command "_circle" pause 25)
Notes
Use global names (_line) so that the routine works with the interface in any language. With CMDECHO set to 0 the command line does not repeat the prompts or what command types (results are still shown).
command-s
(command-s [argument]...)
Same as command.
The arguments are sent to the command line exactly as if typed: names of commands, points as lists, numbers, and strings for options. A routine written with command-s for AutoCAD runs unchanged.
Differences with AutoCAD
In AutoCAD, command-s does not accept pause and can be used inside *error*; in LinceCAD it behaves like command.
vl-cmdf
(vl-cmdf [argument]...)
Like command, but returns T.
Use it inside routines that call commands, such as (vl-cmdf "circle" '(0 0) 5): the command name, then points as lists, numbers and strings for the options, exactly as they would be typed.
Differences with AutoCAD
AutoCAD checks the arguments before typing them; LinceCAD types them directly.
entlast
(entlast)
Name of the last main entity created in the drawing, or nil if it is empty.
(command "_circle" '(0 0) 5) (setq c (entlast))
Entities that belong to others (vertices of old-style polylines, attributes) do not count: it returns the main one. Use it right after command to get what the command has just created.
Example
Draw a circle and read its radius:
(command "_circle" '(0 0) 5) (setq c (entlast)) (cdr (assoc 40 (entget c))) ; 5.0
See also
entnext
(entnext [entity])
With no argument, the first entity of the drawing; with an entity, the next one (or nil at the end). It goes through model space and then the layouts; after an INSERT with attributes come its ATTRIBs and the SEQEND, as in AutoCAD.
(setq e (entnext)) (while e (setq e (entnext e)))
entmod
(entmod list)
Changes the entity of code -1 with the values of the list (the others stay the same). If the layer does not exist, it is created. Returns the list, or nil if it cannot.
(setq e (entget (entlast))) (entmod (subst '(62 . 1) (assoc 62 e) e))
Differences with AutoCAD
Only for LINE, CIRCLE, ARC, LWPOLYLINE, TEXT, MTEXT, POINT, INSERT and ATTRIB.
entdel
(entdel entity)
Erases the entity. If it had already been erased with entdel in this session, it is restored. Returns the name, or nil.
Only entities of the current drawing can be erased, and the call does not ask for confirmation. To erase a whole set at once, loop over a selection set obtained with ssget.
(entdel (entlast))
entupd
(entupd entity)
In AutoCAD, redraws the entity after changing it. In LinceCAD the drawing updates on its own: it returns the name and does nothing.
Routines written for AutoCAD often call it after entmod; keeping the call is harmless, so they run unchanged.
ssget
(ssget [mode] [point [point]] [filter])
Selection set, or nil if it is empty. Without a mode, the user selects (with ":S", only once). Modes: "X" or "A" (the whole drawing), "I" (what was selected before the routine), "P" (the previous one of ssget), "L" (the last entity), "W" and "C" with two points (window and crossing) or a single point. The filter is a list of DXF pairs, with wildcards in strings and the -4 operators ("<AND", "<OR", "<XOR", "<NOT", "<", ">=", "*"…).
(ssget "X" '((0 . "CIRCLE") (8 . "AXES"))) (ssget '((0 . "LINE,LWPOLYLINE")))
More filters
(ssget "X" '((0 . "LINE,CIRCLE"))) ; lines and circles (ssget "X" '((8 . "AX*"))) ; layers starting with AX (ssget "X" '((0 . "CIRCLE") (-4 . ">") (40 . 1.5))) ; circles with radius greater than 1.5 (ssget "X" '((-4 . "<OR") (0 . "LINE") (8 . "AXES") (-4 . "OR>"))) ; lines or anything on AXES
The pairs in a filter are combined with "and" unless they are grouped with -4.
Example
Count the circles on layer AXES without failing if there are none:
(setq ss (ssget "X" '((0 . "CIRCLE") (8 . "AXES")))) (if ss (sslength ss) 0)
Common mistakes
- bad argument type: lselsetp: nil: nothing matched the filter and ssget returned nil; check it before using sslength or ssname.
- Objects are not found by their type: the type is the DXF name (
"LWPOLYLINE","TEXT"), the one in code 0 of entget, not the name of the command that draws them.
See also
ssname
(ssname set index)
Name of the entity at that position (from 0) of the set, or nil.
(ssname ss 0)
The index starts at 0; one outside the set gives nil. With the name, entget gives the data of the object.
Example
Type of the first of the lines in the drawing:
(setq ss (ssget "X" '((0 . "LINE")))) (cdr (assoc 0 (entget (ssname ss 0)))) ; "LINE"
Going through a set
The usual way is a loop from 0 to sslength minus 1. For example, the list of object types in the whole drawing:
(setq ss (ssget "X") i 0 types nil)
(repeat (sslength ss)
(setq types (cons (cdr (assoc 0 (entget (ssname ss i)))) types)
i (1+ i)))
(reverse types) ; for example ("CIRCLE" "LINE")Common mistakes
- bad argument type: lselsetp: nil: ssget found nothing and returned nil. Check it first:
(if ss ...). - It returns nil inside the loop: the index reached the length of the set (the last one is the length minus 1); a negative index also gives nil.
- The order is not the drawing order: do not assume any particular order of the objects in the set.
See also
sslength
(sslength set)
Number of entities in the set.
(sslength (ssget "X"))
Together with ssname it is used to walk the set with repeat or while, from index 0 up to (1- (sslength ss)).
Example
Write the type of each object in the drawing:
(defun c:types (/ ss i)
(setq ss (ssget "X") i 0)
(repeat (sslength ss)
(princ (strcat "\n" (cdr (assoc 0 (entget (ssname ss i))))))
(setq i (1+ i)))
(princ))See also
ssmemb
(ssmemb entity set)
Returns the entity if it is in the set, or nil.
Use it to avoid processing the same object twice, or to check whether the object the user picked is in a set chosen earlier.
Example
(command "_circle" '(0 0) 5) (setq c (entlast) ss (ssget "X")) (ssmemb c ss) ; <Entity name: …> (ssdel c ss) (ssmemb c ss) ; nil
See also
tblsearch
(tblsearch table name [set-tblnext])
DXF list of the name record of the table, or nil. Tables: "LAYER", "LTYPE", "STYLE", "BLOCK" and "DIMSTYLE". With a non-nil third argument, the next tblnext continues after this record.
(if (not (tblsearch "LAYER" "WALLS")) (princ "\nThe WALLS layer is missing."))
Differences with AutoCAD
The VIEW, UCS, VPORT and APPID tables are not available.
getvar
(getvar name)
Value of a system variable, or nil if it is unknown. It reads those of the drawing (CLAYER, LUNITS, TEXTSIZE, DIMZIN…; ANGBASE in radians) and those of the program: OSMODE, ORTHOMODE, SNAPMODE, CMDACTIVE, CMDECHO, DWGNAME, DWGPREFIX, SECURELOAD, ACADVER, PRODUCT and PLATFORM.
(getvar "CLAYER") ; "0" (getvar "OSMODE")
Notes
Variables such as PICKBOX, FILEDIA, CMDDIA, ATTREQ or EXPERT exist with their usual value so that routines do not fail, but they change nothing. See System variables.
setvar
(setvar name value)
Changes a system variable and returns the value. Those of the drawing can be undone; OSMODE, ORTHOMODE and SNAPMODE change the program's object snaps, ortho and snap. An invalid value gives the error AutoCAD variable setting rejected.
(setq old (getvar "OSMODE")) (setvar "OSMODE" 0) (setvar "OSMODE" old)
redraw
(redraw [entity [mode]])
In AutoCAD, redraws the screen or an entity. In LinceCAD it is not needed: it returns nil and does nothing.
It is accepted so that routines written for AutoCAD run unchanged: the drawing is repainted automatically after every change, so there is no need to force it.
regen
(regen)
Regenerates the whole drawing, like REGEN. Returns nil.
It recomputes the image of every object, which can take a while in large drawings. Use it at the end of a routine that changes display variables or many objects with entmod.
Example
(setvar "FILLMODE" 0) (regen) ; nil
See also
open
(open file mode)
Opens a text file: "r" to read, "w" to write (emptying it) or "a" to append. Returns the file, or nil if it cannot be opened.
Always close the file with close when done, and test for nil before reading or writing. Use read-line to read text lines and write-line to write them. Paths accept the forward slash.
(setq f (open "C:/data/points.txt" "r"))
close
(close file)
Closes the file (and saves what was written). Returns nil.
(close f)
Until the file is closed, what was written may not be on disk. Always close the files you open with open, also when the routine may fail (close it in your error).
Example
Write two lines and read the first one back:
(setq f (open "C:/job/notes.txt" "w")) (write-line "Ground floor" f) (write-line "Floor 1" f) (close f) (setq f (open "C:/job/notes.txt" "r")) (read-line f) ; "Ground floor" (close f)
See also
open · read-line · write-line
read-line
(read-line file)
Next line of the file, without the line break, or nil at the end.
(while (setq l (read-line f)) (princ l))
Differences with AutoCAD
Without a file it does not read from the keyboard: use getstring.
write-line
(write-line string [file])
Writes the string and a line break to the file (or on the command line). Returns the string.
Without a file argument it writes on the command line, like princ with a line break. The file must be open in "w" or "a" mode. Unlike princ, it always ends the line, which suits CSV and text exports.
(write-line "X;Y" f)
findfile
(findfile name)
Path of the file if it exists as given, in the drawing folder or in the APPLOAD trusted folders; otherwise nil.
It does not search the whole disk, only the places named above, so give a relative name for files that travel with the drawing. The result can be passed directly to load or open.
(findfile "blocks.lsp")
getenv
(getenv name)
Value of an environment variable of the operating system, or nil.
Typical values are TEMP, USERNAME or PATH. The routine can use them to build file paths in a portable way. Test the result for nil, because a variable that is not defined returns nil.
(getenv "TEMP")
Differences with AutoCAD
It does not read the AutoCAD variables stored in the Windows registry.
load
(load file [on-failure])
Loads a .lsp file (the extension can be omitted), looking for it as given, in the drawing folder and in the trusted folders, and evaluates its expressions. Returns the value of the last one. If it is not found, returns on-failure or gives the error LOAD failed. Outside the trusted folders it may ask first, depending on the APPLOAD security setting.
(load "tools" nil)
vl-filename-base
(vl-filename-base path)
File name without folder or extension.
(vl-filename-base "C:/job/plan.dwg") ; "plan"
If the path has no folder or extension, it is returned as is. With the DWGNAME variable it gives the name of the current drawing, handy to name output files.
Example
(vl-filename-base (getvar "DWGNAME")) ; "Drawing1" in a new, unsaved drawing
See also
vl-filename-directory
(vl-filename-directory path)
Folder of the path, without the file name.
(vl-filename-directory "C:/job/plan.dwg") ; "C:/job"
With no folder in the path, it gives the empty string ((vl-filename-directory "plan.dwg") gives ""). The folder of the current drawing is in the DWGPREFIX variable.
Example
Path of a file next to another one:
(strcat (vl-filename-directory "C:/job/plan.dwg") "/sizes.txt") ; "C:/job/sizes.txt"
See also
vl-filename-extension
(vl-filename-extension path)
Extension of the file, with the dot, or nil if it has none.
(vl-filename-extension "plan.dwg") ; ".dwg"
With several dots in the name it only gives the last extension ("plan.v2.dwg" gives ".dwg").
Example
Check the type of a file:
(= (strcase (vl-filename-extension "PLAN.DWG") T) ".dwg") ; T
See also
vlax-get-acad-object
(vlax-get-acad-object)
Returns the application object, the starting point of the ActiveX object model. There is no COM: LinceCAD resolves these objects on the open drawing, so routines work the same without AutoCAD installed.
What is available
- Document:
vla-get-ActiveDocumentand, from it,vla-get-ModelSpace,PaperSpace,Blocks,Layers,Layouts,TextStylesandLinetypes. - Collections:
vla-get-Count,vla-item(by name or by index from 0),vla-addand vlax-for. - Properties (
vla-get-…andvla-put-…): Layer, Color, TrueColor, Linetype, LinetypeScale, Lineweight, Handle, ObjectName, Normal, StartPoint, EndPoint, Center, Radius, Diameter, Coordinates, TextString, InsertionPoint, Rotation, Height, Area, Length, Closed, Name and EffectiveName, on the entities that have them in AutoCAD. - Create:
vla-AddLine,vla-AddCircle,vla-AddArc,vla-AddLightWeightPolyline,vla-AddText,vla-AddMTextandvla-InsertBlock, in model space, paper space or a block. - Edit:
vla-Copy,vla-Move,vla-Rotate,vla-ScaleEntity,vla-Mirror,vla-Offset,vla-Explode,vla-Delete,vla-Update, and on the documentvla-Regen,vla-StartUndoMarkandvla-EndUndoMark.
Limits
There are no reactors, ActiveX attributes (vla-GetAttributes), dynamic properties, vlax-invoke-method or vlax-ldata-…. Safe arrays have one dimension.
Example
(setq ms (vla-get-ModelSpace (vla-get-ActiveDocument (vlax-get-acad-object)))) (vla-AddCircle ms (vlax-3d-point 0 0 0) 5.0)
See also
vlax-ename->vla-object
(vlax-ename->vla-object ename)
Converts an entity name into its ActiveX object. Two objects of the same entity are equal for eq.
Example
(vla-get-Layer (vlax-ename->vla-object (car (entsel))))
See also
vlax-vla-object->ename
(vlax-vla-object->ename object)
Returns the entity name of an ActiveX object, to carry on with entget and the other classic functions.
Example
(entget (vlax-vla-object->ename obj))
See also
Download LinceCAD Manual contents
Download the free 30-day trial · Open and edit DWG files · AutoCAD LT alternative
Coming from AutoCAD? Check what is compatible · CAD with a perpetual license