
! script name: v3drotary_cut.d/rotaryxyztan
! var_topic: cut xyz curve on a surface using rotary axis cutting

!A
!A NAME:
!A
!A 	Curves: Rotary axis  Cut XYZ Curve	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script drives a cutter along the an xyz curve on a surface using 
!A	Rotary axis machining. The xyz curve must lie on the surface and
!A	if the surface is closed the xyz curve cannot cross over the
!A	junction where the surfaces closes.
!A	
!A
!B INSTRUCTIONS:
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B      Z CUTTER DISPLACEMENT: A positive value will move the cutter
!B                              that distance in the direction of the
!B                              cutter axis. A negative value in the
!B                              reverse direction.
!B
!B
!B
!B      ROTARY AXIS: X, Y, Z:  Select the rotary axis.
!B
!B      CUTTER TILT ANGLE: For a pure rotary cut on a five axis machine
!B                              the cutter can be tilted at a fixed angle
!B                              relative to the rotary axis.
!B
!B      TOLERANCE: Enter the amount of variation allowed.
!B
!B      PICK SURFACE: Pick the surface to be used.
!B
!B      PICK CUT DIRECTION: Pick the direction of cut.
!B
!B
!C NOTE:
!C      This script places the contact point of the tool along the
!C      XYZ curve on the surface. It does not check for tool interference
!C      away from the contact point.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: four axis cut xyz curve ")
DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Toolid$[256]
DIM Surface$[256]
DIM Contour$[256]
DIM Finish_var$[256]
DIM Angle_var$[256]
DIM Axis_line$[256]
DIM Name$[256]

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Zaddon=NamedVarVal("Z Cutter Displacement",Ws3DSystemVars,0.0)
Forward_color=3
Reverse_color=4

ActivateUndo

IF ARGC>1 THEN
	Tool_name$= ARGV(1)
	GOTO Have_tool
END IF
Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:v3drotary_cut.d/rotaryxyztan"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("CUTTER PATH TOOL, New_tool ",Tool_name$)
IF ExitScript(Destination$) THEN Leave
IF Tool_name$= "CANCEL" THEN Leave
IF Tool_name$="New_tool" THEN
	LowerSelectBox
	LOAD "vtools.d/readmilltool:v3drotary_cut.d/rotaryxyztan"
END IF
Have_tool:!
Tool=FindUserData(Tool_name$,WsToolData)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques
LowerSelectBox


Past_dia:!
Finishr=NamedVarVal("finish allowance",WsSystemVars,0.0)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Finishr)
Finish_allow=GetValue("FINISH ALLOWANCE",Answer$)
IF ExitScript(Destination$) THEN Leave
Finishr=Finish_allow
No_finish_var= LastVariableInput(Finish_var$)

Vfinish=CreateVariable("finish allowance",WsSystemVars,Finishr)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Zaddon)
Zaddon=GetValue("Z CUTTER DISPLACEMENT",Answer$)
IF ExitScript(Destination$) THEN Leave
Vzaddon=CreateVariable("Z Cutter Displacement",Ws3DSystemVars,Zaddon)

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Tol)
Tol=GetValue("TOLERANCE",Answer$)
IF ExitScript(Destination$) THEN Leave


Rotary_axis:Stat=SetInputFormat("a1=",",X,Y,Z,")
Stat=SetEqualsValue(0,"Y")
Stat=GetValue("ROTARY AXIS: X, Y, Z ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "X"
        Axis_number=1
CASE "Y"
        Axis_number=2
CASE "Z"
        Axis_number=3
CASE ELSE
        GOTO Rotary_axis
END SELECT


Cut_method:Stat=SetInputFormat("a1=",",N,T,L,")
Stat=SetEqualsValue(0,"N")
Stat=GetValue("CUT METHOD: Normal, Tangent, Locked ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "N"
        Cut_method= WsRotaryAxisCut
        Orient_sign= 1
CASE "T"
        Cut_method= WsRotaryAxisTan1
        Orient_sign= 1
CASE "L"
        Cut_method= WsLockedAxis
        Orient_sign= 1
CASE ELSE
        GOTO Cut_method
END SELECT

IF Cut_method= WsLockedAxis THEN
Axis_line:Axis_line=PickSpecificEnt("PICK AXIS LINE",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidDependency(Axis_line) THEN Axis_line
        Stat= EntityNameCheck(Axis_line,Axis_line$)
        T= LastPickPara
        IF T > 0.5 THEN Orient_sign= -1
        No_angle_var=1
        Tilt_angle=0
ELSE
        Stat=SetInputFormat("n4=","")
        Stat=SetEqualsValue(Tilt_angle)
        Tilt_angle=GetValue("CUTTER TILT ANGLE",Answer$)
        IF ExitScript(Destination$) THEN Leave
        No_angle_var= LastVariableInput(Angle_var$)
END IF

Pick_loop:!
Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Surface) THEN Pick_loop
Stat= EntityNameCheck(Surface,Surface$)

Pick_contour:Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickNoSurf)
Celement=PickEntity("PICK CURVE ON SURFACE ")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Celement) THEN Pick_contour
Stat=SetPickMask(WsPickAll)

