
! script name: vcontours.d/cornerchamfers
! var_topic: chamfer sharp corners on a contour

!A
!A NAME:
!A
!A 	Corner Chamfers		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script automatically chamfers 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 	CHAMFER FOR CW CORNERS: Enter the chamfer to
!B 		be used on corners traversed in a clockwise fashion.
!B
!B 	CHAMFER FOR CCW CORNERS: Enter the chamfer to
!B 		be used on corners traversed in a counterclockwise fashion.
!B
!C NOTES:
!C
!C 	This function will attempt to chamfer any sharp corner it sees on a 
!C 	contour.  The function assumes that the chamfer will fit into the
!C 	elements without eliminating elements.  If the chamfer does not fit,
!C 	then either no chamfer will be generated or the chamfer 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 chamfer corners")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Contour$[256]
DIM Chamfer1_var$[256]
DIM Chamfer2_var$[256]

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Pick_contour:!
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("a99","")
   Stat= GetValue("NON-VDM ENTITY. CANNOT USE ON NON VDM ENTITY ",Answer$)
   Stat=SetInputFormat("n4","")
   GOTO Leave
END IF

Cw_chamfer:!
Stat=SetInputFormat("n4=","")
Stat= SetEqualsValue(Chamfer1)
Chamfer1=GetValue("CHAMFER FOR CW CORNERS")
IF ExitScript(Destination$) THEN Leave
No_chamfer1_var=LastVariableInput(Chamfer1_var$)
C1=LastPickEnt
IF ValidGeometry(C1) THEN
	IF NOT ValidDependency(C1) THEN Cw_chamfer
	No_chamfer1_var=0
	Stat=EntityNameCheck(C1,Chamfer1_var$)
END IF

Ccw_chamfer:!
Stat=SetInputFormat("n4=","")
Stat= SetEqualsValue(Chamfer2)
Chamfer2=GetValue("CHAMFER FOR CCW CORNERS")
IF ExitScript(Destination$) THEN Leave
No_chamfer2_var=LastVariableInput(Chamfer2_var$)
C2=LastPickEnt
IF ValidGeometry(C2) THEN
	IF NOT ValidDependency(C2) THEN Ccw_chamfer
	No_chamfer2_var=0
	Stat=EntityNameCheck(C2,Chamfer1_var$)
END IF

Stat=GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat=RecordDescr("CONTOUR: Corner chamfer")
IF No_chamfer1_var THEN
	V1=FreeParameter("CW Corner Chamfer",Chamfer1)
	IF No_chamfer2_var THEN
		V2=FreeParameter("CCW Corner Chamfer",Chamfer2)
		C1=ChamferContCorners(Contour$,V1,V2)
	ELSE
		C1=ChamferContCorners(Contour$,V1,Chamfer2_var$)
	END IF
ELSE
	IF No_chamfer2_var THEN
		V2=FreeParameter("CCW Corner Chamfer",Chamfer2)
		C1=ChamferContCorners(Contour$,Chamfer1_var$,V2)
	ELSE
		C1=ChamferContCorners(Contour$,Chamfer1_var$,Chamfer2_var$)
	END IF
END IF
RecordFunctionOff
IF ValidGeometry(C1) THEN
	RecordFunctionOn
	Stat=AutoNameGroup(C1,Name$)
	Stat=AssociateTool(C1,Tool)

	ActivateUndo
	Stat= AdvanceUndoNumber("corner chamfers")
	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

