
! script name: vembed.d/embedfile
! var_topic: read file into group and place

!A
!A NAME:
!A
!A 	Embed File	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will embed a static file into variational design.
!A	The file can then be placed at existing point locations.
!A 	
!B INSTRUCTIONS:
!B
!B 	SELECT EXISTING FILE: The selection box will
!B 		appear at this time for a selection to be made.
!B	
!B	LAYERS COPIED, All: Specify the layers
!B 		of the file to be embedded.  An answer of "10,12,15"
!B 		will embed layers 10,12,15 of the source file.
!B
!B 	PICK POINT: Pick location point. The Zero of
!B 		the source file will be located at the location point.
!B 	
!B 	ANGLE: Enter the angle to be used for
!B 		orienting the file to be placed. This angle will
!B 		orient the positive X-axis of the source file.
!B
!C NOTES: 
!C
!C      The embedded file is brought in relative to the current workplane.
!C
!C	The embedded data will inherit the properties of the original
!C	file if no properties are set for the variational record.
!C	If a color, or layer, or line type is applied to the record
!C	then all geometry created by this record will have the same
!C	property.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: embed 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 Center$[256]
DIM Angle_var$[256]
DIM File_name$[256]
DIM Var_name$[256]
DIM Group_name$[256]
DIM Layers$[256]
DIM Colors$[256]
DIM Header$[5000]
DIM Descr$[256]

DEG
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

DeActivateUndo
Start:!

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]*"

Stat=UserFileBox(Dir$,Mask$,D_label$,Sub_label$,F_label$,Choice$)
Stat=SetInputFormat("a99","")
Stat=GetValue("SELECT EXISTING FILE, Variable name ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Answer$= TRIM$(Answer$)
SELECT Answer$
CASE "CANCEL"
	GOTO Leave
CASE "Variable","variable"
	LowerFileBox
	Stat=UserSelectBox("VARIABLES","selected variable",WsVariationVars,WsVarSelect)
	IF Stat=1 THEN Leave
	Stat=SetInputFormat("a99","")
	Stat=GetValue("NAME OF VARIABLE",Answer$)
	IF (ExitScript(Destination$)) THEN Leave
	IF (Answer$="CANCEL") THEN Start

	N=POS(Answer$,"=")
	IF (N>1) THEN
       		 Answer$=Answer$[1,N-1]
       		 Answer$=TRIM$(Answer$)
	END IF
	Var_name$=Answer$
	V1=FindNamedVar(Var_name$,WsVariationVars)
	IF NOT ValidGeometry(V1) THEN Start
	Variable_input=1
	LowerSelectBox
CASE ELSE
	Variable_input=0
	LowerFileBox
	File_name$=Answer$
	IF NamedVarVal("save dirs",WsSystemVars,0) THEN V1=CreateVariable("retrieve dir",WsSystemVars,PathNameDirectory(Answer$))
END SELECT
LowerFileBox

Stat=SetInputFormat("a99","",0)
Stat=GetValue("LAYERS COPIED, All",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF POS(Answer$, "All")  OR POS(Answer$, "ALL") THEN 
	Layers$=""
ELSE
	Layers$=","&Answer$&","
END IF
Colors$=""


Stat= ReadHeaderOnly(File_name$,Header$)
Head=ReadUserData(WsGeneralString,"EMBED HEADER",Header$)
IF ValidGeometry(Head) THEN
	Stat=GetUserString(Head,"Description:",Descr$)
	Stat= DeleteEntity(Head)
ELSE
	Descr$= File_name$
END IF

New_location: !
Stat=SetInputFormat("n4","")
E1=PickEntity("PICK POINT")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E1) THEN New_location
Stat= EntityNameCheck(E1,Center$)
T= LastPickPara
Pick_return= CurrPickReturn

Angle_ques:Angle=InputAngle("ANGLE")
IF (ExitScript(Destination$)) THEN Leave
No_angle_var= LastVariableInput(Angle_var$)
L1= LastPickEnt
IF ValidGeometry(L1) THEN
        IF NOT ValidDependency(L1) THEN Angle_ques
        Stat= EntityNameCheck(L1,Angle_var$)
        No_angle_var=0
END IF

Stat= GenerateVarName(WsVarRecordEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("EMBED: "&Descr$)
Stat= SetRecDelLastExec
Work= CurrentWorkplane
SELECT Pick_return
CASE WsGeometryEnd
        Pc= ParameterReturnPt(Center$,T,WsGeometryEnd)
CASE WsGeometryMid
        Pc= ParameterReturnPt(Center$,T,WsGeometryMid)
CASE WsGeomCenter
        Pc= ParameterReturnPt(Center$,T,WsGeomCenter)
END SELECT
IF No_angle_var THEN
        V1= FreeParameter("Angle",Angle)
	T2=AngleTransform(Pc,0,0,V1,WsWorkplaneVals)
ELSE
	T2=AngleTransform(Pc,0,0,Angle_var$,WsWorkplaneVals)
END IF
T1= MultTransforms(Work,T2)
Stat= DeleteEntity(T2)
Stat= SetWorkplane
PlotOff
IF Variable_input THEN
	V2=FindNamedVar(Var_name$,WsVariationVars)
ELSE
	V2=StringConstant(File_name$)
END IF
G1=ReadGroupFile(Group_name$,Group_type,V2,Layers$,Colors$)
PlotOn
stat= TransGroupTrans(G1,T1)
stat= DeleteEntity(Pc)
Stat= NameEmbeddedGroup(G1,Name$)
RecordFunctionOff
Stat= PlotEntity(G1)
RecordFunctionOn
Stat= DeleteEntity(G1)
Stat= SetWorkplane(Work)
RecordFunctionOff
CloseVarRecord

TransformCleanup
GOTO New_location

Leave:!
TransformCleanUp


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

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


