autolisp prof.dr. demir bayka. autolisp 1.declare and set values to variables 2.pick the elements of...

12
AUTOLISP Prof.Dr. Demir Bayka

Upload: ira-holt

Post on 13-Dec-2015

226 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

AUTOLISP

Prof.Dr. Demir Bayka

Page 2: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

AUTOLISP

1. Declare and set values to variables

2. Pick the elements of lists

3. Implement AutoCad commands

4. Select Entities

IT IS POSSIBLE TO CREATE CAD DRAWINGS IN AUTOCAD THROUGH AUTOLISP TEXT FILES FROM WITHIN A DELPHI 4.0 EXECUTABLE PROGRAM

NECESSARY TOPICS

Page 3: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

• All autolisp commands are written in parenthesis

<< (autolisp_command parameters) >>

• Comment lines begin with << ; >>

• Autolisp files have <<.lsp>> extensions

• Autolisp files can be loaded into Autocad by command line or <<Load Application>> in the <<Tools> menu.

AUTOLISP

Page 4: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

Declare and set values to variablesThe Autolisp command is << setq >>

(setq x 10)................................x = 10

(setq y x)..................................y = 10

(setq y (* x 2))..........................y = 20

(setq y (+ x 15)).......................y = 25

(set y (list 5 67 12 56 “Black” x 12))y is set to the arraythe 4th element of the y-array is 67the 5th element of the y-array is “Black”the 6th element is 10 (assuming x=10)

Page 5: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

In autolisp the arithmetic operations are performed as ;(setq <variable> (<operator> <value1> <value2>))This can be interpreted as ;Variable = value1 (operator) value2Examples:

(setq x (+ 10 5)) x = 15(setq x (- 10 5)) x = 5(setq x (* 10 5)) x = 50(setq x (/ 10 5)) x = 2

(setq a 10)(setq b 5)(setq x (* a b)) x = 50

Page 6: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

In autolisp arrays can also be assigned to variables. Then ofcourse that variable will have a first, second,third... and so on values.

Arrays may be expressed by preceeding with <list>

e.g. An array (1,5,67,99.5,3) may be represented as ;

(list 1 5 67 99.5 3)

If this array is asigned to a variable (say x) then;

(setq x (list 1 5 67 99.5 3))

QUESTION :

How do we pick out the first or third element of x ?

Page 7: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

There are two functions that can be used for this purpose:1. car (picks the first element of the list)2. cdr (picks all elements EXCEPT the first)

If you want to pick out the third element then you can • call <cdr> once (now you have a list starting with the

second element)• call <cdr> again (now you have a list starting with the

third element)• call <car> and pick out the first element which is the third

element of the original list.EXAMPLES(setq x (car (list 1 5 67 99.5 3))) x=1(setq x (car (cdr (cdr (list 1 5 67 99.5 3))))) x=67

(list 5 67 99.5 3) (list 67 99.5 3) 67

Page 8: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

The way you interpret a nested <car – cdr> combination is FROM RIGHT TO LEFT.Examplecaddr means (car (cdr (cdr

First operation

Second operation

Third operation

Therefore :(setq p3D (list 50 4 205))(setq x (car p3D)) x = 50(setq x (cadr p3D)) x= 4(setq x (caddr p3D)) x = 205

JUST REMEMBER car cadr caddr(x) (y) (z)

Page 9: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

Implement AutoCad commands

AUTOCAD COMMANDS ARE IMPLEMENTEDBY THE ........<< command >> FUNCTION OF AUTOLISP

EXAMPLE

(command “line” p1 p2 “”) Draws a line from point p1 to point p2

(command “zoom” “extent”) Zooms to the extent of whatever entities are on the screen

(command "pedit" L1 "Y" "join" L2 L3 "" "")

Joins L1 L2 L3 lines into a polyline

Page 10: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

We have learned;

1. How to declare and set values to variables

2. How to pick the elements of lists with (car, cadr, caddr)

3. How to implement AutoCad commands

Now we will look at another feature ;

SELECTION OF ENTITIES

The “entities” could be “a point”, “a line”, “a set of lines”,

“a polyline”,“a set of polylines”, “a profile”, “a circle”,

“a rectangle”, “a solid”.....etc.

There are various methods. We will look into 2 of these. Namely (ssget “L”) and (ssget “X”) functions.

Page 11: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

(ssget “L”) method is used to select the LAST entity that was created.For example lets draw half of a circle. You may do this by

drawing an ARC that has a center point, starting point and an angle (180).

The points should be 2D (polylines and arcs are drawn on the XY plane)

(setq p0 (list (+ (car cen) R2) (cadr cen) (caddr cen))) p0=(R2,0,0)(setq arccen (list (car cen) (cadr cen))) arccen=(0,0)(setq arc1 (list (car p0) (cadr p0))) arc1=(R2,0)(command "arc" "c" arccen arc1 "a" "180")You have to check from AutoCad Help about the use of the ARC command

Now that the arc is drawn you can assign it (the whole list of properties of an arc) to a variable say <PLarc1>. In order to do this you must first select it and then use the <setq> function.

(setq PLarc1 (ssget "L"))

Page 12: AUTOLISP Prof.Dr. Demir Bayka. AUTOLISP 1.Declare and set values to variables 2.Pick the elements of lists 3.Implement AutoCad commands 4.Select Entities

Draw a profile for extrusion. Assume this profile has 2 arcs (<PLarc1> and <PLarc2>) and two polylines (<PL1> and <PL2>). We can combine them all into a single polyline <Profile> by the <pedit> Autocad command.

(command "pedit" (ssget "X") "Y" "join" (ssget "X") "" "exit")(setq Profile (ssget "L"))

As you have noticed in the <pedit> command we have selected all the entities <PLarc1>, <PL1>, <PLarc2>, <PL2> by (ssget “X”). However after the single polyline was formed we selected only that polyline and assigned it to <Profile>.

(ssget “X”) method is used to select the ALL SELECTABLE ENTITIES that were created.

EXAMPLE