
! script name: datatable.d/toollibtable
! topic: list library tools

!A
!A NAME:
!A
!A 	Tools Library Table
!A
!A DESCRIPTION:
!A
!A 	This script makes a table of the tools present in the
!A 	tool library.
!A
!B INSTRUCTIONS:
!B
!B 	INPUT TYPE OF TOOL: Select the type of tool from the list.
!B
!B NOTES:
!B
!B 	This script is a template only. A user may copy and modify
!B 	this script to produce custom charts. Weber systems is not
!B 	responsible for customization of charts for particular needs.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: make tool list from library ")

OPTION BASE 1

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM A$[256]
DIM B$[256]
DIM File_name$[256]
DIM New_file$[256]
DIM Toolid$[256]
DIM Toolmat$[256]
DIM Search1$[256]
DIM Search2$[256]
DIM Dir$[256]
DIM D_label$[256]
DIM Sub_label$[256]
DIM F_label$[256]
DIM Tool_class$[256]
DIM Choice$[256]
DIM Mask$[32]
DIM Namelist$[32000]
DIM Namelist2$[32000]
DIM Tool_data$[32000]
DIM Question$(20)[80]
DIM Reply$(20)[80]
DIM Fields$(20)[80]
DIM Class_dir$[256]
DIM Tool_name$[256]


ActivateUndo
Display_data=0

Stat= NamedVarStr("tools dir",WsSystemVars,"/usr/weber/util/tooldata/tools_in/",Dir$)


Units= CurrentUnits

Grid_color= 14

Entry_color= 8
Entry_text_height= GetDefault(WsTextHeight)
Entry_text_width= GetDefault(WsTextWidth)

Header_color= 14
Header_ltype= 9
Head_text_height= 2.0*Entry_text_height
Head_text_width= 2.0*Entry_text_width
Header_slant= TAN(30)

Label_color= 8
Label_ltype= 0
Label_text_height= 1.5*Entry_text_height
Label_text_width= 1.5*Entry_text_width
Label_slant= 0

Tool_type:!
D_label$="TOOL TYPE DIRECTORY"
Choice$="TOOL TYPE SELECTED"
Mask$="*"
Stat=DirectoryNames(Dir$,Mask$,Namelist$)
Stat= AlphabeticalOrder(Namelist$,",",WsAscending,Namelist2$)
Stat=GeneralListBox(D_label$,Choice$,Namelist2$)

