Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Length and width output of the rectangle

7 REPLIES 7
Reply
Message 1 of 8
gringooo
1790 Views, 7 Replies

Length and width output of the rectangle

Hi all!

 

sime time ago i found a lisp in the net to create a rectangle by marking objects in a draw.

Very usefull every day!

But it would nice to get at the end of the lisp after creating the rectangle a output in the commandline about the length and with of the created rectangle.

 

someone here who can add the code for the lenght/width output?

 

many thanks!

 

(defun C:wer (/ AUSWAHL INDEX ELEM XYMIN XYMAX MINX MAXX MINY MAXY DATA A)
  (vl-load-com)
  (if(and(or (and (setq AUSWAHL (ssget "I")) (> (sslength AUSWAHL) 0))
            (and (setq AUSWAHL (ssget)) (> (sslength AUSWAHL) 0))
        )
        (or(setq A (getreal "\nRandabstand <0> : "))
            (setq A 0.0)
        )
    )   
    (progn
      (setq INDEX -1)
      (repeat (sslength AUSWAHL)
        (vla-getboundingbox
          (vlax-ename->vla-object
            (ssname AUSWAHL (setq INDEX (1+ INDEX)))
          )
          'XYMIN
          'XYMAX
        )
        (setq DATA (cons (list (vlax-safearray->list XYMIN)
                              (vlax-safearray->list XYMAX)
                        )
                        DATA
                  )
        )
      )
      (setq MINX (-(eval (cons 'min (mapcar 'caar  DATA)))A))
      (setq MINY (-(eval (cons 'min (mapcar 'cadar  DATA)))A))
      (setq MAXX (+(eval (cons 'max (mapcar 'caadr  DATA)))A))
      (setq MAXY (+(eval (cons 'max (mapcar 'cadadr DATA)))A))
      (vla-AddLightweightPolyline
        (if(=(vla-get-activespace(vla-get-activedocument(vlax-get-acad-object)))
            acModelSpace
          )
          (vla-get-modelspace(vla-get-activedocument(vlax-get-acad-object)))
          (vla-get-paperspace(vla-get-activedocument(vlax-get-acad-object)))
        )
        (vlax-safearray-fill
          (vlax-make-safearray vlax-vbdouble (cons 0 9))
          (list
            MINX MINY MAXX MINY MAXX MAXY MINX MAXY MINX MINY
          )         
        )
      )             
    )
  )
  (princ)
)

 

 

don´t now the autor of the lisp, can´t remember.

all creatits for sure to the autor!

MANY THANKS

 

 

regards

Ingo

7 REPLIES 7
Message 2 of 8
Ajilal.Vijayan
in reply to: gringooo

Try with this code

Spoiler
(defun C:wer (/ AUSWAHL INDEX ELEM XYMIN XYMAX MINX MAXX MINY MAXY DATA A)
  (vl-load-com)
  (if(and(or (and (setq AUSWAHL (ssget "I")) (> (sslength AUSWAHL) 0))
            (and (setq AUSWAHL (ssget)) (> (sslength AUSWAHL) 0))
        )
        (or(setq A (getreal "\nRandabstand <0> : "))
            (setq A 0.0)
        )
    )   
    (progn
      (setq INDEX -1)
      (repeat (sslength AUSWAHL)
        (vla-getboundingbox
          (vlax-ename->vla-object
            (ssname AUSWAHL (setq INDEX (1+ INDEX)))
          )
          'XYMIN
          'XYMAX
        )
        (setq DATA (cons (list (vlax-safearray->list XYMIN)
                              (vlax-safearray->list XYMAX)
                        )
                        DATA
                  )
        )
      )
      (setq MINX (-(eval (cons 'min (mapcar 'caar  DATA)))A))
      (setq MINY (-(eval (cons 'min (mapcar 'cadar  DATA)))A))
      (setq MAXX (+(eval (cons 'max (mapcar 'caadr  DATA)))A))
      (setq MAXY (+(eval (cons 'max (mapcar 'cadadr DATA)))A))
      (vla-AddLightweightPolyline
        (if(=(vla-get-activespace(vla-get-activedocument(vlax-get-acad-object)))
            acModelSpace
          )
          (vla-get-modelspace(vla-get-activedocument(vlax-get-acad-object)))
          (vla-get-paperspace(vla-get-activedocument(vlax-get-acad-object)))
        )
        (vlax-safearray-fill
          (vlax-make-safearray vlax-vbdouble (cons 0 9))
          (list
            MINX MINY MAXX MINY MAXX MAXY MINX MAXY MINX MINY
          )         
        )
      )             
    )
  )
  

(print (strcat "Length is " (rtos ( - MAXX MINX))))
(print (strcat "Width is " (rtos ( - MAXY MINY))))
(print (strcat "Rectangle of " (rtos ( - MAXX MINX))"[Length]" " X " (rtos ( - MAXY MINY))"[Width]" " Created")) (princ) )

 

 

Message 3 of 8
gringooo
in reply to: Ajilal.Vijayan

PERFECT!

 

i use it now with this code...

THANKS for quick help!

 

 

