
! script name: vpksolids.d/vpkedgechamfer
! var_solids: set blend chamfer on an edge

!A
!A NAME:
!A
!A 	Blend: Chamfer  VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script sets a blend chamfer on an edge.
!A
!A~ image location - images/pkchamfer.gif
!A
!B INSTRUCTIONS:
!B
!B	PICK EDGE: Fix, Propagate, New chamfer, Unpick ,Restore: Select the
!B 		edges which will receive the chamfer.  Different edges may have
!B 		different chamfers.  When all the desired edges have been
!B 		selected the chamfers must be either fixed or propagated to
!B 		become effective.  Fixing just puts chamfers on the edges
!B 		selected.  Propagate will cause tangent connecting edges to also
!B 		be chamfered. You cannot propagate from one chemfer size to
!B 		a different chemfer size.
!B		
!C NOTE:
!C
!C	It may be possible that some chamfers which do not work properly
!C	when in propagate mode will work if each individual edge is
!C	selected and then are collectively fixed.
!C
!# see also - htdocs/help.d/parasolids.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: set edge chamfer ")

OPTION BASE 1
DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Edgename$[256]
DIM Bodyname$[256]
DIM Name$[256]
REAL Edge_array(100,3)
DIM Chamfer_variables$(100)[256]
DIM Chamfer_var$[256]
DIM Chamfer_var_rem$[256]


Start_color= 3
End_color= 4
Pick_color= 2

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

Chamfer_len=NamedVarVal("solid chamfer length",WsPKSolidsVars,0.0)
Stat= GLPlotOn
Start:!
Propagate= WsFalse
GOSUB Ask_length

Group=CreateGroup("TEMPGROUP")
Num_edges= 0
Edge_loop:!
Stat=SetInputFormat("a1",",F,U,N,R,P,")
E1=WPKPickEntity("PICK EDGE: Fix, Propagate, New chamfer, Unpick ,Restore ",WsPKPickEdge,Answer$)
IF (ExitScript(Destination$)) THEN Leave

SELECT Answer$
CASE "P"
	Propagate= WsTrue
	GOSUB Fix_blends
	GOTO Start
CASE "F"
	GOSUB Fix_blends
	GOTO Start
CASE "N"
	GOSUB Ask_length
	GOTO Edge_loop
CASE "U"
        Num_edges= Num_edges-1
        IF Num_edges<0 THEN Num_edges=0
        N1= GroupCount(Group)
        E1= GroupElement(Group,N1)
        Stat=RemoveFromGrp(Group,N1)
        Stat= DeleteEntity(E1)
        PlotAll
        GOTO Edge_loop
CASE "R"
        Stat= DeleteGroup(Group,WsTrue)
        PlotAll
        GOTO Start
END SELECT
IF Num_edges= 99 THEN Start

IF NOT WPKIsEntity(E1) THEN Edge_loop
Num_edges= Num_edges+1
Edge_array(Num_edges,1)= WPKEntityTag(E1)
Edge_array(Num_edges,2)= Chamfer_len
Edge_array(Num_edges,3)= No_chamfer_var
Chamfer_variables$(Num_edges)= Chamfer_var$
Pt= ParameterPt(E1,0.5)
Stat= SetColor(Pt,Pick_color,WsTrue)
Stat= AddToGroup(Group,Pt)
GOTO Edge_loop

Fix_blends:!
	Stat= DeleteGroup(Group,WsTrue)

	WPKClearErrorFlag
        RecordFunctionOn
        Stat= RecordDescr("BLEND: constant chamfer")
        V1= FreeParameter("Propagate on",Propagate)
        RecordFunctionOff
        Chanfer_rem= 0.0
        Chamfer_count=1
	FOR I= 1 TO Num_edges
		Edge1= Edge_array(I,1)
		Chamfer_len= Edge_array(I,2)
		No_chamfer_var= Edge_array(I,3)
                Chamfer_var$= Chamfer_variables$(I)
                Edgename$= WPKEntityName(Edge1)
                Bodyname$= WPKBodyName(Edge1)
                A$= "Chamfer"&VAL$(Chamfer_count)
		IF No_chamfer_var THEN
                        IF ABS(Chamfer_len-Chamfer_rem)> 0.000001 THEN
                                RecordFunctionOn
                                V5= FreeParameter(A$,Chamfer_len)
                                RecordFunctionOff
                                Chamfer_count= Chamfer_count+1
                                Chamfer_rem= Chamfer_len
                                Chamfer_var_rem$= Chamfer_var$
                        END IF
                ELSE
                        IF NOT (Chamfer_var$= Chamfer_var_rem$) THEN
                                RecordFunctionOn
                                V5= FreeParameter(A$,Chamfer_var$)
                                RecordFunctionOff
                                Chamfer_count= Chamfer_count+1
                                Chamfer_rem= Chamfer_len
                                Chamfer_var_rem$= Chamfer_var$
                        END IF
                END IF
                RecordFunctionOn
		Stat= WPKEdgeChamfer(Edgename$,V5,V5,V1)
                RecordFunctionOff
	NEXT I
	RecordFunctionOn
        Stat= WPKFixBlends(Bodyname$,V1,V4)
        Stat= WPKNameBody(Bodyname$,Bodyname$)
        Stat= WPKSetBodyParent(Bodyname$,1)
        RecordFunctionOff
        Curr_record= CurrentVarRecord
        CloseVarRecord
	
	Stat= WPKSolidRecordOn(Curr_record)
        ActivateUndo
        Stat= AdvanceUndoNumber("edge chamfer")
        Stat= AddItemToUndo(WsUBPKSessionMark)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
        WPKErrorCheck
RETURN

Ask_length: !
Stat= SetEqualsValue(Chamfer_len)
Stat=SetInputFormat("n4=","")
Chamfer_len=GetValue("CHAMFER LENGTH")
IF ExitScript(Destination$) THEN Leave
No_chamfer_var= LastVariableInput(Chamfer_var$)
V1=CreateVariable("solid chamfer length",WsPKSolidsVars,Chamfer_len)
RETURN


GOTO Ploop

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