
! script name: parasolids.d/anglechamfer
! solids: set angle chamfer on an edge

!A
!A NAME:
!A
!A 	Blend: Angle Chamfer
!A
!A DESCRIPTION:
!A
!A 	This script sets a angle chamfer on an edge.
!A
!B INSTRUCTIONS:
!B
!B	LEFT CHAMFER LENGTH: Enter the left chamfer length.
!B
!B	RIGHT CHAMFER LENGTH: Enter the right chamfer length.
!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 chamfer size to
!B 		a different chamfer size.
!B
!B~ image location - images/pkanglechamfer.gif
!B
!C NOTE:
!C
!C	The correspondence of left and right is edge orientation
!C	dependent. They may not correspond to visual left and right.
!C
!C	The chamfer is determined by offseting the adjacent faces
!C	to the edge and intersecting them. This intersect curve is
!C	projected normal onto the adjacent faces. The chamfer connects
!C	these curves.
!C
!C	Caution: the results of this blend for faces which do not
!C	intersect at right angles may not be what the user is expecting.
!C	An alternate method such as sectioning may be needed.
!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]
REAL Edge_array(100,3)


Start_color= 3
End_color= 4
Pick_color= 2

Chamfer_len=NamedVarVal("solid chamfer length",WsPKSolidsVars,0.0)
Chamfer_angle= GetDefault(WsChamferAngle)
IF Chamfer_angle<= 0.001 OR Chamfer_angle> 89.999 THEN Chamfer_angle= 45.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)= Left_chamfer
Edge_array(Num_edges,3)= Right_chamfer
Pt= ParameterPt(E1,0.5)
Stat= SetColor(Pt,Pick_color,WsTrue)
Stat= AddToGroup(Group,Pt)
GOTO Edge_loop

Fix_blends:!
	Stat= DeleteGroup(Group,WsTrue)
	FOR I= 1 TO Num_edges
	   Edge1= Edge_array(I,1)
	   Left_chamfer= Edge_array(I,2)
	   Right_chamfer= Edge_array(I,3)
	   Stat= WPKEdgeChamfer(Edge1,Right_chamfer,Left_chamfer,Propagate)
	NEXT I
	Stat= WPKFixBlends(Edge1,Propagate)
	WPKClearErrorFlag
	ActivateUndo
	Stat= AdvanceUndoNumber("edge chamfer")
	Stat= AddItemToUndo(WsUBPKSessionMark)
	WPKErrorCheck
RETURN

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

Stat= SetEqualsValue(Chamfer_len)
Stat=SetInputFormat("n4=","")
Right_chamfer=GetValue("RIGHT CHAMFER LENGTH")
IF ExitScript(Destination$) THEN Leave
V1=CreateVariable("solid chamfer length",WsPKSolidsVars,Right_chamfer)

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
