你不需要分解这些块,你可以在不分解的情况下穿过其中包含的对象。但你需要稍微改变一下你的想法。我自己做了一些手脚:
- (vl-load-com)
- (setq *mass-fact* 0.00782)
- ;; Command to calculate the volume of selected objects
- (defun c:vol (/ vol:calc vol:calc-block blst sum ss item)
- ;; Helper function to calculate a single entity's volume
- (defun vol:calc (eo / val)
- (cond
- ((eq (vla-get-ObjectName eo) "AcDbBlockReference")
- (vol:calc-block (vla-get-EffectiveName eo))
- )
- ((not (vl-catch-all-error-p (setq val (vl-catch-all-apply 'vla-get-Volume (list eo))))) val)
- (t 0.0)
- )
- )
- ;; Helper function to calculate the volume contained within a block definition
- (defun vol:calc-block (name / blk item sum)
- (setq sum 0.0)
- (if (setq blk (assoc name blst))
- (setq sum (cdr blk))
- (if (not (vl-catch-all-error-p (setq blk (vl-catch-all-apply 'vla-Item (list *BlocksCollection* name))))
- )
- (progn
- (vlax-for item blk
- (setq sum (+ sum (vol:calc item)))
- )
- (setq blst (cons (cons name sum) blst))
- )
- )
- )
- sum
- )
- ;; Initialize variables
- (setq sum 0.0)
- (or *ActiveDocument* (setq *ActiveDocument* (vla-get-ActiveDocument (vlax-get-acad-object))))
- (or *BlocksCollection* (setq *BlocksCollection* (vla-get-Blocks *ActiveDocument*)))
- ;; Step through a selection set, adding the volume of each item to a sum
- (princ "\nSelect entities to calculate volume from: ")
- (if (and (setq ss (ssget '((0 . "3DSOLID,INSERT"))))
- (not (vl-catch-all-error-p
- (setq ss (vl-catch-all-apply 'vla-get-ActiveSelectionSet (list *ActiveDocument*)))
- )
- )
- )
- (vlax-for item ss
- (setq sum (+ sum (vol:calc item)))
- )
- )
- (princ (strcat "\n<< Total Volume = " (rtos (/ sum (expt 1000.0 3)) 2 3) " m³ >>"))
- (princ
- (strcat "\n<< Mass @" (rtos *mass-fact*) " = " (rtos (/ (* *mass-fact* sum) 1000.0) 2 2) " kg >>")
- )
- (princ)
- )
|