
! script name: vpkboolean.d/vpksubtractmany
! var_solids: subtract many solid bodies

!A
!A NAME:
!A
!A 	Subtract Many Bodies VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script subtracts many solid bodies from a body.
!A
!A~ image location - images/pkboolean.gif
!A
!B INSTRUCTIONS:
!B
!B	PICK PRIMARY BODY:  Pick the first of the two bodies.
!B
!B	PICK BODY: Pick the bodies to be subtracted.
!B
!B NOTES:
!B
!B	The resulting body will have the same parasolid tag as
!B	the first body. The resultant body will inherit the properties
!B	of the first body, for example layer and name.
!B	Faces which survive the addition will retain
!B	their colors and other properties. New Faces will have the 
!B	current color properties.
!B
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: subtract many bodies ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Body1$[256]
DIM Body2$[256]

ActivateUndo

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

Stat= GLPlotOn
Start:!
Stat=SetInputFormat("n4","")
E1=WPKPickEntity("PICK PRIMARY BODY")
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E1) THEN Body_loop
Body1= WPKParentBody(E1)
Stat= EntityNameCheck(Body1,Body1$)

Group= CreateGroup("")

Pick_bodies:!
N1= GroupCount(Group)
Stat=SetInputFormat("a1",",S,B,C,")
E1=WPKPickEntity("PICK BODY #"&VAL$(N1+1)&": Subtract ,Backup, Clear ",WsPKPickAll,Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "S"
        GOTO Subtract_bodies
CASE "B"
        N1= GroupCount(Group)
        E1= GroupElement(Group,N1)
        Stat=RemoveFromGrp(Group,N1)
        GOTO Pick_bodies
CASE "C"
        Stat= DeleteEntity(Group)
        PlotAll
        GOTO Start
END SELECT

IF NOT WPKIsEntity(E1) THEN Body_loop
Pkbody2= WPKParentBody(E1)
Stat= AppendToGroup(Group,Pkbody2)
GOTO Pick_bodies

Subtract_bodies:!
N1= GroupCount(Group)
IF N1=0 THEN
        Stat= DeleteEntity(Group)
        GOTO Start
END IF
WPKClearErrorFlag

RecordFunctionOn
Stat= RecordDescr("BOOLEAN: Subtract bodies")
G1= CreateGroup("")
RecordFunctionOff
FOR I=1 TO N1
	Body2= GroupElement(Group,I)
	Stat= EntityNameCheck(Body2,Body2$)
	RecordFunctionOn
	Stat= AddToGroup(G1,Body2$)
	RecordFunctionOff
NEXT I
RecordFunctionOn
G2= WPKSubtractBodies(Body1$,G1)
Stat= WPKNameBodyGroup(G2,Body1$)
Stat= WPKSetBodyParent(G2,1)
Stat= DeleteEntity(G2)
Stat= DeleteEntity(G1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= DeleteEntity(Group)

Stat= WPKSolidRecordOn(Curr_record)
ActivateUndo
Stat= AdvanceUndoNumber("subtract bodies")
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
WPKErrorCheck

GOTO Start

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