
! script name: voffsets.d/zsingleline
! var_topic: mill along single line

!A
!A NAME:
!A
!A 	Single Line Mill
!A
!A DESCRIPTION:
!A
!A	This script takes a previously defined tool and drives it along
!A	a line. There is a leadin and leadout.  Also, multiple depths of
!A	cut can be taken. The result of the script is a group of paths.
!A
!A~ image location - images/singlelinemill.jpg
!B INSTRUCTIONS:
!B
!B	PATH TOOL: Choose the tool to be used.
!B
!B	LEADIN DISTANCE: The additional distance
!B 		added to the start of the line.
!B
!B	LEADOUT DISTANCE: The additional distance
!B 		added to the end of the line.
!B
!B	OVERLAP: The amount that the cutter
!B 		will come back across the line.  It is a
!B 		negative finish allowance.
!B
!B	PICK LINE TO MILL ALONG: Choose the line to mill along.
!B
!B 	PICK WHICH SIDE: A set of yellow and green
!B 		lines will appear at the endpoint of the line
!B 		and perpendicular. Pick either the yellow or
!B 		green line to choose which side to mill.
!B
!B 	PICK WHICH DIRECTION: A set of yellow and
!B 		green lines will appear endpoint of the line.
!B 		Pick either the yellow or green line to choose
!B 		a direction to mill.
!B
!B	Z LEVEL OF FIRST CUT: Pick the Z level
!B 		where the first cut will be.
!B
!B	Z LEVEL OF LAST CUT: Pick the Z level
!B 		where the last cut will be.
!B
!B	DEPTH OF CUT: Choose a step distance for multiple
!B 		depths of cut.  This value cannot be ZERO.
!B
!B	The Free Parameter "Z step method" Defines how
!B 		the depths are spaced.
!B 		0= (Default) All cuts are even and no greater than the
!B			depth of cut (Z step).
!B 		1= Each cut is "Z step" apart.  Except the last cut which
!B			is the remainder of the depth less than "Z step"
!B
!C NOTES:
!C
!C	There is a free parameter to force the contour to be a single line.
!C	The default is set so that there is a leadin and leadout (3 moves).
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: Z single line mill")

DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_image$[256]
DIM Toolid$[256]
DIM Contour$[256]
DIM Overlap_var$[256]
DIM Sclear_var$[256]
DIM Eclear_var$[256]
DIM Element$[256]
DIM Depth_var$[256]
DIM Name$[256]
DIM Zmethod_var$[256],Zstart_var$[256]
DIM Zend_var$[256],Zstep_var$[256]

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

Center_color=2
Forward_color=3
Reverse_color=4

