
! script name: filing.d/storegroup
! topic: store geometry picked in a box.

!A
!A NAME:
!A
!A 	Store picked geometry in a box.
!A
!A DESCRIPTION:
!A
!A
!B INSTRUCTIONS:
!B
!B 	SAVE JOB AS:  Enter the name the job is to be stored as.  The name
!B		may not have any spaces in it.
!B
!B 	FILE DESCR: Enter a brief file description to be stored with
!B 		the job.
!B
!C NOTES:
!C
!C	This routine will store in two modes:
!C
!C		Store_type= WsStoreGeometry : will only store the geometric
!C		elements selected by the pick. Properties,Groups,Contours
!C		Tools etc are not stored.
!C
!C		Store_type= WsStoreStructure : This will track both
!C		group associations and dimension associations and
!C		store all entities contained in these associations
!C		It will also include all property entities attached
!C		to the stored entities. Variational records and
!C		objects will not be stored. Drawings and Views are not
!C		stored.
!C
Stat=ChangeLabelStr("ScriptNameStatus","Function: store group ")

DIM Answer$[256],Destination$[256],New_header$[4000],Old_header$[4000],A$[256]
DIM Descr$[256],Datestr$[256],Sdevice$[256],Jobname$[256]
DIM Tmp$[256],Space$[25],Jobs$[25],Unit$[256],Dir$[256],Dir1$[256]
DIM Mask$[256],Namelist$[5000],Filelist$(100)[100]
DIM Name$[256],D_label$[256],Sub_label$[256],F_label$[256],Choice$[256]


IF ARGC > 1 THEN
	Answer$= ARGV(1) 
	Store_type= VAL(Answer$)
ELSE
	Store_type= WsStoreGeometry
END IF


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

DeActivateUndo

Pick_group:!
Group=CreateGroup("PICKED ENTITIES")
Vnum= LastPickView
Stat=PickBox(Group,Vnum,WsPickPen)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Group) THEN Pick_loop


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||"
	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 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
	LowerFileBox

	IF NOT (POS(Jobname$,".syn") OR POS(Jobname$,".SYN")) THEN
                Jobname$= Jobname$&".syn"
	END IF
	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: OVERWRITE  Yes or 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(Old_header$)
New_header$= Old_header$
IF LEN(New_header$) THEN
   Head=ReadUserData(WsGeneralString,"HEADER",New_header$)
   IF ValidGeometry(Head) THEN
	Stat=SetInputFormat("a99","")
	Stat=GetValue("FILE DESCRIPTION:",Answer$)
	IF (ExitScript(Destination$)) THEN Leave
	Descr$= Answer$

	Stat= DateString(Datestr$)
        Units= CurrentUnits
        Unit$= RPT$("METRIC",Units= WsMetric)&RPT$("INCHES",Units=WsInch)
	Stat=PutUserString(Head,"File name:",Jobname$)
	Stat=PutUserString(Head,"Description:",Descr$)
	Stat=PutUserString(Head,"Creation date:",Datestr$)
	Stat=PutUserString(Head,"Revision date:",Datestr$)
	Stat=PutUserString(Head,"Accumulated Time:","0.00 minutes")
        Stat=PutUserString(Head,"Units:",Unit$)

	Stat= UserDataString(Head,New_header$)
   END IF
END IF

TransformCleanup
UnSelectAll

!        add CadSettings to group
E1= FindUserData("CadSettings",WsCadSettings)
IF ValidGeometry(E1) THEN
	Stat= AddToGroup(Group,E1)
END IF
Stat= DisplayMessage("STORING GROUP")
IF LEN(New_header$) THEN Stat= FileHeader(New_header$)
Stat=StoreGroup(Group,Jobname$,Store_type)

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

IF LEN(Old_header$) THEN Stat= FileHeader(Old_header$)
Stat= DeleteEntity(Group)
Stat= DeleteEntity(Head)
Stat= DisplayMessage("STORE COMPLETE")



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