Simple Bounding Box Code

Simple Bounding Box Code

kotropol
Contributor Contributor
4,645 Views
7 Replies
Message 1 of 8

Simple Bounding Box Code

kotropol
Contributor
Contributor

Hi,

 

I am fairly new to AutoLisp, but I tried to make my onw Bounding Box lisp. I am aware of Lee Mac's Bounding Box lisp but it is too complex for my current understanding of AutoLisp and plus I want to make my own. So, I wrote my code but I have 2 problems.

1) I am getting the following eror:

; error; bad argument type: lentityp

In order to fix this I tried to change the (ssget) with (car(entsel)) but then I lost the ability to multi-select objects.

2) I can't figure out how to draw the rectangle. With RECTANG command I need only 2 points to draw a rectangle (first corner point and other corner point). So why my code is wrong? 

 

(defun c:BoundingBox()

   (setq SelectionSet (vlax-ename->vla-object (ssget)))
   (vla-GetBoundingBox SelectionSet 'minExt 'maxExt)
   (setq minExt (vlax-safearray->list minExt)
      maxExt (vlax-safearray->list maxExt))

   (princ minExt)
   (princ maxExt)
   (princ)

   (setq StartPoint (vlax-3D-point (car(minExt)) (cadr(minExt)) (caddr(minEnt))))
   (setq EndPoint (vlax-3D-point (car(maxExt)) (cadr(maxExt)) (caddr(maxEnt))))
   (setq ModelSpace (vla-get-ModelSpace doc))
   (vla-AddPolyline ModelSpace StartPoint EndPoint)
)

 

If possible I don't wath the code to be more complicated than this.

 

Thanks in advance!

0 Likes
Accepted solutions (1)
4,646 Views
7 Replies
Replies (7)
Message 2 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

The (ssget) function returns a selection set [which could potentially have multiple objects in it], but the (vlax-ename->vla-object) function requires an entity name [of a single entity].  You could just replace the (ssget) with (car (entsel)), or you could get more sophisticated about it.

EDIT:  Also, what if you're in Paper Space when you run it?  Do you want the result in Model space even then?  You're already in the appropriate space if you're selecting an object to draw the box around, and you already have the opposite corner points [your minExt and maxExt variables], so I would just use a RECTANG command and give it those.  I see no need for the last three variables that are set.

Further EDIT:  Now I notice your reference to multiple object selection [wasn't paying close enough attention at first].  You would need to get the bounding box for every object in a selection set, and compare them to each other to find the overall extents of all of them.  I have a routine to draw the bounding box around multiple objects, if you want to see how I did that comparison.

Kent Cooper, AIA
0 Likes
Message 3 of 8

kotropol
Contributor
Contributor
Thanks for your respone! I would appreciate if you could share your routine with me! My last question now is the RECTANG command. I wrote the following and I am getting the bad function error:
(command "rectangle" (car(minExt) (cadr(minExt) (caddr(minExt)) ((car(maxExt) (cadr(maxExt) (caddr(maxExt)) "")
0 Likes
Message 4 of 8

Kent1Cooper
Consultant
Consultant

@kotropol wrote:
.... I wrote the following and I am getting the bad function error:
(command "rectangle" (car(minExt) (cadr(minExt) (caddr(minExt)) ((car(maxExt) (cadr(maxExt) (caddr(maxExt)) "")

You would need to wrap a (list) function around those sets of coordinates, and fix some parentheses problems.  But you don't need those -- you already have the points as points in the minExt and maxExt variables.  Just use those [and the correct command name]:

(command "_.rectang" "_non" minExt "_non" maxExt)

 

See my approach in the attached.

Kent Cooper, AIA
0 Likes
Message 5 of 8

komondormrex
Mentor
Mentor

and if the original set of lines to be rectangle is somewhat rotated?

haven't meant to post it here)

0 Likes
Message 6 of 8

Kent1Cooper
Consultant
Consultant

@komondormrex wrote:

and if the original set of lines to be rectangle is somewhat rotated?


The bounding box approach won't follow the rotation, but will be orthogonal to the furthest extent of the object(s) [dashed green here].

Kent1Cooper_0-1729859531213.png

BUT you could at least get close with the SR command in SmallestRectangle.lsp, >here<.  It checks at 1° intervals of rotation [though that increment could be reduced], so it may not land exactly on your source object's rotation.  Look at other offerings in that same Topic.

Kent Cooper, AIA
0 Likes
Message 7 of 8

komondormrex
Mentor
Mentor

as a matter of a sort of interest. and it is not a bounding box whatsoever but rather an apparent outlining rectangle or quadrangle formed by 2 vertices plines or lines...

komondormrex_0-1730127462783.gif

 

 

0 Likes
Message 8 of 8

miralisibtain110
Explorer
Explorer

Hi can I get this lisp

 

0 Likes