
! script name: wscmill.d/manualmilling
! var_topic: variational manual milling

!A
!A NAME:
!A
!A 	Manual Milling  
!A
!A DESCRIPTION:
!A
!A	This script generates a milling contour by moving a tool 
!a	from point to point.
!A
!B INSTRUCTIONS:
!B
!B	TOOL: Select the tool to be used. The tool must have an image.
!B
!B	Z LEVEL: Input the Z level the contour is to be generated on.
!B		 An input of zero will place it on the current workplane.
!B
!C NOTES:
!C
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: manual milling")

OPTION BASE 1
DIM Destination$[256]
DIM Answer$[256]
DIM Tool_name$[256]
DIM Tool_image$[256]
DIM Point$[256]
DIM Name$[256]
DIM Z_var$[256]
INTEGER Ref_array(200,2)
REAL Param_array(200)

Var_object= CurrentVarObject
IF NOT ValidGeometry(Var_object) THEN
        Var_object=OpenVarObject("Program")
END IF
! Var_object= CheckVarObject
! IF (ExitScript(Destination$)) THEN Leave

Lock_axis=1
Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)
Max_elements= 200

Current_pen=CurrentColor
Stat= SetCurrentColor(4)
IF ARGC>1 THEN
	Answer$= ARGV(1)
	GOTO Have_tool
END IF

Start:!
Stat=ToolSelectBox("TOOLS","selected tool",WsAllMills)
IF Stat THEN
  LOAD "wscmill.d/readmilltool:wscmill.d/manualmilling"
  Stat= MessageFileBox("/usr/weber/database/messages/notoolsloaded","ATTENTION")
  GOTO Leave
