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

!A
!A NAME:
!A
!A 	Drive a milling cutter along a single line.
!A
!A DESCRIPTION:
!A
!A
!A~ image location - images/singlelinemill.jpg
!B INSTRUCTIONS:
!B
!B 	PICK WHICH SIDE: Pick which side of the contour the path should
!B 		be generated.
!B
!B 	FINISH DEPTH: Enter the finish depth.
!B
!C NOTES:
!C
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: 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]


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/singleline"
  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/singleline"
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("START CLEARANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_sclear_var= LastVariableInput(Sclear_var$)

End_clear:!
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(End_clear)
End_clear=GetValue("END CLEARANCE",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$)

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


Finish_depth:!
Finish_depth=NamedVarVal("finish depth",WsSystemVars,0.0)
Stat=SetEqualsValue(Finish_depth)
Stat=SetInputFormat("n4=","")
Finish_depth=GetValue("FINISH DEPTH",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_depth_var= LastVariableInput(Depth_var$)
Vdepth=CreateVariable("finish depth",WsSystemVars,Finish_depth)


Stat=SetCurrentColor(Pen_color)
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("MILL: Single line "&Tool_name$)
Stat= SetRecDelLastExec
Stat= NullReference(Element$)
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)
IF No_depth_var THEN
	V5= FreeParameter("Finish depth",Finish_depth)
ELSE
	V5= FreeParameter("Finish depth",Depth_var$)
END IF
V6= FreeParameter("Direction",Direction)
V7= FreeParameter("Single line",0)
Tool=FindUserData(Tool_name$,WsToolData)
Path=LineMillingPath(Element$,V5,ToolDiameter(Tool),V1,V2,V3,V4,V6,V7)
Stat=AssociateTool(Path,Tool)
Stat= AutoNameGroup(Path,Name$)
V1= FreeParameter("Comp ( 0 )None ( 1 )Left ( -1 )Right",0)
Stat= SetDCompDirection(Path,V1)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Path) THEN 
	DelLastVarRecord
ELSE
	IF Tool_plot THEN
        	Stat= AnimateImage(Tool_group,Path,NULL,1,1,1)
	END IF
	ActivateUndo
	Stat= AdvanceUndoNumber("single line mill")
	Stat= AddGroupToUndo(WsUBCreateMode,Path,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

