
! script name: vcontours.d/cornercircles
! var_topic: fillet sharp corners on a contour

!A
!A NAME:
!A
!A 	Corner Radii		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script automatically fillets sharp corners on a contour.
!A
!B INSTRUCTIONS:
!B
!B 	PICK CONTOUR TO BE CHANGED: Pick an element
!B 		on the contour to be changed.
!B
!B 	RADIUS FOR CW CORNERS: Enter the radius to
!B 		be used on corners traversed in a clockwise fashion.
!B
!B 	RADIUS FOR CCW CORNERS: Enter the radius to
!B 		be used on corners traversed in a counterclockwise fashion.
!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 will fit into the
!C 	elements without eliminating elements.  If the fillet does not fit, then
!C 	either no corner will be generated or the corner radius will be
!C 	generated incorrectly.  It is the operator's responsibility to verify
!C 	that the newly created contour is a valid contour.
!C	The original contour is deleted.
!C

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

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Contour$[256]
DIM Path_group$[256]
DIM Radius1_var$[256]
DIM Radius2_var$[256]

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Pick_contour:!
Stat=SetInputFormat("n4","")
Contour=PickGroupEnt("PICK CONTOUR TO BE CHANGED")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Contour) THEN Pick_contour
Stat=EntityNameCheck(Contour,Contour$)
IF NOT IsEntVariational(Contour) THEN
   Stat=SetInputFormat("a1",",Y,N,")
   Stat= GetValue("NON-VDM ENTITY. LOAD NON-VDM? Yes or No ",Answer$)
   IF Answer$="N" THEN Pick_contour
   Destination$= "contours.d/cornercircles"
   Stat=SetInputFormat("n4","")
   GOTO Leave
END IF

Path_group= ParentGroup(Contour)
IF ValidDependency(Path_group) THEN
IF IsGroupOfPaths(Path_group) THEN
        Stat=SetInputFormat("a1=",",S,P,G,")
        Stat= SetEqualsValue(0,"S")
        Stat=GetValue("APPLY TO: Single path or Path group ",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Answer$= "P" THEN
                Stat=EntityNameCheck(Path_group,Path_group$)
                Contour$= Path_group$
        END IF
        IF Answer$= "G" THEN
                Path_groups= ParentGroup(Path_group)
                IF ValidDependency(Path_groups) THEN
                   IF IsGroupOfPathGrps(Path_groups) THEN
                        Stat=EntityNameCheck(Path_groups,Path_group$)
                        Contour$= Path_group$
                   END IF
		ELSE
                        Stat=EntityNameCheck(Path_group,Path_group$)
                	Contour$= Path_group$
                END IF
        END IF
END IF
END IF

Min_cw_rad:!
Stat=SetInputFormat("n4=","")
R1=InputRadius("RADIUS FOR CW CORNERS")
IF ExitScript(Destination$) THEN Leave
No_radius1_var=LastVariableInput(Radius1_var$)
C1=LastPickEnt
IF ValidGeometry(C1) THEN
	IF NOT ValidDependency(C1) THEN Min_cw_rad
	No_radius1_var=0
	Stat=EntityNameCheck(C1,Radius1_var$)
END IF

Min_ccw_rad:!
Stat=SetInputFormat("n4=","")
R2=InputRadius("RADIUS FOR CCW CORNERS")
IF ExitScript(Destination$) THEN Leave
No_radius2_var=LastVariableInput(Radius2_var$)
C2=LastPickEnt
IF ValidGeometry(C2) THEN
	IF NOT ValidDependency(C2) THEN Min_ccw_rad
	No_radius2_var=0
	Stat=EntityNameCheck(C2,Radius1_var$)
END IF

Stat=GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat=RecordDescr("CONTOUR: Corner circles")
IF No_radius1_var THEN
	V1=FreeParameter("CW Corner Radius",R1)
	IF No_radius2_var THEN
		V2=FreeParameter("CCW Corner Radius",R2)
		C1=FilletContCorners(Contour$,V1,V2)
	ELSE
		C1=FilletContCorners(Contour$,V1,Radius2_var$)
	END IF
ELSE
	IF No_radius2_var THEN
		V2=FreeParameter("CCW Corner Radius",R2)
		C1=FilletContCorners(Contour$,Radius1_var$,V2)
	ELSE
		C1=FilletContCorners(Contour$,Radius1_var$,Radius2_var$)
	END IF
END IF
RecordFunctionOff
IF ValidGeometry(C1) THEN
	RecordFunctionOn
	Stat=AutoNameGroup(C1,Name$)
	Tool= GetAssocTool(Contour$)

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

	Stat=DeleteGroup(Contour$,WsTrue)
	Stat=AssociateTool(C1,Tool)
	RecordFunctionOff
	Stat=PlotEntity(C1)
END IF
RecordFunctionOn
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(C1) THEN
	DelLastVarRecord
ELSE
	Stat= AddGroupToUndo(WsUBCreateMode,C1,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Pick_contour

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

