
! script name: filing.d/mergefile
! topic: read file and place at original location

!A
!A NAME:
!A
!A 	Current Position
!A
!A DESCRIPTION:
!A
!A 	This script allows a file to be read into a group and placed
!A 	at its original location.  This script will not permanently
!A 	merge a file until "Merge" is selected.
!A 	
!B INSTRUCTIONS:
!B
!B 	SELECT EXISTING FILE: The file selection box will appear at
!B 		this time for a selection to be made.
!B
!B 	Undo or Merge: Enter "U" to undo (delete) the file placement.
!B 		Enter "M" to finalize the merge.  Exiting the script
!B 		works like Undo.
!B

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

DIM Answer$[256]
DIM Destination$[256]
DIM Name$[256]
DIM Dir$[256]
DIM D_label$[256]
DIM Sub_label$[256]
DIM F_label$[256]
DIM Choice$[256]
DIM Mask$[256]
DIM Group_name$[256]
DIM Decision$[256]
DIM Mode$[128]

!C 	If Ask_undo$="Y" the "Undo, Restore, Delete" options will be present.
!C
!C 	If Ask_undo$="N" the script will automatically delete the elements
!C 	when they are picked.  NO CHANCE to change the selection.

DeActivateUndo
Ask_undo$="Y"

Stat=SetPickMask(WsPickAll)
Component_group= 30999
Group_type= Component_group


IF ARGC>1 THEN
	Answer$=ARGV(1)
	IF Answer$="igsout" THEN
		F_type= DoesFileExist("igsout_name")
		SELECT F_type
		CASE 1
			ON ERROR GOTO Problem
                	ASSIGN @File TO "igsout_name"
                	ENTER @File;Answer$
Problem:        	ASSIGN @File TO *
                	OFF ERROR
                	IF NOT LEN(Answer$) THEN File_select
		END SELECT
		GOTO Skip_file_select
	END IF
	GOTO Skip_file_select
END IF

File_select:!
Stat= NamedVarStr("retrieve dir",WsSystemVars,"/usr/weber/ST/",Dir$)
D_label$="DIRECTORY"
Sub_label$="SUB DIRECTORIES"
F_label$="FILES"
Choice$="FILE TO OPEN"
Mask$="[A-Za-z0-9]*"
Mask$= "Synergy Files (*.syn)|*.syn|"
Mask$= Mask$&"Drawing Exchange Format (*.dxf)|*.dxf|"
Mask$= Mask$&"Drawing File (*.dwg)|*.dwg|"
Mask$= Mask$&"All Files (*.*)|*.*||"

  Path_num=CurrPathNum
  Cont_num=CurrContourNum
  Hole_num=CurrHoleNum
  Path_grp_num=CurrPathGrpNum
  Hole_grp_num=CurrHoleGrpNum

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

Skip_file_select:!
Name$=Answer$
Group_name$= Answer$
LOOP
  Slash_pos=POS(Group_name$,"/")
  EXIT IF Slash_pos=0
  Group_name$=Group_name$[Slash_pos+1]
END LOOP
Dot_pos=POS(Group_name$,".")
IF Dot_pos THEN
   Group_name$=Group_name$[1,Dot_pos-1]
END IF

StdWorkplane

Decision$=""
Pc=PointXYZ(0,0,0)
Angle=0


PlotOff
Group=ReadGroupFile(Group_name$,Group_type,Name$)
PlotOn

T1=AngleTransform(Pc,0,0,Angle)
stat= TransGroupTrans(Group,T1)
stat= DeleteEntity(Pc)

Stat= ResolveNames(Group)

PlotAll

IF Ask_undo$="Y" THEN
   Decision$="U"
   Stat=SetInputFormat("a1=",",U,M,")
   Stat=SetEqualsValue(0,"M")
   Stat=GetValue("Undo or Merge",Answer$)
   IF (ExitScript(Destination$)) THEN Leave
   Decision$=Answer$
END IF

Leave:!

IF NOT ValidGeometry(Group) THEN Leave_1

IF Ask_undo$="Y" AND Decision$="U" THEN
   Temp_group=Group
   Stat=UnLink(Temp_group)
   Stat=DeleteGroup(Temp_group,1)
   Stat=DeleteEntity(Temp_group)

   Stat=SetCurrPathNum(Path_num)
   Stat=SetCurrContNum(Cont_num)
   Stat=SetCurrHoleNum(Hole_num)
   Stat=SetPathGrpNum(Path_grp_num)
   Stat=SetHoleGrpNum(Hole_grp_num)
ELSE
	IF ARGC>1 THEN Stat= DeleteEntity(Group)
END IF

TransformCleanup

Leave_1: !

LowerFileBox
Stat=SetInputFormat("n4","")
stat= DeleteEntity(Pc)

IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END

