
! script name: zplanes.d/toolworkarea

!A
!A NAME:
!A
!A 	Named Planes: Change
!A
!A DESCRIPTION:
!A
!A 	NO HELP AVAILABLE AT THIS TIME
!A
!B INSTRUCTIONS:
!B
!B 	questions with brief description
!B
!C OPTIONS:
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: named tool workplane")

COM /Ops_var_design/ Use_variational
DIM Destination$[256],Answer$[256]
DIM Wpname$[256],Plane_type$[256]
DIM File_name$[256],Dir$[256]
DIM Current_op$[256]
ENTITY Pt,Ptx,Pty

DeActivateUndo
Stat=NamedVarStr("current operation",WsSystemVars,"",Current_op$)
OperData=FindUserData(Current_op$,WsOperation)
IF NOT ValidGeometry(OperData) THEN Leave

WsNamedComponent= 7041776

Pick_direct:!

Dir$="/usr/weber/userdata/"
Use_box=UserSelectBox("Existing Work Areas","Name:",WsWorkArea,WsUserSelect)

Stat=SetInputFormat("a99","")
Stat=GetValue("WORKPLANE NAME ",Wpname$)
IF ExitScript(Destination$) THEN Leave
IF Wpname$= "CANCEL" THEN Leave
LowerSelectBox

WorkArea= FindUserData(Wpname$,WsWorkArea)

IF NOT ValidGeometry(WorkArea) THEN
	WorkArea=ReadUserData(WsWorkArea,Wpname$,FileNameString(Dir$,"workarea"))
	IF NOT ValidGeometry(WorkArea) THEN Pick_direct

	AskWorkplane(Workarea,Pt,Ptx,Pty,Destination$,Do_exit)
	IF Do_exit THEN Leave1

	Stat=UserSelectBox("NAMED ZONES","selected zone",WsNamedComponent,WsUserSelect)
Start:!
	Stat= SetInputFormat("n0a99","")
	Component= InputComponent("WORK ZONE NUMBER ",Answer$)
	IF ExitScript(Destination$) THEN Leave1
	IF LEN(Answer$) THEN
		IF Answer$="CANCEL" THEN Leave1
		E1= FindUserData(Answer$,WsNamedComponent)
		IF NOT ValidGeometry(E1) THEN Start
		Component= GetUserValue(E1,"Component:")
	END IF
	LowerSelectBox

        Stat=SetInputFormat("a1",",P,S,")
        Stat=GetValue("TYPE OF PLANE Primary or Secondary ",Plane_type$)
        IF ExitScript(Destination$) THEN Leave1

	Stat= NamedVarStr("current tool instance",WsSystemVars,"None",Answer$)
	Stat= SetEqualsValue(0,Wpname$)
        Stat=SetInputFormat("a99=","")
        Stat=GetValue("TOOL INSTANCE, None ",Answer$)
        IF ExitScript(Destination$) THEN Leave1

	GOSUB Make_workplane

	V0=CreateVariable("current tool instance",WsSystemVars,Answer$)
	Stat= PutUserString(WorkArea,"Tool instance:",Answer$)
	Stat= PutUserString(WorkArea,"Plane type:",Plane_type$)
	Stat= PutUserValue(WorkArea,"Component:",Component)
	IF UPC$(Plane_type$)="P" THEN CALL Set_origin_hole(Pt,Component,Dir$)
ELSE
	Stat= GetUserString(WorkArea,"Tool instance:",Answer$)
	V0= CreateVariable("current tool instance",WsSystemVars,Answer$)
	Component= GetUserValue(WorkArea,"Component:")
	IF CheckUserField(WorkArea,"Origin X:",0) THEN
		Px= GetUserValue(WorkArea,"Origin X:")
		Py= GetUserValue(WorkArea,"Origin Y:")
		Pz= GetUserValue(WorkArea,"Origin Z:")
		Ptxx= GetUserValue(WorkArea,"X-axis X:")
		Ptxy= GetUserValue(WorkArea,"X-axis Y:")
		Ptxz= GetUserValue(WorkArea,"X-axis Z:")
		Ptyx= GetUserValue(WorkArea,"Y-axis X:")
		Ptyy= GetUserValue(WorkArea,"Y-axis Y:")
		Ptyz= GetUserValue(WorkArea,"Y-axis Z:")

		StdWorkplane
		Pt= PointXYZ(Px,Py,Pz)
		Ptx= PointXYZ(Ptxx,Ptxy,Ptxz)
		Pty= PointXYZ(Ptyx,Ptyy,Ptyz)
	ELSE
		AskWorkplane(Workarea,Pt,Ptx,Pty,Destination$,Do_exit)
		IF Do_exit THEN Leave
	END IF
	GOSUB Make_workplane
	Stat= GetUserString(WorkArea,"Plane type:",Plane_type$)
	IF UPC$(Plane_type$)="P" THEN CALL Set_origin_hole(Pt,Component,Dir$)
