
! script name: contours.d/cornercircles
! topic: fillet sharp corners on a contour

!A
!A NAME:
!A
!A 	Fillet Contour Corners
!A
!A DESCRIPTION:
!A
!A 	This script automatically fillets sharp corners on a contour.
!A
!B INSTRUCTIONS:
!B
!B 	PICK CONTOUR : Pick an element on the contour.
!B
!B	RADIUS FOR CW CORNERS: Input the radius to be used on 
!B		corners traversed in a clockwise fashion.
!B
!B	RADIUS FOR CCW CORNERS: Input the radius to be used on 
!B		corners traversed in a counterclockwise fashion.
!B
!B	REPLACE ORIGINAL CONTOUR Yes, No, Cancel: 
!B
!B		Y - for yes will replace the original contour with
!B			the new contour.
!B		N - for no will not delete the original but will
!B			keep the new contour.
!B		C - for cancel will delete the new contour but leave
!B			the original unchanged.
!B
!C NOTES:
!C
!C	This function will attempt to fillet any sharp corner it sees on a 
!C	contour. The function assumes that the fillet radius
!C	will fit into the elements without eliminating elements.
!C	If the fillet does not fit then either no corner will be generated
!C	or the corner radius will be generated incorrectly.
!C	It is the operator's responsibility to verify that the
!C	newly created contour is a valid contour.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: fillet contour corners")

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


ActivateUndo
Ploop:!
Stat= SetInputFormat("n4","")
Contour=PickGroupEnt("PICK CONTOUR",0)
IF (ExitScript(Destination$)) THEN Leave
Stat=Highlight(Contour,WsPickPen,WsDirArrowOff)

R1= InputRadius("RADIUS FOR CW CORNERS")
IF (ExitScript(Destination$)) THEN Leave

R2= InputRadius("RADIUS FOR CCW CORNERS")
IF (ExitScript(Destination$)) THEN Leave

New_contour=FilletContCorners(Contour,R1,R2)

Stat= SetInputFormat("a1=",",Y,N,C,")
Stat= GetValue("REPLACE ORIGINAL CONTOUR Yes, No, Cancel",Answer$)
IF (ExitScript(Destination$)) THEN 
	Tool= GetAssocTool(Contour)
        IF ValidGeometry(Tool) THEN
		Stat= AssociateTool(New_contour,Tool)
	END IF
	Stat= AutoNumberCont(New_contour)

	ActivateUndo
	Stat= AdvanceUndoNumber("corner radii")
	Stat= AddGroupToUndo(WsUBCreateMode,New_contour,1,WsTrue)

	GOTO Leave
END IF

Pen_color= CurrentColor
Pen_color= Pen_color+ WsPickPen*(NOT Pen_color)
SELECT Answer$
CASE "Y"
	stat= GroupName(Contour,Name$)
	Tool= GetAssocTool(Contour)
        IF ValidGeometry(Tool) THEN
		Stat= AssociateTool(New_contour,Tool)
	END IF
	stat= ChangeGrpName(New_contour,Name$)

	ActivateUndo
	Stat= AdvanceUndoNumber("corner radii")
	Stat= AddGroupToUndo(WsUBDeleteMode,Contour,1,WsTrue)
	Stat= AddGroupToUndo(WsUBCreateMode,New_contour,1,WsTrue)

	stat= UnLink(Contour)
	stat= DeleteGroup(Contour,1)
	Stat=Highlight(New_contour,Pen_color,WsDirArrowOn)
CASE "N"
	Tool= GetAssocTool(Contour)
        IF ValidGeometry(Tool) THEN
		Stat= AssociateTool(New_contour,Tool)
	END IF
	Stat= AutoNumberCont(New_contour)

	ActivateUndo
	Stat= AdvanceUndoNumber("corner radii")
	Stat= AddGroupToUndo(WsUBCreateMode,New_contour,1,WsTrue)

CASE "C"
	stat= UnLink(New_contour)
	stat= DeleteGroup(New_contour,1)
	Stat=Highlight(Contour,Pen_color,WsDirArrowOff)
END SELECT

GOTO Ploop

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