Stat=SetInputFormat("a99","")
Stat=GetValue("INPUT TYPE OF TOOL",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave
Tool_class$= FileNameString(Dir$,Answer$)
Class_dir$= Answer$

Start:!
Mask$="*"
Stat=DirectoryNames(Tool_class$,Mask$,Namelist$)
Stat= AlphabeticalOrder(Namelist$,",",WsAscending,Namelist2$)

Count_tools: Tcount=1
FOR I=2 TO 100
	Stat= StringFieldStr(Namelist2$,",",I,Answer$)
	IF Stat=1 THEN Start_list
	Tcount= Tcount+1
NEXT I

Start_list:!
Table_pos:Stat=SetInputFormat("n4","")
        Pc=XyzInput("TABLE LOCATION ",WsXyzInput)
        IF (ExitScript(Destination$)) THEN Leave
        IF NOT ValidGeometry(Pc) THEN Table_pos
Tool_table= CreateBlankTable(Pc,Class_dir$,Tcount,1,WsLowerLeft,"          ")
Stat= DeleteEntity(Pc)
Stat= SetTableResize(Tool_table,0,0,WsFalse)
FOR I=2 TO 500
Stat= StringFieldStr(Namelist2$,",",I,Answer$)
IF Stat=1 THEN End_list
File_name$= FileNameString(Tool_class$,Answer$)
Tool_name$= Answer$
Tool=ReadUserData(WsToolData,"temp",File_name$)
IF NOT ValidGeometry(Tool) THEN End_list
Tool_count=I
GOSUB List_tool
NEXT I

End_list:!
Stat= NameEntity(Tool_table,"TOOL LIBRARY CLASS TABLE")
Stat= SetTableColors(Tool_table,Entry_color)
Stat= TableTextSizes(Tool_table,1,0,Head_text_width,Head_text_height,Header_slant)
Stat= SetTableColors(Tool_table,1,0,Header_color)
Stat= SetTableLType(Tool_table,1,0,Header_ltype)
Stat= SetColor(Tool_table,Grid_color)
Stat= PlotEntity(Tool_table)
GOTO Leave

List_tool:!
Stat=GetUserString(Tool,"Tool id name:",Name$)
Stat=ChangeDataName(Tool,Name$)
Stat=GetUserString(Tool,"Tool type:",Toolid$)
CALL ToolFields(Fields$(*),Toolid$,Num_fields)
CALL ReadToolFields(Tool,Question$,Reply$,Fields$,Num_fields)
Question$(1)= "Tool file name:"
Fields$(1)= "File name:"
Reply$(1)= Tool_name$
IF Tool_count=2 THEN
Stat= RedimTableColumns(Tool_table,Num_fields,WsFalse)
END IF
CALL PutToolFields(Tool,Tool_table,Question$(*),Reply$(*),Fields$(*),Tool_count,Num_fields)

ActivateUndo
Stat= AdvanceUndoNumber("tool table")
Stat= AddItemToUndo(WsUBCreateMode,Tool_table)


Delete_tool:!
Stat= DeleteEntity(Tool)
RETURN

Leave:!
LowerSelectBox
Stat= SetInputFormat("n4","")
Stat= DeleteEntity(Tool)
ResizeAll
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END

SUB ReadToolFields(ENTITY Tool,Question$(*),Reply$(*),Fields$(*),Num_fields)
OPTION BASE 1
DIM A$[256],B$[256]

FOR I= 1 TO 20
	Question$(I)= ""
	Reply$(I)= ""
NEXT I

IF ValidGeometry(Tool) THEN 
	FOR I=1 TO Num_fields
		A$= Fields$(I)
		Stat=GetUserString(Tool,A$,B$)
		IF NOT Stat THEN
			Question$(I)= A$
			Reply$(I)= B$
		ELSE
			Question$(I)= A$
			Reply$(I)= VAL$(0)
		END IF
	NEXT I
END IF

SUBEND

SUB PutToolFields(ENTITY Tool,Tool_table,Question$(*),Reply$(*),Fields$(*),Table_row,Num_fields)
OPTION BASE 1
DIM A$[256],B$[256]
DIM Name$[256]
DIM Tool_image$[256]

! Field #1 is the file name which is not in data.
! Assume fields 1,2,3 are string values.

IF ValidGeometry(Tool) THEN

IF Table_row=2 THEN
	FOR I=1 TO Num_fields
		A$= Fields$(I)
		IF LEN(A$)>1 THEN A$= A$[1,LEN(A$)-1]
                Stat= InsertIntoTable(Tool_table,A$,1,I)
		Stat= TableTextOrigin(Tool_table,Table_row,I,WsLeftCenter)
	NEXT I
END IF

	FOR I=1 TO 3
		A$= Fields$(I)
		B$= Reply$(I)
		Stat= InsertIntoTable(Tool_table,B$,Table_row,I)
		Stat= TableTextOrigin(Tool_table,Table_row,I,WsLeftCenter)
	NEXT I

	FOR I=4 TO Num_fields
		A$= Fields$(I)
		Stat= CheckUserField(Tool,A$,0)
		IF NOT Stat THEN
			B$= Reply$(I)
			Stat=  EvaluateAnswer(B$,Value)
			IF NOT Stat THEN
				Stat= InsertIntoTable(Tool_table,Value,"decimal 4",Table_row,I)
				Stat= TableTextOrigin(Tool_table,Table_row,I,WsRightCenter)
			END IF
		ELSE
			B$= Reply$(I)
			Sw= IsStringNumeric(B$)
			IF Sw= WsTrue THEN
				Value= VAL(B$)
				Stat= InsertIntoTable(Tool_table,Value,"decimal 4",Table_row,I)
				Stat= TableTextOrigin(Tool_table,Table_row,I,WsRightCenter)
			ELSE
				Stat= InsertIntoTable(Tool_table,B$,Table_row,I)
				Stat= TableTextOrigin(Tool_table,Table_row,I,WsRightCenter)
			END IF
		END IF
	NEXT I
END IF


SUBEND


SUB DefineTool(Tool,Toolid$)

IF NOT ValidGeometry(Tool) THEN 
	SUBEXIT
END IF

Tip_angle=0
Taper= 0
Corner_chamfer=0
Crad=0
Shank_dia=0
D3=0
D4=0

Tdia=GetUserValue(Tool,"Diameter:")
Flute_length=GetUserValue(Tool,"Flute length:")
Total_length=GetUserValue(Tool,"Tip to holder:")

SELECT Toolid$
CASE "DRILL"
	Cutter_type= WsDrill
        Tip_angle=GetUserValue(Tool,"Point angle:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
CASE "CENTER DRILL"
	Cutter_type= WsCenterDrill
        Tip_angle=GetUserValue(Tool,"Point angle:")
        Shank_dia=GetUserValue(Tool,"Body diameter:")
	Taper= 90- Tip_angle/2
CASE "TWIST DRILL"
	Cutter_type= WsTwistDrill
        Tip_angle=GetUserValue(Tool,"Point angle:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
CASE "TAPERED DRILL"
	Cutter_type= WsTaperedDrill
        Tip_angle=GetUserValue(Tool,"Point angle:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Taper=GetUserValue(Tool,"Taper angle:")
CASE "COUNTER SINK"
	Cutter_type= WsCounterSink
        Tip_angle=GetUserValue(Tool,"Point angle:")
	Overall_len=GetUserValue(Tool,"Overall length:")
        Shank_len=GetUserValue(Tool,"Shank length:")
        Shank_dia=GetUserValue(Tool,"Shank diameter:")
        Flute_length=Overall_len-Shank_len
CASE "COUNTERBORE"
	Cutter_type= WsCounterBore
	Crad=GetUserValue(Tool,"Corner radius:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
        Pilot_dia=GetUserValue(Tool,"Pilot diameter:")
        Pilot_len=GetUserValue(Tool,"Pilot length:")
	D3= Pilot_dia
	D4= Pilot_len
CASE "TAP"
	Cutter_type= WsTap
	Thread_len=GetUserValue(Tool,"Thread length:")
        Shank_dia=GetUserValue(Tool,"Shank diameter:")
        Taper=0
	Tip_angle= 10
	Radius= 0.5*Tdia
	Crad= 0.5*Radius
	Flute_length= Thread_len
CASE "REAMER"
	Cutter_type= WsReamer
        Chamfer_length=GetUserValue(Tool,"Corner chamfer:")
        Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Taper=10
	Radius= 0.5*Tdia
	Taper_length= 0.5*Radius
        Chamfer_angle=45
	Crad= Chamfer_length
	Tip_angle= Chamfer_angle
	D3= Taper_length
CASE "BALLMILL"
	Cutter_type= WsBallCutter
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
CASE "BULLMILL"
	Cutter_type= WsBullmill
	Crad=GetUserValue(Tool,"Corner radius:")
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
CASE "ENDMILL"
	Cutter_type= WsEndmill
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Crad=GetUserValue(Tool,"Corner radius:")
CASE "TAPERMILL"
	Cutter_type= WsTaperedEndmill
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Taper=GetUserValue(Tool,"Taper angle:")
	Crad=GetUserValue(Tool,"Corner radius:")
CASE "TAPERBALL"
	Cutter_type= WsBallCutter
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Taper=GetUserValue(Tool,"Taper angle:")
CASE "CHAMFERMILL"
	Cutter_type= WsChamferTool
	Shank_dia=GetUserValue(Tool,"Shank diameter:")
	Radius= 0.5*Tdia
        Tip_radius=GetUserValue(Tool,"Tip diameter:")/2
	Chamfer_angle=GetUserValue(Tool,"Taper angle:")
	Chamfer_length= (Radius- Tip_radius)/TAN(Chamfer_angle)
	Crad= Chamfer_length
	Tip_angle= Chamfer_angle
CASE "BORING BAR"
	Cutter_type= WsBoringBar
        Crad=GetUserValue(Tool,"Corner radius:")
        Shank_dia=GetUserValue(Tool,"Shank diameter:")
END SELECT


Radius= 0.5*Tdia
Shank_radius= 0.5*Shank_dia
Stat=Setup3dCutter(Cutter_type,Radius,Crad,0.0,0.0,Taper,Flute_length,Tip_angle,Shank_radius,Total_length,D3,D4)


SUBEND

SUB ToolFields(Fields$(*),Toolid$,Num_fields)
OPTION BASE 1

SELECT Toolid$
CASE "ENDMILL"
	Fields$(1)="Tool file name:"
	Fields$(2)="Tool id name:"
	Fields$(3)="Tool material:"
	Fields$(4)="Diameter:"
        Fields$(5)="Corner radius:"
	Fields$(6)="Gage length:"
	Fields$(7)="Shank diameter:"
	Fields$(8)="Flute length:"
	Fields$(9)="Tip to holder:"
	Num_fields= 9
CASE "BULLMILL"
	Fields$(1)="Tool file name:"
        Fields$(2)="Tool id name:"
        Fields$(3)="Tool material:"
        Fields$(4)="Diameter:"
        Fields$(5)="Corner radius:"
        Fields$(6)="Gage length:"
        Fields$(7)="Shank diameter:"
        Fields$(8)="Flute length:"
	Fields$(9)="Tip to holder:"
	Num_fields= 9
CASE "TAPERMILL"
	Fields$(1)="Tool file name:"
        Fields$(2)="Tool id name:"
        Fields$(3)="Tool material:"
        Fields$(4)="Diameter:"
        Fields$(5)="Corner radius:"
        Fields$(6)="Gage length:"
        Fields$(7)="Shank diameter:"
        Fields$(8)="Flute length:"
	Fields$(9)="Tip to holder:"
        Fields$(10)="Taper angle:"
	Num_fields= 10
CASE "TAPERBALL"
	Fields$(1)="Tool file name:"
        Fields$(2)="Tool id name:"
        Fields$(3)="Tool material:"
        Fields$(4)="Diameter:"
        Fields$(5)="Gage length:"
        Fields$(6)="Shank diameter:"
        Fields$(7)="Flute length:"
	Fields$(8)="Tip to holder:"
        Fields$(9)="Taper angle:"
	Num_fields= 9
CASE "BORING BAR"
	Fields$(1)="Tool file name:"
        Fields$(2)="Tool id name:"
        Fields$(3)="Tool material:"
        Fields$(4)="Diameter:"
        Fields$(5)="Corner radius:"
        Fields$(6)="Gage length:"
        Fields$(7)="Shank diameter:"
        Fields$(8)="Flute length:"
	Fields$(9)="Tip to holder:"
	Num_fields= 9
CASE "BALLMILL"
        Fields$(1)="Tool file name:"
        Fields$(2)="Tool id name:"
        Fields$(3)="Tool material:"
        Fields$(4)="Diameter:"
        Fields$(5)="Gage length:"
        Fields$(6)="Shank diameter:"
        Fields$(7)="Flute length:"
	Fields$(8)="Tip to holder:"
	Num_fields= 8
CASE "CHAMFERMILL"
	Fields$(1)="Tool file name:"
        Fields$(2)="Tool id name:"
        Fields$(3)="Tool material:"
        Fields$(4)="Diameter:"
        Fields$(5)="Tip diameter:"
        Fields$(6)="Gage length:"
        Fields$(7)="Shank diameter:"
        Fields$(8)="Flute length:"
	Fields$(9)="Tip to holder:"
        Fields$(10)="Taper angle:"
	Num_fields= 10
CASE "DRILL","TWIST DRILL"
	Fields$(1)= "Tool file name:"
        Fields$(2)= "Tool id name:"
        Fields$(3)= "Tool material:"
        Fields$(4)= "Diameter:"
        Fields$(5)= "Gage length:"
        Fields$(6)= "Shank diameter:"
        Fields$(7)= "Flute length:"
	Fields$(8)= "Tip to holder:"
        Fields$(9)= "Point angle:"
        Fields$(10)= "Feed(ipr):"
	Num_fields= 10
CASE "CENTER DRILL"
	Fields$(1)= "Tool file name:"
        Fields$(2)= "Tool id name:"
        Fields$(3)= "Tool material:"
        Fields$(4)= "Diameter:"
        Fields$(5)= "Gage length:"
        Fields$(6)= "Body diameter:"
        Fields$(7)= "Flute length:"
        Fields$(8)= "Tip to holder:"
        Fields$(9)= "Point angle:"
        Fields$(10)= "Feed(ipr):"
        Num_fields= 10
CASE "TAPERED DRILL"
	Fields$(1)= "Tool file name:"
        Fields$(2)= "Tool id name:"
        Fields$(3)= "Tool material:"
        Fields$(4)= "Diameter:"
        Fields$(5)= "Gage length:"
        Fields$(6)= "Shank diameter:"
        Fields$(7)= "Flute length:"
        Fields$(8)= "Point angle:"
	Fields$(9)="Tip to holder:"
        Fields$(10)= "Taper angle:"
        Fields$(11)= "Feed(ipr):"
	Num_fields= 11
CASE "COUNTER SINK"
	Fields$(1)= "Tool file name:"
        Fields$(2)= "Tool id name:"
        Fields$(3)= "Tool material:"
        Fields$(4)= "Diameter:"
        Fields$(5)= "Gage length:"
        Fields$(6)= "Shank diameter:"
        Fields$(7)= "Flute length:"
	Fields$(8)="Tip to holder:"
        Fields$(9)= "Point angle:"
        Fields$(10)= "Feed(ipr):"
	Num_fields= 9
CASE "COUNTERBORE"
	Fields$(1)= "Tool file name:"
        Fields$(2)= "Tool id name:"
        Fields$(3)= "Tool material:"
        Fields$(4)= "Diameter:"
        Fields$(5)= "Gage length:"
        Fields$(6)= "Shank diameter:"
        Fields$(7)= "Pilot diameter:"
        Fields$(8)= "Pilot length:"
        Fields$(9)= "Flute length:"
	Fields$(10)="Tip to holder:"
	Num_fields= 10
CASE "REAMER"
	Fields$(1)= "Tool file name:"
        Fields$(2)= "Tool id name:"
        Fields$(3)= "Tool material:"
        Fields$(4)= "Diameter:"
        Fields$(5)= "Corner chamfer:"
        Fields$(6)= "Gage length:"
        Fields$(7)= "Shank diameter:"
        Fields$(8)= "Flute length:"
	Fields$(9)="Tip to holder:"
        Fields$(10)= "Feed(ipr):"
	Num_fields= 10
CASE "TAP"
	Fields$(1)= "Tool file name:"
        Fields$(2)= "Tool id name:"
        Fields$(3)= "Tool material:"
        Fields$(4)= "Diameter:"
        Fields$(5)= "Gage length:"
        Fields$(6)= "Shank diameter:"
	IF CurrentUnits= WsMetric THEN
		Fields$(7)= "Pitch:"
	ELSE
		Fields$(7)= "Threads-inch:"
	END IF
        Fields$(8)= "Thread length:"
	Fields$(9)="Tip to holder:"
	Num_fields= 9
END SELECT

SUBEND
