
! script name: vpksection.d/vpkzsectionbody
! var_solids: section a body with a z plane

!A
!A NAME:
!A
!A 	Z Section Body  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script sections a body with a Z Plane. This script divides the
!A 	body into two pieces with a Z plane. 
!A
!A~ image location - images/pkzsectionbody.gif
!A
!B INSTRUCTIONS:
!B
!B 	Z VALUE: Input the Z value of the section. The plane
!B 		is a plane parallel to the XY plane with this Z value.
!B
!B 	PICK BODY TO BE SECTIONED: Pick body to be sectioned.
!B
!B 	SAVE WHICH SIDE:  Top, Bottom ,All   Which side
!B 		of the plane do you want to save. All means
!B 		that there will be two bodies.
!B
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: z section body ")

DIM Destination$[256]
DIM Answer$[256]
DIM Body$[256]
DIM Point$[256]
DIM Z_var$[256]

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

Stat= GLPlotOn

Start:!
Ask_value:!
Pick_return= CurrPickReturn
Stat=SetPickReturn(WsGeometryZ)
Stat=SetInputFormat("n4a1=",",P,")
Stat=SetEqualsValue(0.0)
Zvalue=GetValue("Z VALUE  Pick ",Answer$)
Stat=SetPickReturn(Pick_return)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "P" THEN
Pick_point:Stat=SetInputFormat("n4=","")
	Pc=XyzInput("PICK",WsXyzInput)
	IF (ExitScript(Destination$)) THEN Leave
	IF NOT ValidGeometry(Pc) THEN Pick_point
	E1= LastPickEnt
	Pick_para= LastPickPara
	Pick_return= CurrPickReturn
	IF NOT ValidDependency(E1) THEN
       		 E1= CopyEntity(Pc)
       		 Stat= EntityNameCheck(E1,Point$)
       		 Pc_created= WsTrue
	ELSE
       		 Stat= EntityNameCheck(E1,Point$)
       		 Pc_created= WsFalse
	END IF
	Stat=SetLastPoint(Pc)
	Stat= DeleteEntity(Pc)
        Zvalue= ZPointValue(Pc)
	Picked_point=1
ELSE
	No_z_var= LastVariableInput(Z_var$)
	Picked_point=0
       	Pc_created= WsFalse
END IF

Stat=SetInputFormat("a1=",",T,B,A,")
Stat=SetEqualsValue(0,"T")
Stat=GetValue("SAVE WHICH SIDE:  Top , Bottom , All ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "T"
        Which_side=1
CASE "B"
        Which_side=2
CASE "A"
        Which_side=0
END SELECT


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

WPKClearErrorFlag
RecordFunctionOn
Stat= RecordDescr("SECTION: z section")
IF Picked_point THEN
	SELECT Pick_return
	CASE WsGeometryEnd
       		 Pc= ParameterReturnPt(Point$,Pick_para,WsGeometryEnd)
	CASE WsGeometryMid
       		 Pc= ParameterReturnPt(Point$,Pick_para,WsGeometryMid)
	CASE WsGeomCenter
       		 Pc= ParameterReturnPt(Point$,Pick_para,WsGeomCenter)
	END SELECT
ELSE
	IF No_z_var THEN
        	V1= FreeParameter("Z Value",Zvalue)
	ELSE
        	V1= FreeParameter("Z Value",Z_var$)
	END IF
	Pc= PointXYZ(0,0,V1)
END IF
V2= FreeParameter("Which side",Which_side)
G1= WPKPlnSectionBody(Body$,Pc,V2)
Stat= WPKNameBodyGroup(G1,Body$)
Stat= WPKSetBodyParent(G1,1)
Stat= DeleteEntityList(G1,Pc,Pz,L1,Trans1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= WPKSolidRecordOn(Curr_record)
ActivateUndo
Stat= AdvanceUndoNumber("z section body")
IF Pc_created THEN Stat= AddItemToUndo(WsUBCreateMode,Point$)
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
WPKErrorCheck


Num1= WPKBodiesCount
IF Num1 > 1 THEN Body_loop

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