Grrr Posted May 13, 2016 Posted May 13, 2016 Hi guys, I'm trying to think of a way to draw a square, in the same way circle is drawn, by specifying its centerpoint and radius. For the square it would be a/2. I'm thinking of drawing the circle, then do some point listing and draw a rectangle in that square shape (which is inscribed around the circle) and in the end entdel the circle. Does anyone have better ideas, or how i could find those 4 points? I'm not on my laptop so i can't post any code from here, sorry! EDIT: I just figured that getting the bounding box of the circle might do the job a lot easier. Quote
ReMark Posted May 13, 2016 Posted May 13, 2016 Did you know there are lisp routines that will draw a rectangle by picking a center point first? Quote
Grrr Posted May 13, 2016 Author Posted May 13, 2016 Even easier than I thought, thanks Tharwat! Remark, Yes i was aware of such routines, but I'm not as good when it comes with dealing point coordinates(using angles and polar function). However, right after I posted this thread I've found my answer - by using circle's bounding box. This method would also allow me to draw the square by its side A - by drawing the circle by its diameter first. Thanks! Quote
dbroada Posted May 13, 2016 Posted May 13, 2016 Hi, command: POLYGON , 4 I an hour late I almost posted this solution. I'm glad I read all the replies first (for a change). Quote
Tharwat Posted May 13, 2016 Posted May 13, 2016 Even easier than I thought, thanks Tharwat! You are welcome. Yes i was aware of such routines, but I'm not as good when it comes with dealing point coordinates(using angles and polar function). This could be the best opportunity for you to start with since it is a simple thing to do. A few codes for you to learn from but the way I used to retrieve the four points may seem a bit difficult to know if you don't comprehend the mechanism process of mapcar & lambda functions. Anyway have a look at the following codes and I left out the last action which should draw the closed polyline and hope you don't use the command rectang to finish this. (if (and (setq c (car (entsel "\nCircle please:"))) (eq (cdr (assoc 0 (setq e (entget c)))) "CIRCLE") ) (progn (setq r (cdr (assoc 40 e)) p (cdr (assoc 10 e)) ) (mapcar '(lambda (x) (setq l (cons (list (polar x (* pi 0.5) r) (polar x (* pi 1.5) r) ) l))) (list (polar p pi r) (polar p 0. r) ) ) (setq l (apply 'append l)) ) ) an hour late I almost posted this solution. I'm glad I read all the replies first (for a change). Hi dbroada, I searched for your reply that could be similar to this issue but could not find any. can you clarify this? Quote
Lee Mac Posted May 13, 2016 Posted May 13, 2016 Alternatively: (defun c:test ( / cen ent enx rad ) (if (and (setq ent (car (entsel "\nSelect circle: "))) (= "CIRCLE" (cdr (assoc 0 (setq enx (entget ent))))) ) (progn (setq cen (trans (cdr (assoc 10 enx)) ent 1) rad (cdr (assoc 40 enx)) ) (command "_.rectang" "_non" (mapcar '- cen (list rad rad)) "_non" (mapcar '+ cen (list rad rad)) ) ) ) (princ) ) Quote
Grrr Posted May 13, 2016 Author Posted May 13, 2016 Thanks alot! I'll practice when I get home and I'll post the result later. I just have one more question regarding the object's rotation: for example I know this method: (command "_.rotate" (entlast) "" cenbbox 45) This should rotate the object from its bounding box's center on 45 deg, but I was wondering is it possible to avoid the send command method and use instead some vla functions, like vla-put-rotation? Btw, Lee's code is very clear since I understood how it works without testing it. Quote
dbroada Posted May 13, 2016 Posted May 13, 2016 I searched for your reply that could be similar to this issue but could not find any. can you clarify this?I didn't post it. You were faster than me. Quote
Tharwat Posted May 13, 2016 Posted May 13, 2016 I didn't post it. You were faster than me. Ohh. sorry for that. Quote
Grrr Posted May 13, 2016 Author Posted May 13, 2016 Tharwat and Lee mac, I didn't had the chance to try your routines, but later I will. However, I managed to code the thing I was asking for [in a probably weird way] : ; First you draw a line (x------x), and then it draws the square like this: ; ***************************** ; the drawn square -> * * ; * * ; * * ; * * ; x-------------*-------------x <- the drawn line ; * * ; * * ; * * ; * * ; ***************************** (defun C:Square-FullSide ( / *error* oldcmech oldosm tmp-line vla-tmp-line end1 end2 midpt line-ang tmp-circle vla-tmp-circle bbox mn mx rec) (defun *error* ( msg ) (if oldosm (setvar 'osmode oldosm)) (if oldcmech (setvar 'cmdecho oldcmech)) (if (and (= 'ename (type tmp-line)) (entget tmp-line)) (entdel tmp-line) ) (if (and (= 'ename (type tmp-circle)) (entget tmp-circle)) (entdel tmp-circle) ) (if (not (member msg '("Function cancelled" "quit / exit abort"))) (princ (strcat "\nError: " msg)) ) (princ) ) (while (or (command "_.line" pause pause "") (and (setq tmp-line (entlast)) (eq (cdr (assoc 0 (entget tmp-line))) "LINE") ) ) (progn (setq oldcmech (getvar 'cmdecho)) (setq oldosm (getvar 'osmode)) (setvar 'cmdecho 0) (setvar 'osmode 0) (setq end1 (cdr (assoc 10 (entget tmp-line)))) (setq end2 (cdr (assoc 11 (entget tmp-line)))) (setq midpt (mid end1 end2)) (setq ang (angle end1 end2)) (setq vla-tmp-line (vlax-ename->vla-object tmp-line)) (setq line-ang (vla-get-angle vla-tmp-line)) (command "_.circle" "2p" end1 end2) (setq tmp-circle (entlast)) (setq vla-tmp-circle (vlax-ename->vla-object tmp-circle)) (setq bbox (vla-getboundingbox vla-tmp-circle 'mn 'mx)) (command "_.rectangle" (trans (vlax-safearray->list mn) 0 1) (trans (vlax-safearray->list mx) 0 1) ) (setq rec (entlast)) (command "_.rotate" rec "" midpt (angtos line-ang)) (vla-delete vla-tmp-circle) (vla-delete vla-tmp-line) (setvar 'osmode oldosm) (setvar 'cmdecho oldcmech) );progn );if (princ) ) (defun mid (p1 p2) (mapcar '(lambda (x1 x2) (/ (+ x1 x2) 2.0)) p1 p2) ) ; First you draw a line (x------x), and then it draws the square like this: ; ***************************** ; the drawn square -> * * ; * * ; * * ; * * ; * x-------------x <- the drawn line ; * * ; * * ; * * ; * * ; ***************************** (defun C:Square-Halfside (/) (while T (command "_.polygon" 4 pause "C" pause) ) (princ) ) These are 2 methods for the routine [see the graphs in the code]. Now I'll try to learn something new from your codes, since I will need to work with these vector functions for my other ideas. Thanks again! Quote
Grrr Posted May 13, 2016 Author Posted May 13, 2016 I don't know what marko's codes looks like, and I wouldn't bother to remove his name from the code if it was his. I managed to do it by collecting examples from you and Lee mac, and by understanding entity and vla-objects from your help. Quote
Tharwat Posted May 13, 2016 Posted May 13, 2016 Did you read or try my simple codes that I posted for you? Quote
Grrr Posted May 13, 2016 Author Posted May 13, 2016 Not yet, but by looking at it I can expect how it would work. Its missing that vector line I need to define the square rotation, try the routine I posted. Still I have there 4 send commands - and I'm not sure can I avoid them [well maybe some of them with entmake]. And the 4th send command is the rotation issue I asked for in post #8. EDIT: Well your first example I implemented in the "Square-HalfSide" function. Quote
Tharwat Posted May 13, 2016 Posted May 13, 2016 You did not mention how you would like to create that rectangle ! Do you have a circle and you wan to select it then draw the box or specify a point with radius then draw a circle wrapped with a box .... etc? Quote
Grrr Posted May 13, 2016 Author Posted May 13, 2016 Sorry Tharwat, But for what I was asking for, your first post gave me the needed answer! Then i just decided to do this other method like in the "Square-FullSide" function. Your 2nd example and Lee's are great, I know they require existing circle, but I have no problems at modifying the codes. I don't blame you at anything, just wanted to show the results that might be useful to someone. And yeah, the code I posted is based on what I have learnt from you and Lee in this forum... Quote
Tharwat Posted May 13, 2016 Posted May 13, 2016 Simply like this would be enough in my opinion: (defun c:test (/ p1 p2 ang dis lst) (if (and (setq p1 (getpoint "\nSpecify first point :")) (setq p2 (getpoint "\nNext point :" p1)) ) (progn (setq p1 (trans p1 1 0) p2 (trans p2 1 0) ang (angle p1 p2) dis (/ (distance p1 p2) 2.) ) (mapcar '(lambda (x) (setq lst (cons (list (polar x (+ ang (* pi 0.5)) dis) (polar x (+ ang (* pi 1.5)) dis) ) lst))) (list p1 p2) ) (setq lst (apply 'append lst)) (entmakex (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) '(70 . 1) (cons 10 (car lst)) (cons 10 (cadr lst)) (cons 10 (last lst)) (cons 10 (caddr lst)) ) ) ) ) (princ) ) Quote
Lee Mac Posted May 13, 2016 Posted May 13, 2016 Here's an alternative method: (defun c:test ( / a b n p q u v w ) (and(setq p (getpoint "\nSpecify 1st point: ")) (setq q (getpoint "\nSpecify 2nd point: " p)) (setq v (mapcar '/ (mapcar '- q p) '(2 2)) u (list (- (cadr v)) (car v)) w (list (cadr v) (- (car v))) n (trans '(0 0 1) 1 0 t) ) (entmake (vl-list* '(000 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(090 . 4) '(070 . 1) (cons 210 n) (mapcar '(lambda ( a b ) (cons 10 (trans (mapcar '+ a b) 1 n))) (list p q q p) (list w w u u) ) ) ) ) (princ) ) Quote
BIGAL Posted May 14, 2016 Posted May 14, 2016 My $0.05 for a rectang Pick pt, Enter L, Enter W, Enter ang and use 1st principles of a double polar to work out cnr points Make the w blank and you get a square, same with angle if blank then its horizontal. Look at lisp below. Maybe drag a line For L same for W but use centre pt of first line. Like others their is probably one already done I just added the front end. simplepit.lsp 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.