 
! script name: amo.d/lthtasktable
! topic: Amo tasks work zone table.

!A
!A NAME:
!A
!A 	Process Table
!A
!A DESCRIPTION:
!A
!A 	Makes a table of tasks and workzones for turning machines.
!A	Used as a pre-processor for Amo execute.
!A

stat=ChangeLabelStr("ScriptNameStatus","Function: lathe tasks work zone table ")

DIM Answer$[256]
DIM Destination$[256]
DIM Dir$[256],Namelist$[32000],Namelist2$[32000]
DIM Toolname$[256],Next_name$[256],Toolmat$[256]

DeActivateUndo
WsAmoData= 23051
WsEventData= 12345

Method=NamedVarVal("amo direction",WsToolVars,0.0)
Use_tools=NamedVarVal("use tool numbers",WsAmoVars,WsTrue)
Init_tool=NamedVarVal("initial tool number",WsAmoVars,101)
Tool_inc=NamedVarVal("tool number increment",WsAmoVars,101)

Use_tool_table= 1

Task_ent= FindUserData("AMO OPERATIONS",WsTaskPacket)
IF ValidGeometry(Task_ent) THEN
	Tasks= PropGrpFromFields(Task_ent,"Number tools:","Tool ",1,WsToolData,WsEventData)
ELSE
	Tasks= FindNamedGrp("AMO OPERATIONS")
END IF
IF NOT ValidGeometry(Tasks) THEN Leave
Zonelist= FindUserData("AMO WORK ZONES",WsAmoData)

Material=FindUserData("MATERIAL",WsMaterial)
IF NOT ValidGeometry(Material) THEN
	Get_material:!
	Stat= NamedVarStr("material dir",WsSystemVars,"/usr/weber/util/tooldata/materials/",Dir$)
	Stat=DirectoryNames(Dir$,"*",Namelist$)
	Stat= AlphabeticalOrder(Namelist$,",",WsAscending,Namelist2$)
	Stat=GeneralListBox("MATERIALS","MATERIAL SELECTED",Namelist2$)
	Stat=SetInputFormat("a99","")
	Stat=GetValue("INPUT MATERIAL FILE NAME",Answer$)
	LowerSelectBox
	IF ExitScript(Destination$) THEN Leave
	IF Answer$="CANCEL" THEN Leave
	Material=ReadUserData(WsMaterial,"MATERIAL",FileNameString(Dir$,Answer$))
	IF NOT ValidGeometry(Material) THEN Get_material
END IF

Stat=RemoveDupTools
Stat= DeleteEntity("Amo process table")
PlotOff
Pc= PointXYZ(0,0,0)
Amo_table= AmoTasksZonesTable(Tasks,Zonelist,Pc,Method)
IF NOT ValidGeometry(Amo_table) THEN Leave

Stat= AmoTaskTableTnums(Amo_Table,Use_tools,Init_tool)

Stat= RedimTableColumns(Amo_Table,2,WsTrue)
IF CurrentUnits= WsMetric THEN
	Stat=InsertIntoTable(Amo_Table,"Feed(mmpr)",1,5)
	Stat=InsertIntoTable(Amo_Table,"Speed(smpm)",1,6)
ELSE
	Stat=InsertIntoTable(Amo_Table,"Feed(ipr)",1,5)
	Stat=InsertIntoTable(Amo_Table,"Speed(sfpm)",1,6)
END IF

Stat= RedimTableColumns(Amo_Table,2,WsTrue)
IF CurrentUnits= WsMetric THEN
	Stat=InsertIntoTable(Amo_Table,"Feed(mmpm)",1,7)
ELSE
	Stat=InsertIntoTable(Amo_Table,"Feed(ipm)",1,7)
END IF
Stat=InsertIntoTable(Amo_Table,"Speed(rpm)",1,8)
Stat=TaskTableFeedSpeed(Amo_Table,Material,8,7)

Stat= CleanAmoTasksTable(Amo_table)
Nrows= NumberTableRows(Amo_table)
FOR I=2 TO Nrows
    Tool= AmoTaskTableTool(Amo_Table,I)
    IF ValidGeometry(Tool) THEN
	Stat=GetUserString(Tool,"Insert material:",Toolmat$)
	Feed=GetFeedrate(Tool,Material,2)
	IF CurrentUnits= WsMetric THEN
		Speedcode=GetUserValue(Material,"Turn(smpm)("&Toolmat$&"):")
		IF Feed THEN Stat= InsertIntoTable(Amo_Table,Feed,"decimal 2",1,5)
	ELSE
		Speedcode=GetUserValue(Material,"Turn(sfpm)("&Toolmat$&"):")
		IF Feed THEN Stat= InsertIntoTable(Amo_Table,Feed,"decimal 3",I,5)
	END IF
	IF Speedcode THEN Stat= InsertIntoTable(Amo_Table,Speedcode,"noformat",I,6)
    END IF
NEXT I

Stat= NameEntity(Amo_table,"Amo process table")
Stat= DeleteEntity(Pc)
PlotOn
Stat= PlotEntity(Amo_table)

Stat= CleanAmoTasksTable(Amo_table)

Leave:!
IF ValidGeometry(Task_ent) THEN Stat= DeleteEntity(Tasks)
Stat= DeleteEntity(Pc)
PlotOn
IF (NOT LEN(Destination$)) THEN
         LOAD "typecontrol"
ELSE
         LOAD Destination$
END IF
END
