Jump to content

Draw rectangle


spiker7221

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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)  

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)
)

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...