
! script name: v3dfive_axis.d/fiveaxisxyz
! var_topic: cut xyz curve on surface using five axis cutting

!A
!A NAME:
!A
!A 	Curves: Five axis  Cut XYZ Curve on a Surface   VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script drives a cutter along a curve on a surface using 
!A	five axis machining.
!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 	TOLERANCE: Enter the amount of variation allowed.
!B
!B 	PICK SURFACE: Pick the surface to be used.
!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: five axis cut xyz curve ")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Surface_dir$[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:v3dfive_axis.d/fiveaxisxyz"
  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
	LOAD "vtools.d/readmilltool:v3dfive_axis.d/fiveaxisxyz"
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

Cut_method:!
Cut_rulings=0
Stat=SetInputFormat("a1=",",N,T,L,R,")
Stat=SetEqualsValue(0,"N")
Stat=GetValue("CUT METHOD: Normal, Tangent, Rulings, Locked ",Answer$)
IF ExitScript(Destination$) THEN Leave
SELECT Answer$
CASE "N"
        Cut_method= WsFiveAxisNormal
        Orient_sign= 1
CASE "T"
        Cut_method= WsFiveAxisTan1
        Orient_sign= 1
CASE "R"
        Cut_method= WsFiveAxisTan1
        Orient_sign= 1
        Cut_rulings=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$)
Pt=LastUVSurfPick(Surface)
IF NOT ValidGeometry(Pt) THEN Pick_loop
U=XPointValue(Pt)
V=YPointValue(Pt)
Stat=DeleteEntity(Pt)

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: Five Axis XYZ Curve On Surface")
Stat= SetRecDelLastExec
Stat= NullReference(Surface$)
Stat= NullReference(Contour$)
Work= CurrentWorkplane
Stat= SetWorkplane
Rotary_axis= PointXYZ(0,0,1)
IF Cut_method= WsLockedAxis THEN
        Locked_axis= LineVector(Axis_line$)
        V1= FreeParameter("Cutter Tilt angle",0)
ELSE
        Locked_axis= LineVector("")
        IF No_angle_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 WsFiveAxisNormal
        V3= FreeParameter("Cut method",WsFiveAxisNormal)
CASE WsFiveAxisTan1
        V3= FreeParameter("Cut method",WsFiveAxisTan1)
CASE WsFiveAxisTan2
        V3= FreeParameter("Cut method",WsFiveAxisTan2)
CASE WsFiveAxisTanU
        V3= FreeParameter("Cut method",WsFiveAxisTanU)
CASE WsFiveAxisTanV
        V3= FreeParameter("Cut method",WsFiveAxisTanV)
END SELECT
V4= FreeParameter("Tolerance",Tol)
V5= FreeParameter("Direction",Direction)
V6= FreeParameter("Orient sign",Orient_sign)
V8= 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(V8)
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("five 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_rulings THEN
        GOSUB UVTangent_dir
ELSE
        IF Cut_method= WsFiveAxisTan1 OR Cut_method= WsFiveAxisTan2 THEN
                GOSUB Tangent_dir
        END IF
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
	GOSUB Del_curve_vectors
RETURN

Show_tangent_dir:!
        if Cut_rulings THEN
           U_linef= SurfFlowLine(Surface,U,V,Normal_len,1)
           U_liner= SurfFlowLine(Surface,U,V,-Normal_len,1)
           Puf=ParameterPt(U_linef,1.0)
           Pur=ParameterPt(U_linef,-1.0)
           V_linef= SurfFlowLine(Surface,U,V,Normal_len,0)
           V_liner= SurfFlowLine(Surface,U,V,-Normal_len,0)
           Pvf=ParameterPt(V_linef,1.0)
           Pvr=ParameterPt(V_linef,-1.0)
        ELSE
           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)
        END IF

	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= WsFiveAxisTan1
        Orient_sign= 1
	IF (Du1<= Dv1) THEN
		IF (Du1<= Du2) THEN
			IF (Du1<= Dv2) THEN
					Cut_method= WsFiveAxisTan2
					Orient_sign= -1
			ELSE
					Cut_method= WsFiveAxisTan1
					Orient_sign= -1
			END IF
		ELSE
			IF(Du2<= Dv2) THEN
					Cut_method= WsFiveAxisTan2
					Orient_sign= 1
			ELSE
					Cut_method= WsFiveAxisTan1
					Orient_sign= -1
			END IF
		END IF
	ELSE
		IF (Dv1<= Du2) THEN
			IF (Dv1<= Dv2) THEN
					Cut_method= WsFiveAxisTan1
					Orient_sign= 1
			ELSE
					Cut_method= WsFiveAxisTan1
					Orient_sign= -1
			END IF
		ELSE
			IF(Du2<= Dv2) THEN
					Cut_method= WsFiveAxisTan2
					Orient_sign= 1
			ELSE
					Cut_method= WsFiveAxisTan1
					Orient_sign= 1
			END IF
		END IF
	END IF
	GOSUB Del_tan_vectors
RETURN

UVTangent_dir:!
        GOSUB Show_tangent_dir
        Stat=SetInputFormat("n4","")
Pick_uvorient: L1=PickSpecificEnt("PICK CUTTER ORIENTATION ",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Pick_uvorient
        Pt=ParameterPt(L1,1.0)
        Du1=DistTwoPts(Pt,Puf)
        Dv1=DistTwoPts(Pt,Pvf)
        Du2=DistTwoPts(Pt,Pur)
        Dv2=DistTwoPts(Pt,Pvr)
        Cut_method= WsFiveAxisTanU
        Orient_sign= 1
        IF (Du1<= Dv1) THEN
                IF (Du1<= Du2) THEN
                        IF (Du1<= Dv2) THEN
                                Cut_method= WsFiveAxisTanU
                                Orient_sign= 1
                        ELSE
                                Cut_method= WsFiveAxisTanV
                                Orient_sign= -1
                        END IF
                ELSE
                        IF(Du2<= Dv2) THEN
                                Cut_method= WsFiveAxisTanU
                                Orient_sign= -1
                        ELSE
                                Cut_method= WsFiveAxisTanV
                                Orient_sign= -1
                        END IF
                END IF
        ELSE
	   IF (Dv1<= Du2) THEN
                        IF (Dv1<= Dv2) THEN
                                Cut_method= WsFiveAxisTanV
                                Orient_sign= 1
                        ELSE
                                Cut_method= WsFiveAxisTanV
                                Orient_sign= -1
                        END IF
                ELSE
                        IF(Du2<= Dv2) THEN
                                Cut_method= WsFiveAxisTanU
                                Orient_sign= -1
                        ELSE
                                Cut_method= WsFiveAxisTanV
                                Orient_sign= -1
                        END IF
                END IF
        END IF
        GOSUB Del_tan_vectors
RETURN

Del_tan_vectors:!
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

Del_curve_vectors:!
Stat=DeleteEntity(Vf)
Stat=DeleteEntity(Vr)
Stat=DeleteEntity(Pf)
Stat=DeleteEntity(Pr)
Stat=DeleteEntity(Pt)
RETURN


Leave:!
GOSUB Del_tan_vectors
GOSUB Del_curve_vectors
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
