Jump to content
spiker7221

Draw rectangle

Recommended Posts

spiker7221

Learning how POLAR works by creating a rectangle at offset points from the midpoint of a line. It works sometimes depending on the rotation of the line selected. LtoR seems to work, but RtoL is off a bit. I'm pretty sure I need some sort of COND statement like...

 

(if (and (>= entrot 0.0) (<= entrot 90.0))

Here's what I've come up with so far...

 

(defun C:drawbox (/)

   (defun dtr (a)
     (/ (* a 180.0) pi)
   )

   (progn
       (setq ent (car (entsel)))
       (setq entlay (cdr (assoc 8 (entget ent))))
       (setq opt1 (cdr (assoc 10 (entget ent))))
       (setq opt2 (cdr (assoc 11 (entget ent))))
       (setq ang1 (angle opt1 opt2))
       (setq fang (dtr ang1))
       (setq tmpt (/ (distance opt1 opt2) 2.0))
       (setq mpt (polar opt1 ang1 tmpt))
   );end progn
       
   (progn
       (setq d6 18.0)
       (setq a6 5.0)
       (setq npt6 (polar mpt (- fang a6) d6))
       (command "text" npt6 "2.0" fang "6")

       (setq d7 16.0)
       (setq a7 14.0)
       (setq npt7 (polar mpt (- fang a7) d7))
       (command "text" npt7 "2.0" fang "7")

       (setq d8 18.0)
       (setq a8 7.0)
       (setq npt8 (polar mpt (- fang a8) d8))
       (command "text" npt8 "2.0" fang "8")

       (setq d9 18.0)
       (setq a9 14)
       (setq npt9 (polar mpt (+ fang a9) d9))
       (command "text" npt9 "2.0" fang "9")

       (command "pline" npt8 npt9 npt6 npt7 "c")
   );end progn
)

Thanks for any help,

Mike

Share this post


Link to post
Share on other sites
jdiala

Try this but without the text. Change some variables -> less typing.

 

(defun C:drawbox (/ e lay p1 p2 a mid d l w npt1 npt2 npt3 npt4 )
(setq e   (car (entsel))
     lay  (cdr (assoc 8 (entget e))) ;; not needed
     p1   (cdr (assoc 10 (entget e)))
     p2   (cdr (assoc 11 (entget e)))
     a    (angle p1 p2)
     mid  (mapcar '(lambda (a b ) (/ (+ a b ) 2.)) p1 p2)
     l    (distance p1 p2) 
     w    10 ;;; width of rectangle
     npt1 (polar (polar mid (+ a (/ pi 2.)) (/ w 2. ))	(+ a pi) (/ l 2.)) 
     npt2 (polar npt1 (+ a (* pi 1.5)) w)
     npt3 (polar npt2 a l)
     npt4 (polar npt3 (+ a (/ pi 2.)) w)
)
(command "_.pline" "_non" npt1 "_non" npt2 "_non" npt3 "_non" npt4 "c")
(princ)
)

Edited by jdiala

Share this post


Link to post
Share on other sites
BIGAL

A simple fix but it needs your co-operation you don't pick near mid rather always what you want as near left end this way it will always create the rectang on the correct side. Even upside down. This is an oldie pretty sure there is a shorter VL lisp version.

 

(defun sel_obj ()
 (SETQ TP1 (entsel "\nSelect inside wall near left end: "))
 (setq tpp1 (entget (car tp1)))
 (setq clay (cdr (assoc 8 tpp1)))
 (setq p1 (cdr (assoc 10 tpp1)))
 (setq p2 (cdr (assoc 11 tpp1)))
 (setq p3 (cadr tp1))
 (setq ht (cdr (assoc 40 tpp1)))
 (setq el (caddr p1))
 (setq d1 (distance p1 p3))
 (setq d2 (distance p2 p3))
   (if (> d1 d2)
   (progn
   (setq temp p1)
   (setq p1 p2)
   (setq p2 temp)
   )
   )                  
   (setq ang (angle p1 p2))
 (setq ang1 (+ ang 1.5708))
)
(sel_obj)  

Share this post


Link to post
Share on other sites
David Bethel

a couple things jump out

(setq ang1 (angle opt1 opt2))
(setq fang (dtr ang1))

 

(angle) returns radians

no further conversions are needed

 

 

As an aside, the (dtr) call you have is really a RadianToAngle conversion

 

(defun AtoR (a) (* pi (/ a 180.0)))            ;Angle To Radian
(defun RtoA (r) (/ (* r 180.0) pi))            ;Radian To Angle

 

A (polar) call required the angle in radians.

 

-David

Share this post


Link to post
Share on other sites
spiker7221

Good morning/afternoon JDiala,

Here's your modified code to work for my scenerio..

 

            (setq entmpt (mapcar '(lambda (ang1 b ) (/ (+ ang1 b ) 2.)) pt1a pt2a))
           (setq npt6 (polar (polar entmpt (+ ang1 (/ pi 2.)) (/ w 2.0 ))    (+ ang1 pi) (/ l 2.)))
           (setq npt7 (polar npt6 (+ ang1 (* pi 1.5)) w))
           (setq npt8 (polar npt7 ang1 l))
           (setq npt9 (polar npt8 (+ ang1 (/ pi 2.)) w))
;........................ capture text ...................................

           (setq entxt (ssget "_WP" (list npt8 npt9 npt6 npt7)
                       '((0 . "TEXT") (8 . "A-ANNO*"))
                       );end ssget
           );end setq

 

It works great...if my lines are drawn from left to right. I know how to capture for the entity rotation, but how do I flip the rectangle calculation to flip as well?

 

Thanks,

Mike

 

Try this but without the text. Change some variables -> less typing.

 

(defun C:drawbox (/ e lay p1 p2 a mid d l w npt1 npt2 npt3 npt4 )
(setq e   (car (entsel))
     lay  (cdr (assoc 8 (entget e))) ;; not needed
     p1   (cdr (assoc 10 (entget e)))
     p2   (cdr (assoc 11 (entget e)))
     a    (angle p1 p2)
     mid  (mapcar '(lambda (a b ) (/ (+ a b ) 2.)) p1 p2)
     l    (distance p1 p2) 
     w    10 ;;; width of rectangle
     npt1 (polar (polar mid (+ a (/ pi 2.)) (/ w 2. ))    (+ a pi) (/ l 2.)) 
     npt2 (polar npt1 (+ a (* pi 1.5)) w)
     npt3 (polar npt2 a l)
     npt4 (polar npt3 (+ a (/ pi 2.)) w)
)
(command "_.pline" "_non" npt1 "_non" npt2 "_non" npt3 "_non" npt4 "c")
(princ)
)

Share this post


Link to post
Share on other sites
jdiala

Can you post a picture on what you are trying to do. Where do you want your first point? will it clockwise or counter clockwise?

Edited by jdiala

Share this post


Link to post
Share on other sites
jdiala

It works great...if my lines are drawn from left to right. I know how to capture for the entity rotation, but how do I flip the rectangle calculation to flip as well?

 

 

Like this:

 

(defun C:drawbox (/ e lay p1 p2 a mid d l w npt1 npt2 npt3 npt4 )
(setq e   (car (entsel))
     lay  (cdr (assoc 8 (entget e))) ;; not needed
     p1   (cdr (assoc 10 (entget e)))
     p2   (cdr (assoc 11 (entget e)))
     a    (LM:MakeReadable (angle p1 p2))
     mid  (mapcar '(lambda (a b ) (/ (+ a b ) 2.)) p1 p2)
     l    (distance p1 p2) 
     w    10 ;;; width of rectangle
     npt1 (polar (polar mid (+ a (/ pi 2.)) (/ w 2. ))	(+ a pi) (/ l 2.)) 
     npt2 (polar npt1 (+ a (* pi 1.5)) w)
     npt3 (polar npt2 a l)
     npt4 (polar npt3 (+ a (/ pi 2.)) w)
)
(command "_.pline" "_non" npt1 "_non" npt2 "_non" npt3 "_non" npt4 "c")
(setq entxt (ssget "_CP" (list npt1 npt2 npt3 npt4)
                       '((0 . "TEXT") (8 . "A-ANNO*"))
                       );end ssget
           )
(princ)
)
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
(defun LM:MakeReadable ( a )
 ((lambda ( a )
     (cond
       ( (and (> a (/ pi 2)) (<= a pi)) (- a pi))
       ( (and (> a pi) (<= a (/ (* 3 pi) 2))) (+ a pi))
       ( a )
     )
   )
   (rem a (* 2 pi))
 )
)

Edited by jdiala

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×