(defun C:wer (/ AUSWAHL INDEX ELEM XYMIN XYMAX MINX MAXX MINY MAXY DATA A)
  (vl-load-com)
  (if(and(or (and (setq AUSWAHL (ssget "I")) (> (sslength AUSWAHL) 0))
            (and (setq AUSWAHL (ssget)) (> (sslength AUSWAHL) 0))
        )
        (or(setq A (getreal "\nRandabstand <0> : "))
            (setq A 0.0)
        )
    )   
    (progn
      (setq INDEX -1)
      (repeat (sslength AUSWAHL)
        (vla-getboundingbox
          (vlax-ename->vla-object
            (ssname AUSWAHL (setq INDEX (1+ INDEX)))
          )
          'XYMIN
          'XYMAX
        )
        (setq DATA (cons (list (vlax-safearray->list XYMIN)
                              (vlax-safearray->list XYMAX)
                        )
                        DATA
                  )
        )
      )
      (setq MINX (-(eval (cons 'min (mapcar 'caar  DATA)))A))
      (setq MINY (-(eval (cons 'min (mapcar 'cadar  DATA)))A))
      (setq MAXX (+(eval (cons 'max (mapcar 'caadr  DATA)))A))
      (setq MAXY (+(eval (cons 'max (mapcar 'cadadr DATA)))A))
      (vla-AddLightweightPolyline
        (if(=(vla-get-activespace(vla-get-activedocument(vlax-get-acad-object)))
            acModelSpace
          )
          (vla-get-modelspace(vla-get-activedocument(vlax-get-acad-object)))
          (vla-get-paperspace(vla-get-activedocument(vlax-get-acad-object)))
        )
        (vlax-safearray-fill
          (vlax-make-safearray vlax-vbdouble (cons 0 9))
          (list
            MINX MINY MAXX MINY MAXX MAXY MINX MAXY MINX MINY
          )         
        )
      )             
    )
  )
(print (strcat (rtos ( - MAXX MINX)) " / " (rtos ( - MAXY MINY)) ))
  (princ)
)

 

Message 4 of 8
Lee_Mac
in reply to: gringooo

Here is another way to write it:

 

(defun c:wer ( / a b s )
    (if
        (and
            (setq s (ssget))
            (setq b (LM:ssboundingbox s))
        )
        (progn
            (initget 4)
            (setq a (cond ((getdist "\nRandabstand <0.0>: ")) (0.0)))
            (entmake
                (append
                   '(
                        (000 . "LWPOLYLINE")
                        (100 . "AcDbEntity")
                        (100 . "AcDbPolyline")
                        (090 . 4)
                        (070 . 1)
                    )
                    (mapcar
                       '(lambda ( w x )
                            (cons 10 (mapcar '(lambda ( y z ) ((eval z) ((eval y) b) a)) w x))
                        )
                       '(
                            (caar   cadar)
                            (caadr  cadar)
                            (caadr cadadr)
                            (caar  cadadr)
                        )
                       '(   (- -) (+ -) (+ +) (- +)   )
                    )
                )
            )
            (princ
                (strcat "\n"
                    (rtos (+ a a (- (caadr  b) (caar  b)))) " / "
                    (rtos (+ a a (- (cadadr b) (cadar b))))
                )
            )
        )
    )
    (princ)
)

;; Selection Set Bounding Box  -  Lee Mac
;; Returns a list of the lower-left and upper-right WCS coordinates of a
;; rectangular frame bounding all objects in a supplied selection set.
;; s - [sel] Selection set for which to return bounding box

(defun LM:ssboundingbox ( s / a b i m n o )
    (repeat (setq i (sslength s))
        (if
            (and
                (setq o (vlax-ename->vla-object (ssname s (setq i (1- i)))))
                (vlax-method-applicable-p o 'getboundingbox)
                (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list o 'a 'b))))
            )
            (setq m (cons (vlax-safearray->list a) m)
                  n (cons (vlax-safearray->list b) n)
            )
        )
    )
    (if (and m n)
        (mapcar '(lambda ( a b ) (apply 'mapcar (cons a b))) '(min max) (list m n))
    )
)
(vl-load-com) (princ)

 

The above uses my Selection Set Bounding Box function.

 

Lee

Message 5 of 8
Kent1Cooper
in reply to: gringooo


@gringooo wrote:

.... it would nice to get at the end of the lisp after creating the rectangle a output in the commandline about the length and with of the created rectangle.

.... 


It doesn't look as though that routine would ever draw a non-orthogonal rectangle, but for anyone needing the dimensions of such a thing, this would report the size [in current drawing units] of a rectangle drawn at any angle and in any Coordinate System:

 

(vl-load-com); if needed

(prompt

  (strcat

    "\nRectangle is "

    (rtos (distance (vlax-curve-getStartPoint (entlast)) (vlax-curve-getPointAtParam (entlast) 1)))

    " x "

    (rtos (distance (vlax-curve-getPointAtParam (entlast) 1) (vlax-curve-getPointAtParam (entlast) 2)))

    "."

  ); strcat

); prompt

(princ)

Kent Cooper, AIA
Message 6 of 8
gringooo
in reply to: Kent1Cooper

hi lee_mac,

nice to see you again. you write the code new just for fun. cool. works also perfekt! witch one makes a better job or is smoother for daily use?
Message 7 of 8
Lee_Mac
in reply to: gringooo


gringooo wrote:
hi lee_mac,

nice to see you again. you write the code new just for fun. cool. works also perfekt! witch one makes a better job or is smoother for daily use?

Thank you gringooo Smiley Happy

 

My code will be slightly more efficient due to the use of separate lists for the lower-left & upper-right coordinates of the bounding box, and also because entmake is faster for entity generation than the equivalent ActiveX methods or in-built commands.

 

My version also includes error trapping to account for objects which either do not support the getboundingbox method or return an error when the method is applied (as is the case with some objects found in the Autodesk Vertical applications).

 

Lee

Message 8 of 8
gringooo
in reply to: Lee_Mac

right. some times with some objects i got errors/no rectangle!

 

try it in future with your code! thanks! Smiley Very Happy

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost