
! script name: vpkgroups.d/vpkcreateedgegrp
! var_solids: create a parasolids group of edges

!A
!A NAME:
!A
!A 	Create Edge Group  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script makes a parasolids group of edges. This group is
!A	part of the solid model and may be understood by any system which
!A	can read a parasolid file. The created group will be colored to
!A	the current color.
!A
!B INSTRUCTIONS:
!B
!B	FEATURE DESCRIPTION : Give a description of the group to be created.
!B
!B	PICK EDGES: Make group, Propagate, Unpick ,Clear: Select the edges
!B		for the group. Order is unimportant. Propagate will also
!B		include any edges which connect smoothly to the selected edges.
!B
!B NOTE:
!B
!B	Avoid placing an entity in more than one group.
!B
!B	Imported files may already have groups. Confusion may result
!B	if the user is unaware of this. The user might wish to remove all
!B	groups before starting on an imported file.
!B
!# see also - htdocs/help.d/pkgroups.htm
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: make edge group ")

OPTION BASE 1
DIM Destination$[256]
DIM Answer$[256]
DIM Descr$[256]
DIM Edgename$[256]
DIM Bodyname$[256]
DIM Name$[256]
REAL Edge_array(500)

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

Pick_color=2

Angle_tol=NamedVarVal("solid edge propagate angle",WsPKSolidsVars,3.0)

Stat= GLPlotOn
Start:!
Stat=SetInputFormat("a99","",WsFalse)
Stat=GetValue("FEATURE DESCRIPTION",Descr$)
IF (ExitScript(Destination$)) THEN Leave
IF Descr$="CANCEL" THEN Leave

Num_edges= 0
Mark_group=CreateGroup("TEMPGROUP")

Edge_loop:!
Stat=SetInputFormat("a1",",M,P,U,C,")
E1=WPKPickEntity("PICK EDGES: Make group, Propagate, Unpick ,Clear ",WsPKPickEdge,Answer$)
IF (ExitScript(Destination$)) THEN Leave

SELECT Answer$
CASE "M"
	GOSUB Make_group
	GOTO Start
CASE "P"
        IF NOT Num_edges THEN Edge_loop
        GOSUB Propagate
        GOTO Start
CASE "U"
	Num_edges= Num_edges-1
	IF Num_edges<0 THEN Num_edges=0
	N1= GroupCount(Mark_group)
	E1= GroupElement(Mark_group,N1)
	Stat=RemoveFromGrp(Mark_group,N1)
	Stat= DeleteEntity(E1)
	N1= GroupCount(Mark_group)
	IF N1= 0 THEN 
		Stat= DeleteEntity(Ps)
	END IF
	PlotAll
	GOTO Edge_loop
CASE "C"
	Stat= DeleteGroup(Mark_group,WsTrue)
	Stat= DeleteEntity(Ps)
	PlotAll
	GOTO Start
END SELECT

IF Num_edges>= 499 THEN Start
IF NOT WPKIsEntity(E1) THEN Edge_loop
Body= WPKParentBody(E1)

Edge1= WPKEntityTag(E1)
FOR I= 1 TO Num_edges
	IF Edge1= Edge_array(I) THEN Edge_loop
NEXT I
Num_edges= Num_edges+1
Edge_array(Num_edges)= Edge1
Pt= ParameterPt(E1,0.5)
Stat= SetColor(Pt,Pick_color,WsTrue)
Stat= AddToGroup(Mark_group,Pt)
GOTO Edge_loop

Make_group:!
WPKClearErrorFlag
Stat= DeleteGroup(Mark_group,WsTrue)
Edge1= Edge_array(1)
Bodyname$= WPKBodyName(Edge1)
Pen_color= CurrentColor

WPKClearErrorFlag
Stat= GenerateVarName("PkGroup",Name$)
RecordFunctionOn
Stat= RecordDescr("GROUP: create edge group")
RecordFunctionOff
FOR I= 1 TO Num_edges
    RecordFunctionOff
    Edge1= Edge_array(I)
    Edgename$= WPKEntityName(Edge1)
    RecordFunctionOn
    IF I=1 THEN
        PkEdgeGrp= WPKCreateGroup(Bodyname$,WsPKEdgeEnt,Edgename$)
    ELSE
        Stat= WPKAddToGroup(PkEdgeGrp,Edgename$)
    END IF
    RecordFunctionOff
NEXT I
RecordFunctionOn
Stat= WPKNameBody(Bodyname$,Bodyname$)
Stat= WPKSetFeature(PkEdgeGrp,WsPKEdgeFeature)
V1= StringConstant(Descr$)
Stat= WPKSetFeatureDescr(PkEdgeGrp,V1)
IF Pen_color THEN 
V2= FreeParameter("Pen color",Pen_color)
Stat= WPKColorEntity(PkEdgeGrp,V2)
END IF
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= WPKSolidRecordOn(Curr_record)
ActivateUndo
Stat= AdvanceUndoNumber("create edge group")
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
WPKErrorCheck
RETURN

Propagate:!
WPKClearErrorFlag
Stat= DeleteGroup(Mark_group,WsTrue)
Edge1= Edge_array(1)
Bodyname$= WPKBodyName(Edge1)
Pen_color= CurrentColor

WPKClearErrorFlag
Pkgroup= WPKPropagateEdges(Num_edges,Edge_array,Angle_tol)
Num_edges= WPKGroupCount(Pkgroup)

WPKClearErrorFlag
RecordFunctionOn
Stat= RecordDescr("GROUP: create edge group")
RecordFunctionOff
FOR I= 1 TO Num_edges
    RecordFunctionOff
    Edge1= WPKGroupElement(Pkgroup,I)
    Edgename$= WPKEntityName(Edge1)
    RecordFunctionOn
    IF I=1 THEN
        PkEdgeGrp= WPKCreateGroup(Bodyname$,WsPKEdgeEnt,Edgename$)
    ELSE
        Stat= WPKAddToGroup(PkEdgeGrp,Edgename$)
    END IF
    RecordFunctionOff
NEXT I
RecordFunctionOn
Stat= WPKNameBody(Bodyname$,Bodyname$)
Stat= WPKSetFeature(PkEdgeGrp,WsPKEdgeFeature)
V1= StringConstant(Descr$)
Stat= WPKSetFeatureDescr(PkEdgeGrp,V1)
IF Pen_color THEN
V2= FreeParameter("Pen color",Pen_color)
Stat= WPKColorEntity(PkEdgeGrp,V2)
END IF
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
Stat= WPKRemoveGroup(Pkgroup)

Stat= WPKSolidRecordOn(Curr_record)
ActivateUndo
Stat= AdvanceUndoNumber("create edge group")
Stat= AddItemToUndo(WsUBPKSessionMark)
Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
WPKErrorCheck
RETURN



Leave:!
Stat= DeleteGroup(Mark_group,WsTrue)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
