Discus84 Posted August 27 Posted August 27 Hi, is there a script to create a rectangular bounding box around selected objects? Regards, Lu Quote
Discus84 Posted August 27 Author Posted August 27 Google AI... (defun C:BBOX ( / ss index ent vlaObj minPt maxPt minExt maxExt pt1 pt2 ) (vl-load-com) (princ "\nSelect objects to enclose in a bounding box: ") ;; Prompt user to select objects (if (setq ss (ssget)) (progn (setq index 0) ;; Loop through all selected objects (repeat (sslength ss) (setq ent (ssname ss index)) (setq vlaObj (vlax-ename->vla-object ent)) ;; Safely catch objects that do not support a bounding box (if (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list vlaObj 'minPt 'maxPt)))) (progn (setq pt1 (vlax-safearray->list minPt) pt2 (vlax-safearray->list maxPt)) ;; Initialize or update the overall minimum and maximum coordinates (if (not minExt) (setq minExt pt1 maxExt pt2) (setq minExt (mapcar 'min pt1 minExt) maxExt (mapcar 'max pt2 maxExt)) ) ) ) (setq index (1+ index)) ) ;; Draw the bounding box if valid coordinates were collected (if (and minExt maxExt) (progn ;; Deactivate Object Snap temporarily to ensure precision (setq oldOsmode (getvar "OSMODE")) (setvar "OSMODE" 0) ;; Draw a standard rectangle using the global coordinates (command "_.rectangle" minExt maxExt) ;; Restore original Object Snap settings (setvar "OSMODE" oldOsmode) (princ "\nBounding box successfully created!") ) (princ "\nError: Could not calculate bounding box for selected objects.") ) ) (princ "\nNo objects selected.") ) (princ) ) (princ "\nType BBOX to run the command.") (princ) 1 Quote
BIGAL Posted August 27 Posted August 27 A google should have pointed you to the great Website by Lee-mac, Bounding Box. 2 Quote
Paul Li Posted Monday at 09:54 PM Posted Monday at 09:54 PM Well your ai generated version actually serves a little different purpose. Here's Lee Mac's version which when given obj (which is a vla object) returns a list of 4 points that represent the 4 corners of the bounding box: ; https://www.lee-mac.com/boundingbox.html ;; Bounding Box - Lee Mac ;; Returns the point list describing the rectangular frame bounding the supplied object. ;; obj - [vla] VLA-Object (defun LM:boundingbox ( obj / a b lst ) (if (and (vlax-method-applicable-p obj 'getboundingbox) (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list obj 'a 'b)))) (setq lst (mapcar 'vlax-safearray->list (list a b))) ) (mapcar '(lambda ( a ) (mapcar '(lambda ( b ) ((eval b) lst)) a)) '( (caar cadar) (caadr cadar) (caadr cadadr) (caar cadadr) ) ) ) ) The section of the ai code that kind of matches this is here: ;; Safely catch objects that do not support a bounding box (if (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list vlaObj 'minPt 'maxPt)))) (progn (setq pt1 (vlax-safearray->list minPt) pt2 (vlax-safearray->list maxPt)) But in this case vlaObj is the variable that represent the vla object. Also instead of a list of 4 points, what's returned are 2 variables pt1 and pt2 each representing the lower left & the upper right corners of the bounding box. Then the ai code goes further by comparing these 2 points with 2 points from previous selected object cycling through all selected objects ultimately concluding with the least minimum or lowest left corner point minExt and the maximum or greatest upper right corner point maxExt: ;; Initialize or update the overall minimum and maximum coordinates (if (not minExt) (setq minExt pt1 maxExt pt2) (setq minExt (mapcar 'min pt1 minExt) maxExt (mapcar 'max pt2 maxExt)) ) Finally, the ai code concludes by drawing a single bounding box using these 2 corner points with the rectangle command: ;; Draw a standard rectangle using the global coordinates (command "_.rectangle" minExt maxExt) 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.