
! script name: wsccommon.d/storageas
! topic: rename and save file

!A
!A NAME:
!A
!A 	Save as ...
!A
!A DESCRIPTION:
!A
!A 	This script will rename and save a file under the designated name
!A 	in the designated "storage dir" path.  Note: "storage dir" is the
!A 	directory path for storing files, e.g. /usr/weber/ST
!A
!A 	The following are the 4 different Synergy system component files
!A 	that will be stored in the file.
!A
!A 		Geometry
!A		Turbo post command files
!A		Turbo post tape files
!A		Turbo post notepad files
!A
!B INSTRUCTIONS:
!B
!B 	SAVE MODE IS Entire, Geometry:
!B
!B 		E - Entire will save the entire job, geometry and turbo_info.
!B 		G - Geometry will only save the geometry portion of the file.
!B 		T - Turbo_info will only save the TurboPost information.  This feature is hidden from the question line.
!B
!B 	SAVE JOB AS: Enter the name the job is to be stored as.
!B 		The name may not have any spaces in it.
!B
!B 	FILE DESCRIPTION : Enter a brief file description
!B 		to be stored with the job.
!B

DIM Answer$[256],Destination$[256],Header$[4000],Header1$[4000]
DIM A$[256],Descr$[256],Datestr$[256],Sdevice$[256]
DIM Jobname$[256],Tmp$[256],Space$[25],Jobs$[25],Unit$[256]

DIM Dir$[256],Mask$[256],Namelist$[5000],Filelist$(100)[100],Dir1$[256]

Stat=ChangeLabelStr("ScriptNameStatus","Function: save ")

Stat= NamedVarStr("storage dir",WsSystemVars,"/usr/weber/ST/",Sdevice$)
DeActivateUndo

V1= CreateVariable("how to save files",WsSystemVars,2.0)
Op_mode= 2.0
! Stat=SetInputFormat("a1",",E,G,T,")
! Stat=GetValue("SAVE MODE IS Entire, Geometry ",Answer$)
! IF (ExitScript(Destination$)) THEN Leave
! Op_mode= 1*(Answer$="G")+2*(Answer$="E")+3*(Answer$="T")

	Space$= " "
