
! script name: vzplanes.d/createoriginpt
! var_topic: create work zone origin point

!A
!A NAME:
!A
!A 	Create Work Zone Origin Point         VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	Creates an origin point at (0,0,0) of the current work coordinates.
!A
!A NOTE: The point is actually a HOLE of with 0 for its dimensions.  This
!A	 allows the post processor to recognize the orientation.
!A

Stat=ChangeLabelStr("ScriptNameStatus","Function: create work zone origin ")

DIM Destination$[256]
DIM Answer$[256]
DIM Wpname$[256]
DIM Plane_type$[256]
DIM File_name$[256]
DIM Prop_dir$[256]
DIM Namelist$[32000]
DIM Namelist2$[32000]
DIM ZoneInfo$[32000]
DIM Descr$[256]
DIM Gname$[256]
DIM Ques$[256],Status_line$[256]
DIM Choices$[128]
DIM A$[512],B$[512]
ENTITY Pt

Stat= NamedVarStr("properties dir",WsSystemVars,"/usr/weber/database/properties/",Prop_dir$)

DeActivateUndo
Units= CurrentUnits
IF Units=WsMetric THEN
        Normal_len= 7
ELSE
        Normal_len= 0.25
END IF


WsNamedComponent= 7041776

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

Pick_direct:!
Wzone= CurrentWorkZone

GOSUB Zone_list

Status_line$="Current Work Zone: #"&VAL$(Wzone)
Stat=ChangeLabelStr("Status",Status_line$)

Zone_origin= FindZoneOrigin(WsWorkZoneOrigin,Wzone)
IF NOT ValidGeometry(Zone_origin) THEN
	Choices$= ",N,C,E,"
	Ques$="New_work_zone, Create_origin_point , Exit_script"
ELSE
	Choices$= ",N,U,R,E,"
	Ques$="ORIGIN EXISTS, New_zone, Use_origin, Replace_origin, Exit_script"
	Zone_origin= FindZoneOrigin(WsWorkZoneOrigin,Wzone)
	Pplane= GetAssocEnt(Zone_origin,WsWorkZoneOrigin)
	IF NOT ValidGeometry(Zone_origin) THEN Bad_pplane
	IF ValidGeometry(Zone_origin) THEN Start_ques
Bad_pplane:Stat= DeleteEntity(Pplane)
	GOTO Pick_direct
END IF

Start_ques:Stat= SetInputFormat("a1=",Choices$)
Stat= SetEqualsValue(0,"N")
Stat= GetValue(Ques$,Answer$)
IF ExitScript(Destination$) THEN Leave

SELECT UPC$(Answer$)
CASE "CREATE_ORIGIN_POINT","C"

Create_origin:!
	Answer$=VAL$(Wzone)
	CALL WorkzoneOriginInfo(ZoneInfo$,Destination$,Wzone,Leave_script)
	IF Leave_script THEN Leave
	ZoneInfo=ReadUserData(WsGeneralString,"Work zone",ZoneInfo$,WsTrue)
	Precedence= GetUserValue(ZoneInfo,"Precedence:")
	Fixture_number= GetUserValue(ZoneInfo,"Fixture number:")
	Wzone= GetUserValue(ZoneInfo,"Zone number:")

	Stat= GenerateVarName(WsGroupEnt,Gname$)
	RecordFunctionOn
	Stat= RecordDescr("ORIGIN POINT:  Create work zone #"&VAL$(Wzone)&" origin point")
	V1= FreeParameter("Work zone",Wzone)
	V2= StringConstant(Prop_dir$)
	V3= StringConstant("WorkZone")
	V4= StringConstant("template")
	V5= StringConstant(FileNameString(V2,V3,V4))
	V6= StringConstant(VAL$(GetVariableVal(V1)))
	Pplane= ReadUserData(WsWorkZoneOrigin,V6,V5)
	RecordFunctionOff
	Namelist$= DataFieldListStr(ZoneInfo,":",",",NoOrder,WsFalse)
	Nfields= StringFieldCount(Namelist$,",")
	FOR I= 1 TO Nfields
        	A$= DataFieldStr(Namelist$,",",I)
        	IF LEN(A$) THEN
               	   IF NOT (A$= "Class name" OR A$= "Class id number") THEN
               	   IF NOT (A$= "Name" OR A$= "Zone number") THEN
               	   IF NOT (A$= "Precedence" OR A$= "Fixture number") THEN
                	A$= A$&":"
                	B$= UserDataStr(ZoneInfo,A$)
                	RecordFunctionOn
                	V7= StringConstant(A$)
                	V8= StringConstant(B$)
                	Stat= PutUserString(Pplane,V7,V8)
                	RecordFunctionOff
		   END IF
		   END IF
               	   END IF
                END IF
	NEXT I
	RecordFunctionOn
	V7= StringConstant("Name:")
        Stat= PutUserString(Pplane,V7,V6)
	V8= StringConstant("Zone number:")
	Stat= PutUserValue(Pplane,V8,V1)
	V9= StringConstant("Fixture number:")
	V10= FreeParameter("Fixture number",Fixture_number)
	Stat= PutUserValue(Pplane,V9,V10)
	V11= StringConstant("Precedence:")
	V12= FreeParameter("Precedence",Precedence)
	Stat= PutUserValue(Pplane,V11,V12)
	Pt= PointXYZ(0,0,0)
	G1= DatumPointMarker(Pt,WsWorkZoneOrigin,Normal_len,CHR$(171))
	Stat= AssocUserData(G1,Pplane,WsWorkZoneOrigin)
	Stat= SetWorkZone(G1,V1)
	Stat= AutoNameGroup(G1,Gname$)
	Stat= DeleteEntity(Pt)
	Stat= DeleteEntity(G1)
	RecordFunctionOff
	CloseVarRecord

	Stat= DeleteEntity(ZoneInfo)