END IF
Tool_ques:!
Stat=SetInputFormat("a99","")
Stat=GetValue("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 "wscmill.d/readmilltool:wscmill.d/manualmilling"
END IF

Have_tool:!
Tool_name$= Answer$
Tool=FindUserData(Tool_name$,WsToolData)
IF NOT ValidGeometry(Tool) THEN Tool_ques

Stat=GetUserString(Tool,"Tool image:",Tool_image$)
Tool_group=FindNamedGrp(Tool_image$)
IF NOT ValidGeometry(Tool_group) THEN
	Stat= SetEqualsValue(0,"P")
	Stat=SetInputFormat("a1=",",P,E,")
	Stat=GetValue("TOOL NEEDS AN IMAGE Proceed,  Exit ",Answer$)
	IF (ExitScript(Destination$)) THEN Leave
	IF Answer$= "E" THEN Leave
	IF Answer$= "P" THEN Start
	GOTO Start
END IF
Tool_plot=0


Stat=GetUserString(Tool,"Tool type:",Tool_name$)
Tool_name$=Answer$
Cutter_dia=GetUserValue(Tool,"Diameter:")
IF NOT Cutter_dia THEN Start
LowerSelectBox


Contour= CreateGroup("MANUAL PATH",WsContourGroup)
Pt_refs= CreateGroup("Point references",WsTempGroup)
Stat= AssociateTool(Contour,Tool)

PlotOff
Trans1= CurrentWorkplane
IF NOT ValidGeometry(Trans1) THEN
	Work_trans= IdentityTrans
ELSE
	Work_trans= OrientationTrans(Trans1)
END IF
Identity_trans= IdentityTrans
PlotOn

stat= SetInputFormat("n4=","")
Stat=SetPickReturn(WsGeometryZ)
Zlevel=GetValue("Z LEVEL (+ up - down)  ")
IF (ExitScript(Destination$)) THEN Leave
No_z_var= LastVariableInput(Z_var$)
Stat=SetPickReturn(WsGeometryEnd)

PlotOff
Pm=PointXYZ(0.0,0.0,Zlevel)
T1=AngleTransform(Pm,0,0,0,WsWorkplaneVals)
Tool_group1= TransformGroup(Tool_group,T1,"Tool image group")
Stat= DeleteEntity(Pm)
PlotOn


Start_point:!
ActivateUndo
Element_num=GroupCount(Pt_refs)+1
IF Element_num>= Max_elements THEN Leave
Stat=SetInputFormat("n4","")
Stat=SetXyzInput(WsXyzInput)
Pstart=XyzInput("INPUT START POINT",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Pstart) THEN Start_point
Stat=SetLastPoint(Pstart,WsXyzInput)

Stat= SetSelectableFlag(Pstart,WsFalse)
Element= LastPickEnt
Para= LastPickPara
Pick_return= CurrPickReturn
IF NOT ValidDependency(Element) THEN
        Element= CopyEntity(Pstart,WsFalse)
	Stat= SetSelectableFlag(Element,WsTrue)
        Stat= EntityNameCheck(Element,Point$)
	Ref_array(Element_num,1)= 0
	Ref_array(Element_num,2)= 0
	Stat= AddToGroup(Pt_refs,Element)
ELSE
        Stat= EntityNameCheck(Element,Point$)
	Ref_array(Element_num,1)= 1
	Ref_array(Element_num,2)= Pick_return
	Param_array(Element_num)= Para
	Stat= AddToGroup(Pt_refs,Element)
END IF
Stat=SetLastPoint(Pstart)


IF Tool_plot THEN
	CALL PlotToolImage(Tool_group1,Pstart,Work_trans)
END IF

Pick_next_pt:!
PlotOff
FadeUndoButtons
Graph_tool= CopyGroup(Tool_group1,WsFalse)
Shift_trans=AngleTransform(Pstart,0,0,0,WsRealSpaceVals)
IF ValidGeometry(Trans1) THEN
	Trans1=MultTransforms(Shift_trans,Work_trans)
        Stat=TransGroupTrans(Graph_tool,Trans1)
	Stat= DeleteEntity(Shift_trans)
ELSE
        Stat=TransGroupTrans(Graph_tool,Shift_trans)
END IF
Stat= MakeInvisible(Graph_tool)
Angle= 0
Pnext= CopyEntity(Pstart,WsFalse)
Stat= SetSelectableFlag(Pnext,WsFalse)
Vnum= LastPickView
Erase_original= WsFalse
stat= SetInputFormat("n4b","")
Stat=SetPickMask(WsPickNoSurf)
Stat= MakeInvisible(Graph_tool)
Stat= MakeInvisible(Pnext)
Stat= MakeInvisible(Pstart)
W1= Cutter_dia/2
PlotOn
Rotate_angle= DynamicMoveGroup(Vnum,Graph_tool,Pstart,Pnext,Angle,"PICK NEXT POINT",Erase_original,WsFalse,WsTrue,W1)
PlotOff
Stat= DeleteGroup(Graph_tool,WsTrue)
Stat= DeleteEntity(Shift_trans)
PlotOn
IF ExitScript(Destination$) THEN Leave

Element= LastPickEnt
Para= LastPickPara
Pick_return= CurrPickReturn

IF Lock_axis THEN
   Stat= XyzPointValues(Pstart,Xr,Yr,Zr)
   Stat= XyzPointValues(Pnext,X,Y,Z)

   Dx= ABS(X-Xr)
   Dy= ABS(Y-Yr)
   IF Dy< 0.1*Dx THEN
        Stat= DeleteEntity(Pnext)
        Pnext= PointXyz(X,Yr,Z)
   ELSE
     IF Dx<  0.1*Dy THEN
        Stat= DeleteEntity(Pnext)
        Pnext= PointXyz(Xr,Y,Z)
     END IF
   END IF
END IF

Stat= SetSelectableFlag(Pnext,WsFalse)
Stat=SetLastPoint(Pnext,WsXyzInput)
Dist= DistTwoPts(Pstart,Pnext)
IF Dist< 0.0001 THEN 
	Stat= SetSelectableFlag(Pnext,WsTrue)
	Stat=DeleteEntity(Pnext)
	GOTO Pick_next_pt
END IF

Element_num= GroupCount(Pt_refs)+1
IF NOT ValidDependency(Element) THEN
        Element= CopyEntity(Pnext,WsFalse)
	Stat= SetSelectableFlag(Element,WsTrue)
	Stat= MakeVisible(Element)
        Stat= EntityNameCheck(Element,Point$)
        Ref_array(Element_num,1)= 0
        Ref_array(Element_num,2)= 0
        Stat= AddToGroup(Pt_refs,Element)
ELSE
        Stat= EntityNameCheck(Element,Point$)
        Ref_array(Element_num,1)= 1
        Ref_array(Element_num,2)= Pick_return
        Param_array(Element_num)= Para
        Stat= AddToGroup(Pt_refs,Element)
END IF

Stat= SetSelectableFlag(Contour,WsFalse)
Stat= SetSelectedFlag(Contour,WsTrue)
Stat= PlotEntity(Contour)
CALL MoveToNextPoint(Tool,Tool_group1,Contour,Pstart,Pnext,Tool,Work_trans,Identity_trans,Tool_plot)
Stat= SetSelectableFlag(Pstart,WsTrue)
Stat=DeleteEntity(Pstart)
Pstart= CopyEntity(Pnext)
Stat=DeleteEntity(Pnext)
PlotAll


Control_ques:!
Stat= SetSelectableFlag(Contour,WsFalse)
Stat= SetSelectedFlag(Contour,WsTrue)
Stat= PlotEntity(Contour)
Stat=SetInputFormat("a1=",",O,N,P,B,E,R,L,")
Stat= SetEqualsValue(0,"P")
Stat=GetValue("Point, Location(xyz), Backup, New tool, Exit ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "O"
	Lock_axis= (Lock_axis=0)
CASE "A"
	Tool_plot=1
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group1,Pstart,Work_trans)
		Stat= AnimateImage(Tool_group1,Contour,Identity_trans,1,1,1)
		CALL PlotToolImage(Tool_group1,Pstart,Work_trans)
	END IF
	Tool_plot=0
	GOTO Control_ques
CASE "F"
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group1,Pstart,Work_trans)
	END IF
	CALL FillToolImage(Cutter_dia,ENTITY Contour,Work_trans)
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group1,Pstart,Work_trans)
	END IF
	GOTO Control_ques
