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

!A
!A NAME:
!A
!A 	Dynamic Merge File
!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

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$[32]
DIM Group_name$[256]
DIM Component_name$[256]
DIM Component_class$[256]
DIM Decision$[256]
DIM Mode$[128]
DIM Namelist$[4000]
DIM Namelist2$[5000]
DIM File_name$[256]

DeActivateUndo
Start:!
Component_group= 30999
Group_type= Component_group

Stat= NamedVarStr("components dir",WsSystemVars,"/usr/weber/util/mold_cmpnts/",Dir$)

Active_workplane=0
Work=CurrentWorkplane
IF ValidGeometry(Work) THEN Active_workplane=1

Single_component=0
IF ARGC>1 THEN
        Answer$=ARGV(1)
	Component_class$= FileNameString(Dir$,Answer$)
	IF ARGC> 2 THEN
        	Answer$=ARGV(2)
		Component_name$= FileNameString(Component_class$,Answer$)
		Single_component=1
		GOTO Component_name
	END IF
        GOTO Component_select
END IF

Directory_select:D_label$="COMPONENT TYPE DIRECTORY"
Choice$="COMPONENT TYPE SELECTED"
Mask$="*"
Stat=DirectoryNames(Dir$,Mask$,Namelist$)
Stat= AlphabeticalOrder(Namelist$,",",WsAscending,Namelist2$)
Stat=GeneralListBox(D_label$,Choice$,Namelist2$)

Stat=SetInputFormat("a99","")
Stat=GetValue("INPUT TYPE OF COMPONENT",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave
Component_class$= FileNameString(Dir$,Answer$)

Component_select:!
D_label$="COMPONENT DIRECTORY"
Choice$="COMPONENT SELECTED"
Mask$="*"
Stat=DirectoryNames(Component_class$,Mask$,Namelist$)
Stat= AlphabeticalOrder(Namelist$,",",WsAscending,Namelist2$)
Stat=GeneralListBox(D_label$,Choice$,Namelist2$)

Stat=SetInputFormat("a99","")
Stat=GetValue("INPUT COMPONENT",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave
Component_name$= FileNameString(Component_class$,Answer$)

LowerSelectBox

Component_name:!
Name$=Component_name$
Group_name$= Component_name$
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

Read_file:!
Active_workplane=0
Work=CurrentWorkplane
IF ValidGeometry(Work) THEN Active_workplane=1

PlotOff
Group=ReadGroupFile(Group_name$,Group_type,Name$)
IF Active_workplane THEN
        Stat=TransGroupTrans(Group,Work)
END IF

Stat= ResolveNames(Group)

PlotOn
Angle= 0
Pref=  PointXYZ(0,0,0)
Pdest= PointXYZ(0,0,0)
Paxis= PointXYZ(0,0,1)
Axis_line= LineTwoPoints(Pref,Paxis)

Vnum= LastPickView
Stat=SetInputFormat("n4b","")
Group1= CopyEntity(Group)
Stat= StripNonGeometry(Group1,WsFalse)
Rotate_angle= DynamicMoveGroup(Vnum,Group1,Pref,Pdest,Angle,"PICK DESTINATION POINT")
Stat= DeleteEntity(Group1)
IF ExitScript(Destination$) THEN 
	Stat= UnLink(Group)
	Stat= DeleteGroup(Group,WsTrue)
	GOTO Leave
END IF

StdWorkplane
Xs=XPointValue(Pref)
Ys=YPointValue(Pref)
Zs=ZPointValue(Pref)

Xe=XPointValue(Pdest)
Ye=YPointValue(Pdest)
Ze=ZPointValue(Pdest)

Xmove=Xe-Xs
Ymove=Ye-Ys
Zmove=Ze-Zs
Pm=PointXYZ(Xmove,Ymove,Zmove)

Shift_trans=AngleTransform(Pm,0,0,0,WsWorkplaneVals)
Axis_trans= RotAboutAxisTrans(Axis_line,Rotate_angle)
Trans1=MultTransforms(Shift_trans,Axis_trans)
Stat=TransGroupTrans(Group,Trans1)

Stat= DeleteEntity(Pm)
Stat= DeleteEntity(Pref)
Stat= DeleteEntity(Pdest)
Stat= DeleteEntity(Paxis)
Stat= DeleteEntity(Axis_line)

IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF
GOTO Read_file

Leave:!
TransformCleanup
Stat= DeleteEntity(Pm)
Stat= DeleteEntity(Pref)
Stat= DeleteEntity(Pdest)
Stat= DeleteEntity(Paxis)
Stat= DeleteEntity(Axis_line)

Leave_1: !
LowerSelectBox
IF Active_workplane THEN
        Stat=SetWorkplane(Work)
ELSE
        StdWorkplane
END IF
Stat=SetInputFormat("n4","")

IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 IF Destination$= "SCRIPTSTART" THEN Start
         IF Destination$= "BACKUP" THEN Start
	 LOAD Destination$
END IF
END

