
! script name: interrogate.d/partvolume
! topic: calculate volume, surface area, and centroid of part

!A
!A NAME:
!A
!A 	PART VOLUME
!A
!A DESCRIPTION:
!A
!A 	This script calculates the volume, surface area, and centroid of a part.
!A
!B INSTRUCTIONS:
!B
!B 	PICK SURFACE GROUP: Pick the surface group defining the part.
!B
!C NOTES:
!C
!C 	Normal vectors must be aligned consistently for all surfaces.
!C
!C 	If the part is open the volume will be the volume of the
!C 	surfaces projected to the current workplane.
!C
!C 	Any holes or gaps between the surfaces will cause the
!C 	calculations to be incorrect.
!C
!C 	The tolerance used is the tolerance of the approximation
!C 	to the surfaces used to calculate the volume not the
!C 	tolerance of the volume,surface area or centroid.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: part volume ")

DIM Destination$[256]
DIM Answer$[256]
DIM A$[256]
DIM Title$[256]
DIM File$[256]
DIM Name$[256]


File$= "tempdata"
Stat= RemoveFile(File$)
Tol=NamedVarVal("3D Volume Tol",Ws3DSystemVars,0.040)

Start:!

Pick_loop:!
ActivateUndo

Material=FindUserData("MATERIAL",WsMaterial)
IF ValidGeometry(Material) THEN 
	Wt=GetUserValue(Material,"Weight(lb/cu. in.):")
	Cost=GetUserValue(Material,"Cost (lb):")
	stat= GetUserString(Material,"Material id name:",Name$)
ELSE
	Name$= "STEEL"
	Wt= 0.283
	Cost= 0.50
END IF
Units= CurrentUnits
Wt_conv= 1.0
Cost_conv= 1.0
Units$= " lb"
Vol$= " cu. in."
Sarea$= " sq. in."
IF Units= WsMetric THEN
	Wt_conv= 0.4536/(25.4^3)
	Cost_conv= Cost/(0.4536)
	Units$= " kg"
	Vol$= " cu. mm."
	Sarea$= " sq. mm."
END IF

Stat= SetPickMask(WsPickSurfOnly)
stat= SetInputFormat("n4","")
Part_group=PickGroupEnt("PICK SURFACE GROUP",WsSurfaceGroup)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Part_group) THEN Pick_loop


G1=PartVolume(Part_group,Tol)

Pt= GroupElement(G1,1)
Center= GroupElement(G1,2)
stat= DeleteEntity(G1)

Volume= XPointValue(Pt)
Surface_area= YPointValue(Pt)
stat= DeleteEntity(Pt)

Xc= XPointValue(center)
Yc= YPointValue(center)
Zc= ZPointValue(center)

Title$= "PART VOLUME"
stat= OutputString(File$,CHR$(10),WsNewFile)
stat= OutputString(File$,"MATERIAL: "&Name$&CHR$(10),WsAppend)
stat= OutputString(File$,"          "&CHR$(10),WsAppend)
OUTPUT A$ USING "16A,15D.6D,16A"; "Volume = : ",Volume,Vol$
stat= OutputString(File$,A$&CHR$(10),WsAppend)
OUTPUT A$ USING "16A,15D.6D,16A"; "Weight = : ",Volume*Wt*Wt_conv,Units$
stat= OutputString(File$,A$&CHR$(10),WsAppend)
OUTPUT A$ USING "16A,15D.6D"; "Cost   = : ",Volume*Wt*Wt_conv*Cost*Cost_conv
stat= OutputString(File$,A$&CHR$(10),WsAppend)
OUTPUT A$ USING "16A,15D.6D,16A"; "AREA   = : ",Surface_area,Sarea$
stat= OutputString(File$,A$&CHR$(10),WsAppend)

stat= OutputString(File$,"CENTROID:"&CHR$(10),WsAppend)
OUTPUT A$ USING "3(5A,8D.6D)"; " X = ",Xc," Y = ",Yc," Z = ",Zc
stat= OutputString(File$,A$&CHR$(10),WsAppend)

stat= OutputString(File$,CHR$(10),WsAppend)
Stat=MessageFileBox(File$,Title$)


GOTO Start

Leave:!
Stat= SetPickMask(WsPickGeometry)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
