Tom James Posted December 3 Posted December 3 In order to find duplicate blocks exported from revit, I am trying to compare two blocks using their boundary boxes to determine whether they are identical, mirrored across x or y axis or neither. I have the following script. (defun c:CompareBlocks () (vl-load-com) (defun GetBoundingBox (entity) (vla-getboundingbox entity 'minPoint 'maxPoint) (list (vlax-safearray->list minPoint) (vlax-safearray->list maxPoint)) ) (defun CombineBoundingBoxes (bbox1 bbox2) (list (mapcar 'min (car bbox1) (car bbox2)) (mapcar 'max (cadr bbox1) (cadr bbox2)) ) ) (defun GetTotalBoundingBox (block) (setq blockObj (vlax-ename->vla-object block)) (setq totalBBox nil) (setq blockDef (vla-item (vla-get-blocks (vla-get-document blockObj)) (vla-get-effectivename blockObj))) (vlax-for entity blockDef (setq entityBBox (GetBoundingBox entity)) (if totalBBox (setq totalBBox (CombineBoundingBoxes totalBBox entityBBox)) (setq totalBBox entityBBox) ) ) totalBBox ) (defun CompareBoundingBoxes (bbox1 bbox2) ;; Split the bounding box into min and max points (setq min1x (car (car bbox1))) (setq min1y (cadr (car bbox1))) (setq max1x (car (cadr bbox1))) (setq max1y (cadr (cadr bbox1))) (setq min2x (car (car bbox2))) (setq min2y (cadr (car bbox2))) (setq max2x (car (cadr bbox2))) (setq max2y (cadr (cadr bbox2))) ;; Compare the bounding boxes (setq identical (equal bbox1 bbox2)) (setq mirrored-x (and (equal min1x min2x) (equal max1x max2x) (equal min1y -max2y) (equal max1y -min2y))) (setq mirrored-y (and (equal min1x -max2x) (equal max1x -min2x) (equal min1y min2y) (equal max1y max2y))) (cond (identical (princ "\nThe blocks are identical.")) (mirrored-x (princ "\nThe blocks are mirrored along the x-axis.")) (mirrored-y (princ "\nThe blocks are mirrored along the y-axis.")) (t (princ "\nThe blocks are not identical."))) ) (setq block1 (car (entsel "\nSelect first block: "))) (setq block2 (car (entsel "\nSelect second block: "))) (if (and block1 block2) (progn (setq bbox1 (GetTotalBoundingBox block1)) (setq bbox2 (GetTotalBoundingBox block2)) (princ (strcat "\nBounding box of the first block: " (vl-princ-to-string bbox1))) (princ (strcat "\nBounding box of the second block: " (vl-princ-to-string bbox2))) (CompareBoundingBoxes bbox1 bbox2) ) (princ "\nSelection error.") ) (princ) ) The example: bbox1 = ((-25.3115, -40.2049), (65.6885, 43.7951)) bbox2 = ((-25.3115, -43.7951), (65.6885, 40.2049)) evaluates to "not identical" and I can't figure out why mirrored-x is evaluating to nil. (setq mirrored-x (and (equal min1x min2x) (equal max1x max2x) (equal min1y -max2y) (equal max1y -min2y))) min1y (-40.2049) should be equal to -max2y(-40.2049), and the same should be true of max1y(43.7951) and -min2y(43.7951). Quote
rlx Posted December 3 Posted December 3 (edited) Welcome to CadTutor! Maybe add fuzz factor to equal function or round off numbers. What you see is not always what you get. Autocad may show so many digits (for efficiency purposes / smaller files) , under the hood it probably uses more for calculations. Its like PI , you learn its 3.14 but you know the list goes on. So use something like (equal min1y -max2y 0.0001) or whatever fuzz makes you happy. Edited December 3 by rlx 1 Quote
Tom James Posted December 4 Author Posted December 4 Someone else had the answer: Quote You can't just put a minus sign in front of a variable name for its negative. Try it this way: .... (equal min1y (- max2y)) (equal max1y (- min2y)))) I have also added fuzz just to be sure there won't be any hidden issues with rounding. Thank you for the suggestion! 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.