Tpara= LastPickPara
Curve= ParentGroup(Celement,0)
IF NOT ValidDependency(Curve) THEN Pick_contour
Stat= EntityNameCheck(Curve,Contour$)

Stat=Highlight(Curve,WsPickPen,WsDirArrowOff)
GOSUB Pick_directions
Direction= Curve_dir


Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Rotary Axis XYZ Curve On Surface")
Stat= SetRecDelLastExec
Stat= NullReference(Surface$)
Work= CurrentWorkplane
Stat= SetWorkplane
V1= FreeParameter("Axis number",Axis_number)
Rotary_axis= StdRotaryAxis(V1)
IF Cut_method= WsLockedAxis THEN
        Locked_axis= LineVector(Axis_line$)
        V1= FreeParameter("Cutter Tilt angle",0)
ELSE
        Locked_axis= LineVector("")
        IF No_finish_var THEN
                V1= FreeParameter("Cutter Tilt angle",Tilt_angle)
        ELSE
                V1= FreeParameter("Cutter Tilt angle",Angle_var$)
        END IF
END IF
IF No_finish_var THEN
        V2= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V2= FreeParameter("Finish allowance",Finish_var$)
END IF
SELECT Cut_method
CASE WsLockedAxis
        V3= FreeParameter("Cut method",WsLockedAxis)
CASE WsRotaryAxisCut
        V3= FreeParameter("Cut method",WsRotaryAxisCut)
CASE WsRotaryAxisTan1
        V3= FreeParameter("Cut method",WsRotaryAxisTan1)
CASE WsRotaryAxisTan2
        V3= FreeParameter("Cut method",WsRotaryAxisTan2)
