Jump to content

Recommended Posts

Posted

Hi, is there a script to create a rectangular bounding box around selected objects?

Regards,

Lu

Posted

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)

 

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