
! script name: vpkconvert.d/vpkedgecontour
! var_solids: make a contour by picking edges

!A
!A NAME:
!A
!A 	Edge Contour  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script makes a contour by picking edges.
!A
!B INSTRUCTIONS:
!B
!B	PICK EDGE: Make contour, Propagate, Undo ,Clear: Select the 
!B	edges to be used in the contour.
!B	The first edge selected will determine the start point.
!B	The edges selected are converted into synergy geometry and
!B	they are then linked into a synergy contour.
!B
!B	If Propagate is chosen the edges picked will be propagated to
!B	all smooth connections. The resulting contour direction and
!B	start point not predictable.
!B
!B	The propagation is not done variationally. The variational record
!B	only creates a list of edges to be connected.
!B		
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: edge contour ")

OPTION BASE 1
DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Element$[256]
REAL Edge_array(200)
INTEGER Edge_dir(200)


Start_color= 3
Pick_color= 2
Angle_tol=NamedVarVal("solid edge propagate angle",WsPKSolidsVars,3.0)

Stat= GLPlotOn
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave
Start:!
Num_edges= 0
Mark_group=CreateGroup("TEMPGROUP")

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

SELECT Answer$
CASE "M"
	GOSUB Make_contour
	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= 99 THEN Start
T= LastPickPara
IF T < 0.5 THEN
        T1= 0.0
        Edge_dir1= WsForward
ELSE
        T1= 1.0
        Edge_dir1= WsReverse
END IF
IF NOT WPKIsEntity(E1) THEN Edge_loop

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
Edge_dir(Num_edges)= Edge_dir1
Pt= ParameterPt(E1,0.5)
Stat= SetColor(Pt,Pick_color,WsTrue)
Stat= AddToGroup(Mark_group,Pt)
IF Num_edges= 1 THEN
	Ps= ParameterPt(E1,T1)
	Stat= SetColor(Ps,Start_color,WsTrue)
END IF
GOTO Edge_loop

Make_contour:!
Stat= DeleteGroup(Mark_group,WsTrue)
Stat= DeleteEntity(Ps)
IF Num_edges= 0 THEN RETURN
Edge_dir1= Edge_dir(1)

WPKClearErrorFlag
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CONTOUR: edge contour")
G1= CreateGroup("edges")
RecordFunctionOff
FOR I= 1 TO Num_edges
        Edge1= Edge_array(I)
        RecordFunctionOff
	Element$= WPKEntityName(Edge1)
        RecordFunctionOn
        Stat= AddToGroup(G1,Element$)
        RecordFunctionOff
NEXT I
RecordFunctionOn
G2= WPKConvertEntity(G1,WsPKConvertEdges)
IF Edge_dir1= WsReverse THEN
      E1= GroupElement(G2,1)
      Stat= RevEntDirection(E1)
END IF
G3= JoinEntities(G2)
Stat= AutoNameGroup(G3,Name$)
Stat= DeleteEntityList(G1,G2)
RecordFunctionOff
IF ValidGeometry(G3) THEN
        Stat= PlotEntity(G3)
        ActivateUndo
        Stat= AdvanceUndoNumber("edge contour")
        Stat= AddGroupToUndo(WsUBCreateMode,G3,0,WsTrue)
        Keep_record= WsTrue
ELSE
        Keep_record= WsFalse
END IF
RecordFunctionOn
Stat= DeleteEntity(G3)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

IF Keep_record= WsFalse THEN
        DelLastVarRecord
ELSE
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
RETURN

Propagate:!
WPKClearErrorFlag
Stat= DeleteGroup(Mark_group,WsTrue)
Stat= DeleteEntity(Ps)
Edge_group= WPKPropagateEdges(Num_edges,Edge_array,Angle_tol)
Num_edges= WPKGroupCount(Edge_group)

WPKClearErrorFlag
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CONTOUR: edge contour")
G1= CreateGroup("edges")
RecordFunctionOff
FOR I= 1 TO Num_edges
        Edge1= WPKGroupElement(Edge_group,I)
        RecordFunctionOff
        Element$= WPKEntityName(Edge1)
        RecordFunctionOn
        Stat= AddToGroup(G1,Element$)
        RecordFunctionOff
NEXT I
RecordFunctionOn
G2= WPKConvertEntity(G1,WsPKConvertEdges)
G3= JoinEntities(G2)
Stat= AutoNameGroup(G3,Name$)
Stat= DeleteEntityList(G1,G2)
RecordFunctionOff
IF ValidGeometry(G3) THEN
        Stat= PlotEntity(G3)
        ActivateUndo
        Stat= AdvanceUndoNumber("edge contour")
        Stat= AddGroupToUndo(WsUBCreateMode,G3,0,WsTrue)
        Keep_record= WsTrue
ELSE
        Keep_record= WsFalse
END IF
RecordFunctionOn
Stat= DeleteEntity(G3)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= WPKRemoveGroup(Edge_group)
IF Keep_record= WsFalse THEN
        DelLastVarRecord
ELSE
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
RETURN

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