
! script name: pkvmach.d/vpkflowcurves3
! var_topic: cuts a face by flow curves

!A
!A NAME:
!A
!A 	Curves: Flow Curves	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script generates cuts along the flow curves on a face.
!A
!B INSTRUCTIONS:
!B
!B 	CUTTER PATH TOOL: Select the tool to be used.
!B
!B 	WIDTH OF CUT: Enter the actual width of cut.
!B
!B 	FINISH ALLOWANCE: Enter the amount of material to be left for a
!B 		finish pass.
!B
!B 	TOLERANCE: Enter the amount of variation allowed.
!B
!B 	PICK FACE: Pick the face to be cut.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: cut along flow curves ")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_type$[256]
DIM Surface_dir$[256]
DIM Tool_name$[256]
DIM Toolid$[256]
DIM Mach_def$[256]
DIM Face$[256]
DIM Finish_var$[256]
DIM Width_var$[256]
DIM Name$[256]

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

Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
Zigzag_cuts=NamedVarVal("3D Zigzag Cuts",Ws3DSystemVars,1.0)
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Forward_color=3
Reverse_color=4

ActivateUndo

IF ARGC>1 THEN
	Tool_name$= ARGV(1)
	GOTO Have_tool
END IF

Start:!
    ! StdWorkplane

Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:pkvmach.d/vpkflowcurves3"
  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:pkvmach.d/vpkflowcurves3"
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:!
Cutter_dia=GetUserValue(Tool,"Diameter:")
Widthr=NamedVarVal("width of cut",WsSystemVars,Width_cut)
Width_val=FindNamedVar("factor for width of cut",WsToolVars)
IF ValidGeometry(Width_val) THEN
	Cut_width=Cutter_dia*GetVariableVal(Width_val)
ELSE
	Cut_width= Cutter_dia
END IF
IF Widthr>Cut_width THEN Widthr= Cut_width

Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Widthr)
Width_cut=GetValue("WIDTH OF CUT",Answer$)
IF ExitScript(Destination$) THEN Leave
Width_cut=ABS(Width_cut)
Widthr=Width_cut
No_width_var= LastVariableInput(Width_var$)
Vwidth=CreateVariable("width of cut",WsSystemVars,Widthr)

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(Tol)
Tol=GetValue("TOLERANCE",Answer$)
IF ExitScript(Destination$) THEN Leave

Pick_loop:!
Stat=SetInputFormat("n4","")
Surface=WPKPickEntity("PICK FACE",WsPKPickFace)
IF ExitScript(Destination$) THEN Leave
Stat= EntityNameCheck(Surface,Face$)
Stat= EntityNameCheck(Surface,Surface$)
Stat=SetPickMask(WsPickAll)

Normal_sign=1
UVpoint=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint) THEN Pick_surf1
U=XPointValue(UVpoint)
V=YPointValue(UVpoint)
GOSUB UorV_cut_dir
Stat= DeleteEntity(UVpoint)
IF UorV THEN
	U_direction= Direction
	V_direction= WsForward*(V<= 0.5)+ WsReverse*(V> 0.5)
ELSE
	V_direction= Direction
	U_direction= WsForward*(U<= 0.5)+ WsReverse*(U> 0.5)
END IF
IF U_direction=WsForward THEN
	Startu= 0.0
	Endu= 1.0
ELSE
	Startu= 1.0
	Endu= 0.0
END IF
IF V_direction=WsForward THEN
	Startv= 0.0
	Endv= 1.0
ELSE
	Startv= 1.0
	Endv= 0.0
END IF


Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Flow Curves")
Stat= SetRecDelLastExec
Us= FreeParameter("Start U",Startu)
Vs= FreeParameter("Start V",Startv)
Ue= FreeParameter("End U",Endu)
Ve= FreeParameter("End V",Endv)
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
IF No_width_var THEN
	V2= FreeParameter("Width cut",Width_cut)
ELSE
	V2= FreeParameter("Width cut",Width_var$)
END IF
V3= FreeParameter("Side",Normal_sign)
V4= FreeParameter("Uorv",Uorv)
V5= FreeParameter("Tolerance",Tol)
V6= FreeParameter("Zigzag cuts",Zigzag_cuts)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,V3,V2,V1,0.0,V6)
Cutter_path=CutFlowCurves(Face$,Mdef1,Us,Ue,Vs,Ve,V5,V4)
Stat= NameEntity(Cutter_path,Name$)
Stat= DeleteEntity(Mdef1)

RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Cutter_path) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("cut flow curves")
	Stat= AddItemToUndo(WsUBCreateMode,Cutter_path)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Pick_loop

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

ShowCutDirection:!
        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)

	Stat= SetForceVisible(U_linef,WsTrue)
        Stat= SetForceVisible(U_liner,WsTrue)
        Stat= SetForceVisible(Puf,WsTrue)
        Stat= SetForceVisible(Pur,WsTrue)

        Stat=SetColor(U_linef,Forward_color)
        Stat=SetColor(U_liner,Forward_color)
        Stat=SetColor(Puf,Forward_color)
        Stat=SetColor(Pur,Forward_color)
        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)

        Stat= SetForceVisible(V_linef,WsTrue)
        Stat= SetForceVisible(V_liner,WsTrue)
        Stat= SetForceVisible(Pvf,WsTrue)
        Stat= SetForceVisible(Pvr,WsTrue)

        Stat=SetColor(V_linef,Reverse_color)
        Stat=SetColor(V_liner,Reverse_color)
        Stat=SetColor(Pvf,Reverse_color)
        Stat=SetColor(Pvr,Reverse_color)
RETURN

UorV_cut_dir:!
        GOSUB ShowCutDirection
	Stat=SetPickMask(WsPickAll)
        Stat=SetInputFormat("n4","")
Pick_uorv: L1=PickSpecificEnt("PICK CUT DIRECTION ",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Pick_uorv
        Pt=ParameterPt(L1,1.0)
        D1=DistTwoPts(Pt,Puf)
        D2=DistTwoPts(Pt,Pvf)
        D3=DistTwoPts(Pt,Pur)
        D4=DistTwoPts(Pt,Pvr)
        Uorv=1
        Direction= WsForward
        Para_value= V
        IF (D2< D1) AND (D2< D3) AND (D2< D4)  THEN
                Uorv=0
                Direction= WsForward
                Para_value= U
        END IF
        IF (D3< D1) AND (D3< D2) AND (D3< D4)  THEN
                Uorv=1
                Direction= WsReverse
                Para_value= V
        END IF
        IF (D4< D1) AND (D4< D2) AND (D4< D3)  THEN
                Uorv=0
                Direction= WsReverse
                Para_value= U
        END IF
        Stat=DeleteEntity(U_linef)
        Stat=DeleteEntity(V_linef)
        Stat=DeleteEntity(U_liner)
        Stat=DeleteEntity(V_liner)
        Stat=DeleteEntity(Puf)
        Stat=DeleteEntity(Pvf)
        Stat=DeleteEntity(Pur)
        Stat=DeleteEntity(Pvr)
        Stat=DeleteEntity(Pt)
RETURN

END

