Try this as a start:
(defun C:WHATEVER (/ *error* perim file minpt maxpt LL UR pickpt cut inc ent outer inner outerobj)
(defun *error* (errmsg)
(if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
(princ (strcat "\nError: " errmsg))
); if
(close file)
(princ)
); defun - *error*
(setq
perim (car (entsel "\nSelect outline [outer perimeter if hollow]: "))
file (open "C:/Your/File/Path/YourFileName.csv" "w"); <-------EDIT filepath and name
); setq
(write-line "Step,Area,COG depth,Product" file); headers <-------EDIT wordings
(vla-getboundingbox (vlax-ename->vla-object perim) 'minpt 'maxpt)
(setq
LL (vlax-safearray->list minpt)
UR (vlax-safearray->list maxpt)
pickpt (list (/ (+ (car LL) (car UR)) 2) (- (cadr UR) 0.5)); 1/2 unit below top middle
); setq
(command
"_.zoom" "_object" perim ""
"_.xline" "_hor" "_non" UR ""
); command
(setq
cut (entlast); the Xline
inc 0
); setq
(while (> (caddr (assoc 10 (entget cut))) (cadr LL)); still intersects
(command
"_.move" cut "" "0,-1" ""
"_.boundary" "_advanced" "_object" "_region" "" pickpt ""
); command
(setq
inc (1+ inc)
ent cut
outer (setq ent (entnext ent)); the outermost Region [first if multiple]
inner (ssadd); initially empty selection set of inner Region(s)
); setq
(while (setq ent (entnext ent)); internal Region(s)?
(ssadd ent inner); put in selection set
); while
(if (> (sslength inner) 0); were there internal Regions?
(command "_.subtract" outer "" inner ""); outer keeps entity name
); if
(setq outerobj (vlax-ename->vla-object outer))
(write-line
(strcat ; <-------EDIT precision arguments in three (rtos) functions
(itoa inc) "," ; Line number
(rtos (setq area (vla-get-Area outerobj)) 2 3) "," ; Area
(rtos (setq cogd (- (cadr UR) (cadr (vlax-get outerobj 'Centroid)))) 2 3) "," ; COG depth
(rtos (* area cogd) 2 3)
); strcat
file
); write-line
(entdel outer)
); while [move Xline]
(entdel cut)
(close file)
(princ)
); defun
EDIT the command name from "WHATEVER" to whatever you prefer, and see the lines marked <-------EDIT for other things you need to change to suit your setup and preferences.
It does not yet do the Table in the drawing, but it does the spreadsheet file, since I know more readily how to do that. It's not difficult to make a Table and just link the .csv file -- I haven't looked into automating that.
It should work for shapes such as in your original image, but there could be configurations in which it would fail, particularly these possibilities:
A) if the location at 1/2 unit below the top at the middle of the width is outside the perimeter object; or
B) if anything crosses that location, so that the pick for the BOUNDARY command will be exactly on something.
Also, it relies on something I think is always true, but I'm not sure: that if BOUNDARY results in more than one Region, the outermost of them is always the first one in drawing order.
But give it a try, for what it can do so far.
Kent Cooper, AIA