CASE "NEW_WORK_ZONE","N","NEW_ZONE"

	GOSUB Zone_list

	Stat= SetInputFormat("a99","")
	E1= PickEntity("WORK ZONE NUMBER ",Answer$)
	IF ExitScript(Destination$) OR Answer$="CANCEL" THEN Leave
	IF ValidGeometry(E1) THEN
		Wzone=GetWorkZone(E1)
	ELSE
		Wzone= NumericPrefix(Answer$)
	END IF
	Stat= SetCurrWorkZone(Wzone)
	LowerSelectBox
	GOTO Pick_direct
CASE "USE_ORIGIN","U"
	Answer$= VAL$(Wzone)
	Descr$= "WORK ZONE: Use work zone #"&Answer$&" origin "
	RecordFunctionOn
	Stat= RecordDescr(Descr$)
	V1= FreeParameter("Work zone",Wzone)
	Hle= FindZoneOrigin(WsWorkZoneOrigin,V1)
	Stat=SetWorkplane(Hle)
	RecordFunctionOff
	CloseVarRecord
	IF IsRulerOn THEN PlotAll
CASE "REPLACE_ORIGIN","R"
	Answer$=VAL$(Wzone)
	Stat= GenerateVarName(WsGroupEnt,Gname$)
	RecordFunctionOn
	Stat= RecordDescr("ORIGIN POINT:  Override work zone #"&Answer$&" origin point")
	V1= FreeParameter("Work zone",Wzone)
	H1= FindZoneOrigin(WsWorkZoneOrigin,V1)
	Pplane= GetAssocEnt(H1,WsWorkZoneOrigin)
	G1= GrpByProp(Pplane,WsWorkZoneOrigin)
	Stat= DeleteGroup(G1,WsTrue)
	Pt= PointXYZ(0,0,0)
        G1= DatumPointMarker(Pt,WsWorkZoneOrigin,Normal_len,CHR$(171))
        Stat= AssocUserData(G1,Pplane,WsWorkZoneOrigin)
        Stat= SetWorkZone(G1,V1)
        Stat= AutoNameGroup(G1,Gname$)
	Stat= DeleteEntity(Pt)
	Stat= DeleteEntity(G1)
	RecordFunctionOff
	CloseVarRecord
CASE "EXIT_SCRIPT","E"
	GOTO Leave
END SELECT
GOTO Leave



Zone_list:!
Group= GrpOfUserProps(WsWorkZoneOrigin,WsWorkZoneOrigin)
IF  GroupCount(Group) THEN 
	Namelist$=""
	FOR I=1 TO GroupCount(Group)
       		 E1= GroupElement(Group,I)
       		 Zone_num= GetUserValue(E1,"Zone number:")
       		 if(Zone_num) THEN
       		         Descr$= UserDataStr(E1,"Description:")
       		         Namelist$= Namelist$&VAL$(Zone_num)&": "&Descr$&","
       		 END IF
	NEXT I
	IF LEN(Namelist$) THEN
		Stat= NumericalOrder(Namelist$,",",WsAscending,Namelist2$)
		Stat=GeneralListBox("WORK ZONES","zone selected",Namelist2$)
	END IF
END IF
Stat= DeleteEntity(Group)
RETURN




Leave:!
Stat= DeleteEntity(Pt)
Stat= DeleteEntity(Group1)
LowerSelectBox
Stat=ChangeLabelStr("Status","                   ")
Stat=SetInputFormat("n4","")
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END


SUB WorkzoneOriginInfo(ZoneInfo$,Destination$,Wzone,Leave_script)
DIM A$[512]
DIM Prop_dir$[512]

Leave_script=0
ZoneInfo$=""
Info:!
Stat= NamedVarStr("properties dir",WsSystemVars,"/usr/weber/database/properties/",Prop_dir$)
A$= UniformPathName(FileNameString(Prop_dir$,"/WorkZone/template"))
ZoneInfo= ReadUserData(WsWorkZoneOrigin,"Work zone info",A$)
IF NOT ValidGeometry(ZoneInfo) THEN Leave_script

Stat= DeleteUserField(ZoneInfo,"Name:")
Stat= DeleteUserField(ZoneInfo,"Class name:")
Stat= DeleteUserField(ZoneInfo,"Class id number:")

Stat= PutUserValue(ZoneInfo,"Zone number:",Wzone)
Stat= PutUserValue(ZoneInfo,"Precedence:",1)

Edit_data:!
Input_stat= MultiEditDataStr(ZoneInfo,":")
IF (ExitScript(Destination$)) THEN Leave_script
IF Input_stat=27 THEN Leave_script
IF Input_stat=1 THEN Edit_data

Stat= PutUserString(ZoneInfo,"Name:",VAL$(Wzone))
Stat= PutUserValue(ZoneInfo,"Class id number:",WsWorkZoneOrigin)
Stat= PutUserString(ZoneInfo,"Class name:","Work zone origin point")

Stat= UserDataString(ZoneInfo,ZoneInfo$)
Stat= DeleteEntity(ZoneInfo)
GOTO Leave_info

Leave_script: Leave_script=1
Stat= DeleteEntity(ZoneInfo)
Leave_info:!
SUBEND
