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

!A
!A NAME:
!A
!A 	Scale Embed File	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script will embed a static file into variational design.
!A	The file is scaled by the scale factor.
!A	The file can then be placed at existing point locations.
!A 	
!B INSTRUCTIONS:
!B
!B      SELECT EXISTING FILE: The selection box will appear at this time for a
!B              selection to be made.
!B
!B      LAYERS COPIED, All:     Specify the layers of the file to be embedded.
!B                              An answer of "10,12,15" will embed layers
!B                              10,12,15 of the source file.
!B
!B	SCALE FACTOR:  Enter the scale factor. The file will be scaled
!B			about the origin of the source file before
!B			it is placed.
!B
!B      PICK POINT: Pick location point. The Zero of the source file will
!B                      be located at the location point.
!B
!B      ANGLE: Enter the angle to be used for orienting the file to
!B              be placed. This angle will orient the positive X-axis of the
!B              source file.
!B
!C NOTES:
!C
!C      The embedded file is brought in relative to the current workplane.
!C
!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: scale 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]
DIM Scale_var$[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

stat= SetInputFormat("n4=","")
stat= SetEqualsValue(1.0)
Scale_factor= GetValue("SCALE FACTOR",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_scale_var= LastVariableInput(scale_var$)

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
IF No_scale_var THEN
        V3= FreeParameter("Scale factor",Scale_factor)
ELSE
        V3= FreeParameter("Scale factor",Scale_var$)
END IF
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$)
Pz= PointXYZ(0,0,0)
Stat= ScaleEmbeddedGroup(G1,Pz,V3)
Stat= RemoveDimsInGroup(G1)
PlotOn
stat= TransGroupTrans(G1,T1)
Stat= NameEmbeddedGroup(G1,Name$)
stat= DeleteEntity(Pc)
stat= DeleteEntity(Pz)
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


