
! script name: v3dcpaths.d/cutedgecurve
! var_topic: drives a cutter along the edge of a surface

!A
!A NAME:
!A
!A 	Curves: Cut Edge Curve	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script drives a cutter along the edge of a surface.
!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 SURFACE: Pick the surface to be used.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: 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 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:v3dcpaths.d/cutedgecurve"
  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:v3dcpaths.d/cutedgecurve"
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:!

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
GOSUB Cut_direction
Normal_sign=1

Stat= GenerateVarName(WsCPathFileEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CUTTERPATH: Edge 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("Which edge",Which1)
V4= FreeParameter("Tolerance",Tol)
V5= FreeParameter("Direction",Direction)
Tool=FindUserData(Tool_name$,WsToolData)
Mdef1= ToolMillingDef(Tool,Mill_dir)
Stat=AssociateTool(Mdef1,Tool)
Stat= MillDefCutInfo(Mdef1,V2,0.0,V1,0.0,1,V5)
Cutter_path=CutSurfaceCurve(Surface$,Mdef1,V3,0.0,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 edge curve")
	Stat= AddItemToUndo(WsUBCreateMode,Cutter_path)
	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)

SELECT Surf_edge
    CASE 1,3
	Edge_dir=WsForward
	IF U>0.5 THEN Edge_dir=WsReverse
	UorV=1
    CASE 2,4
	Edge_dir=WsForward
	IF V>0.5 THEN Edge_dir=WsReverse
	UorV=0
END SELECT
RETURN


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

	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,Reverse_color)
        Stat=SetColor(Puf,Forward_color)
        Stat=SetColor(Pur,Reverse_color)
ELSE
        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,Forward_color)
        Stat=SetColor(V_liner,Reverse_color)
        Stat=SetColor(Pvf,Forward_color)
        Stat=SetColor(Pvr,Reverse_color)
END IF
RETURN



Cut_direction:!
        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)
	IF UorV THEN
        	D1=DistTwoPts(Pt,Puf)
        	D2=DistTwoPts(Pt,Pur)
	Direction= WsForward
	IF D2< D1 THEN Direction= WsReverse
	ELSE
        	D1=DistTwoPts(Pt,Pvf)
        	D2=DistTwoPts(Pt,Pvr)
	END IF
	Direction= WsForward
	IF D2< D1 THEN Direction= WsReverse
        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






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

END