CASE "N"
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group1,Pstart,Work_trans)
	END IF
	DetachGroupImage
	PlotOff
	Stat= SetSelectableFlag(Pstart,WsTrue)
	Stat= DeleteEntity(Pstart)
	Stat= DeleteGroup(Graph_tool,WsTrue)
	PlotOn
	GOSUB Make_record
        Contour= CreateGroup("MANUAL PATH",WsContourGroup)
        Pt_refs= CreateGroup("Point references",WsTempGroup)
        Stat= AssociateTool(Contour,Tool)
	Stat= DeleteGroup(Tool_group1,WsTrue)
	GOTO Start
CASE "E"
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group1,Pstart,Work_trans)
	END IF
	DetachGroupImage
	PlotOff
	Stat= SetSelectableFlag(Pstart,WsTrue)
	Stat= DeleteEntity(Pstart)
	Stat= DeleteGroup(Graph_tool,WsTrue)
	PlotOn
	GOSUB Make_record
	Stat= DeleteGroup(Tool_group1,WsTrue)
	GOTO Leave
CASE "P"
	GOTO Pick_next_pt
CASE "B"
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group1,Pstart,Work_trans)
	END IF
	Stat= SetSelectableFlag(Pstart,WsTrue)
	Stat= DeleteEntity(Pstart)
	N1= GroupCount(Contour)
	Last_element= GroupElement(Contour,N1)
	Stat= RemoveFromGrp(Contour,N1)
	Stat= SetSelectableFlag(Last_element,WsTrue)
	Stat= DeleteEntity(Last_element)
	N1= GroupCount(Contour)

	N2= GroupCount(Pt_refs)
        IF N2 THEN
        	Element= GroupElement(Pt_refs,N2)
                Stat= RemoveFromGrp(Pt_refs,N2)
                IF Ref_array(N2,1)=0 THEN
			Stat= SetSelectableFlag(Element,WsTrue)
                        Stat= DeleteEntity(Element)
                END IF
                Ref_array(N2,1)=0
                Ref_array(N2,2)=0
                Param_array(N2)=0
                Element_num= GroupCount(Pt_refs)+1
        END IF
	IF N1= 0 THEN 
		Stat= SetSelectableFlag(Contour,WsTrue)
		Stat= DeleteGroup(Contour,WsTrue)
		Stat= DeleteEntity(Pt_refs)
		Contour= CreateGroup("MANUAL PATH",WsContourGroup)
		Pt_refs= CreateGroup("Point references",WsTempGroup)
		Stat= AssociateTool(Contour,Tool)
		GOTO Start_point
	END IF
	Last_element= GroupElement(Contour,N1)
	Pstart= ParameterPt(Last_element,1.0)
	!Stat= SetSelectableFlag(Pstart,WsFalse)
	Stat=SetLastPoint(Pstart,WsXyzInput)
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group1,Pstart,Work_trans)
	END IF
	GOTO Control_ques