Current_pen=CurrentColor
Pen_color=WsZigzagCutPen
Stat=SetCurrentColor(Pen_color)

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

Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN 
  LOAD "vtools.d/readmilltool:voffset.d/zsingleline"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("PATH TOOL, New_tool ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN
	LowerSelectBox
	GOTO Leave
END IF
IF Answer$="New_tool" THEN
	LowerSelectBox
	LOAD "vtools.d/readmilltool:voffset.d/zsingleline"
END IF

Have_tool:!
Tool_name$= Answer$
Tool=FindUserData(Answer$,WsToolData)

Stat=GetUserString(Tool,"Tool type:",Toolid$)
IF NOT POS(Toolid$, "MILL") THEN Tool_ques
Cutter_dia=GetUserValue(Tool,"Diameter:")
LowerSelectBox
Stat=GetUserString(Tool,"Tool image:",Tool_image$)
Tool_group=FindNamedGrp(Tool_image$)
IF NOT ValidGeometry(Tool_group) THEN
        Tool_plot=0
ELSE
        Tool_plot=1
END IF

Start_clear:!
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Start_clear)
Start_clear=GetValue("LEADIN DISTANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_sclear_var= LastVariableInput(Sclear_var$)

End_clear:!
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(End_clear)
End_clear=GetValue("LEADOUT DISTANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_eclear_var= LastVariableInput(Eclear_var$)

Overlap:!
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Overlap)
Overlap=GetValue("OVERLAP",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_overlap_var= LastVariableInput(Overlap_var$)

Pick_loop:!
Stat=SetPickMask(WsPickNoSurf)
Line_ent=PickEntity("PICK LINE TO MILL ALONG")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Line_ent) THEN Pick_loop
Stat= EntityNameCheck(Line_ent,Element$)

Stat= SetInputFormat("n4=","")
Stat= SetEqualsValue(Zstart)
Stat=SetPickReturn(WsGeometryZ)
Zstart=GetValue("Z LEVEL OF FIRST CUT ")
Stat=SetPickReturn(WsGeometryEnd)
IF ExitScript(Destination$) THEN Leave
No_zstart_var= LastVariableInput(Zstart_var$)

Stat= SetInputFormat("n4=","")
Stat= SetEqualsValue(Zend)
Stat=SetPickReturn(WsGeometryZ)
Zend=GetValue("Z LEVEL OF LAST CUT ")
Stat=SetPickReturn(WsGeometryEnd)
IF ExitScript(Destination$) THEN Leave
No_zend_var= LastVariableInput(Zend_var$)

IF Zstart<>Zend THEN
Get_zstep:Stat= SetInputFormat("n4=","")
	Zstep=NamedVarVal("depth of cut",WsSystemVars,0.0)
	Stat=SetEqualsValue(Zstep)
	Zstep=GetValue("DEPTH OF CUT ")
	IF ExitScript(Destination$) THEN Leave
	No_zstep_var= LastVariableInput(Zstep_var$)
	IF NOT Zstep THEN Get_zstep
	Ncuts= ABS((Zstart-Zend)/Zstep)
	Vdepth=CreateVariable("depth of cut",WsSystemVars,Zstep)
ELSE
	Zstep= 0
	No_zstep_var= 1
	Ncuts= 1
END IF

Normal_len= ABS(Cutter_dia*0.5)
Side=1
Direction= WsForward
IF Normal_len>0 THEN
	Element= Line_ent
	Para_value= LastPickPara
	GOSUB Which_side
END IF
GOSUB Which_dir

Stat=SetCurrentColor(Pen_color)
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("MILL: Single line mill "&Tool_name$)
Stat= SetRecDelLastExec
Stat= NullReference(Element$)
V1= FreeParameter("Z step method",0)
IF No_zstart_var THEN
	V2= FreeParameter("Z start",Zstart)
ELSE
	V2= FreeParameter("Z start",Zstart_var$)
END IF
IF No_zend_var THEN
	V3= FreeParameter("Z end level",Zend)
ELSE
	V3= FreeParameter("Z end level",Zend_var$)
END IF
IF No_zstep_var THEN
	V4= FreeParameter("Z step",Zstep)
ELSE
	V4= FreeParameter("Z step",Zstep_var$)
END IF
Zgroup= ZStepsPtGrp(V1,V2,V3,V4)
IF No_sclear_var THEN
	V1= FreeParameter("Start clearance",Start_clear)
ELSE
	V1= FreeParameter("Start clearance",Sclear_var$)
END IF
IF No_eclear_var THEN
	V2= FreeParameter("End clearance",End_clear)
ELSE
	V2= FreeParameter("End clearance",Eclear_var$)
END IF
IF No_overlap_var THEN
	V3= FreeParameter("Overlap",Overlap)
ELSE
	V3= FreeParameter("Overlap",Overlap_var$)
END IF
V4= FreeParameter("Side",Side)
V6= FreeParameter("Direction",Direction)
V7= FreeParameter("Single line",0)
Tool=FindUserData(Tool_name$,WsToolData)
Paths=ZLineMillingPath(Element$,Zgroup,ToolDiameter(Tool),V1,V2,V3,V4,V6,V7)
Stat=AssociateTool(Paths,Tool)
Stat= AutoNameGroup(Paths,Name$)
Stat= DeleteGroup(Zgroup,WsTrue)
V1= FreeParameter("Comp ( 0 )None ( 1 )Left ( -1 )Right",0)
Stat= SetDCompDirection(Paths,V1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Paths) THEN 
	DelLastVarRecord
ELSE
	IF Tool_plot THEN
        	Stat= AnimateImage(Tool_group,Paths,NULL,1,1,1)
	END IF
	ActivateUndo
	Stat= AdvanceUndoNumber("single line mill")
	Stat= AddGroupToUndo(WsUBCreateMode,Paths,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Pick_loop


ShowNormalDir:!
     Center_point=ParameterPt(Element,Para_value)
     Stat=SetColor(Center_point,Center_color)
     Stat= SetForceVisible(Center_point,WsTrue)

     PlotOff
     Ps1= ParameterPt(Element,0.0)
     Pe1= ParameterPt(Element,1.0)
     Line1= LineTwoPoints(Ps1,Pe1)
     PlotOn

     Forward_normal= ParameterLine(Line1,Para_value,Normal_len,WsNormal)
     Pforward= ParameterPt(Forward_normal,1.0)

     Reverse_normal= ParameterLine(Line1,Para_value,-Normal_len,WsNormal)
     Preverse= ParameterPt(Reverse_normal,1.0)

     PlotOff
     Stat= DeleteEntity(Ps1)
     Stat= DeleteEntity(Pe1)
     Stat= DeleteEntity(Line1)
     PlotOn

     Stat= SetForceVisible(Forward_normal,WsTrue)
     Stat= SetForceVisible(Pforward,WsTrue)
     Stat= SetForceVisible(Reverse_normal,WsTrue)
     Stat= SetForceVisible(Preverse,WsTrue)

     Stat=SetColor(Forward_normal,Forward_color)
     Stat=SetColor(Pforward,Forward_color)
     Stat=SetColor(Reverse_normal,Reverse_color)
     Stat=SetColor(Preverse,Reverse_color)
RETURN

ShowElementDir:!
     Center_point=ParameterPt(Element,Para_value)
     Stat=SetColor(Center_point,Center_color)
     Stat= SetForceVisible(Center_point,WsTrue)

     Forward_line= ParameterLine(Element,Para_value,Normal_len,WsTangent)
     Pforward= ParameterPt(Forward_line,1.0)

     Reverse_line= ParameterLine(Element,Para_value,-Normal_len,WsTangent)
     Preverse= ParameterPt(Reverse_line,1.0)

     Stat= SetForceVisible(Forward_line,WsTrue)
     Stat= SetForceVisible(Pforward,WsTrue)
     Stat= SetForceVisible(Reverse_line,WsTrue)
     Stat= SetForceVisible(Preverse,WsTrue)

     Stat=SetColor(Forward_line,Forward_color)
     Stat=SetColor(Pforward,Forward_color)
     Stat=SetColor(Reverse_line,Reverse_color)
     Stat=SetColor(Preverse,Reverse_color)
RETURN


Which_side:!
        GOSUB ShowNormalDir
Pick_normal: L1=PickSpecificEnt("PICK WHICH SIDE",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Line_loop
        T1= LastPickPara
        Side=0
        Pt=ParameterPt(L1,T1)
        D1= DistTwoPts(Pt,Center_point)
        IF D1< 0.15*Normal_len THEN
                Side=0
        ELSE
                D1=DistTwoPts(Pt,Pforward)
                D2=DistTwoPts(Pt,Preverse)
                IF D1<= D2 THEN
                        Side= 1
                ELSE
                        Side= -1
                END IF
        END IF
Stat=DeleteEntity(Center_point)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
RETURN

Which_dir:!
        GOSUB ShowElementDir
Pick_dir: L1=PickSpecificEnt("PICK WHICH DIRECTION",WsPickLines)
        IF ExitScript(Destination$) THEN Leave
        IF NOT ValidGeometry(L1) THEN Line_loop
        T1= LastPickPara
        Pt=ParameterPt(L1,T1)
        D1=DistTwoPts(Pt,Pforward)
        D2=DistTwoPts(Pt,Preverse)
        IF D1<= D2 THEN
		IF IsDirReversed(Element)=WsTrue THEN
                       	Direction= -1
		ELSE
                       	Direction= 1
		END IF
       ELSE
		IF IsDirReversed(Element)=WsTrue THEN
                       	Direction= 1
		ELSE
                       	Direction= -1
		END IF
       END IF
Stat=DeleteEntity(Center_point)
Stat=DeleteEntity(Forward_line)
Stat=DeleteEntity(Reverse_line)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
RETURN






Leave:!
Stat=SetPickMask(WsPickGeometry)
LowerSelectBox
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Center_point)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat=SetCurrentColor(Current_pen)
Stat=SetInputFormat("n4","")
Stat=DeleteEntity(Pt)
IF (NOT LEN(Destination$)) THEN
	  LOAD "typecontrol"
ELSE
	  LOAD Destination$
END IF
END

