TunzaGibbo Posted May 18, 2018 Share Posted May 18, 2018 After I pick my dimension point I will pick a point at about say about 20 degrees. Then what I'm trying to say with the first "IF" statement is that if p2 is somewhere between 0 deg and 45 deg then my "SETQ" will give me a direction to draw my line later in the program. I will probably have 8 "IF" statements to cover all 45 deg segments in a circle. For some reason the if statement won't work. I've tried many combinations with no luck. Can somebody steer me in the right direction??? Regards Tony (setq p1 (getpoint "\nPick a dimension point:") p2 (getpoint p1 "\nPick a location point:")) (setq ang (angle p1 p2)) (if (and (> (rtd ang) 0) (< (rtd ang) 45)) (setq direction (polar p1 (dtr 0))) (if (and (> (rtd ang) 45) (< (rtd ang) 90)) (setq direction (polar p1 (dtr 90))) Quote Link to comment Share on other sites More sharing options...
Tharwat Posted May 18, 2018 Share Posted May 18, 2018 Hi, You have omitted one argument with your polar function. (polar pt ang dist) Quote Link to comment Share on other sites More sharing options...
BIGAL Posted May 19, 2018 Share Posted May 19, 2018 The angle is returned in radians so 45=0.7853981633974483 or (* pi 0.25) or (setq a45 (* pi 0.25)) and use a45 its just me rather than using the rtd function all the time. Quote Link to comment Share on other sites More sharing options...
dlanorh Posted May 19, 2018 Share Posted May 19, 2018 After I pick my dimension point I will pick a point at about say about 20 degrees. Then what I'm trying to say with the first "IF" statement is that if p2 is somewhere between 0 deg and 45 deg then my "SETQ" will give me a direction to draw my line later in the program. I will probably have 8 "IF" statements to cover all 45 deg segments in a circle.For some reason the if statement won't work. I've tried many combinations with no luck. Can somebody steer me in the right direction??? Regards Tony (setq p1 (getpoint "\nPick a dimension point:") p2 (getpoint p1 "\nPick a location point:")) (setq ang (angle p1 p2)) (if (and (> (rtd ang) 0) (< (rtd ang) 45)) (setq direction (polar p1 (dtr 0))) (if (and (> (rtd ang) 45) (< (rtd ang) 90)) (setq direction (polar p1 (dtr 90))) Use a condition statement otherwise it will always evaluate all 8 "if's" (setq p1 (getpoint "\nPick a dimension point:") p2 (getpoint p1 "\nPick a location point:")) (setq ang (angle p1 p2)) (cond ( (and (> (rtd ang) 0) (< (rtd ang) 45)) (setq direction (polar p1 (dtr 0))) );end sub ( (and (> (rtd ang) 45) (< (rtd ang) 90)) (setq direction (polar p1 (dtr 90))) );end sub etc.... ( (and (> (rtd ang) 270) (< (rtd ang) 315)) (setq direction (polar p1 .......) );end sub ( t ;will only get here if angle > 315 (setq direction (polar p1 ......) );end sub );end_cond As soon as it finds a condition it satisfies it will exit the (cond). It also allows multiple lines of code in each sub without having to use progn:celebrate: Quote Link to comment Share on other sites More sharing options...
dlanorh Posted May 19, 2018 Share Posted May 19, 2018 I don't know if you've thought of this but if text is going to be involved, text at angles between 90 and 270 will be upside down to a greater or lesser degree. Quote Link to comment Share on other sites More sharing options...
TunzaGibbo Posted May 19, 2018 Author Share Posted May 19, 2018 I'm really frustrated with this one I just can't quite get it right. If you look at the attachment you see 8 green lines. Using the code below I want to draw only 1 line but it could be any of these positions so I thought if I picked a corner (p1) and then another point in the 45 deg segment (p2). I could setq 2 points based from the corner point. The line is 2mm long and is 1mm in the x & y from the corner. (defun c:ref (/ p1 p2 ang StartPt EndPt) (setq p1 (getpoint "\nPick a line reference point:") p2 (getpoint p1 "\nPick a location point:")) (setq ang (angle p1 p2)) (cond ( (and (> (deltartd ang) 270) (< (deltartd ang) 315)) (setq StartPt (polar p1 (deltadtr 315) 1.412)) (setq EndPt (polar p1 (deltadtr 288.4349) 2.0)) ) :End Sub ( (and (> (deltartd ang) 315) (< (deltartd ang) 0)) (setq StartPt (polar p1 (deltadtr 315) 1.412)) (setq EndPt (polar StartPt (deltadtr 0) 2.0)) ) ) ;End Cond (command "_line" StartPt EndPt "" ) ) Quote Link to comment Share on other sites More sharing options...
dlanorh Posted May 19, 2018 Share Posted May 19, 2018 (defun c:ref (/ p1 p2 ang StartPt EndPt) (setq p1 (getpoint "\nPick a line reference point:") p2 (getpoint p1 "\nPick a location point:")) (setq ang (angle p1 p2)) (cond ( (and ([color=red]>[/color] (deltartd ang) 270) ([color=red]<[/color] (deltartd ang) 315))[color=red]; one of these needs to either >= or <= (IMHO use >= in first)[/color] (setq StartPt (polar p1 (deltadtr 315) 1.412)) (setq EndPt (polar p1 (deltadtr 288.4349) 2.0)) ) :End Sub ( (and ([color=red]>[/color] (deltartd ang) 315) ([color=red]<[/color] (deltartd ang) [color=red]0[/color]))[color=red];see above 0 needs to be 360 otherwise it will always fail[/color] (setq StartPt (polar p1 (deltadtr 315) 1.412)) (setq EndPt (polar StartPt (deltadtr 0) 2.0)) ) ) ;End Cond (command "_line" StartPt EndPt "" ) ) See above. Angles of exactly 0 45, 90, 135 etc never meet any condition Quote Link to comment Share on other sites More sharing options...
FranknBeans Posted May 19, 2018 Share Posted May 19, 2018 cond statements are also good for excluding possiblities they go, so you don't need to keep checking if the angle is within a range just that it is below the next upper limit. edit: misunderstood request :/ Quote Link to comment Share on other sites More sharing options...
TunzaGibbo Posted May 19, 2018 Author Share Posted May 19, 2018 Thank you very much for all the input It now works absolutely perfect. Do you think it's a bit long winded. I am an amateur of course. Sorry Frank I didn't get to try your method. I'm sure it works fine to. I had to turn the Osnaps of because it wasn't consistent with them on. (defun c:ref (/ p1 p2 ang StartPt EndPt) (setq p1 (getpoint "\nPick a line reference point:") p2 (getpoint p1 "\nPick a location point:")) (setq ang (angle p1 p2)) (setvar "osmode" 0) (cond ( (and (>= (deltartd ang) 0) (< (deltartd ang) 45)) (setq StartPt (polar p1 (deltadtr 45) 1.412)) (setq EndPt (polar StartPt (deltadtr 0) 2.0))) ( (and (>= (deltartd ang) 45) (< (deltartd ang) 90)) (setq StartPt (polar p1 (deltadtr 45) 1.412)) (setq EndPt (polar StartPt (deltadtr 90) 2.0))) ( (and (>= (deltartd ang) 90) (< (deltartd ang) 135)) (setq StartPt (polar p1 (deltadtr 135) 1.412)) (setq EndPt (polar StartPt (deltadtr 90) 2.0))) ( (and (>= (deltartd ang) 135) (< (deltartd ang) 180)) (setq StartPt (polar p1 (deltadtr 135) 1.412)) (setq EndPt (polar StartPt (deltadtr 180) 2.0))) ( (and (>= (deltartd ang) 180) (< (deltartd ang) 225)) (setq StartPt (polar p1 (deltadtr 225) 1.412)) (setq EndPt (polar StartPt (deltadtr 180) 2.0))) ( (and (>= (deltartd ang) 225) (< (deltartd ang) 270)) (setq StartPt (polar p1 (deltadtr 225) 1.412)) (setq EndPt (polar StartPt (deltadtr 270) 2.0))) ( (and (>= (deltartd ang) 270) (< (deltartd ang) 315)) (setq StartPt (polar p1 (deltadtr 315) 1.412)) (setq EndPt (polar StartPt (deltadtr 270) 2.0))) ( (and (>= (deltartd ang) 315) (< (deltartd ang) 360)) (setq StartPt (polar p1 (deltadtr 315) 1.412)) (setq EndPt (polar StartPt (deltadtr 0) 2.0))) ) (command "_line" StartPt EndPt "" ) ) Regards Tony Quote Link to comment Share on other sites More sharing options...
David Bethel Posted May 19, 2018 Share Posted May 19, 2018 Maybe this can help : [b][color=BLACK]([/color][/b]defun c:langle [b][color=FUCHSIA]([/color][/b]/ c p1 p2 a d i[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]initget 6[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setq c [b][color=NAVY]([/color][/b]getangle [color=#2f4f4f]"\nDirection Vector Angle <45>: "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]or c [b][color=NAVY]([/color][/b]setq c [b][color=MAROON]([/color][/b]* pi 0.25[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]initget 1[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setq p1 [b][color=NAVY]([/color][/b]getpoint [color=#2f4f4f]"\nBase Point: "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]setq p2 [b][color=MAROON]([/color][/b]getpoint p1 [color=#2f4f4f]"\nDirection P2: "[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]setq a [b][color=MAROON]([/color][/b]angle p1 p2[b][color=MAROON])[/color][/b] d nil i 0[b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]grdraw p1 p2 2 3[b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]not d[b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]setq i [b][color=GREEN]([/color][/b]+ i c[b][color=GREEN])[/color][/b] d [b][color=GREEN]([/color][/b]<= a i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]grdraw p1 [b][color=GREEN]([/color][/b]polar p1 i [b][color=BLUE]([/color][/b]distance p1 p2[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b] 1 3[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]princ [b][color=MAROON]([/color][/b]strcat [color=#2f4f4f]"\nDirection Angle = "[/color] [b][color=GREEN]([/color][/b]angtos i 0 2[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] Angles are always CCW positive values -David Quote Link to comment Share on other sites More sharing options...
dlanorh Posted May 19, 2018 Share Posted May 19, 2018 Thank you very much for all the input It now works absolutely perfect.Do you think it's a bit long winded. I am an amateur of course. Sorry Frank I didn't get to try your method. I'm sure it works fine to. I had to turn the Osnaps of because it wasn't consistent with them on. (defun c:ref (/ p1 p2 ang StartPt EndPt) (setq p1 (getpoint "\nPick a line reference point:") p2 (getpoint p1 "\nPick a location point:")) (setq ang (angle p1 p2)) (setvar "osmode" 0) (cond ( (and (>= (deltartd ang) 0) (< (deltartd ang) 45)) (setq StartPt (polar p1 (deltadtr 45) 1.412)) (setq EndPt (polar StartPt (deltadtr 0) 2.0))) ( (and (>= (deltartd ang) 45) (< (deltartd ang) 90)) (setq StartPt (polar p1 (deltadtr 45) 1.412)) (setq EndPt (polar StartPt (deltadtr 90) 2.0))) ( (and (>= (deltartd ang) 90) (< (deltartd ang) 135)) (setq StartPt (polar p1 (deltadtr 135) 1.412)) (setq EndPt (polar StartPt (deltadtr 90) 2.0))) ( (and (>= (deltartd ang) 135) (< (deltartd ang) 180)) (setq StartPt (polar p1 (deltadtr 135) 1.412)) (setq EndPt (polar StartPt (deltadtr 180) 2.0))) ( (and (>= (deltartd ang) 180) (< (deltartd ang) 225)) (setq StartPt (polar p1 (deltadtr 225) 1.412)) (setq EndPt (polar StartPt (deltadtr 180) 2.0))) ( (and (>= (deltartd ang) 225) (< (deltartd ang) 270)) (setq StartPt (polar p1 (deltadtr 225) 1.412)) (setq EndPt (polar StartPt (deltadtr 270) 2.0))) ( (and (>= (deltartd ang) 270) (< (deltartd ang) 315)) (setq StartPt (polar p1 (deltadtr 315) 1.412)) (setq EndPt (polar StartPt (deltadtr 270) 2.0))) ( (and (>= (deltartd ang) 315) (< (deltartd ang) 360)) (setq StartPt (polar p1 (deltadtr 315) 1.412)) (setq EndPt (polar StartPt (deltadtr 0) 2.0))) ) (command "_line" StartPt EndPt "" ) ) Regards Tony Looks OK, you don't really need this ( (and (>= (deltartd ang) 315) (< (deltartd ang) 360)) (setq StartPt (polar p1 (deltadtr 315) 1.412)) (setq EndPt (polar StartPt (deltadtr 0) 2.0))) it could be written as : ( t (setq StartPt (polar p1 (deltadtr 315) 1.412)) (setq EndPt (polar StartPt (deltadtr 0) 2.0))) since anything reaching this point will be >= 315. Everyone has their own coding style. Leave it as it is to be safe. Quote Link to comment Share on other sites More sharing options...
dlanorh Posted May 19, 2018 Share Posted May 19, 2018 Just noticed this (setvar "osmode" 0) If you are changing system variables you should restore them to their pre run state you can use some of these or search the forum for others (defun c:???? ( / cmde .....) (setq cmde (getvar 'cmdecho)) ;then at the end (setvar 'cmdecho cmde) );end_defun ;or (defun c:???? ( / cmde .....) (if (/= (getvar 'cmdecho) 0) (setq cmde (getvar 'cmdecho))) ;this only stores it if its not 0 ;at the end (if cmde (setvar 'cmdecho cmde));this restores it if its got a value );end_defun Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted May 19, 2018 Share Posted May 19, 2018 Unless I've misunderstood, I think the code could be reduced to something like this: (defun c:ref ( / a1 p1 p2 ) (and (setq p1 (getpoint "\nPick a line reference point: ")) (setq p2 (getpoint p1 "\nPick a location point: ")) (setq a1 (angle p1 p2) p1 (polar p1 (- (LM:roundm (+ a1 (/ pi 4)) (/ pi 2)) (/ pi 4)) (sqrt 2)) ) (entmake (list '(0 . "LINE") (cons 10 p1) (cons 11 (polar p1 (LM:roundm a1 (/ pi 2)) 2)))) ) (princ) ) ;; Round Multiple - Lee Mac ;; Rounds 'n' to the nearest multiple of 'm' (defun LM:roundm ( n m ) (* m (fix ((if (minusp n) - +) (/ n (float m)) 0.5))) ) Quote Link to comment Share on other sites More sharing options...
hanhphuc Posted May 19, 2018 Share Posted May 19, 2018 (edited) FWIW (and ([color="blue"]>[/color] x 0 ) ([color="blue"]<[/color] x 90) ) same ([color="blue"]<[/color] 0 x 90) David's grread idea (defun c:ref1 (/ l p1 p2 ang i cur ls ) (and (setq l (mapcar ''((l)(mapcar ''((x) (* pi x)) l)) '((0.0 0.5 1.0 1.5 2.0)(0.25 0.75 1.25 1.75))) p1 (getpoint "\nPick line reference point.. ") ) (while (and (setq cur (grread t 13 0)) (=(car cur ) 5) ) (redraw) (setq p2 (cadr cur) ang (angle p1 [color="red"]p2[/color] ) ls (mapcar ''((a b) (polar p1 (nth ((if (equal ang (caar l) 1e-4) + 1- ) (vl-position ang (vl-sort (cons ang (car l)) '<))) a) b)) l (list 2. (/ 1. (abs (cos (* pi 0.25))))) ) ) (foreach p ls (grdraw p1 p 1 1) ) ) ) (princ) ) not sure? (defun c:ref2 (/ p1 p2 a r cur ) (and (setq p1 (getpoint "\nPick line reference point.. ") ) (setq p2 (getpoint p1 "\nPick line destination point.. ") ) (setq r (distance p1 p2)) (while (and (setq cur (grread t 13 0)) (= (car cur ) 5) ) (redraw) (setq p2 (cadr cur) a (angle p1 p2) p2 (polar p1 (- a ([color="blue"]rem[/color] a (* 0.25 pi))) r ) ) (grdraw p1 p2 1 1) ) ) (princ) ) happy weekend Edited May 20, 2018 by hanhphuc added example Quote Link to comment Share on other sites More sharing options...
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.