CASE "R"
	Stat=RulerCenter(Pstart)
	Stat= RulerToggle(1)
CASE "L"
	Stat=SetInputFormat("n4","")
	Pnext=XyzInput("INPUT NEXT POINT",WsXyzInput)
	IF (ExitScript(Destination$)) THEN Leave

	!Stat= SetSelectableFlag(Pstart,WsFalse)

	Stat=SetLastPoint(Pnext)
	Element= LastPickEnt
	Para= LastPickPara
	Pick_return= CurrPickReturn

	Dist= DistTwoPts(Pstart,Pnext)
	IF Dist< 0.0001 THEN
		Stat= SetSelectableFlag(Pnext,WsTrue)
		Stat=DeleteEntity(Pnext)
		GOTO Control_ques
	END IF
	Element_num= GroupCount(Pt_refs)+1
	IF NOT ValidDependency(Element) THEN
       		 Element= CopyEntity(Pnext,WsFalse)
		Stat= SetSelectableFlag(Element,WsTrue)
       		 Stat= MakeVisible(Element)
       		 Stat= EntityNameCheck(Element,Point$)
       		 Ref_array(Element_num,1)= 0
       		 Ref_array(Element_num,2)= 0
       		 Stat= AddToGroup(Pt_refs,Element)
	ELSE
       		 Stat= EntityNameCheck(Element,Point$)
       		 Ref_array(Element_num,1)= 1
       		 Ref_array(Element_num,2)= Pick_return
       		 Param_array(Element_num)= Para
       		 Stat= AddToGroup(Pt_refs,Element)
	END IF

	CALL MoveToNextPoint(Tool,Tool_group1,Contour,Pstart,Pnext,Tool,Work_trans,Identity_trans,Tool_plot)
	Stat= SetSelectableFlag(Pstart,WsTrue)
	Stat=DeleteEntity(Pstart)
	Pstart= CopyEntity(Pnext)
	Stat=DeleteEntity(Pnext)
END SELECT
GOTO Control_ques


Make_record:!
Stat= SetSelectableFlag(Contour,WsTrue)
Stat= DeleteGroup(Contour,WsTrue)
IF NOT ValidGeometry(Pt_refs) THEN RETURN
IF NOT GroupCount(Pt_refs) THEN RETURN
Nelements= GroupCount(Pt_refs)

New_pts_grp= CreateGroup("New points created",WsPointGroup)

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CONTOUR: Manual milling")
Stat= SetRecDelLastExec
G1= CreateGroup(" ")
RecordFunctionOff
FOR I= 1 TO Nelements
	Element= GroupElement(Pt_refs,I)
	IF Ref_array(I,1)= 0 THEN     ! FREE POINT
        	Stat= EntityNameCheck(Element,Point$)
		Stat= AddToGroup(New_pts_grp,Element)
		Stat= SetSelectableFlag(Element,WsTrue)
        	RecordFunctionOn
        	Pt= CopyEntity(Point$,WsFalse)
        	RecordFunctionOff
	ELSE
       		 Pick_return= Ref_array(I,2)
       		 Para= Param_array(I)
       		 Stat= EntityNameCheck(Element,Point$)
       		 RecordFunctionOn
       		 SELECT Pick_return
       		 CASE WsGeometryEnd
       		          Pt= ParameterReturnPt(Point$,Para,WsGeometryEnd)
       		 CASE WsGeometryMid
       		          Pt= ParameterReturnPt(Point$,Para,WsGeometryMid)
       		 CASE WsGeomCenter
       		          Pt= ParameterReturnPt(Point$,Para,WsGeomCenter)
       		 CASE WsNearest
       		          Pt= ParameterReturnPt(Point$,Para,WsNearest)
       		 END SELECT
       		 RecordFunctionOff
	END IF
        RecordFunctionOn
	Stat= AddToGroup(G1,Pt)
	RecordFunctionOff
