
! script name: vpkintersect.d/vpkbodycentroid
! var_solids: find the center of gravity of a solid body

!A
!A NAME:
!A
!A 	Body Centroid  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script finds the centroid of a solid body.
!A 	The centroid is the center of gravity of the body, assuming
!A 	constant density.
!A
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: body centroid ")

DIM Destination$[256]
DIM Answer$[256]
DIM Body$[256]
DIM Name$[256]

ActivateUndo
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave


Start:!
Pkbody= WPKCurrentBody
IF NOT WPKIsEntity(Pkbody) THEN
        Stat=SetInputFormat("n4","")
        E1=WPKPickEntity("PICK BODY")
        IF (ExitScript(Destination$)) THEN Leave
        IF NOT WPKIsEntity(E1) THEN Start
        Pkbody= WPKParentBody(E1)
END IF
Stat= EntityNameCheck(Pkbody,Body$)



ActivateUndo
Stat= AdvanceUndoNumber("body centroid")
Stat= AddItemToUndo(WsUBCreateMode,P1)

WPKClearErrorFlag
Stat= GenerateVarName(WsPointEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("POINT: Body Centroid")
P1= WPKBodyCentroid(Body$)
Stat= NameEntity(P1,Name$)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

IF ValidGeometry(P1) THEN
        Stat= WPKSolidRecordOn(Curr_record)
        ActivateUndo
        Stat= AdvanceUndoNumber("body centroid")
        Stat= AddItemToUndo(WsUBCreateMode,P1)
        Stat= AddItemToUndo(WsUBPKSessionMark)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
ELSE
        DelLastVarRecord
END IF
WPKErrorCheck

Leave:!
Stat=SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
