
! script name: vtools.d/readmilltool
! var_topic: read data file for specified milling tool

!A
!A NAME:
!A
!A 	SELECT TOOL FROM LIBRARY	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will create a tool variationally from a data file.
!A
!A
!B INSTRUCTIONS:
!B
!B 	INPUT MATERIAL FILE NAME: The selection box with the available list
!B 		of MATERIALS will automatically appear for a selection to
!B 		be made.
!B
!B 	INPUT TYPE OF TOOL: The selection box with the available list of TOOL
!B 		TYPES will automatically appear for a selection to be made.
!B
!B 	INPUT TOOL FILE NAME: The selection box with the TOOL FILES available
!B 		for the specified tool type will automatically appear for a
!B 		selection to be made.
!B
!B	TOOL NAME,  Default:  This is the name of the tool which is to
!B			      appear on menus. The default will variationally
!B			      name the tool from the Tool id name field
!B			      in the data structure. If the user intends to
!B			      have the tool selected depend variationally
!B			      on variables he should give the tool a name
!B			      which references the tool by its use.
!B			      This name should be a valid entity name. No 
!B			      spaces or not alpha numeric characters.
!B			      Periods and underbars are accepted.
!B			      The actual tool to be imported will
!B			      not have the same id name from one time 
!B			      to the next. 
!B
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: select mill tool from library ")

DIM Destination$[256],Answer$[256]
DIM Name$[256],File_name$[256]
DIM Toolid$[256],Timage$[256],Toolmat$[256]
DIM Search1$[256],Search2$[256]
DIM Dir$[256],D_label$[256],Sub_label$[256],F_label$[256]
DIM Choice$[256],Mask$[32]
DIM Class_dir$[256],Tool_class$[256],Tool_file$[256],Tool_name$[256]
DIM Namelist$[32000],Namelist2$[32000]
DIM Iormm$[64],Sformm$[64]
DIM Dir_var$[256],Default_dir$[256]
DIM From_file$[256],Pass_parms$[256]

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Get_material:!

Material=FindUserData("MATERIAL",WsMaterial)
IF NOT ValidGeometry(Material) THEN

	Stat= NamedVarStr("material dir",WsSystemVars,"/usr/weber/util/tooldata/materials/",Dir$)
        D_label$="MATERIALS DIRECTORY"
        Choice$="MATERIALS SELECTED"
        Stat=DirectoryNames(Dir$,"*",Namelist$)
	Stat= AlphabeticalOrder(Namelist$,",",WsAscending,Namelist2$)
        Stat=GeneralListBox(D_label$,Choice$,Namelist2$)

        Stat=SetInputFormat("a99","")
        Stat=GetValue("INPUT MATERIAL FILE NAME",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Answer$="CANCEL" THEN Leave
        File_name$= FileNameString(Dir$,Answer$)

        Name$="MATERIAL"

        Material=ReadUserData(WsMaterial,Name$,File_name$)
        IF NOT ValidGeometry(Material) THEN Get_material

END IF

IF ARGC>1 THEN
	One_tool= 1
	From_file$= ARGV(1)
ELSE
	One_tool= 0
END IF

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

Start:!

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$=Answer$
Class_dir$= FileNameString(Dir$,Answer$)

Get_tool:!

D_label$="TOOL AVAILABLE"
Choice$="TOOL SELECTED"
Stat=DirectoryNames(Class_dir$,Mask$,Namelist$)
Stat= AlphabeticalOrder(Namelist$,",",WsAscending,Namelist2$)
Stat=GeneralListBox(D_label$,Choice$,Namelist2$)

Stat=SetInputFormat("a99","")
Stat=GetValue("INPUT TOOL FILE NAME",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Start
Tool_file$= Answer$
LowerSelectBox

Tool_name:!
Stat=SetInputFormat("a99=","")
Stat=SetEqualsValue(0,"D")
Stat=GetValue("TOOL NAME,  Default ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "D", "d", "Default", "default","DEFAULT"
	Tool_name$=""
	Default_name=1
CASE ELSE
	IF NOT ValidEntityName(Answer$) THEN 
		Stat=SetInputFormat("a1",",P,")
                Stat=GetValue("INVALID ENTITY NAME,  Proceed",Answer$)
                IF ExitScript(Destination$) THEN Leave
                IF Answer$="P" THEN Tool_name
	END IF
	E1= FindNamedEntity(Answer$)
	IF ValidGeometry(E1) THEN
       		 Stat=SetInputFormat("a1",",P,")
       		 Stat=GetValue("NAME IN USE,  Proceed",Answer$)
       		 IF ExitScript(Destination$) THEN Leave
       		 IF Answer$="P" THEN Tool_name
	END IF
	Tool_name$= Answer$
	Default_name=0
END SELECT

Make_tool_record:!
RecordFunctionOn
Stat= SetRecDelLastExec
Stat=RecordDescr(Tool_class$&" :"&Tool_file$)
V2= StringConstant(Tool_file$)
V3= StringConstant(Tool_class$)
V4= StringConstant(Dir$)
V5= StringConstant(FileNameString(V4,V3,V2))
Tool_ent= ReadUserData(WsToolData,V2,V5)
IF Default_name THEN
Stat= NameEntity(Tool_ent,ToolIdName(Tool_ent))
ELSE
Stat= NameEntity(Tool_ent,Tool_name$)
END IF
Stat=PutToolFileName(Tool_ent,V5)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Tool_ent) THEN
        DelLastVarRecord
ELSE
	Stat= RecordCalcToggle(Curr_record,WsFalse)
        ActivateUndo
	Tool_name$= ToolIdName(Tool_ent)
        Stat= AdvanceUndoNumber(Tool_name$)
        Stat= AddItemToUndo(WsUBCreateMode,Tool_ent)
        Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

IF NOT One_tool THEN
	GOTO Get_tool
ELSE
	Destination$="tools.d/maketool:"
	IF Default_name THEN
	   Destination$= Destination$&EntityNameStr(Tool_ent)
	ELSE
	   Destination$= Destination$&Tool_name$
	END IF
	Destination$= Destination$&","&From_file$
END IF

Leave:!
LowerSelectBox
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END