NEXT I
RecordFunctionOn
IF No_z_var THEN
	V1= FreeParameter("Z level",Zlevel)
ELSE
	V1= FreeParameter("Z level",Z_var$)
END IF
Path= ContourFromPoints(G1,V1)
Stat= DeleteGroup(G1)
Tool=FindUserData(Tool_name$,WsToolData)
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

Stat= PlotEntity(Pt_refs)
Stat= DeleteEntity(Pt_refs)
IF NOT ValidGeometry(Path) THEN
        DelLastVarRecord
	Stat= DeleteGroup(New_pts_grp,WsTrue)
ELSE
        IF Tool_plot THEN Stat= AnimateTool(Tool,Path)
        ActivateUndo
        Stat= AdvanceUndoNumber("manual milling")
        Stat= AddGroupToUndo(WsUBCreateMode,New_pts_grp,0,WsFalse)
        Stat= AddGroupToUndo(WsUBCreateMode,Path,1,WsFalse)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
	Stat= DeleteEntity(New_pts_grp)
END IF
RETURN

Leave:!
DetachGroupImage
Stat= DeleteGroup(Tool_group1,WsTrue)
Stat= SetSelectableFlag(Contour,WsTrue)
Stat= DeleteGroup(Contour,WsTrue)
Stat= DeleteGroup(Graph_tool,WsTrue)
Stat= SetSelectableFlag(Pstart,WsTrue)
Stat= SetSelectableFlag(Pnext,WsTrue)
Stat= DeleteEntity(Pstart)
Stat= DeleteEntity(Pnext)
Stat=SetCurrentColor(Current_pen)
Stat=SetInputFormat("n4","")
LowerSelectBox
IF (NOT LEN(Destination$)) THEN
	LOAD "typecontrol"
ELSE
	IF Destination$= "SCRIPTSTART" THEN Start
	IF Destination$= "BACKUP" THEN Start
	LOAD Destination$
END IF
END

SUB PlotToolImage(ENTITY Tool_group,Pt,Work_trans)
	Trans1= AngleTransform(Pt,0.0,0.0,0.0)
	Trans2= MultTransforms(Trans1,Work_trans)
	Stat= PlotGroupImage(Tool_group,Trans2,1,1,1,1)
	Stat= AttachGroupImage(Tool_group,Trans2,1,1,1,1)
	Stat= DeleteEntity(Trans1)
	Stat= DeleteEntity(T2)
SUBEND

SUB FillToolImage(REAL Cutter_dia,ENTITY Contour,Work_trans)
IF Cutter_dia > 0 THEN 
	Fill_pen= CurrentColor
        Radius=ABS(0.5*Cutter_dia)
        D1= Radius/(5*(1+24.4*(CurrentUnits=WsMetric)))
        Stat= AnimateSpeed(D1)
	Identity_trans= IdentityTrans
        stat= AnimateCircle(Radius,Contour,Identity_trans,TpFillTool,fill_pen)
	Stat= DeleteEntity(Identity_trans)
END IF
SUBEND

SUB MoveToNextPoint(ENTITY Tool,Tool_group,Contour,Pstart,Pnext,Tool,Work_trans,Identity_trans,INTEGER Tool_plot)

	Stat=SetLastPoint(Pnext,WsXyzInput)
	L1=LineTwoPoints(Pstart,Pnext,WsTRIMMED)
	Stat= AddToGroup(Contour,L1)
	Stat= AssociateTool(L1,Tool)
	Stat= AssociateTool(Contour,Tool)
	IF Tool_plot THEN
		CALL PlotToolImage(Tool_group,Pstart,Work_trans)
		Stat= AnimateImage(Tool_group,L1,Identity_trans,1,1,1)
		CALL PlotToolImage(Tool_group,Pnext,Work_trans)
	END IF

SUBEND

