Home > Article > Computer Tutorials > Teach yourself how to write Lisp programs for CAD
Constructed two commands: hide and show.
I wrote it simply, but the functions are not complete. You can use it as you like
Copy the following code and create acad.lsp. Place it in the first layer folder of the CAD installation directory and it can be loaded automatically:
; Enter the command hidden on the command line, and then select the graphics elements. As a result, the unselected elements are changed to the hidden layer
(defun C:hidsel(/ all;all primitives
xs;Non-hidden primitive
yc;Hide primitive
n nam dat)
(setvar "CMDECHO" 0); turn off command echo
(command "_layer" "n" "Temporarily hidden layer""")(command "_layer" "off" "Temporarily hidden layer" "")
(setvar "CMDECHO" 1)
(setq all (ssget "x")); select all
(setq xs (ssget)); select non-hidden primitives
(setq n -1 yc all);Operation hidden primitive
(repeat (sslength xs)
(setq n ( n 1))
(setq nam (ssname xs n)); Return the name of the Nth primitive
(setq yc (ssdel nam yc))
)
(setq N -1);Hide
(repeat (sslength yc)
(setq N ( n 1))
(setq nam (ssname yc n)); Return the name of the Nth primitive
(setq dat (entget nam));DAT stores the Nth element data
(entmod (subst (cons 8 "Temporarily hidden layer") (Assoc 8 dat) dat)); Hide the yc collection
)
(setq all nil xs nil yc nil n nil nam nil dat nil); clear data occupied memory
(princ)
)
;Show all primitives
(defun c:shoal(/ all n nam dat)
(setq all (ssget "x")); select all
(setq N -1);Hide
(repeat (sslength all)
(setq N ( n 1))
(setq nam (ssname all n)); Return the name of the Nth primitive
(setq dat (entget nam));DAT stores the Nth element data
(entmod (subst (cons 8 "0") (Assoc 8 dat) dat)); Hide the yc collection
)
(setvar "CMDECHO" 0)
(COMMAND "PURGE" "LA" "Temporarily Hide Layer" "Y" "Y" """)
(setvar "CMDECHO" 1)
(setq all nil n nil nam nil dat nil); clear data occupied memory
(princ)
)
Lisp itself is a development program belonging to CAD. It provides some simple function calculations, and the rest is all for the drawing functions of autocad. The lisp program is similar to the stored procedure in the database and can process and draw cad graphics in batches.
Learning lisp is very simple. You only need to master the use of a few main commands. You also need to study the statement format very carefully. Lisp itself has quite a few functions, so you don’t need to memorize them.
Load LISP
1. You can use the APPLOAD command, then find the LISP file to be loaded and load it.
2. You can drag the LISP file from the file manager to the graphics window of ACAD, or you can load it
3. Use it after the command line, (load "c:\\temp\\xxx.lsp") can also be loaded. Please enter the actual path for the path name.
Another: For an LSP program, (what follows defun is a command or function. Generally, the program should have a prompt. If not, the words following the identifier c: are commands that can be used under ACAD, either after COMMAND: Just enter it directly to execute.
AutoLISP language is a programming language that is based on the ordinary LISP language and expands many functions suitable for CAD applications. It is an interpretive value language that is slow and difficult to keep secret. It can be edited with any text editing software and saved in plain text format. For example:
(setq i 1); that is, the value of code i is 1
(command "CIRCLE" '(0 0) 100); Command to draw a circle with a radius of 100 at the coordinate origin.
All components in the AutoLISP language are given in the form of functions. It has no statement concepts or other grammatical structures. Executing an AutoLISP program means executing some functions and then calling other functions. For example:
(setq pt1 (getpoint "\nPlease select the center insertion point"))
(command "CIRCLE" pt1 100); Command this insertion point to draw a circle with a radius of 100
AutoLISP expresses data and programs into a unified table structure, so programs can be processed as data, and data can also be executed as programs.
For example: a straight line, starting point coordinates' (0 0), end point coordinates' (1000 0), layer 0, color 1 [red] This is some data of a straight line, how to execute it into program code as follows:
(entmake (list '(0 . "LINE") '(8 . "0") '(62 . 1) '(10 0 0) '(11 1000 0)))
You can view the data visually, or you can paste it into the command line of AutoCAD to draw the straight line you want above.
The program running process in AutoLISP language is the process of processing function values, and the function of the function is realized in the process of processing function values. In AutoCAD, all objects can be regarded as composed of countless points, each point has its own coordinates. The operation of the function is to calculate the values of these points and make judgments based on the obtained values.
The main control structure of AutoLISP language is recursive. The use of recursion makes programming simple and easy to understand. For example:
(setq &k1 (entsel)); select object
(setq &k1 (car &k1)); Extract primitives
(setq #g1 (entget &k1)); Get attribute list
(setq c0 (cdr (assoc 0 #g1))); Get the primitive name
The above is written down one by one, it can be written as follows:
(setq c0 (cdr (assoc 0 (entget (car (entsel)))))); simple and easy to understand
Because autolisp is simple and easy, you can get started quickly. After writing a program, the drawing efficiency can be greatly improved. The famous [Tianzheng] plug-in is a good helper for drawing.
The autolisp program also has shortcomings, such as extracting coordinates. This can only be extracted from the primitive attributes. In this case, there will be limitations. The coordinate values of normal coordinates 1 and -1 are the same, but the positions of the graphics are different, resulting in It is difficult to calculate coordinates, so it is best to use the VLAX function to extract coordinate values.
The above is the detailed content of Teach yourself how to write Lisp programs for CAD. For more information, please follow other related articles on the PHP Chinese website!