
! script name: pkvmach.d/vpkcutflowcurve
! var_topic: drives a cutter along a face flow curve

!A
!A NAME:
!A
!A 	Curves: Cut Flow Curve	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script generates a cutterpath along a flow curve on a face.
!A
!B INSTRUCTIONS:
!B
!B 	CUTTER PATH TOOL: Select the tool to be used.
!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 cut along.
!B

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

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 Face$[256]
DIM Finish_var$[256]
DIM Para_var$[256]
DIM Name$[256]

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

ActivateUndo
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.005)
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:pkvmach.d/vpkcutflowcurve"
  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/vpkcutflowcurve"
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(Tol)
Tol=GetValue("TOLERANCE",Answer$)
IF ExitScript(Destination$) THEN Leave

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

UVpoint=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint) THEN Pick_surf1
U=XPointValue(UVpoint)
V=YPointValue(UVpoint)
GOSUB UorV_cut_dir
Stat= DeleteEntity(UVpoint)

Normal_sign=1

Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Flow Curve")
Stat= SetRecDelLastExec
IF No_finish_var THEN
        V1= FreeParameter("Finish allowance",Finish_allow)
ELSE
        V1= FreeParameter("Finish allowance",Finish_var$)
END IF
V2= FreeParameter("Side",Normal_sign)
V3= FreeParameter("Uorv",Uorv)
V4= FreeParameter("Tolerance",Tol)
V5= FreeParameter("Direction",Direction)
V6= FreeParameter("Parameter",Para_value)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,V2,0.0,V1,0.0,2,V5)
Cutter_path=CutSurfaceCurve(Face$,Mdef1,V3,V6,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 curve")
	Stat= AddItemToUndo(WsUBCreateMode,Cutter_path)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Intersect2

Leave:!
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=SetInputFormat("n4","")
Stat=SetPickMask(WsPickGeometry)
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=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
