
! script name: vpkgroups.d/vpkgroupbycolor
! var_solids: group parasolid entities by color

!A
!A NAME:
!A
!A 	Group By Color  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script allows parasolid entities to be grouped by color.
!A	The group is a part of the solid model and can be understood by
!A	other systems which read parasolids files.
!A
!B INSTRUCTIONS:
!B
!B	PICK ENTITY FOR COLOR: Pick an entity of the solid model for the
!B 		color to group by. This function groups by the synergy pen
!B 		number. RGB colored entities will be treated as the closest
!B 		synergy pen.
!B
!B NOTE:
!B
!B 	Avoid placing an entity into more than one group.
!B
!B 	Imported solids files may already have some groups check for groups
!B 	by getting a listing of the entities. You may wish to remove all
!B 	groups before starting to work with the file.
!B
!B 	Bodies can have colors but you cannot create a group of bodies.
!B
!B 	Faces and Edges may now be assigned colors. When a face is created
!B 	it will be assigned the current color and current layer. Edges are
!B 	not automatically assigned any layer or color.
!B
!# see also - htdocs/help.d/pkgroups.htm
!# see also - htdocs/help.d/parasolids.htm


stat=ChangeLabelStr("ScriptNameStatus","Function: group by color ")

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

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

Highlight_pen= 3
Pick_loop:!
E1=WPKPickEntity("PICK ENTITY FOR COLOR")
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(E1) THEN Pick_loop
IF WPKEntityClass(E1)= WsPKEdgeEnt THEN
	Edge_group=1
ELSE
	Edge_group=0
END IF
Bodyname$= WPKBodyName(E1)
Pen1= GetColor(E1)

WPKClearErrorFlag
RecordFunctionOn
Stat= RecordDescr("GROUP: group by color")
V1= FreeParameter("Pen color",Pen1)
IF NOT Edge_group THEN
	Pkgroup= WPKGroupByColor(Bodyname$,V1,WsPKFaceEnt)
        Groups= WPKDisjointSubGrps(Pkgroup)
	Stat= WPKNameBody(Bodyname$,Bodyname$)
        E2= WPKRemoveGroup(Pkgroup)
        Stat= DeleteEntity(Groups)
ELSE
	Pkgroup= WPKGroupByColor(Bodyname$,V1,WsPKEdgeEnt)
	Stat= WPKNameBody(Bodyname$,Bodyname$)
END IF
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= WPKSolidRecordOn(Curr_record)
ActivateUndo
Stat= AdvanceUndoNumber("group by color")
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
WPKErrorCheck


GOTO Pick_loop

Leave:!
LowerColorBox
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
