AutoLISP: AutoCAD's inbuilt programming language
Introduction to Autolisp:
AutoLISP is AutoCAD's inbuilt programming language. AutoLISP is used to create programs that will automatically generate drawings. User defined customized commands can be added to AutoCAD using AutoLISP. These customized commands will perform the activities that we want to automate. The very simple example can be creating parametric shapes of any complexity. These shapes will be generated considering the parameters defined and values provided at the time of generation.
It is very important to know what the most important element of the code is because it helps us create the structure to what we're intending to do in the software. It's the variable. It could be any letter but not a number since the latter is a constant.
To understand how Autolisp works, we're creating a code for a basic function which is the creation of a command that allow us to attach circular annotations with numbers to the different elements of a project. It is divided into two main parts: the first one defines the data and variables and the second defines the commands that are going to use the information in the first lines to generate a result.
(setq p (getpoint "insert center of circle")): Sets the point the we're going to establich by the first click
(command "circle" p a): Defines the next command which is the circle with its radius
(command "text" "j" "m" p t 0 n ""): Defines the text in the center of the circle with the preset height
(setq n (+ n 1)): Allows the fucntion to be continuous, so everytime the user click the action repeats itself by adding 1 to the previous number
There are two ways to execute the command: We copying and paste the code in the command bar. After, we press enter and we write the name of the command which is 123. The second method is by apploading th lsp file using APPLOAD and then it's possible to use it by typing 123 in the command bar.
Using the Autolisp programming language in creating a chair:
The original code for the chair gives the possibility to create a chair in AutoCAD with multiple variables and commands comprised:
(defun c:cadeira ()
(setq pontos2d (getvar "osmode"))
(setq pontos3d (getvar "3dosmode"))
(setvar "osmode" 0)
(setvar "3dosmode" 0)
(setq pt1 (getpoint "indique o ponto de inserção da cadeira: "))
(If (= nil
(setq comprimento (getreal " indique o comprimento da cadeira (em x): ")))
(setq comprimento 0.50))
(If (= nil
(setq largura (getreal "indique a largura da cadeira(em y): ")))
(setq largura 0.50))
(If (= nil
(setq altura (getreal "indique a altura da cadeira : (em z): ")))
(setq altura 0.60))
(If (= nil
(setq esp1 (getreal "indique a espessura do assento: ")))
(setq esp1 0.02))
(If (= nil
(setq esp2 (getreal "indique a espessura dos pés: ")))
(setq esp2 0.02))
(If (= nil
(setq costas (getreal "indique a altura das costas: ")))
(setq costas 0.5))
(If (= nil
(setq esp3 (getreal "indique a espessura das costas: ")))
(setq esp3 0.05))
(modelar)
(modelar2)
(setvar "osmode" pontos2d)
(setvar "3dosmode" pontos3d)
(command "ucs" "")
)
(defun modelar ()
(command "ucs" pt1 "")
(command "box" (list 0 0 (- altura esp1)) (list comprimento largura (+ altura esp1)))
(setq e1 (entlast))
(command "box" (list 0 0 0) (list esp2 esp2 (- altura esp1)))
(command "array" (entlast) "" "r" 2 2 (- largura esp2) (- comprimento esp2))
)
(defun modelar2 ()
(command "box" (list 0 0 (+ altura esp1)) (list comprimento esp3 (+ altura esp1 costas)))
) When the code is entered in the command bar the software asks us to enter the values for the different dimensions of the chair, if we just click enter it gives the default dimensions that we include to the code.
AutoLISP is AutoCAD's inbuilt programming language. AutoLISP is used to create programs that will automatically generate drawings. User defined customized commands can be added to AutoCAD using AutoLISP. These customized commands will perform the activities that we want to automate. The very simple example can be creating parametric shapes of any complexity. These shapes will be generated considering the parameters defined and values provided at the time of generation.
It is very important to know what the most important element of the code is because it helps us create the structure to what we're intending to do in the software. It's the variable. It could be any letter but not a number since the latter is a constant.
To understand how Autolisp works, we're creating a code for a basic function which is the creation of a command that allow us to attach circular annotations with numbers to the different elements of a project. It is divided into two main parts: the first one defines the data and variables and the second defines the commands that are going to use the information in the first lines to generate a result.
(defun c:123 ()This code is written in Notepad and saved as .lsp
(setq a 0.05)
(setq t 0.04)
(setq n 1)
(while
(setq p (getpoint "insert center of circle"))
(command "circle" p a)
(command "text" "j" "m" p t 0 n "")
(setq n (+ n 1))
)
)
defun: Defines a function, which is in our case called 123
set q: Sets the value of a symbol or symbols to associated expressions. It is defining in our code the radius (a), the text's height (t) and the number from which the sequence is supposed to start (n)
while: Evaluates a test expression, and if it is not nil, evaluates other expressions; repeats this process until the test expression evaluates to nil. In other words, it allows us to continue the function as we click until we stop(setq p (getpoint "insert center of circle")): Sets the point the we're going to establich by the first click
(command "circle" p a): Defines the next command which is the circle with its radius
(command "text" "j" "m" p t 0 n ""): Defines the text in the center of the circle with the preset height
(setq n (+ n 1)): Allows the fucntion to be continuous, so everytime the user click the action repeats itself by adding 1 to the previous number
There are two ways to execute the command: We copying and paste the code in the command bar. After, we press enter and we write the name of the command which is 123. The second method is by apploading th lsp file using APPLOAD and then it's possible to use it by typing 123 in the command bar.
Using the Autolisp programming language in creating a chair:
The original code for the chair gives the possibility to create a chair in AutoCAD with multiple variables and commands comprised:
(setq pontos2d (getvar "osmode"))
(setq pontos3d (getvar "3dosmode"))
(setvar "osmode" 0)
(setvar "3dosmode" 0)
(setq pt1 (getpoint "indique o ponto de inserção da cadeira: "))
(If (= nil
(setq comprimento (getreal " indique o comprimento da cadeira (em x): ")))
(setq comprimento 0.50))
(If (= nil
(setq largura (getreal "indique a largura da cadeira(em y): ")))
(setq largura 0.50))
(If (= nil
(setq altura (getreal "indique a altura da cadeira : (em z): ")))
(setq altura 0.60))
(If (= nil
(setq esp1 (getreal "indique a espessura do assento: ")))
(setq esp1 0.02))
(If (= nil
(setq esp2 (getreal "indique a espessura dos pés: ")))
(setq esp2 0.02))
(If (= nil
(setq costas (getreal "indique a altura das costas: ")))
(setq costas 0.5))
(If (= nil
(setq esp3 (getreal "indique a espessura das costas: ")))
(setq esp3 0.05))
(modelar)
(modelar2)
(setvar "osmode" pontos2d)
(setvar "3dosmode" pontos3d)
(command "ucs" "")
)
(defun modelar ()
(command "ucs" pt1 "")
(command "box" (list 0 0 (- altura esp1)) (list comprimento largura (+ altura esp1)))
(setq e1 (entlast))
(command "box" (list 0 0 0) (list esp2 esp2 (- altura esp1)))
(command "array" (entlast) "" "r" 2 2 (- largura esp2) (- comprimento esp2))
)
(defun modelar2 ()
(command "box" (list 0 0 (+ altura esp1)) (list comprimento esp3 (+ altura esp1 costas)))
) When the code is entered in the command bar the software asks us to enter the values for the different dimensions of the chair, if we just click enter it gives the default dimensions that we include to the code.
Aucun commentaire: