
! script name: v3dfive_axis.d/fiveaxisedge
! var_topic: cut edge curve using five axis cutting

!A
!A NAME:
!A
!A 	Curves: Five axis  Cut Edge Curve	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script drives a cutter along the edge of 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	CUT METHOD: 
!B		Normal- places the cutter normal to the surface.
!B		Tangent- places the cutter tangent to surface perpendicular
!B			 to the cutting curve.
!B		Rulings- places the cutter tangent to surface along the
!B			 U,V curves of the surface.
!B		Locked- locks the cutter axis in a direction specified by
!B			a line.
!B			
!B
!B 	PICK SURFACE: Pick the surface to be used.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: five axis cut edge 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 Finish_var$[256]
DIM Angle_var$[256]
DIM Axis_line$[256]
DIM Name$[256]

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

ActivateUndo
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

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/fiveaxisedge"
  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:v3dfive_axis.d/fiveaxisedge"
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
        Cut_rulings=1
        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$)

GOSUB Which_edge
IF NOT Surf_edge THEN Pick_loop
Which1=Surf_edge
Direction=Edge_dir
Orient_sign1= Surface_sign*Orient_sign


Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Five Axis Edge Curve")
Stat= SetRecDelLastExec
Stat= NullReference(Surface$)
Work= CurrentWorkplane
Stat= SetWorkplane
Raxis= PointXYZ(0,0,1)
IF Cut_method= WsLockedAxis THEN
	Laxis= LineVector(Axis_line$)
        V1= FreeParameter("Cutter Tilt angle",0)
ELSE
	Laxis= 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_sign1)
V7= FreeParameter("Which edge",Which1)
V8= FreeParameter("Z cutter displacement",Zaddon)
V10= FreeParameter("Start parameter",0.0)
V11= FreeParameter("End parameter",1.0)
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=RotaryCutEdgeCurve(Surface$,Raxis,Laxis,V1,V3,V6,V7,V4,V5,0,V10,V11)
Stat= PrintToVarRecord("PlotOff")
Stat= NameEntity(Cutter_path,Name$)
Stat=AssociateTool(Cutter_path,Tool)
Stat= DeleteEntityList(Mdef1,Raxis,Laxis)
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

Which_edge: Surf_edge=0
Edge_dir=WsForward
Pt=LastUVSurfPick(Surface)
IF NOT ValidGeometry(Pt) THEN RETURN
U=XPointValue(Pt)
V=YPointValue(Pt)
Stat=DeleteEntity(Pt)
Surf_edge=ClosestSurfEdge(Surface,U,V)

GOSUB Edge_direction
Surface_sign=1
IF Cut_rulings THEN
        Surface_sign=1
        GOSUB UVTangent_dir
ELSE
        IF Cut_method= WsFiveAxisTan1 OR Cut_method= WsFiveAxisTan2 THEN
                Surface_sign= SurfOrientSign(Surface,U,V)
                GOSUB Tangent_dir
        END IF
END IF
RETURN

ShowEdgeDirection:!
SELECT Surf_edge
CASE 1,3
        Uorv= 1
CASE 2,4
        Uorv= 0
END SELECT
Vf= SurfFlowLine(Surface,U,V,Normal_len,Uorv)
Pf=ParameterPt(Vf,1.0)
Vr= SurfFlowLine(Surface,U,V,-Normal_len,Uorv)
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

Edge_direction:!
        GOSUB ShowEdgeDirection
        Stat=SetInputFormat("n4","")
Pick_edge_dir: L1=PickSpecificEnt("PICK CUT DIRECTION ",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Pick_edge_dir
        Pt=ParameterPt(L1,1.0)
        D1=DistTwoPts(Pt,Pf)
        D2=DistTwoPts(Pt,Pr)
        Edge_dir=WsForward
        IF D2<D1 THEN Edge_dir= WsReverse
	GOSUB Del_curve_vectors
RETURN

Show_tangent_dir:!
        U_linef= SurfFlowLine(Surface,U,V,Normal_len,1)
        Puf=ParameterPt(U_linef,1.0)
        U_liner= SurfFlowLine(Surface,U,V,-Normal_len,1)
        Pur=ParameterPt(U_liner,1.0)
        V_linef= SurfFlowLine(Surface,U,V,Normal_len,0)
        Pvf=ParameterPt(V_linef,1.0)
        V_liner= SurfFlowLine(Surface,U,V,-Normal_len,0)
        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= WsFiveAxisTan1
        Orient_sign= 1
	IF (Du1<= Dv1) THEN
		IF (Du1<= Du2) THEN
			IF (Du1<= Dv2) THEN
				IF UorV=1 THEN
					Cut_method= WsFiveAxisTan1
					Orient_sign= 1
					Surface_sign=1
				ELSE
					Cut_method= WsFiveAxisTan2
					Orient_sign= -1
				END IF
			ELSE
				IF UorV=1 THEN
					Cut_method= WsFiveAxisTan2
					Orient_sign= -1
				ELSE
					Cut_method= WsFiveAxisTan1
					Orient_sign= -1
					Surface_sign=1
				END IF
			END IF
		ELSE
			IF(Du2<= Dv2) THEN
				IF UorV=1 THEN
					Cut_method= WsFiveAxisTan1
					Orient_sign= -1
				ELSE
					Cut_method= WsFiveAxisTan2
					Orient_sign= 1
				END IF
			ELSE
				IF UorV=1 THEN
					Cut_method= WsFiveAxisTan2
					Orient_sign= -1
				ELSE
					Cut_method= WsFiveAxisTan1
					Orient_sign= -1
				END IF
			END IF
		END IF
	ELSE
		IF (Dv1<= Du2) THEN
			IF (Dv1<= Dv2) THEN
				IF UorV=1 THEN
					Cut_method= WsFiveAxisTan2
					Orient_sign= 1
				ELSE
					Cut_method= WsFiveAxisTan1
					Orient_sign= 1
					Surface_sign=1
				END IF
			ELSE
				IF UorV=1 THEN
					Cut_method= WsFiveAxisTan2
					Orient_sign= -1
				ELSE
					Cut_method= WsFiveAxisTan1
					Orient_sign= -1
				END IF
			END IF
		ELSE
			IF(Du2<= Dv2) THEN
				IF UorV=1 THEN
					Cut_method= WsFiveAxisTan1
					Orient_sign= -1
					Surface_sign=1
				ELSE
					Cut_method= WsFiveAxisTan2
					Orient_sign= 1
				END IF
			ELSE
				IF UorV=1 THEN
					Cut_method= WsFiveAxisTan2
					Orient_sign= -1
				ELSE
					Cut_method= WsFiveAxisTan1
					Orient_sign= 1
				END IF
			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_curve_vectors
GOSUB Del_tan_vectors
Stat= DeleteEntity(Raxis)
Stat= DeleteEntity(Laxis)
LowerSelectBox
Stat=SetInputFormat("n4","")
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF


END
