
! script name: parasolids.d/subtractmany
! solids: subtract many solid bodies

!A
!A NAME:
!A
!A 	Subtract Many Bodies
!A
!A DESCRIPTION:
!A
!A 	This script subtracts many bodies from a primary body.
!A
!A~ image location - images/pkboolean.gif
!A
!B INSTRUCTIONS:
!B
!B	PICK PRIMARY BODY:  Pick the first body.
!B
!B	PICK BODY: Pick the bodies to be united.
!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]

ActivateUndo

Stat= GLPlotOn
Start:!

E1=WPKPickEntity("PICK PRIMARY BODY")
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E1) THEN Body_loop
Pkbody1= WPKParentBody(E1)

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:!

WPKClearErrorFlag
G1= WPKSubtractBodies(Pkbody1,Group)
Stat= DeleteEntity(G1)
Stat= DeleteEntity(Group)

ActivateUndo
Stat= AdvanceUndoNumber("unite bodies")
Stat= AddItemToUndo(WsUBPKSessionMark)
WPKErrorCheck

GOTO Start

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