END SELECT
V4= FreeParameter("Tolerance",Tol)
V5= FreeParameter("Direction",Direction)
V6= FreeParameter("Orient sign",Orient_sign)
V7= FreeParameter("Z cutter displacement",Zaddon)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,1,0,V2)
Stat=Setup3dCutter(Mdef1)
Stat= Set3dZAddon(V7)
Stat= PrintToVarRecord("PlotOn")
Cutter_path=RotaryCutXYZCurve(Surface$,Contour$,Rotary_axis,Locked_axis,V1,V3,V6,V4,V5)
Stat= PrintToVarRecord("PlotOff")
Stat= NameEntity(Cutter_path,Name$)
Stat=AssociateTool(Cutter_path,Tool)
Stat= DeleteEntityList(Mdef1,Rotary_axis,Locked_axis)
Stat= SetWorkplane(Work)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
	Stat= AnimateTool(Tool,Cutter_path)
        ActivateUndo
        Stat= AdvanceUndoNumber("four axis cut")
        Stat= AddGroupToUndo(WsUBCreateMode,Cutter_path,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Pick_loop


Pick_directions:!
Stat=SetPickMask(WsPickGeometry)
Curve_dir=WsForward

GOSUB Curve_direction
Surface_sign=1
IF Cut_method= WsRotaryAxisTan1 OR Cut_method= WsRotaryAxisTan2 THEN
	GOSUB Tangent_dir
END IF


RETURN

ShowCurveDirection:!
	Vf= ParameterLine(Celement,Tpara,Normal_len,WsTangent)
        Pf=ParameterPt(Vf,1.0)
        Vr= ParameterLine(Celement,Tpara,-Normal_len,WsTangent)
        Pr=ParameterPt(Vr,1.0)

        Stat= SetForceVisible(Vf,WsTrue)
        Stat= SetForceVisible(Pf,WsTrue)
        Stat= SetForceVisible(Vr,WsTrue)
        Stat= SetForceVisible(Pr,WsTrue)

        Stat=SetColor(Vf,Forward_color,WsTrue)
        Stat=SetColor(Pf,Forward_color,WsTrue)
        Stat=SetColor(Vr,Reverse_color,WsTrue)
        Stat=SetColor(Pr,Reverse_color,WsTrue)

RETURN

Curve_direction:!
        GOSUB ShowCurveDirection
        Stat=SetInputFormat("n4","")
Pick_curve_dir: L1=PickSpecificEnt("PICK CUT DIRECTION ",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Pick_curve_dir
        Pt=ParameterPt(L1,1.0)
        D1=DistTwoPts(Pt,Pf)
        D2=DistTwoPts(Pt,Pr)
        Curve_dir=WsForward
        IF D2<D1 THEN Curve_dir= WsReverse
        Stat=DeleteEntity(Vf)
        Stat=DeleteEntity(Vr)
        Stat=DeleteEntity(Pf)
        Stat=DeleteEntity(Pr)
        Stat=DeleteEntity(Pt)
RETURN

Show_tangent_dir:!
	U_linef= ParameterLine(Celement,Tpara,Normal_len,WsNormal)
        Puf=ParameterPt(U_linef,1.0)
        U_liner= ParameterLine(Celement,Tpara,-Normal_len,WsNormal)
        Pur=ParameterPt(U_liner,1.0)

        V_linef= ParameterLine(Celement,Tpara,Normal_len,WsTangent)
        Pvf=ParameterPt(V_linef,1.0)

        V_liner= ParameterLine(Celement,Tpara,-Normal_len,WsTangent)
        Pvr=ParameterPt(V_liner,1.0)

	Stat= SetForceVisible(U_linef,WsTrue)
        Stat= SetForceVisible(V_linef,WsTrue)
        Stat= SetForceVisible(U_liner,WsTrue)
        Stat= SetForceVisible(V_liner,WsTrue)
        Stat= SetForceVisible(Puf,WsTrue)
        Stat= SetForceVisible(Pvf,WsTrue)
        Stat= SetForceVisible(Pur,WsTrue)
        Stat= SetForceVisible(Pvr,WsTrue)

        Stat=SetColor(U_linef,Forward_color,WsTrue)
        Stat=SetColor(V_linef,Forward_color,WsTrue)
        Stat=SetColor(U_liner,Reverse_color,WsTrue)
        Stat=SetColor(V_liner,Reverse_color,WsTrue)
        Stat=SetColor(Puf,Forward_color,WsTrue)
        Stat=SetColor(Pvf,Forward_color,WsTrue)
        Stat=SetColor(Pur,Reverse_color,WsTrue)
        Stat=SetColor(Pvr,Reverse_color,WsTrue)

RETURN


Tangent_dir:!
	GOSUB Show_tangent_dir
	Stat=SetInputFormat("n4","")
Pick_orient: L1=PickSpecificEnt("PICK CUTTER ORIENTATION ",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Pick_orient
        Pt=ParameterPt(L1,1.0)
        Du1=DistTwoPts(Pt,Puf)
        Dv1=DistTwoPts(Pt,Pvf)
        Du2=DistTwoPts(Pt,Pur)
        Dv2=DistTwoPts(Pt,Pvr)
	Cut_method= WsRotaryAxisTan1
        Orient_sign= 1
	IF (Du1<= Dv1) THEN
		IF (Du1<= Du2) THEN
			IF (Du1<= Dv2) THEN
					Cut_method= WsRotaryAxisTan2
					Orient_sign= -1
			ELSE
					Cut_method= WsRotaryAxisTan1
					Orient_sign= -1
			END IF
		ELSE
			IF(Du2<= Dv2) THEN
					Cut_method= WsRotaryAxisTan2
					Orient_sign= 1
			ELSE
					Cut_method= WsRotaryAxisTan1
					Orient_sign= -1
			END IF
		END IF
	ELSE
		IF (Dv1<= Du2) THEN
			IF (Dv1<= Dv2) THEN
					Cut_method= WsRotaryAxisTan1
					Orient_sign= 1
			ELSE
					Cut_method= WsRotaryAxisTan1
					Orient_sign= -1
			END IF
		ELSE
			IF(Du2<= Dv2) THEN
					Cut_method= WsRotaryAxisTan2
					Orient_sign= 1
			ELSE
					Cut_method= WsRotaryAxisTan1
					Orient_sign= 1
			END IF
		END IF
	END IF
	Stat=DeleteEntity(U_linef)
	Stat=DeleteEntity(U_liner)
	Stat=DeleteEntity(V_linef)
	Stat=DeleteEntity(V_liner)
	Stat=DeleteEntity(Puf)
	Stat=DeleteEntity(Pur)
	Stat=DeleteEntity(Pvf)
	Stat=DeleteEntity(Pvr)
	Stat=DeleteEntity(Pt)
RETURN



Leave:!
Stat=DeleteEntity(Vf)
Stat=DeleteEntity(Vr)
Stat=DeleteEntity(Pf)
Stat=DeleteEntity(Pr)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(U_linef)
Stat=DeleteEntity(U_liner)
Stat=DeleteEntity(V_linef)
Stat=DeleteEntity(V_liner)
Stat=DeleteEntity(Puf)
Stat=DeleteEntity(Pur)
Stat=DeleteEntity(Pvf)
Stat=DeleteEntity(Pvr)
Stat= DeleteEntity(Rotary_axis)
Stat= DeleteEntity(Locked_axis)
LowerSelectBox
Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF

END
