tagkelas Posted January 29, 2009 Posted January 29, 2009 Hi, I am new in this forum (and also in Autolisp). I modified a code I saw here. It transform polyline's coordinates This is the code i found here: (defun c:poly() (setq ps (entsel "Pick a polyline")) ;Get a selection (setq pl (car ps)) ;find the polyline (setq pll (entget pl)) ;get it's entity list (setq new nil) ;create a new list (repeat (setq i (length pll)) ;Walk thru the list (if (= 10 (car (nth (setq i (1- i)) pll))) ;If found a node... (progn (setq x (cadr (nth i pll)) ;extract the coords y (caddr (nth i pll)) ) (setq x (+ x 100)) ;do anything with (setq y (* 1.0 y)) ;X and Y (setq new (cons (list 10 x y) new)) ;and add them to the new ) (setq new (cons (nth i pll) new)) ;If not a node, just add it ) ) (entdel pl) ;delete the polyline (entmake new) ;create a new one ) And this is mine (it's only modified in the algorithm): (defun c:poly() (setq ps (entsel "Pick a polyline")) ;Get a selection (setq pl (car ps)) ;find the polyline (setq pll (entget pl)) ;get it's entity list (setq new nil) ;create a new list (repeat (setq i (length pll)) ;Walk thru the list (if (= 10 (car (nth (setq i (1- i)) pll))) ;If found a node... (progn (setq x (cadr (nth i pll)) ;extract the coords y (caddr (nth i pll)) ) (setq x [color=Red](- (+ 327063.85 (* 0.9997106 x) (* 0.0229562 y) (* 0.00000000205 y y)) (* 0.00000000216 x x) (* 0.00000000008 x y))[color=Black])[/color] [/color] ;do anything with (setq y [color=Red](- (+ 4457285.55 (* 0.9997092 y) (* 0.00000000034 x x) (* 0.00000000009 y y)) (* 0.00000000422 x y) (* 0.0229499 x))[/color]) ;X and Y (setq new (cons (list 10 x y) new)) ;and add them to the new ) (setq new (cons (nth i pll) new)) ;If not a node, just add it ) ) (entdel pl) ;delete the polyline (entmake new) ;create a new one ) Well if you multiple and sum x-coordinate , the new one is ok. But there is something wrong in y-coordinate but I do not know what it is. For example for a point (0,0), Excel (checked) gives (327063.85 4457285.55) But with this code gives (327063.85 4449815.837) Here is the functions for excel: X=327063.85+0.9997106*x+0.0229562*y-0.00000000216*x*x+0.00000000205*y*y-0.00000000008*x*y Y=4457285.55-0.0229499*x+0.9997092*y+0.00000000034*x*x+0.00000000009*y*y-0.00000000422*x*y Can anyone help me? Quote
Lee Mac Posted January 29, 2009 Posted January 29, 2009 Not sure if this is any different: (setq y (- (+ (- 4457285.55 (* 0.0229499 x)) (* 0.9997092 y) (* 0.00000000034 x x) (* 0.00000000009 y y)) (* 0.00000000422 x y)) Quote
Lee Mac Posted January 29, 2009 Posted January 29, 2009 But to be honest, the LISP isn't particularly well-written, I might consider re-writing it Quote
Lee Mac Posted January 29, 2009 Posted January 29, 2009 A better way of writing it to allow for a null selection and selection of wrong entity type: (defun c:poly (/ pl plLst i x y new) (if (and (setq pl (car (entsel "\nPick a Polyline > "))) (= "LWPOLYLINE" (cdr (assoc 0 (setq plLst (entget pl)))))) (progn (repeat (setq i (length plLst)) (if (= 10 (car (nth (setq i (1- i)) plLst))) (progn (setq x (cadr (nth i plLst)) y (caddr (nth i plLst)) x (+ x 100.0) y (* y 2.0)) (setq new (cons (list 10 x y) new))) (setq new (cons (nth i plLst) new)))) (entdel pl) (entmake new)) (princ "\n<!> No Polyline Selected <!>")) (princ)) Quote
tagkelas Posted January 30, 2009 Author Posted January 30, 2009 Not sure if this is any different: (setq y (- (+ (- 4457285.55 (* 0.0229499 x)) (* 0.9997092 y) (* 0.00000000034 x x) (* 0.00000000009 y y)) (* 0.00000000422 x y)) Does this need another ) at the end of the line? I've tried the ther one also but nothing. Any other idea? Thanks for your interest I 've checked it again and again but nothing.If I write first the algorithm of y-coordinate and then x-coordinate, the destination y-coord is ok but there is a wrong with x-coord Quote
CarlB Posted January 30, 2009 Posted January 30, 2009 The problem is you are using the variables x & y for the initial coordinates & the transformed coordinates. When the new x is calculated for point 0,0, the calculation is as intended as it correctly uses x=0 & y=0. But in the calculation for y, it uses the just calculated x=327063.85 instead of x=0. Quote
tagkelas Posted January 30, 2009 Author Posted January 30, 2009 Well I thought it might be that,but i 've done the calculation for y-coord (manually) with the new x-coord (x=327063.85) but the result is different than this .lsp gives. Quote
CarlB Posted January 30, 2009 Posted January 30, 2009 Well I used your equation, put it in Excel, & I get the same answer as the lisp answer when x=327063.85 & y=0. So maybe aproblem with your manual method Quote
tagkelas Posted January 30, 2009 Author Posted January 30, 2009 But now I 've made a change in points parameters and I think it works. Thanks for this advise CarlB and for the modified new code Lee Mac Here is the final: (defun c:poly() (setq ps (entsel "Pick a polyline")) ;Get a selection (setq pl (car ps)) ;find the polyline (setq pll (entget pl)) ;get it's entity list (setq new nil) ;create a new list (repeat (setq i (length pll)) ;Walk thru the list (if (= 10 (car (nth (setq i (1- i)) pll))) ;If found a node... (progn (setq x (cadr (nth i pll)) ;extract the coords y (caddr (nth i pll)) ) (setq [color=Red]E[/color] (- (+ 327063.85 (* 0.9997106 x) (* 0.0229562 y) (* 0.00000000205 y y)) (* 0.00000000216 x x) (* 0.00000000008 x y))) ;do anything with (setq [color=Red]N[/color] (- (+ 4457285.55 (* 0.9997092 y) (* 0.00000000034 x x) (* 0.00000000009 y y)) (* 0.00000000422 x y) (* 0.0229499 x))) ;X and Y (setq new (cons (list 10 [color=Red]E N[/color]) new)) ;and add them to the new ) (setq new (cons (nth i pll) new)) ;If not a node, just add it ) ) (entdel pl) ;delete the polyline (entmake new) ;create a new one ) Quote
tagkelas Posted February 27, 2010 Author Posted February 27, 2010 Can we store temporary variables in the previous code? For example (setq E (- (+ 327063.85 (* 0.9997106 x) (* 0.0229562 y) (* 0.00000000205 y y)) (* 0.00000000216 x x) (* 0.00000000008 x y))) if E = B1+B2 and B1=x(x+y) and B2=y(x+y) I want first to calculate B1 and B2 and then calculate E. Can this be done? Quote
tagkelas Posted February 17, 2011 Author Posted February 17, 2011 Well, CarlB & Lee Mac can you help me to modify the code, again? How do we change it to transform not only polylines but also circles, arcs, lines etc..... How abouts all objects (including blocks and images) ? 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.