Question:!
D_label$="DIRECTORY"
Sub_label$="SUB DIRECTORIES"
F_label$="FILES"
Choice$="FILE TO SAVE"
Mask$="[A-Za-z0-9]*"
Stat=UserFileBox(Sdevice$,Mask$,D_label$,Sub_label$,F_label$,Choice$,"Synergy")
Ques2:!
Stat=SetInputFormat("a99","")
Stat=GetValue("SAVE JOB AS: "&Space$,Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave
IF POS(Answer$," ") THEN
	Space$= " (NO SPACES) "
	GOTO Ques2
ELSE
	Space$= " "
END IF
IF NOT POS(Answer$,"/") THEN
	Jobname$=FileNameString(Sdevice$,Answer$)
ELSE
	Testposr=1
	REPEAT
		Testpos=POS(Answer$[Testposr],"/")
		Testposr=Testposr+Testpos
	UNTIL (NOT Testpos)
	Namelen=LEN(Answer$)-Testposr+1
	
	IF NOT Namelen THEN Ques2
	Jobname$= Answer$
END IF
IF NOT (POS(Jobname$,".syn") OR POS(Jobname$,".SYN")) THEN
                Jobname$= Jobname$&".syn"
END IF
LowerFileBox
IF NamedVarVal("save dirs",WsSystemVars,0) THEN V1=CreateVariable("storage dir",WsSystemVars,PathNameDirectory(Jobname$))

F_type= DoesFileExist(Jobname$)
SELECT F_type
CASE 1
        Stat=SetInputFormat("a1",",Y,N,")
        Stat=GetValue("FILE EXISTS: REPLACE Yes / No",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Answer$= "N" THEN
		GOTO Question
        END IF
CASE 2
        Stat=SetInputFormat("a1",",P,")
        Stat=GetValue("COPY NAME IS A DIRECTORY: Proceed ",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
	GOTO Question
END SELECT

stat= CurrentHeader(Header1$)
Header$= Header1$
IF LEN(Header$) THEN
	Head=ReadUserData(WsGeneralString,"HEADER",Header$)
	IF ValidGeometry(Head) THEN
		Stat=SetInputFormat("a99","")
		Stat=GetValue("FILE DESCRIPTION:",Answer$)
		IF (ExitScript(Destination$)) THEN Leave
		Descr$= Answer$

		Stat= GetUserString(Head,"Creation date:",Datestr$)
		IF ( NOT LEN(Datestr$)) OR POS(Datestr$,"NONE") THEN
			Stat= DateString(Datestr$)
			Stat= PutUserString(Head,"Creation date:",Datestr$)
			Stat= PutUserString(Head,"Revision date:","")
		ELSE
			stat= DateString(Datestr$)
			Stat= PutUserString(Head,"Revision date:",Datestr$)
		END IF

		Stat=PutUserString(Head,"File name:",Jobname$)
		Stat=PutUserString(Head,"Description:",Descr$)
        	Units= CurrentUnits
        	Unit$= RPT$("METRIC",Units= WsMetric)&RPT$("INCHES",Units=WsInch)
        	Stat=PutUserString(Head,"Units:",Unit$)
		Stat= UserDataString(Head,Header$)
		stat= FileHeader(Header$)
	END IF
	AccTime=TIMEDATE-NamedVarVal("job start time",WsSystemVars,0.0)
	AccTimer=NamedVarVal("accumulated time",WsSystemVars,0.0)
	AccTime=AccTime/60+AccTimer
	Stat=PutUserString(Head,"Accumulated Time:"," "&VAL$(PROUND(AccTime,-2))&" minutes")
END IF

Stat= DelUnusedProp(WsFixtureWorkplane)
Stat= DelUnusedProp(WsPocketBdy)
Stat= DelUnusedProp(WsOffsetBdy)
Stat= DelUnusedProp(Ws3dCpath)
TransformCleanup
UnSelectAll

Filecnt= 1
IF Op_mode<3 THEN
	Filelist$(Filecnt)="Geometry"
	First=0
ELSE
	First= 1
END IF

IF LEN(Header1$) THEN Stat= FileHeader(Header1$)

IF ValidGeometry(Head) THEN
	FOR I=1 TO 2
		Stat= PutUserString(Head,"Component files "&VAL$(I)&":","")
	NEXT I
END IF

IF Op_mode> 1 THEN
	Dir$= "./"
	Mask$= "notepad*"
	Stat= DirectoryNames(Dir$,Mask$,Namelist$)
	GOSUB AddFiles
	Mask$= "command_file*"
	Stat= DirectoryNames(Dir$,Mask$,Namelist$)
	GOSUB AddFiles
	Mask$= "tapefile*"
	Stat= DirectoryNames(Dir$,Mask$,Namelist$)
	GOSUB AddFiles
END IF
IF ValidGeometry(Head) THEN
	FOR I=1 TO Filecnt
		Stat= PutUserString(Head,"Component files "&VAL$(I)&":",Filelist$(I))
	NEXT I
	WHILE CheckUserField(Head,"Component files "&VAL$(I)&":",1)
		Stat= PutUserString(Head,"Component files "&VAL$(I)&":","EMPTY")
		I= I+1
	END WHILE
	Enti= UserDataString(Head,Header$)
	Stat= FileHeader(Header$)
	Stat= DeleteEntity(Head)
END IF

stat=ChangeLabelStr("websys",Jobname$)
Stat= UpdateSettings("CadSettings",WsCadSettings)
Stat=StoreFile(Jobname$,Op_mode)
IF Stat THEN
           Stat=SetInputFormat("a99","")
           Stat=GetValue("WARNING -- FILE NOT STORED: cannot open file",Answer$)
           IF (ExitScript(Destination$)) THEN Leave
           GOTO Leave
END IF

Leave:!
LowerFileBox
IF ValidGeometry(Head) THEN Stat= DeleteEntity(Head)
IF Op_mode>1 THEN
	Group= GrpByEntType(WsGeneralFile)
	IF ValidGeometry(Group) THEN Stat= DeleteGroup(Group)
END IF

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

Addfiles:!
Comma= POS(Namelist$,",")
WHILE Comma
	Jobs$= Namelist$[1,Comma-1]
	Namelist$= Namelist$[Comma+1]
	File_there= CreateGeneralFile(Jobs$)
	IF File_there THEN
		IF (LEN(Filelist$(Filecnt))+LEN(Jobs$))>50 THEN 
			Filecnt=Filecnt+1
		ELSE 
			IF First THEN
				First= 0
			ELSE 
				Filelist$(Filecnt)[LEN(Filelist$(Filecnt))+1]= ",  "
			END IF
		END IF
		Filelist$(Filecnt)[LEN(Filelist$(Filecnt))+1]= Jobs$
	END IF
	Comma= POS(Namelist$,",")
END WHILE
RETURN
END
