
! script name: vtrimming.d/globalptfillet
! var_topic: construct all corner point fillets

!A
!A NAME:
!A
!A 	Global Corner Point Fillet          VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This script constructs three types of fillets at a corner between
!A	two elements. The elements must be trimmed to their intersect point.
!A      This fillet may be used in VDM to create fillets which are stable
!A      even when the direction of a corner changes.
!A
!A	The types are based on how many of the elements will be	trimmed. 
!A	Both - The two elements picked will be trimmed to the ends of the arc.
!A	Single - The first element picked will be trimmed to the end of the
!A	         arc and the second element will not be trimmed.
!A	None- Both elements will remain as they are and the arc will be added.
!A
!A 	This script assumes that once the fillet radius has been defined.
!A	All subsequent radii will be the same until Radius is chosen.
!A
!B INSTRUCTIONS:
!B
!B 	FILLET RADIUS: Enter the radius of the arc that should be constructed
!B 		tangent to each element picked.
!B
!B	PICK FIRST ELEMENT. Single, Both, Chamfer: 
!B	PICK ELEMENT TO BE TRIMMED. None, Both, Chamfer:
!B	PICK ELEMENT TO BE TRIMMED. None, Single, Chamfer:
!B		Choose the first element tangent to the arc of the fillet.
!B		There are three options: Both - Trim both elements
!B		                       Single - Trim the first element picked
!B		                         None - Trim no elements
!B
!B	PICK SECOND ELEMENT: Pick the other element that should be tangent
!B		to the arc constructed.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: global corner pt fillet ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name1$[256]
DIM Name2$[256]
DIM Name$[256]
DIM Radius_var$[256]

DEG
Num_trims= 2
IF ARGC>=2 THEN Num_trims= VAL(ARGV(1))

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Ask_radius:Stat= SetInputFormat("n4=","")
Radius=InputRadius("FILLET RADIUS")
IF (ExitScript(Destination$)) THEN Leave
No_radius_var=LastVariableInput(Radius_var$)
C1=LastPickEnt
IF ValidGeometry(C1) THEN
        IF NOT ValidDependency(C1) THEN Ask_radius
        No_radius_var=0
        Stat=EntityNameCheck(C1,Radius_var$)
END IF

Ploop:!
Stat= SetInputFormat("a1=",",N,R,B,S,")
SELECT Num_trims
CASE 0
Stat= SetEqualsValue(0,"B")
E1=PickEntity("PICK FIRST ELEMENT. Single, Both, Radius",Answer$)
CASE 1
Stat= SetEqualsValue(0,"B")
E1=PickEntity("PICK ELEMENT TO BE TRIMMED. None, Both, Radius",Answer$)
CASE 2
Stat= SetEqualsValue(0,"S")
E1=PickEntity("PICK ELEMENT TO BE TRIMMED. None, Single, Radius",Answer$)
END SELECT
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "R" THEN Ask_radius
IF Answer$= "B" OR Answer$= "S" OR Answer$= "N" THEN
	Num_trims= (Answer$="S")+2*(Answer$="B")
	GOTO Ploop
END IF
IF NOT ValidDependency(E1) THEN Ploop
Stat=EntityNameCheck(E1,Name1$)

Pick_tan2:Stat= SetInputFormat("n4","")
E2=PickEntity("PICK SECOND ELEMENT ")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E2) THEN Pick_tan2
Stat=EntityNameCheck(E2,Name2$)


ActivateUndo
Stat= AdvanceUndoNumber("fillet")
Stat= AddItemToUndo(WsUBModifyMode,Name1$)
Stat= AddItemToUndo(WsUBModifyMode,Name2$)

Stat=GenerateVarName(WsCircleEnt,Name$)
RecordFunctionOn
SELECT Num_trims
CASE 0
	Stat=RecordDescr("CIRCLE: Corner Pt Fillet - No Trimming")
	IF No_radius_var THEN
		V1=FreeParameter("Radius",Radius)
		E3= CopyEntity(Name1$)
		E4= CopyEntity(Name2$)
		C1=CornerPointFillet(V1,E3,E4)
	ELSE
		E3= CopyEntity(Name1$)
		E4= CopyEntity(Name2$)
		C1=CornerPointFillet(Radius_var$,E3,E4)
	END IF
	Stat=Unlink(E3)
	Stat=Unlink(E4)
	Stat=DeleteEntity(E3)
	Stat=DeleteEntity(E4)
	RecordFunctionOff
	Stat= PlotEntity(E1)
	Stat= PlotEntity(E2)
	Stat= PlotEntity(C1)
	RecordFunctionOn
CASE 1
	Stat=RecordDescr("CIRCLE: Corner Pt Fillet - Single Trimming")
	IF No_radius_var THEN
		V1=FreeParameter("Radius",Radius)
		E3= CopyEntity(Name2$)
		C1=CornerPointFillet(V1,Name1$,E3)
	ELSE
		E3= CopyEntity(Name2$)
		C1=CornerPointFillet(Radius_var$,Name1$,E3)
	END IF
	Stat=Unlink(E3)
	Stat=DeleteEntity(E3)
	RecordFunctionOff
	Stat= PlotEntity(E2)
	Stat= PlotEntity(C1)
	RecordFunctionOn
CASE 2
	Stat=RecordDescr("CIRCLE: Corner Pt Fillet - Both Trimmed")
	IF No_radius_var THEN
		V1=FreeParameter("Radius",Radius)
		C1=CornerPointFillet(V1,Name1$,Name2$)
	ELSE
		C1=CornerPointFillet(Radius_var$,Name1$,Name2$)
	END IF
END SELECT

Stat=NameEntity(C1,Name$)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= PlotEntity(C1)
Stat= PlotEntity(Name1$)
Stat= PlotEntity(Name2$)
IF NOT ValidGeometry(C1) THEN 
	DelLastVarRecord
ELSE
	Stat= AddItemToUndo(WsUBCreateMode,C1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Ploop

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