END IF

Set_components:!
Stat=SetCurrComponent(Component)
V0=CreateVariable("current work area",123456,Wpname$)

GOTO Leave

Make_workplane:!
	T1=ThreePtTrans(Pt,Ptx,Pty)
	Stat=SetWorkplane(T1)
RETURN

Leave1:!
IF ValidGeometry(WorkArea) THEN Stat= DeleteEntity(WorkArea)
Leave:!
IF ValidGeometry(Pt) THEN Stat= DeleteEntity(Pt)
IF ValidGeometry(Ptx) THEN Stat= DeleteEntity(Ptx)
IF ValidGeometry(Pty) THEN Stat= DeleteEntity(Pty)
LowerSelectBox
Stat=SetInputFormat("n4","")
IF NOT LEN(Destination$) THEN
	IF ValidGeometry(OperData) THEN
		Stat= GetUserString(OperData,"Operation script:",Answer$)
		LOAD Answer$
	ELSE
		LOAD "typecontrol"
	END IF
ELSE
	LOAD Destination$
END IF
END

SUB AskWorkplane(Workarea,Pt,Ptx,Pty,Destination$,Do_exit)
	Do_exit= 0
	Start_work= CurrentWorkplane
	StdWorkplane
	Stat=SetInputFormat("n4","")
	Pt=XyzInput("WORKPLANE ORIGIN ",WsXYZInput)
	IF ExitScript(Destination$) THEN Leave

	Ptx=XyzInput("X AXIS DEFINING POINT",WsXYZInput)
	IF ExitScript(Destination$) THEN Leave

	Pty=XyzInput("Y AXIS DEFINING POINT",WsXYZInput)
	IF ExitScript(Destination$) THEN Leave

	Stat= PutUserValue(WorkArea,"Origin X:",XPointValue(Pt))
	Stat= PutUserValue(WorkArea,"Origin Y:",YPointValue(Pt))
	Stat= PutUserValue(WorkArea,"Origin Z:",ZPointValue(Pt))
	Stat= PutUserValue(WorkArea,"X-axis X:",XPointValue(Ptx))
	Stat= PutUserValue(WorkArea,"X-axis Y:",YPointValue(Ptx))
	Stat= PutUserValue(WorkArea,"X-axis Z:",ZPointValue(Ptx))
	Stat= PutUserValue(WorkArea,"Y-axis X:",XPointValue(Pty))
	Stat= PutUserValue(WorkArea,"Y-axis Y:",YPointValue(Pty))
	Stat= PutUserValue(WorkArea,"Y-axis Z:",ZPointValue(Pty))
	SUBEXIT
Leave:!
	Do_exit=1
	IF ValidGeometry(Start_work) THEN Stat=SetWorkplane(Start_work)
SUBEND

SUB Set_origin_hole(Pt,Component,Dir$)

WsPplane= 7746279
DIM Namelist$[512]

Pplane= FindUserData(VAL$(Component),WsPplane)
IF NOT ValidGeometry(Pplane) THEN
	Namelist$= Dir$&"primeplanes"
	Pplane=ReadUserData(WsPplane,VAL$(Component),Namelist$)
	Hle=Hole(Pt,0,0,0)
	Stat= AssocUserData(Hle,Pplane,WsPplane)
ELSE
	Group1= GrpByProp(Pplane,WsPplane)
	IF NOT ValidGeometry(Group1) THEN
		Hle=Hole(Pt,0,0,0)
		Stat= AssocUserData(Hle,Pplane,WsPplane)
	END IF
	Stat= DeleteEntity(Group1)
END IF

SUBEND
