
! script name: vdatatable.d/importtable
! var_topic: import a table of data

!A
!A NAME:
!A
!A	Import Table	VARIATIONAL
!A
!A DESCRIPTION:
!A	
!A	Imports a tab separated ascii list of data.
!A	Entries in a row are separated by tabs.
!A	Rows are separated by line feeds.
!A
!# see also - datatable.d/datatables.hlp

Stat=ChangeLabelStr("ScriptNameStatus","Function: import table")

DIM Answer$[256]
DIM Name$[256]
DIM Point$[256]
DIM Filename$[256]
DIM Destination$[256]
DIM Tab$[32]
DIM Dir$[256]
DIM D_label$[256]
DIM Sub_label$[256]
DIM F_label$[256]
DIM Choice$[256]
DIM Mask$[32]

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

DEG

Tab$= CHR$(9)

ActivateUndo
Ask_file:!
Filename$=""
Stat= NamedVarStr("datatables dir",WsSystemVars,"/usr/weber/database/tables/",Dir$)
D_label$="DIRECTORY"
Sub_label$="SUB DIRECTORIES"
F_label$="FILES"
Choice$="FILE TO OPEN"
Mask$="[A-Za-z0-9]*"

Stat=UserFileBox(Dir$,Mask$,D_label$,Sub_label$,F_label$,Choice$)
Stat=SetInputFormat("a99","")
Stat=GetValue("SELECT EXISTING ASCII FILE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN Leave
LowerFileBox
IF NamedVarVal("save dirs",WsSystemVars,0) THEN V1=CreateVariable("datatables dir",WsSystemVars,PathNameDirectory(Answer$))
Filename$=Answer$

Center_point:stat= SetInputFormat("n4","")
E1=PickEntity("LOCATION")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E1) THEN Center_point
Stat= EntityNameCheck(E1,Point$)
T1= LastPickPara
Pick_return1= CurrPickReturn
Pc= ParameterReturnPt(E1,T1,Pick_return1)
Stat=SetLastPoint(Pc)
Stat= DeleteEntity(Pc)

Stat= GenerateVarName(WsDataTableEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("TABLE: import ascii file")
SELECT Pick_return1
CASE WsGeometryEnd
        Pc= ParameterReturnPt(Point$,T1,WsGeometryEnd)
CASE WsGeometryMid
        Pc= ParameterReturnPt(Point$,T1,WsGeometryMid)
CASE WsGeomCenter
        Pc= ParameterReturnPt(Point$,T1,WsGeomCenter)
END SELECT
V1= StringConstant(Filename$)
V2= FreeParameter("Table origin",WsUpperLeft)
Table1= ImportTable(V1,Pc,V2,Tab$)
Stat= NameEntity(Table1,Name$)
Stat= DeleteEntity(Pc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF ValidGeometry(Table1) THEN
	Stat= PlotEntity(Table1)
	ActivateUndo
	Stat= AdvanceUndoNumber("import table")
	Stat= AddItemToUndo(WsUBcreateMode,Table1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
ELSE
	DelLastVarRecord
END IF

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