! script name: contrib/ascript/tsm.d/storageas
! topic: rename and save file
! Version_270423.a

!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 TURBO POST information.
!B                  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
!# see also - htdocs/help.d/fileextensions.htm

DIM Answer$[256],Destination$[256],Header$[4000],Header1$[4000]
DIM A$[256],Name$[256],Descr$[256],Datestr$[256],Sdevice$[256]
DIM Jobname$[256],Tmp$[256],Space$[25],Jobs$[25],Unit$[256]
DIM Dir$[256],Mask$[512],Namelist$[5000],Filelist$(100)[100],Dir1$[256]

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

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

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]*"
Mask$= "Synergy Files (*.syn)|*.syn|"
Mask$= Mask$&"Parasolid Files (*.x_t)|*.x_t|"
Mask$= Mask$&"Drawing Exchange Format (*.dxf)|*.dxf|"
Mask$= Mask$&"Drawing File (*.dwg)|*.dwg||"
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

Save_DXF:!
LowerFileBox
IF (POS(Jobname$,".dxf") OR POS(Jobname$,".dxf")) THEN
        GOSUB Does_file_exist
        IF File_exists THEN Question
        Stat= WriteDxfFile(Jobname$)
        GOTO Leave
END IF

Save_DWG:!
IF (POS(Jobname$,".dwg") OR POS(Jobname$,".dwg")) THEN
        GOSUB Does_file_exist
        IF File_exists THEN Question
        Stat= WriteDxfFile(Jobname$)
        GOTO Leave
END IF

Save_parasolids:!
IF (POS(Jobname$,".x_t") OR POS(Jobname$,".X_T")) THEN
        GOSUB Does_file_exist
        IF File_exists THEN Question

        Name$= Jobname$
        n1= POS(Name$,".x_t")
        IF N1 THEN Name$= Name$[1,n1-1]
        n1= POS(Name$,".X_T")
        IF N1 THEN Name$= Name$[1,n1-1]

        Stat= WPKSaveFile(Name$)
        GOTO Leave
END IF

Save_synergy:!
IF NOT (POS(Jobname$,".syn") OR POS(Jobname$,".SYN")) THEN
                Jobname$= Jobname$&".syn"
END IF
GOSUB Does_file_exist
IF File_exists THEN Question

IF NamedVarVal("save dirs",WsSystemVars,0) THEN V1=CreateVariable("storage dir",WsSystemvars,PathNameDirectory(Jobname$))
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 Answer$= Jobname$
                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
   IF Stat= 2 THEN
      Stat=SetInputFormat("a99","")
      Stat=GetValue("WARNING -- NOT SAVED: file not synergy file",Answer$)
   ELSE
      Stat=SetInputFormat("a99","")
      Stat=GetValue("WARNING -- FILE NOT STORED: cannot open file",Answer$)
   END IF
   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

Does_file_exist:!
File_exists=0
F_type= DoesFileExist(Jobname$)
SELECT F_type
CASE 1
        File_exists=1
        Stat=SetInputFormat("a1",",Y,N,")
        Stat=GetValue("FILE EXISTS: OVERWRITE  Yes or No",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Answer$= "Y" THEN 
                File_exists=0
        END IF
CASE 2
        Stat=SetInputFormat("a1",",P,")
        Stat=GetValue("COPY NAME IS A DIRECTORY: Proceed ",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        File_exists=1
END SELECT
RETURN

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
