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

!A
!A NAME:
!A
!A 	SELECT TOOL FROM LIBRARY
!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]
DIM Class_dir$[512],Prop_dir$[256],Prop_class$[256],Prop_name$[512]
DIM A$[32000],Last_dir$[512],Now_dir$[512]

Var_object= CurrentVarObject
IF NOT ValidGeometry(Var_object) THEN
	Var_object=OpenVarObject("Program")
END IF
! 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$)
Stat=TreeBox("MaterialList","Material",Dir$,3)
File_name:stat= SetInputFormat("a99","")
Stat=GetValue("INPUT MATERIAL FILE NAME",Answer$)
LowerSelectBox

!        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$)
Class_dir$= Dir$
Prop_dir$= Dir$

Start:!
Get_tool:!
D_label$="TOOL TYPE DIRECTORY"
Choice$="TOOL TYPE SELECTED"
Stat=TreeBox("ToolList","Tool",Class_dir$,3)

File_name:stat= SetInputFormat("a99","")
Stat=GetValue("INPUT TYPE OF TOOL",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT LEN(Answer$) THEN Start
IF Answer$="CANCEL" THEN Leave
Prop_name$= FileNameString(Class_dir$,Answer$)
IF DoesFileExist(Prop_name$)=2 THEN
        Prop_name$= Class_dir$
        Class_dir$= FileNameString(Class_dir$,Answer$)
        GOTO Start
END IF
Tool_class$= PathNameDirectory(Answer$)
IF NOT LEN(Tool_class$) THEN
	Tool_file$= Answer$
ELSE
	Tool_file$= BaseNamePath(Answer$,Tool_class$)
END IF
Dir$= Class_dir$
LowerSelectBox

Tool_name:!
! Stat=SetInputFormat("a99=","")
! Stat=SetEqualsValue(0,"D")
! Stat=GetValue("TOOL NAME,  Default ",Answer$)
! IF (ExitScript(Destination$)) THEN Leave
Answer$= "D"
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= ReadToolData(V2,V5,WsTrue)
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
	IF NOT One_tool THEN GOTO Get_tool
	GOTO Leave
ELSE
        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$="wscmill.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
