+ All Categories
Home > Documents > MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Date post: 08-Apr-2015
Category:
Upload: swapnil-kale
View: 1,345 times
Download: 4 times
Share this document with a friend
Description:
S.E. Mechanical Course
25
S w a p n i l A . K a l e / M D C G / L I S P / N o t e s [AutoLISP Lessons - Solution of Important Questions] Programmes for Questions on topic 7 1. Input the length of sides of triangle and check whether it forms a triangle or not. If it forms a triangle draw the triangle. (defun c:triang() (command “erase” “all” “”) (setq s1 (getreal "Enter length of 1st side: ")) (terpri) (setq s2 (getreal "Enter length of 2nd side: ")) (terpri) (setq s3 (getreal "Enter length of 3rd side: ")) (terpri) (cond ;For sorting given sides in ascending order ( (> s1 s2) (setq temp1 s2) (setq s2 s1) (setq s1 temp1) ) ( (> s2 s3) (setq temp2 s3) (setq s3 s2) (setq s2 temp2) ) ( (> s1 s2) (setq temp3 s2) (setq s2 s1) (setq s1 temp3) ) ) (cond ( (> (+ s1 s2) s3) ;For checking condition for triangle (setq pt1 (getpoint "Enter insertion point of triangle: ")) (terpri) (setq pt2 (list (+ (car pt1) s1) (cadr pt1))) (setq x (/ (+ (* s1 s1) (- (* s2 s2) (* s3 s3))) (* 2 s1))) ;x=(a^2 + b^2 - c^2)/(2a) (setq z (- (* s2 s2) (* x x))) ;y=sqrt(b^2 - x^2) (setq y (sqrt z)) (setq pt3 (list (+ (car pt1) x) (+ (cadr pt1) y))) (command "line" pt1 pt2 pt3 pt1 "") Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential
Transcript
Page 1: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

Programmes for Questions on topic 7

1. Input the length of sides of triangle and check whether it forms a triangle or not. If it forms a triangle draw the triangle.

(defun c:triang()(command “erase” “all” “”)(setq s1 (getreal "Enter length of 1st side: ")) (terpri)(setq s2 (getreal "Enter length of 2nd side: ")) (terpri)(setq s3 (getreal "Enter length of 3rd side: ")) (terpri)(cond ;For sorting given sides in ascending order

((> s1 s2)(setq temp1 s2)(setq s2 s1)(setq s1 temp1))((> s2 s3)(setq temp2 s3)(setq s3 s2)(setq s2 temp2))((> s1 s2)(setq temp3 s2)(setq s2 s1)(setq s1 temp3))

)(cond

((> (+ s1 s2) s3) ;For checking condition for triangle

(setq pt1 (getpoint "Enter insertion point of triangle: ")) (terpri)(setq pt2 (list (+ (car pt1) s1) (cadr pt1)))(setq x (/ (+ (* s1 s1) (- (* s2 s2) (* s3 s3))) (* 2 s1))) ;x=(a^2 + b^2 - c^2)/(2a)(setq z (- (* s2 s2) (* x x))) ;y=sqrt(b^2 - x^2)(setq y (sqrt z))(setq pt3 (list (+ (car pt1) x) (+ (cadr pt1) y))) (command "line" pt1 pt2 pt3 pt1 "")

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 2: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

)((<= (+ s1 s2) s3) (terpri)(princ "These sides do not form a triangle"))

))DEFUN 2-18 (defun SYMBOL ARGUMENT-LIST EXPRESSION...)Defines a function named SYMBOL, with any arguments defined in ARGUMENT-LIST, containing some number of expressions; returns the value of the last expression evaluated.

SETQ 1-12 (setq SYMBOL1 VALUE1 [SYMBOL2 VALUE2] ...) Assigns VALUE1 to SYMBOL1, VALUE2 to SYMBOL2, etc.; returns the last value assigned.

TERTRI5-4 (terpri)This function prints a new line to the command line.CAR 3-5 (car LIST)Returns the first element of LIST.

Returns a list containing all but the first element of LIST.

CADR 3-9 (cadr LIST)Returns the second element of LIST.

CDR 3-6 (cdr LIST)CADDR 3-10 (caddr LIST)Returns the third element of LIST.

LIST 3-2 (list EXPR...)

Combines any number of expressions into a list.GETREAL 4-3 (getreal [PROMPT})Pauses for user input of a real number; returns that real number; displays PROMPT string, (if included).PRINC 7-14 (princ [EXPR [FILE-DESC]])Prints a string to the command line or writes it to the open file represented by FILE-DESC.COND 8-19 (cond (TEST1 ACTION1 ...) ...)Evaluates the test expressions in order until one returns a non-nil value, then evaluates the ACTION expression for the test that succeeded, and returns the value of the ACTION. If no ACTION is specified, the value of the TEST expression is returned.> (greater than) 8-4 (> NUMSTR [NUMSTR] ...)Returns T if each argument is numerically greater than the next argument, and returns nil otherwise.

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 3: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

2. Input the diameter of flange. According to diameter of the flange create no of holes on the flange.

(defun c:flange()(command “erase” “all” “”)(terpri)(setq c1 (getpoint "Select center of flange: "))(terpri)(setq dia (getreal "Enter diameter of flange: "))(terpri)(setq pcd (getreal "Enter p.c.d: "))(terpri)(setq c2 (list (car c1) (+ (cadr c1) (/ pcd 2)))) (terpri)(setq rhole (getreal "Enter hole radius :")) (terpri)(setq n (getint "Enter no.of holes :")) (terpri)(command "circle" c1 (/ dia 2))(command "circle" c2 rhole)(command "array" "l" "" "p" c1 n "360" "n")(setq pt1 (list (- (car c1) (* dia 0.8)) (cadr c1)))(setq pt2 (list (+ (car c1) (* dia 0.8)) (cadr c1)))(command "line" pt1 pt2 "" )(setq pt3 (list (car c1) (- (cadr c1) (* dia 0.8))))(setq pt4 (list (car c1) (+ (cadr c1) (* dia 0.8))))(command "line" pt3 pt4 "" ))

GETPOINT 3-3 (getpoint [PT] [PROMPT])Pauses for user to pick a point; displays rubberband from PT argument (if included); displays PROMPT string (if included); returns a list containing X, Y, and Z coordinates of point picked.

GETINT 4-2 (getint [PROMPT])Pauses for user input of an integer; returns that integer; displays PROMPT string (if included).

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 4: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

3. Input the end points of diagonal of the quadrilateral. Find out where it is square or rectangle. If it is a square or rectangle then draw the same. Assume base of quadrilateral is parallel to x-axis.( defun C:sqrec()(command “erase” “all” “”) ( setq p1 ( getpoint "\n pick start point of digonal:")) (terpri)( setq p2 ( getpoint "\n pick end point of digonal:")) (terpri)( setq ang ( angle p1 p2 )) (terpri) ( command "rectangle" p1 p2 ) ( if ( or ( = ang ( / pi 4 )) ( = ang ( / ( * 3 pi ) 4 ))) ( prompt "\n it is square") ( prompt "\n it is rectangle")) ( princ ) )IF 8-8 (if TESTEXPR THENEXPR [ELSEEXPR])If TESTEXPR evaluates to a non-nil value, THENEXPR is evaluated, otherwise ELSEEXPR (if present) is evaluated. If TESTEXPR evaluates to nil, ELSEEXPR (if present) is evaluated. Returns the value of the evaluated expression, or, if TESTEXPR evaluates to nil and ELSEEXPR is missing, returns nil.PROMPT 2-13 (prompt MESSAGE)Prints string MESSAGE to the command line.

4. Input the length of sides of triangle and find out which type of triangle it is and draw the same.( defun C:trian()(command “erase” “all” “”) ( setq a ( getreal "\n enter length of side1 of triangle:")) (terpri) ( setq b ( getreal "\n enter length of side2 of triangle:")) (terpri) ( setq c ( getreal "\n enter length of side3 of triangle:")) (terpri) ( if ( and ( > ( + a b ) c ) ( > ( + b c ) a ) ( > ( + c a ) b ))

( progn ( setq p1 ( getpoint "\n pick start point of triangle")

s ( / ( + a b c ) 2 ) x ( sqrt ( / ( * ( - s a ) ( - s c )) ( * a c ))) y ( sqrt ( - 1 ( * x x ))) z ( / x y ) ang ( - pi ( * 2 ( atan z ))) p2 ( polar p1 0 c ) p3 ( polar p2 ang a ))

(terpri) ( command "pline" p1 p2 p3 p1 "") ( progn

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 5: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

( if ( = a b c ) ( prompt "\n it is equilateral") ( progn

( if ( or ( = a b ) ( = b c ) ( = c a )) ( prompt "\n it is isoceles") ( prompt "\n it is scalene triangle"))))))

( prompt "\n doesnt form triangle"))(terpri) ( princ ) )

PROGN 8-10 (progn [EXPR] ...)Evaluates each expression sequentially, and returns the value of the last expression.COMMAND 2-14 (command [ARGUMENTS] ...)Executes one or more AutoCAD commands as specified by ARGUMENTS.

5. Draw a circle and pick a point on the screen. Find out whether the point lies inside or outside the circle.

( defun C:pcir() ( setq a ( entget ( car ( entsel " select circle ")))) (terpri) ( setq radius ( cdr ( assoc 40 a ))) (terpri) ( setq center ( cdr ( assoc 10 a ))) (terpri) ( setq p1 ( getpoint "\n pick point to find whether it is inside or outside of selected circle")) (terpri) ( setq d ( distance center p1 )) (terpri) ( if ( > d radius ) ( prompt "\n pointt is outside the circle") ( prompt "\n point is inside")) (terpri) ( princ ) (terpri) )

ENTGET 13-4 (entget ENAME [APPLIST])Returns an association list representing the definition record of entity ENAME.ENTSEL 13-11 (entsel [PROMPT])Prompts the user to select a single object by specifying a point; returns a list containing the ENAME of the object and a point list representing the point picked. If the optional PROMPT is not included, prompts the user with the Select objects: prompt. For complex entities, the function returns the ENAME of the main entity record; it does not provide subentity access.DISTANCE 5-5 (distance PT1 PT2)Returns the 3D distance between the two points specified by PT1 and PT2.ASSOC 10-3 (assoc ITEM ALIST)Searches an association list for the ITEM element and returns that association list entry.

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 6: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

6. Select a geometrical entity (line/circle). If it is line find the length angle of line. If it is a circle find radius and center points coordinate.

( defun C:GOM() ( setq a ( entget ( car ( entsel "\n select line or circle:"))) name ( cdr ( assoc 0 a ))) (terpri) ( if ( = name "CIRCLE" ) ( progn

( prompt "\n it is circle") ( setq radius ( cdr ( assoc 40 a )))

( prompt "\n radius=") ( princ radius ) ( setq center ( cdr ( assoc 10 a ))) ( prompt "\n center=") ( princ center ))) (terpri) ( if ( = name "LINE" ) ( progn

( setq a1 ( cdr ( assoc 10 a ))) ( prompt "\n co-ordinate of starting point=") ( princ a1 ) ( setq b ( cdr ( assoc 11 a ))) ( prompt "\n co-ordinate of end point=") ( princ b )

( setq ang ( angle a1 b)) ( setq ang ( / ( * ang 180 ) pi )) ( prompt "\n angle=") ( princ ang )

( setq L ( distance a1 b )) ( prompt "\n length of line=") ( princ L ))) (terpri) ( princ ) (terpri) )

ANGLE 5-4 (angle PT1 PT2)Returns the angle in radians of a line defined by endpoints PT1 and PT2; the line is measured from the X axis of the current construction plane.TERTRI5-4 (terpri)This function prints a new line to the command line.

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 7: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

7. Draw a circle. Select option for hatching (1/2). If it is 1, draw hatch using select object option and if it is 2, then draw the hatch with pick point option.

(defun c:HCIR()(command “erase” “all” “”)(setq c (getpoint "Select center of circle: ")) (terpri)(setq r (getreal "Enter radius of circle: ")) (terpri)(command "circle" c r) (terpri)(setq opt (getint "Select option for hatching 1 or 2 : "))

(if (= opt 1)(progn(command "bhatch" "select")(setq a (entsel "Select object to hatch: "))(command a "" "")) (terpri)(progn(command "bhatch" "internal" "")(setq b (getpoint "Select internal point for hatching: "))(command b ""))) (terpri)

)

8. Draw an arc. Find out the included angle of the arc. Depending upon the angle decide the no of division of the arc and divide the arc.( defun C:arcdiv() ( setq s ( entsel )

a ( entget ( car s )) x ( cadr s ) name ( car s) a1 ( cdr ( assoc 50 a )) a2 ( cdr ( assoc 51 a )) ang ( / ( * 180 ( abs ( - a2 a1 ))) pi ))

( if ( <= ang 90 ) ( setq n 3 ) ( progn ( if ( and ( > ang 90 ) ( <= ang 180 )) ( setq n 6 ) ( setq n 8 )))) ( command "divide" x n ) ( prompt "\n included angle=") ( princ ang ) ( princ ) )

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 8: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

9. Draw square and circle (inscribe of circumscribe). Find out who is circumscribing what according to dimension.

( defun C:sqrcir() (terpri) ( setq a ( entget ( car ( entsel "select square")))) (terpri) ( setq p1 ( cdr (nth 14 a))

P3 ( cdr (nth 18 a))) ( setq d ( distance p1 p3 )) ( setq x ( entget ( car ( entsel " select circle ")))) (terpri) ( setq radius ( cdr ( assoc 40 x ))) ( if ( = d ( * 2 radius )) ( prompt "\n circle is inscribed:") ( prompt "\n square is inscribed:")) ( princ ) )

10. Draw a line and pick a point on the line with mouse. Find out whether that point lie left or right of the midpoint of the line.

( defun C:midline() ( setq a ( entget ( car ( entsel "\n select line")))

p1 ( cdr ( assoc 10 a )) p2 ( cdr ( assoc 11 a )) m ( list ( / ( + ( car p1 ) ( car p2 )) 2 ) ( / ( + ( cadr p1 ) ( cadr p2 )) 2 )) p ( getpoint "\n select point on the line") x ( car p ) y ( car m ) w ( - x y ) ang ( angle p1 p2))

( if ( or ( = ang ( / pi 2 )) ( = ang ( / ( * 3 pi ) 2 ))) ( prompt "\n the line is vertical:") ( progn ( if ( = w 0 ) ( prompt "\n you have selected mid point:") ( progn ( if ( > w 0 ) ( prompt "\n the point is on the RHS of the mid") ( prompt "\n the point is on the LHS of the mid")))))) ( princ ) )

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 9: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

11. Find the summation of series like:Sum=x+x^2+x^3+x^4+…..Sum= x-x^2+x^3-x^4+…..Sum= x-x^2/2!+x^3/3!-x^4/4!+…..

( defun C:series() ; (1) first series= x+x^2+x^3+x^4+..... ( setq x ( getreal "\n enter a number:")

n ( getreal "\n enter no of terms upto which you want series:") s 0 p 1 i 1 )

( while ( <= i n ) ( setq p ( * p x )

s ( + s p ) i ( + i 1 )))

( prompt "\n sum of first series:") ( princ s )

;(2)second series is=x-x^2+x^3-x^4+..... ( setq s 0 p 1 i 1 ) ( while ( <= i n ) ( setq p ( * p ( - 0 x ))

s ( + s p ) i ( + i 1 )))

( prompt "\n sum of second series:") ( princ ( - 0 s )) ;(3) third series is=x-x^2/2!+x^3/3!-x^4/4! ( setq s 0 p 1 i 1 f 1 ) ( while ( <= i n ) ( setq p ( * p ( - 0 x ))

f ( * i f ) c ( / p f ) s ( + s c ) i ( + i 1 )))

( prompt "\n sum of third series=") ( princ ( - 0 s )) ( princ ) )WHILE 9-3 (while TEXTEXPR EXPR ...)If TESTEXPR, evaluates to a non-nil value, the included expressions are evaluated; repeats until the test expression evaluates to nil.

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 10: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

12. Input number of forces and find the resultant (magnitude and direction). Draw the force polygon.

( defun C:forcepoly()(command “erase” “all” “”) ( setq n ( getint "\n enter no of forces:")

i 1 p1 ( getpoint "\n pick start point:") x 0 y 0 ps p1 )

( while ( <= i n ) ( setq f ( getreal "\n enter magnitude of force:")

a ( getreal "\n enter direction of force:") fx ( * f ( cos ( * a ( / pi 180 )))) fy ( * f ( sin ( * a ( / pi 180 )))) x ( + x fx ) y ( + y fy ) p2 ( polar p1 ( * a ( / pi 180 )) f ))

( command "line" p1 p2 "") ( setq p1 p2 ) ( setq i ( + i 1 )) ) ( command "line" ps p2 "") ( setq resultant ( sqrt ( + ( * x x ) ( * y y )))) ( prompt "\n resultant=") ( princ resultant ) ( setq direction ( / ( * ( angle ps p2 ) 180 ) pi)) ( prompt "\n direction=") ( princ direction ) ( princ ) )POLAR 5-5 (polar PT ANGLE DISTANCE)Returns a point list containing the 3D point at an angle specified by ANGLE and a distance specified by DISTANCE from the point specified by PT.

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 11: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

13. Input the number of sides and draw polygon. (Create polygon command)

( defun C:polyg()(command “erase” “all” “”) ( setq n ( getint "\n enter no of sides:")

i 1 p1 ( getpoint "\n pick start point:") L ( getreal "\n enter length of side:") ps p1 )

( setq ang ( / ( * 2 pi ) n )) ( setq x ang ) ( while ( <= i n ) ( setq p2 ( polar p1 ang L )) ( command "line" p1 p2 "" ) ( setq p1 p2 ) ( setq ang ( + ang x )) ( setq i ( + i 1 )) ) ( princ ) )

14. Create polar array command for circle.( defun C:polcir() ( setq a ( entget ( car ( entsel "\n select circle:")))

c ( cdr ( assoc 10 a )) r ( cdr ( assoc 40 a )) c1 ( getpoint "\n specify center of polar array:") theta1 ( angle c c1 ) r1 ( distance c c1 ) n ( getint "\n enter no of objects") thetav ( / ( * 2 pi ) n ) thetain ( + thetav theta1 ) i 1)

( while ( <= i n ) ( setq c2 ( polar c1 thetain r1 )) ( command "circle" c2 r ) ( setq thetain ( + thetain thetav )

i ( + i 1 ))) ( princ ) )

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 12: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

15. Create rectangular array command for circle.( defun C:Rectcir() ( setq a ( entget ( car ( entsel " select circle ")))) ( setq r ( cdr ( assoc 40 a ))) ( setq c ( cdr ( assoc 10 a ))

m ( getint "\n enter no of rows:") n ( getint "\n enter no of columns:") ro ( getreal "\n specify distance between rows:") co ( getreal "\n specify distance between columns:") i 1 c3 c )

( while ( <= i m ) ( setq j 1 ) ( while ( < j n ) ( setq c1 ( polar c 0 co )) ( command "circle" c1 r ) ( setq c c1

j ( + j 1 ))) ( setq c2 ( polar c3 ( / pi 2 ) ro )) ( if ( < i m ) ( command "circle" c2 r )) ( setq c3 c2

c c2 i ( + i 1 )))

( princ ) )

16. Create multiple copy command for circle.( defun C:multicopy() ( setq a ( entget ( car ( entsel "select circle")))

c ( cdr ( assoc 10 a )) r ( cdr ( assoc 40 a )) p1 ( getpoint "\n base point"))

( while ( setq p2 ( getpoint "\n displacement point")

d ( distance p1 p2 ) ang ( angle p1 p2 ))

( command "circle" ( setq c1 ( polar c ang d )) r))

( princ ) )

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 13: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

17. Draw f(x) curve for given limits.( defun C:curve()(command “erase” “all” “”) ( prompt "here f(x) is y^2=4x") ( setq ll ( getreal "\n enter lower limit:")

ul ( getreal "\n enter upper limit:") x ll y ( sqrt ( * 4 x )) p1 ( list x y ))

( command "spline" p1 ) ( while ( < x ul ) ( setq x1 ( + x 1 )

y1 ( sqrt ( * 4 x1 )) p2 ( list x1 y1 ))

( command p2 ) ( setq p2 p1

x ( + x 1 ))) ( command "" "" "" ) ( princ ) )

18. Creating bill of material.

19. Move the object, if the object is selected is not nil with entsel.

( defun C:moveobj() ( setq a ( entsel "\n select entity")

p1 ( getpoint "\n specify base point") p2 ( getpoint "\n specify next point"))

( command "copy" ( cadr a ) "" p1 p2 "" ) ( command "erase" a "" ) ( princ ) )

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 14: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

20. Input x and y coordinates of n points and draw curve/line through them.

( defun C:curvexy()(command “erase” “all” “”) ( setq n ( getreal "\n enter no of points")

i 1 p1 ( getpoint "\n pick co-ordinate of first point"))

( command "spline" p1 ) ( while ( < i n ) ( setq p2 ( getpoint "\n pick co-ordinate of next point")) ( command p2 ) ( setq p1 p2

i ( + i 1 )) ) ( command "" "" "" ) ( princ ) )

21. Select multiple entities of various types with ssget and find the types of entities.

Programmes for Questions on topic 1-6

1. Select line from screen and find coordinates of end points and length of line.( defun C:coordline() (terpri) ( setq a ( entget ( car ( entsel " select line ")))) (terpri) ( setq a1 ( cdr ( assoc 10 a ))) ( prompt "\n co-ordinate of starting point=") ( princ a1 ) ( setq b ( cdr ( assoc 11 a ))) ( prompt "\n co-ordinate of end point=") (terpri) ( princ b ) ( setq L ( distance a1 b )) ( prompt "\n length of line=") (terpri) ( princ L ) ( princ ) )

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 15: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

2. Select circle from screen and find coordinates of centre point, radius and area.( defun C:cocircle() (terpri) ( setq a ( entget ( car ( entsel " select circle ")))) (terpri) ( setq radius ( cdr ( assoc 40 a ))) (terpri) ( prompt "\n radius=") ( princ radius ) ( setq center ( cdr ( assoc 10 a ))) (terpri) ( prompt "\n center=") ( princ center ) ( setq area ( * pi radius radius )) (terpri) ( prompt "\n area of circle=") (terpri) ( princ area ) ( princ ) ) 3. Select entity with ssget and find the types of entities.( defun C:ssg() ( setq a ( ssget )

i 0) ( prompt "\n you have selected-") ( while ( <= i ( - ( sslength a ) 1 )) ( setq a1 ( ssname a i )

b1 ( entget a1 ) name ( cdr ( assoc 0 b1 )))

( prompt "\n") ( princ name) ( setq i ( + i 1 ))) ( princ ) )

4. Draw rectangle.( defun C:Rec()(command “erase” “all” “”) ( setq p1 ( getpoint "\n select starting point of rectangle")) ( setq L ( getreal "\n enter length of rectangle:")

W ( getreal "\n enter width of rectangle:") )

( setq p2 ( list ( + ( car p1 ) L ) ( cadr p1 ))) ( setq p3 ( list ( car p2 ) ( + ( cadr p2 ) w ))) ( setq p4 ( list ( car p1 ) (+ ( cadr p1 ) w )))

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 16: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

( command "line" p1 p2 p3 p4 p1 "") ( princ ) )

5. Draw triangle.( defun C:Triang()(command “erase” “all” “”) ( setq p1 ( getpoint "\n pick first point of triangle:")) ( setq p2 ( getpoint "\n pick second point of triangle:")) ( command "line" p1 p2 "" ) ( setq p3 ( getpoint "\n pick third point:")) ( command "line" p2 p3 p1 "" ) ( princ ) )

6. Draw hexagon.( defun C:Hexago()(command “erase” “all” “”) ( setq L ( getreal "\n enter length of side of hexagon:")

p1 ( getpoint "\n pick start point") p2 ( polar p1 0 L ) p3 ( polar p2 ( / pi 3 ) L ) p4 ( polar p3 ( / ( * 2 pi ) 3 ) L ) p5 ( polar p4 pi L ) p6 ( polar p5 ( / ( * 4 pi ) 3 ) L ))

( command "pline" p1 p2 p3 p4 p5 p6 p1 "" ) ( princ ) )

7. Input two strings from the user and perform various string operations.( defun C:Strin() ( setq a ( getstring "\n enter first string:")

b ( getstring "\n enter second string:") )

( setq c ( strcat a b )) ( prompt "\n strcat=") ( princ c ) ( setq d ( strlen c )) ( prompt "\n length of catanated string=") ( princ d ) ( setq e ( strcase c )) ( prompt "\n string in capital is=")

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 17: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

( princ e ) ( setq f ( strcase c 1 )) ( prompt "\n string in small is=") ( princ f ) ( setq g ( substr c 2 5 )) ( prompt "\n sub string is=") ( princ g ) ( princ ) )

8. Input two points from the user and perform various list filtering operations.

9. Find f(x) for two/three x values.( defun C:Fx() ( setq x ( getreal "\n enter first value of x" )) ( setq y1 ( + ( / ( + ( * 2 ( sin x ) ) ( exp x )) ( + ( log x ) ( * x x ))) ( / ( * ( cos x ) ( exp x )) ( + 1 ( cos x ))))) ( prompt "\n y1=") ( princ y1 ) ( setq x1 ( getreal "\n enter second value of x" )) ( setq y2 ( + ( / ( + ( * 2 ( sin x1 ) ) ( exp x1 )) ( + ( log x1 ) ( * x1 x1 ))) ( / ( * ( cos x1 ) ( exp x1 )) ( + 1 ( cos x1 ))))) ( prompt "\n y2=") ( princ y2 ) ( princ ) )

10. Input height of equilateral triangle and find the length of side, area and perimeter. Draw the same triangle.( defun C:Equitriang()(command “erase” “all” “”) ( setq h ( getreal "\n enter height of equilateral triangle:")) ( setq p1 ( getpoint "\n specify first point:")) ( setq side ( / h ( sin ( / pi 3 )))) ( command "pline" p1

( strcat "@" ( rtos side ) "<0") ( strcat "@" ( rtos side ) "<120") p1 "" )

( prompt "\n side=") ( princ side )

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 18: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

( setq area ( * 0.5 side h )) ( prompt "\n area=") ( princ area ) ( setq perimeter ( * 3 side )) ( prompt "\n perimeter=") ( princ perimeter ) ( princ ) )

11. Input height and base of isosceles triangle and find the length of side, area and perimeter. Draw the same triangle.( defun C:Isosceles()(command “erase” “all” “”) ( setq h ( getreal "\n enter height of isoceles=")) ( setq b ( getreal "\n enter base of isoceles=")) ( setq p1 ( getpoint "\n pick start point:")) ( setq side ( sqrt ( + ( * h h ) ( * b b 0.25 )))) ( setq p2 ( list ( + ( car p1 ) b ) ( cadr p1 ))) ( setq p3 ( list ( + ( car p1 ) ( / b 2 )) ( + ( cadr p1 ) h ))) ( command "pline" p1 p2 p3 p1 "" ) ( setq area ( * 0.5 b h )) ( setq perimeter ( + b side side )) ( prompt "\n side=") ( princ side ) ( prompt "\n perimeter=") ( princ perimeter ) ( prompt "\n area=") ( princ area ) ( princ ) )

12. Input diagonal of square and find the length of side, area and perimeter. Draw the same triangle.

( defun C:Sqr()(command “erase” “all” “”) ( setq digonal ( getreal "\n enter digonal of square:")) ( setq p1 ( getpoint "\n pick starting point of square:")) ( setq side ( / digonal ( sqrt 2 ))) ( command "pline" p1

( strcat "@" ( rtos side ) "<0" ) ( strcat "@" ( rtos side ) "<90" )

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 19: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

( strcat "@" ( rtos side ) "<180" ) ( strcat "@" ( rtos side ) "<270" ) "")

( prompt "\n side=") ( princ side ) ( setq area ( * side side )

perimeter ( * 4 side )) ( prompt "\n area=") ( princ area ) ( prompt "\n perimeter=") ( princ perimeter ) ( princ ) ) 13. Input area of equilateral triangle and find the length of side and perimeter. Draw the same triangle.

( defun C:Equitri()(command “erase” “all” “”) ( setq area ( getreal "\n enter area of equilateral:")) ( setq side ( sqrt ( / ( * 4 area ) ( sqrt 3 )))) ( setq p1 ( getpoint "\n pick starting point:")) ( command "pline" p1

( strcat "@" ( rtos side ) "<0" ) ( strcat "@" ( rtos side ) "<120" ) p1 "" )

( setq perimeter ( * 3 side )) ( prompt "\n side=") ( princ side ) ( prompt "\n perimeter=") ( princ perimeter ) ( princ ) )

14. Input area of circle and find radius and perimeter. Draw the same circle.

( defun C:CiRcL()(command “erase” “all” “”) ( setq area ( getreal "\n enter area of circle:"))

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 20: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

( setq radius ( sqrt ( / area pi ))) ( setq center ( getpoint "\n enter center of circle:")) ( command "circle" center radius ) ( prompt "\n radius=") ( princ radius ) ( setq perimeter ( * 2 pi radius )) ( prompt "\n perimter=") ( princ perimeter ) ( princ ) )

15. Parametric program of nut, bolt, surface roughness symbols etc.( defun dtr(deg) ( setq deg ( / ( * deg pi ) 180.0)) )( defun C:NBSR()(command “erase” “all” “”) ( setq pt1 ( getpoint "\n enter insertion point")

d ( getreal "\n enter bolt diameter:") L ( getreal "\n enter bolt length:") pt2 ( polar pt1 ( dtr 90.0 ) ( / d 2.0 )) pt3 ( polar pt1 ( dtr 90.0 ) d ) pt4 ( polar pt3 ( dtr 180.0 ) ( * 0.8 d )) pt5 ( list ( - ( car pt1 ) d ) ( - ( cadr pt4 ) ( / d 4.0 ))) pt6 ( list ( car pt4 ) ( cadr pt2 )) pt7 ( list ( car pt5 ) ( cadr pt1 )) pt8 ( polar pt6 ( dtr 270.0 ) d ) pt9 ( polar pt2 0 L ) pt10 ( polar pt9 ( dtr 270.0 ) ( / d 2.0 )) pt11 ( polar pt10 0 ( * 0.2 d )) pt12 ( polar pt9 ( dtr 270.0 ) d ) pt13 ( polar pt2 0 ( / L 3.0 )) pt14 ( polar pt13 ( dtr 270.0 ) ( * 0.1 d )) pt15 ( polar pt13 ( dtr 180.0 ) ( * 0.1 d )) pt16 ( list ( car pt9 ) ( cadr pt14 )) pt17 ( polar pt13 ( dtr 270.0 ) ( / d 2.0 )) pt18 ( polar pt11 ( dtr 90.0 ) d ) pt19 ( list ( car pt7 ) ( - ( cadr pt7 ) d )) )

( command "line" pt1 pt3 pt4 "" "line" pt5 pt7 ""

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 21: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

"line" pt6 pt9 pt10 "" "line" pt15 pt14 pt16 "" "line" pt13 pt17 "" "arc" pt4 pt5 pt6 "" "arc" pt6 pt7 pt8 "" "arc" pt12 pt11 pt9 "" "mirror" "w" pt7 pt18 "" pt7 pt11 ""))

16. Input two points from mouse and find distance and angle between the points.( defun C:finddist()(terpri)(command “erase” “all” “”) ( setq p1 ( getpoint "pick first point:")) (terpri) ( setq p2 ( getpoint " pick second point:")) (terpri) (setq d ( distance p1 p2 )) (terpri) ( setq a ( angle p1 p2 )) (terpri) ( setq a ( / ( * a 180) pi )) ( prompt "\n distance=") (terpri) ( princ d ) (terpri) ( prompt "\n angle=") (terpri) ( princ a ) (terpri) ( princ ) )

17. Using string and data conversion functions draw rectangle.( defun C:recstring()(command “erase” “all” “”) ( setq L ( getreal "\n enter length of rectangle:")

W ( getreal "\n enter width of rectangle:")) ( setq p1 ( getpoint "\n pick starting point of rectangle:")) ( command "line" p1

( strcat "@" ( rtos L ) "<0" ) ( strcat "@" ( rtos W ) "<90" ) ( strcat "@" ( rtos L ) "<180" ) p1 "")

( princ ) )

18. Using string and data conversion functions draw hexagon.( defun C:Stringhexagon()(command “erase” “all” “”)

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 22: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

( setq p1 ( getpoint "\n select starting point of hexagon")) ( setq a ( getreal "\n enter side of hexagon:")) ( command "line" p1

( strcat "@" ( rtos a ) "<0" ) ( strcat "@" ( rtos a ) "<60" ) ( strcat "@" ( rtos a ) "<120" ) ( strcat "@" ( rtos a ) "<180" ) ( strcat "@" ( rtos a ) "<240" ) ( strcat "@" ( rtos a ) "<300" ) "")

(princ) )

19. Using string and data conversion functions draw pentagon.( defun C:StringPentagon()(command “erase” “all” “”) ( setq p1 ( getpoint "\n pick first point of pentagon:")) ( setq a ( getreal "\n enter side of pentagon:")) ( command "pline" p1

( strcat "@" ( rtos a ) "<0") ( strcat "@" ( rtos a ) "<72") ( strcat "@" ( rtos a ) "<144") ( strcat "@" ( rtos a ) "<216") ( strcat "@" ( rtos a ) "<288") "" )

( princ ) ) 20. Input string and number and perform various string functions.( defun C:StringCircle() ( setq a ( getstring "\n enter string:")

b ( getint "\n enter a number:") )

( setq b ( rtos b )) ( setq c ( strcat a b )) ( prompt "\n strcat=") ( princ c ) ( setq d ( strlen c )) ( prompt "\n length of catanated string=") ( princ d ) ( setq e ( strcase c )) ( prompt "\n string in capital is=")

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 23: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

( princ e ) ( setq f ( strcase c 1 )) ( prompt "\n string in small is=") ( princ f ) ( setq g ( substr c 2 5 )) ( prompt "\n sub string is=") ( princ g ) ( princ ))

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Page 24: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential

Function-Handling FunctionsLOAD 2-6 (load FILENAME [ONFAILURE])Causes AutoLISP to read the program file specified by string argument FILENAME, check for syntax errors, and evaluate the expressions in the file; if successful, returns the value of the final expression within the file; if not, returns string in ONFAILURE (if included).

DEFUN 2-18 (defun SYMBOL ARGUMENT-LIST EXPRESSION...)Defines a function named SYMBOL, with any arguments defined in ARGUMENT-LIST, containing some number of expressions; returns the value of the last expression evaluated.

SETQ 1-12 (setq SYMBOL1 VALUE1 [SYMBOL2 VALUE2] ...) Assigns VALUE1 to SYMBOL1, VALUE2 to SYMBOL2, etc.; returns the last value assigned.

QUOTE 2-10 (quote EXPRESSION) Returns an expression literally, without evaluating it.

List Manipulation Functions CADDR 3-10 (caddr LIST)Returns the third element of LIST.

NTH 10-9 (nth N LIST)Returns the Nth element (specified by the integer N argument) of the list specified by LIST; the first element is number 0.

LIST 3-2 (list EXPR...)Combines any number of expressions into a list.

APPEND 9-5 (append LIST ...)Combines any number of lists into a single list.

ASSOC 10-3 (assoc ITEM ALIST)Searches an association list for the ITEM element and returns that association list entry.

CONS 10-4 (cons NEW-FIRST-ELEMENT LIST)(cons NEW-FIRST-ELEMENT ATOM)Basic list constructor; the first format adds a new element to the beginning of a list; the second format creates a dotted pair.

LAST 3-15 (last LIST)Returns the last element of LIST.

LENGTH 3-15 (length LIST)Returns the number of elements contained in LIST.

SUBST 13-20 (subst NEWITEM OLDITEM LIST)Searches a list for OLDITEM and returns a copy of the LIST with NEWITEM substituted for every occurrence of OLDITEM.

Manipulating System VariablesGETVAR 4-10 (getvar VARNAME)Returns the value of the AutoCAD system variable specified in string VARNAME.

SETVAR 4-10 (setvar VARNAME VALUE)Sets AutoCAD system variable in string VARNAME to value specified by VALUE.

Arithmetic Functions+ (Addition) 1-5 (+ [NUM NUM] ...)Accepts one or more numbers and returns the sum of all the numbers.

Page 25: MDCG - LISP Solution of Programmes From Question Bank _ Swapnil Kale

Swap

nil A

. Kal

e/M

DC G

/LIS

P/N

otes

[AutoLISP Lessons - Solution of Important Questions]

Swapnil A. Kale, Department of Mechanical Engineering ,ICEM | Confidential


Recommended