
! script name: vdraft.d/textfile
! var_topic: read and place ascii file

!A
!A NAME:
!A
!A 	Text File	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will read an ASCII file in and place it at the specified
!A 	location.
!A
!B INSTRUCTIONS:
!B
!B 	TEXT POSITION POINT: Pick the point to be
!B 		used for the location of the text file.
!B
!B 	INPUT TEXT FILE NAME: Enter the name of the
!B 		file to be read in and placed at the specified location.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: text file ")

DIM Answer$[256]
DIM Destination$[256]
DIM Text$[256]
DIM Point$[256]
DIM Name$[256]
DIM Dir$[256],New_dir$[256]
DEG

Start:!
Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Text_loop:!

Stat= NamedVarStr("text dir",WsSystemVars,".",Dir$)
Stat=UserFileBox(Dir$,"*","DIRECTORY","SUB DIRECTORIES","FILES","FILE TO OPEN")
Stat=SetInputFormat("a99","")
Stat=GetValue("INPUT TEXT FILE NAME",Answer$)
IF ExitScript(Destination$) OR Answer$= "CANCEL" THEN Leave
LowerFileBox
IF NamedVarVal("save dirs",WsSystemVars,0) THEN V1=CreateVariable("text dir",WsSystemVars,PathNameDirectory(Answer$))

stat= SetInputFormat("n4","")
E1=PickEntity("TEXT POSITION POINT")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(E1) THEN Text_loop
T1= LastPickPara
Pick_return= CurrPickReturn
Stat= EntityNameCheck(E1,Point$)

Stat= GenerateVarName("D",Name$)
RecordFunctionOn
Stat= RecordDescr("DIM: Text file")
Stat= SetRecDelLastExec
SELECT Pick_return
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(Answer$)
E1=TextFile(Pc,V1)
Stat= NameEntity(E1,Name$)
Stat=DeleteEntity(Pc)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= PlotEntity(E1)
Stat= PlotEntity(Point$)
IF NOT ValidGeometry(E1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("text file")
	Stat= AddDimToUndo(WsUBCreateMode,E1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

! This is to strip out the basename
Dir$= Answer$
J= POS(Answer$,"/")
WHILE (J AND J<LEN(Answer$))
	Answer$= Answer$[J+1]
	J= POS(Answer$,"/")
END WHILE
J= POS(Dir$,"/"&Answer$)
New_dir$= Dir$[1,J-1]
V1= CreateVariable("text dir",WsSystemVars,New_dir$)
GOTO Text_loop

Leave:!
LowerFileBox
stat= SetInputFormat("n4","")
Stat=DeleteEntity(Pc)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
        IF Destination$= "SCRIPTSTART" THEN Start
        IF Destination$= "BACKUP" THEN Start
	LOAD Destination$
END IF
END
