Bill Tillman Posted April 9, 2012 Posted April 9, 2012 I am trying to make myself familiar with the LAMBDA and MAPCAR commands and I have specific task which I'd like to try it out on. I have a list containing 8 points, ( pt1 pt2...pt8 ). I want to write a function which will draw a cross shape (+) 1" long in each direction, with it's center on each of the eight points. I assigned the variables as follows: (setq pt1 '(0.0 0.0) pt2 (polar pt1 (dtr 0.0) a) pt3 (polar pt2 (dtr 270.0) b) pt4 (polar pt1 (dtr 270.0) b) pt5 (polar pt2 (dtr 180.0) (+ b 36)) pt6 (polar pt5 (dtr 270.0) a) pt7 (polar pt4 (dtr 270.0) 36) pt8 (polar pt7 (dtr 0.0) b) Variables 'a' and 'b' are assigned elsewhere to set the distances between the points. I then tried this: (setq aa '(pt1 pt2 pt3 pt4 pt5 pt6 pt7 pt8)) (mapcar (command "._LINE" (polar aa (dtr 90.0) 0.5) (polar aa (dtr 270.0) 0.5) "") (command "._LINE" (polar aa (dtr 180.0) 0.5) (polar aa (dtr 270.0) 0.5) "") ) (dtr) is the standard degrees to radian function. Quote
MSasu Posted April 9, 2012 Posted April 9, 2012 Please check if this what you are looking for: (mapcar (function (lambda(p) (command "_LINE" (polar p (dtr 90.0) 0.5) (polar p (dtr 270.0) 0.5) "" "_LINE" (polar p (dtr 180.0) 0.5) (polar p (dtr 270.0) 0.5) ""))) aa) Quote
pBe Posted April 9, 2012 Posted April 9, 2012 if variable aa is a list of variable names (mapcar (function (lambda (p) (command "_LINE" (polar p (dtr 90.0) 0.5) (polar p (dtr 270.0) 0.5) "" "_LINE" (polar p (dtr 180.0) 0.5) (polar p [color=blue][b]0[/b][/color] 0.5) ""))) ([b][color=blue]mapcar[/color][/b] 'eval [color=blue][b]aa[/b][/color])) Besides, this sample is a not a good way to show what mapcar can do. and its more than just creating a list. what do you have in mind? for example. i want to create a list of the points the "+" shapes (setq all (mapcar (function (lambda (p / [b][color=blue]w x y z[/color][/b]) (command "_LINE" [color=blue](setq w ([/color]polar p (dtr 90.0) 0.5)[color=blue])[/color] [color=blue](setq x[/color] (polar p (dtr 270.0) 0.5)[color=blue])[/color] "" "_LINE" [color=blue](setq y[/color] (polar p (dtr 180.0) 0.5)[color=blue])[/color] [color=blue] (setq z[/color] (polar p 0 0.5)[color=blue])[/color] "") [color=blue][b](list w x y z)[/b][/color])) (mapcar 'eval aa))) if im not going to utilize the resulting list, i might as well use foreach (foreach p (mapcar 'eval aa) (command "_LINE" (polar p (dtr 90.0) 0.5) (polar p (dtr 270.0) 0.5) "" "_LINE" (polar p (dtr 180.0) 0.5) (polar p 0 0.5) "")) Quote
BlackBox Posted April 9, 2012 Posted April 9, 2012 FWIW - Lee already has some great tutorials on Mapcar & Lambda, and I found this Autodesk University course to be helpful when first tackling the Apply, Mapcar, and Lambda functions. ** Note - CADTutor will not allow PDF's over 500 KB, so I've added a .DWG file extension (which allows 1000 KB), simply remove .DWG and open the resultant .PDF file. HTH CP304-1-Handout-.pdf.dwg Quote
MSasu Posted April 9, 2012 Posted April 9, 2012 @RenderMan: Thas seems a really great course, especially the part on recursion. Thank you for sharing! Quote
pBe Posted April 9, 2012 Posted April 9, 2012 ..... simply remove .DWG and open the resultant .PDF file. HTH sometime ago somebody pointed me to that same handout. it helped me a lot to understand lambda . Didnt go far with recursive though .. Quote
Tharwat Posted April 9, 2012 Posted April 9, 2012 Renderman , file can't be opened . even on cad 2012 Thanks Quote
pBe Posted April 9, 2012 Posted April 9, 2012 Renderman , file can't be opened . even on cad 2012 Thanks Read the Rendermans note tharwat Quote
Tharwat Posted April 9, 2012 Posted April 9, 2012 Read the Rendermans note tharwat Thank you mate . just got it . Thank you Renderman , that's a very generous post from you. Highly appreciated. Quote
BlackBox Posted April 9, 2012 Posted April 9, 2012 Happy to help guys, I hope you can put the course materials to good use. It's just a shame that Autodesk keeps removing course year(s) from the online course database... I guess I'll just have to keep downloading each-and-every-course that I find useful. Quote
BIGAL Posted April 10, 2012 Posted April 10, 2012 DTR function if standard angles then why not put in startup lisp (setq pi45 0.7853981634) (setq pi90 1.57070796327) (setq pi180 3.141592654) (setq pi270 4.71238898) (setq pt3 (polar pt1 pi90 x)) Quote
pBe Posted April 10, 2012 Posted April 10, 2012 For Accuracy (/ pi 4.0 ) ;--> 45 (/ pi 2.0) ;--> 90 (* pi 1.5) ;--> 270 pi ;--> 180 Quote
irneb Posted April 10, 2012 Posted April 10, 2012 DTR function if standard angles then why not put in startup lisp (setq pi45 0.7853981634) (setq pi90 1.57070796327) (setq pi180 3.141592654) (setq pi270 4.71238898) (setq pt3 (polar pt1 pi90 x)) For Accuracy (/ pi 4.0 ) ;--> 45 (/ pi 2.0) ;--> 90 (* pi 1.5) ;--> 270 pi ;--> 180 X1! Much more efficient & less typing & easier to read. Or this: (setq tmp 45) (repeat 6 (set (read (strcat "pi" (itoa tmp))) (* (/ tmp 180.) pi)) (setq tmp (+ tmp 45))) (setq tmp nil) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.