
! script name: filing.d/3dplace
! topic: read 3D file and place

!A
!A NAME:
!A
!A 	Reposition in 3D
!A
!A DESCRIPTION:
!A
!A 	This script allows a 3D file to be read into a group and placed
!A 	at several locations.  It is assumed that a workplane has been
!A 	defined with a reference point on it that may be picked.  This
!A 	script will not permanently place a file until "Place" is selected.
!A
!B INSTRUCTIONS:
!B
!B 	SELECT EXISTING FILE: The selection box will appear
!B 		at this time for a selection to be made.
!B
!B 	GROUP NAME: Enter the name to be associated with
!B 		the file to be placed.  The group name will have
!B 		a number appended to it in sequential order each
!B 		additional time the file is placed.
!B
!B 	PICK ENTITY FOR WORKPLANE: Pick an entity on
!B 		the workplane to be used as the reference.
!B
!B 	Undo or Place: Enter "U" to undo (delete) the
!B 		file placement.  Enter "P" to finalize the placement
!B 		of the file.  Place must be selected to finalize the
!B 		process, exiting the script works like Undo.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: place 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 Gname$[256]
DIM Decision$[256]

!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)
	Name$=Answer$
	GOTO Skip_file_select
END IF


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 (*.*)|*.*||"

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("text dir",WsSystemVars,PathNameDirectory(Answer$))

Name$=Answer$

Stat=SetInputFormat("a99","")
Stat=GetValue("GROUP NAME",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Skip_file_select:!
Group_name$= Answer$
Gname$= Answer$

Group_num= 0
New_location:!

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

Decision$=""
Stat=SetPickMask(WsPickNoSurf)
E1=PickEntity("PICK ENTITY FOR WORKPLANE")
IF (ExitScript(Destination$)) THEN Leave

Stat=SetWorkplane(E1)
Work= CurrentWorkplane
IF NOT ValidGeometry(Work) THEN
	Pc= PointXYZ(0,0,0)
	T1= AngleTransform(Pc,0,0,0)
ELSE
	T1= Work
END IF
StdWorkplane
PlotOff
Group=ReadGroupFile(Group_name$,Group_type,Name$)
PlotOn

stat= TransGroupTrans(Group,T1)
stat= DeleteEntity(Pc)

PlotAll

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

SELECT Decision$

  CASE "P"

    Group_num= Group_num+1
    Group_name$= Gname$&VAL$(Group_num)

  CASE "U"

    Temp_group=Group
    IF NOT ValidGeometry(Temp_group) THEN New_location
    Stat=UnLink(Temp_group)
    Stat=DeleteGroup(Temp_group,1)
    Stat=DeleteEntity(Temp_group)
    GOSUB Reset_counters

END SELECT

TransformCleanup

Stat=SetPickMask(WsPickAll)
GOTO New_location

Reset_counters: !

  Stat=SetCurrPathNum(Path_num)
  Stat=SetCurrContNum(Cont_num)
  Stat=SetCurrHoleNum(Hole_num)
  Stat=SetPathGrpNum(Path_grp_num)
  Stat=SetHoleGrpNum(Hole_grp_num)

RETURN

Leave:!
Stat=SetPickMask(WsPickAll)

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)
   TransformCleanUp
   GOSUB Reset_counters
ELSE
	IF ARGC>1 THEN Stat= DeleteEntity(Group)
END IF

Leave_1: !
Stat=SetPickMask(WsPickAll)

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

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

