
! script name: vtrimming.d/globalfillet
! var_topic: construct all trim corner fillets

!A
!A NAME:
!A
!A 	Global Fillet Routine           VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This is a global function to do three types of fillets between two
!A	elements.  The types are based on how many of the elements will be
!A	trimmed. 
!A
!A~ image location - images/cornerfillet.jpg
!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 fillet routine ")

DIM Destination$[256]
DIM Answer$[256]
DIM Descr$[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
Para1=LastPickPara
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
Para2=LastPickPara
Stat=EntityNameCheck(E2,Name2$)

SELECT Num_trims
CASE 0
        Trim_sw= WsTrimFillet
        Descr$= "CIRCLE: Fillet - No Trimming"
CASE 1
        Trim_sw= WsTrimFirst
        Descr$= "CIRCLE: Fillet - Single Trimming"
CASE 2
        Trim_sw= WsTrimmed
        Descr$= "CIRCLE: Fillet - Both Trimmed"
CASE ELSE
        Trim_sw= WsTrimmed
        Descr$= "CIRCLE: Fillet - Both Trimmed"
END SELECT


P1=ParameterPt(E1,Para1)
P2=ParameterPt(E2,Para2)

Sign1=PtLeftRight(E1,P2)
Sign2=PtLeftRight(E2,P1)
Stat=DeleteEntity(P1)
Stat=DeleteEntity(P2)

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

Stat=GenerateVarName(WsCircleEnt,Name$)
RecordFunctionOn
Stat=RecordDescr(Descr$)
Pt=ParameterPt(Name1$,Para1)
V0=FreeParameter("Trim switch",Trim_sw)
IF No_radius_var THEN
	V1=FreeParameter("Radius",Radius)
	C1=Fillet(V1,Name1$,Sign1,Name2$,Sign2,Pt,V0)
ELSE
	C1=Fillet(Radius_var$,Name1$,Sign1,Name2$,Sign2,Pt,V0)
END IF
Stat=DeleteEntity(Pt)
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
