
! script name: vzplanes.d/originpoints
! 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]
DIM Choices$[128]
DIM A$[512],B$[512]
DIM Call_type$[256]
ENTITY Pt

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

DeActivateUndo
IF ARGC>1 THEN
	Call_type$= ARGV(1)
ELSE
	Call_type$= "create"
END IF

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:!
Leave_script=0
SELECT Call_type$
CASE "create"
Next_zone:GOSUB Ask_zone_number
	Made_plane=0
	CALL Work_planes(Destination$,Made_plane,Leave_script)
	IF Leave_script THEN Leave
	Stat= SetCurrWorkZone(Wzone)
	CALL WorkzoneOriginInfo(ZoneInfo$,Destination$,Wzone,Leave_script)
	IF Leave_script AND Made_plane THEN
		DelLastVarRecord
		Made_plane= 0
		GOTO Leave
	END IF
	IF Leave_script THEN Leave
	GOSUB Create_origin
	PlotRuler
	PlotAll
	GOTO Next_zone
CASE "modify"
Next_edit:GOSUB Ask_zone_list
	Zone_origin= FindZoneOrigin(WsWorkZoneOrigin,Wzone)
	Pplane= GetAssocEnt(Zone_origin,WsWorkZoneOrigin)
	CALL EditZoneData(Pplane,ZoneInfo$,Destination$,Wzone,Leave_script)
	IF Leave_script THEN Leave
	GOSUB Modify_origin_data
CASE "move"
Next_move:GOSUB Ask_zone_list
	CALL Work_planes(Destination$,Made_plane,Leave_script)
	IF Leave_script THEN Leave
	GOSUB Replace_origin
	PlotRuler
	PlotAll
	GOTO Next_move
CASE "switch"
	GOSUB Ask_zone_list
	GOSUB Switch_to_origin
	Stat= SetCurrWorkZone(Wzone)
	PlotRuler
	PlotAll
	GOTO Leave
END SELECT
GOTO Leave

Zone_origin= FindZoneOrigin(WsWorkZoneOrigin,Wzone)
IF NOT ValidGeometry(Zone_origin) THEN
	Choices$= ",N,C,E,"
	Ques$= "ZONE #"&VAL$(Wzone)&" New_zone, Create_origin , Exit "
ELSE
	Choices$= ",N,U,R,E,"
	Ques$= "CURRENT ZONE #"&VAL$(Wzone)&" New_zone, Exit "
	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

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

Ask_zone_list:!
	GOSUB Zone_list
	GOSUB Ask_zone_number
	LowerSelectBox
	RETURN

Ask_zone_number:!
	Stat= SetInputFormat("a99","")
	IF Call_type$="create" THEN
		E1= PickEntity("WORK ZONE NUMBER, Listing ",Answer$)
	ELSE
		E1= PickEntity("WORK ZONE NUMBER ",Answer$)
	END IF
	IF ExitScript(Destination$) THEN Leave
	IF Answer$="CANCEL" THEN
		IF Call_type$<>"create" THEN Leave
		GOTO Ask_zone_number
	END IF
	IF Answer$="Listing" THEN
		GOSUB Zone_list
		GOTO Ask_zone_number
	END IF
	IF ValidGeometry(E1) THEN
		Wzone= GetWorkZone(E1)
	ELSE
		Wzone= NumericPrefix(Answer$)
	END IF
RETURN

Switch_to_origin:!
	Descr$= "WORK ZONE: Use work zone origin "
	RecordFunctionOn
	Stat= RecordDescr(Descr$)
	V1= FreeParameter("Work zone",Wzone)
	Hle= FindZoneOrigin(WsWorkZoneOrigin,V1)
	Stat=SetWorkplane(Hle)
	RecordFunctionOff
	CloseVarRecord
	IF IsRulerOn THEN PlotAll
RETURN

Modify_origin_data:!
	ZoneInfo=ReadUserData(WsGeneralString,"Work zone",ZoneInfo$,WsTrue)
	Precedence= GetUserValue(ZoneInfo,"Precedence:")
	Fixture_number= GetUserValue(ZoneInfo,"Fixture number:")
	Wzone= GetUserValue(ZoneInfo,"Zone number:")

	RecordFunctionOn
        Stat= RecordDescr("WORK ZONE:  Modify work zone data")
	V1= FreeParameter("Work zone",Wzone)
	H1= FindZoneOrigin(WsWorkZoneOrigin,V1)
	Pplane= GetAssocEnt(H1,WsWorkZoneOrigin)
	V6= StringConstant(VAL$(GetVariableVal(V1)))
	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)
	RecordFunctionOff
	CloseVarRecord
	Stat= DeleteEntity(ZoneInfo)
RETURN

Create_origin:!
	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 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)
RETURN

Replace_origin:!
	Stat= GenerateVarName(WsGroupEnt,Gname$)
	RecordFunctionOn
	Stat= RecordDescr("ORIGIN POINT:  Override work zone 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
RETURN

Leave:!
Stat= DeleteEntity(Pt)
Stat= DeleteEntity(Group1)
LowerSelectBox
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

CALL Raise_edit_box(ZoneInfo,ZoneInfo$,Destination$,Wzone,Leave_script)
IF Leave_script THEN Leave_script
Stat= UserDataString(ZoneInfo,ZoneInfo$)
Stat= DeleteEntity(ZoneInfo)
SUBEXIT
Leave_script: Leave_script=1
Stat= DeleteEntity(ZoneInfo)
SUBEND

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

Leave_script=0
ZoneInfo$=""
CALL Raise_edit_box(ZoneInfo,ZoneInfo$,Destination$,Wzone,Leave_script)
IF Leave_script THEN Leave_script
Stat= UserDataString(ZoneInfo,ZoneInfo$)
SUBEXIT
Leave_script: Leave_script=1
SUBEND

SUB Raise_edit_box(ZoneInfo,ZoneInfo$,Destination$,Wzone,Leave_script)
Leave_script=0
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")
SUBEXIT 
Leave_script: Leave_script=1
SUBEND

SUB Work_planes(Destination$,Made_plane,Leave_script)
DIM Answer$[256]
DIM Namelist$[4096]
Made_plane= 0

Namelist$="CURRENT WORKPLANE,"
Namelist$=Namelist$&"WORKPLANE 3 POINTS,"
Namelist$=Namelist$&"XY PLANE,"
Namelist$=Namelist$&"XZ PLANE,"
Namelist$=Namelist$&"YZ PLANE,"
Namelist$=Namelist$&"INVERTED XY PLANE (HMC),"
Namelist$=Namelist$&"INVERTED XY PLANE (VMC),"
Namelist$=Namelist$&"INVERTED XZ PLANE,"
Namelist$=Namelist$&"INVERTED YZ PLANE,"
Namelist$=Namelist$&"PICK EXISTING WORKPLANE,"
Namelist$=Namelist$&"INVERTED EXISTING WORKPLANE,"
Namelist$=Namelist$&"PARALLEL THROUGH A POINT,"
Namelist$=Namelist$&"PARALLEL TO A SOLID PLANE,"
Namelist$=Namelist$&"PARALLEL AT A DISTANCE,"
Namelist$=Namelist$&"STANDARD WORKPLANE,"
Namelist$=Namelist$&"NORMAL LINE AND A POINT,"
Namelist$=Namelist$&"VERTICAL TO A LINE,"
Namelist$=Namelist$&"NORMAL LINE ORIENT X AXIS,"
Namelist$=Namelist$&"NORMAL LINE ORIENT Y AXIS"

Stat=SetInputFormat("a99","")
Stat=GeneralListBox("Zplanes","Choices",Namelist$)
Stat=GetValue("SELECT WORKPLANE TYPE",Answer$)
IF ExitScript(Destination$) OR Answer$="CANCEL" THEN Leave
LowerSelectBox

SELECT Answer$
CASE "CURRENT WORKPLANE"
SUBEXIT
CASE "WORKPLANE 3 POINTS"
CALL Wp_plane3pts(Destination$,Leave_script)
CASE "XY PLANE"
CALL Wp_x_y_or_z_zplane("XY",Destination$,Leave_script,0,0,0)
CASE "XZ PLANE"
CALL Wp_x_y_or_z_zplane("XZ",Destination$,Leave_script,90,0,0)
CASE "YZ PLANE"
CALL Wp_x_y_or_z_zplane("YZ",Destination$,Leave_script,0,-90,0)
CASE "INVERTED XY PLANE (HMC)"
CALL Wp_x_y_or_z_zplane("XY",Destination$,Leave_script,0,180,0)
CASE "INVERTED XY PLANE (VMC)"
CALL Wp_x_y_or_z_zplane("XY",Destination$,Leave_script,180,0,0)
CASE "INVERTED XZ PLANE"
CALL Wp_x_y_or_z_zplane("XZ",Destination$,Leave_script,-90,0,0)
CASE "INVERTED YZ PLANE"
CALL Wp_x_y_or_z_zplane("YZ",Destination$,Leave_script,0,90,0)
CASE "PICK EXISTING WORKPLANE"
CALL Wp_pickworkplane("F",Destination$,Leave_script)
CASE "INVERTED EXISTING WORKPLANE"
CALL Wp_pickworkplane("R",Destination$,Leave_script)
CASE "PARALLEL THROUGH A POINT"
CALL Wp_parallelpt(Destination$,Leave_script)
CASE "PARALLEL TO A SOLID PLANE"
CALL Wp_flatsurfwkpln(Destination$,Leave_script)
CASE "PARALLEL AT A DISTANCE"
CALL Wp_parallelplane(Destination$,Leave_script)
CASE "STANDARD WORKPLANE"
	RecordFunctionOn
	Stat= RecordDescr("WORKPLANE: Standard")
	Stat= SetWorkplane
	RecordFunctionOff
	CloseVarRecord
	IF IsRulerOn THEN PlotAll
CASE "NORMAL LINE AND A POINT"
CALL Wp_lineverttrans(Destination$,Leave_script)
CASE "VERTICAL TO A LINE"
CALL Wp_vertlineplane(Destination$,Leave_script)
CASE "NORMAL LINE ORIENT X AXIS"
CALL Wp_normal_to_point("X",Destination$,Leave_script)
CASE "NORMAL LINE ORIENT Y AXIS"
CALL Wp_normal_to_point("Y",Destination$,Leave_script)
END SELECT
IF Leave_script THEN Leave
Made_plane= 1
SUBEXIT
Leave:!
LowerSelectBox
Leave_script= 1
Made_plane= 0
SUBEND

SUB Wp_flatsurfwkpln(Destination$,Leave_script)
DIM Answer$[256]
DIM Surface$[256]
DIM Name$[256]
DIM Center$[256]

Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Tol=NamedVarVal("3D Fillet Tol",Ws3DSystemVars,0.005)
Forward_color=3
Reverse_color=4

stat= SetInputFormat("n4","")

Center_point: Stat=SetPickMask(WsPickNoSurf)
E1=PickEntity("PICK WORKPLANE ZERO")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E1) THEN Center_point
T= LastPickPara
Pick_return= CurrPickReturn
Stat= EntityNameCheck(E1,Center$)

Pick_surface:!
Stat=SetPickMask(WsPickSurfOnly)
Surface=PickEntity("PICK PLANAR SURFACE")
IF (ExitScript(Destination$)) THEN Leave
IF IsSurfacePlanar(Surface)= WsFalse THEN Pick_surface
IF NOT ValidDependency(Surface) THEN Pick_surface
Stat= EntityNameCheck(Surface,Surface$)
Stat=SetPickMask(WsPickGeometry)

UVpoint=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint) THEN Pick_surface
U=XPointValue(UVpoint)
V=YPointValue(UVpoint)
Stat=DeleteEntity(UVPoint)
Forward_normal=SurfNormalLine(Surface,U,V,Normal_len,WsForward)
Pforward=ParameterPt(Forward_normal,1.0)

Reverse_normal=SurfNormalLine(Surface,U,V,Normal_len,WsReverse)
Preverse=ParameterPt(Reverse_normal,1.0)

Stat= SetForceVisible(Forward_normal,WsTrue)
Stat= SetForceVisible(Pforward,WsTrue)
Stat= SetForceVisible(Reverse_normal,WsTrue)
Stat= SetForceVisible(Preverse,WsTrue)

Stat=SetColor(Forward_normal,Forward_color)
Stat=SetColor(Pforward,Forward_color)
Stat=SetColor(Reverse_normal,Reverse_color)
Stat=SetColor(Preverse,Reverse_color)
Pick_normal: L1=PickSpecificEnt("PICK WHICH SIDE OF SURFACE",WsPickLines)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(L1) THEN Line_loop
Norm_sign=0
Pt=ParameterPt(L1,1.0)
D1=DistTwoPts(Pt,Pforward)
D2=DistTwoPts(Pt,Preverse)
Norm_sign=1
IF D2<D1 THEN Norm_sign=-1
IF NOT Norm_sign THEN Pick_normal
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Normal_sign=Norm_sign

Stat= GenerateVarName(WsTransformEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("WORKPLANE: point, parallel to planar surface")
Stat=SetWorkplane
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
V1= FreeParameter("Normal direction",Normal_sign)
L1=SurfNormalLine(Surface$,0.5,0.5,1.0,V1)
Trans1=LineToVertTrans(L1,Pc)
Stat=SetWorkplane(Trans1)
Stat= NameEntity(Trans1,Name$)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(L1)
RecordFunctionOff
CloseVarRecord

Stat=SetPickMask(WsPickGeometry)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(L1)
Stat=DeleteEntity(UVPoint)
IF IsRulerOn THEN PlotAll
SUBEXIT
Leave:!
Leave_script= 1
Stat=SetPickMask(WsPickGeometry)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(L1)
Stat=DeleteEntity(UVPoint)
SUBEND

SUB Wp_lineverttrans(Destination$,Leave_script)
DIM Answer$[256]
DIM Element$[256]
DIM Name$[256]
DIM Center$[256]

stat= SetInputFormat("n4","")

Center_point: E1=PickEntity("PICK WORKPLANE ZERO")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E1) THEN Center_point
T= LastPickPara
Pick_return= CurrPickReturn
Stat= EntityNameCheck(E1,Center$)

Pick_line:L1=PickSpecificEnt("PICK LINE NORMAL TO WORKPLANE",WsPickLines)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(L1) THEN Pick_line
Stat= EntityNameCheck(L1,Element$)

Stat= GenerateVarName(WsTransformEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("WORKPLANE: point, normal to line")
Stat=SetWorkplane
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
Trans1=LineToVertTrans(Element$,Pc)
Stat=SetWorkplane(Trans1)
Stat= NameEntity(Trans1,Name$)
Stat=DeleteEntity(Pc)
RecordFunctionOff
CloseVarRecord
IF IsRulerOn THEN PlotAll

SUBEXIT
Leave:!
Leave_script= 1
SUBEND

SUB Wp_normal_to_point(Mode$,Destination$,Leave_script)
DIM Answer$[256]
DIM Element$[256]
DIM Name$[256]
DIM Center$[256]
DIM Point$[256]

stat= SetInputFormat("n4","")

Stat=SetPickMask(WsPickNoSurf)
Center_point: E1=PickEntity("PICK WORKPLANE ZERO")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E1) THEN Center_point
Pick_para1= LastPickPara
Pick_return1= CurrPickReturn
Stat= EntityNameCheck(E1,Center$)

Stat=SetPickMask(WsPickNoSurf)
Pick_line:L1=PickSpecificEnt("PICK LINE NORMAL TO WORKPLANE",WsPickLines)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(L1) THEN Pick_line
Stat= EntityNameCheck(L1,Element$)

Stat=SetPickMask(WsPickNoSurf)
Orient_point:!
IF Mode$="X" THEN
E2=PickEntity("PICK X AXIS ORIENT POINT")
ELSE
E2=PickEntity("PICK Y AXIS ORIENT POINT")
END IF
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E2) THEN Orient_point
Pick_para2= LastPickPara
Pick_return2= CurrPickReturn
Stat= EntityNameCheck(E2,Point$)

Stat= GenerateVarName(WsTransformEnt,Name$)
RecordFunctionOn

IF Mode$="X" THEN
Stat= RecordDescr("WORKPLANE: point, normal to line orient x axis")
ELSE
Stat= RecordDescr("WORKPLANE: point, normal to line orient y axis")
END IF

Stat=SetWorkplane
SELECT Pick_return1
CASE WsGeometryEnd
        Pc= ParameterReturnPt(Center$,Pick_para1,WsGeometryEnd)
CASE WsGeometryMid
        Pc= ParameterReturnPt(Center$,Pick_para1,WsGeometryMid)
CASE WsGeomCenter
        Pc= ParameterReturnPt(Center$,Pick_para1,WsGeomCenter)
END SELECT
SELECT Pick_return2
CASE WsGeometryEnd
        Pt= ParameterReturnPt(Point$,Pick_para2,WsGeometryEnd)
CASE WsGeometryMid
        Pt= ParameterReturnPt(Point$,Pick_para2,WsGeometryMid)
CASE WsGeomCenter
        Pt= ParameterReturnPt(Point$,Pick_para2,WsGeomCenter)
END SELECT
IF Mode$="X" THEN
Trans1=NormalLineXPtTrans(Element$,Pc,Pt)
ELSE
Trans1=NormalLineYPtTrans(Element$,Pc,Pt)
END IF
Stat=SetWorkplane(Trans1)
Stat= NameEntity(Trans1,Name$)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(Pt)
RecordFunctionOff
CloseVarRecord
IF IsRulerOn THEN PlotAll
SUBEXIT
Leave:!
Leave_script= 1
SUBEND

SUB Wp_parallelplane(Destination$,Leave_script)
DIM Answer$[256]
DIM Name$[256]
DIM Dist_var$[256]
DIM Element$[256]

Distr=NamedVarVal("parallel distance",WsSystemVars,.5)
Stat=SetInputFormat("n4","")

Pick_entity:E1=PickEntity("PICK ENTITY FOR EXISTING WORKPLANE")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E1) THEN Pick_entity
Stat= EntityNameCheck(E1,Element$)

Stat=SetEqualsValue(Distr)
Stat=SetInputFormat("n4=","")
Dist= GetValue("PARALLEL DISTANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
No_dist_var= LastVariableInput(Dist_var$)


Stat= GenerateVarName(WsTransformEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("WORKPLANE: Parallel")
Stat=SetWorkplane(Element$)
Trans1= CurrentWorkplane
IF No_dist_var THEN
        V1= FreeParameter("Distance",Dist)
	Trans2=ParallelTransform(Trans1,V1)
ELSE
	Trans2=ParallelTransform(Trans1,Dist_var$)
END IF
Stat=SetWorkplane(Trans2)
Stat= NameEntity(Trans2,Name$)
RecordFunctionOff
CloseVarRecord
IF IsRulerOn THEN PlotAll
SUBEXIT
Leave:!
Leave_script= 1
SUBEND

SUB Wp_parallelpt(Destination$,Leave_script)
DIM Answer$[256]
DIM Element$[256]
DIM Name$[256]
DIM Center$[256]

stat= SetInputFormat("n4","")

Pick_entity:E1=PickEntity("PICK ENTITY FOR EXISTING WORKPLANE")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E1) THEN Pick_entity
Stat= EntityNameCheck(E1,Element$)

Center_point: E2=PickEntity("PICK WORKPLANE ZERO")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E2) THEN Center_point
T= LastPickPara
Pick_return= CurrPickReturn
Stat= EntityNameCheck(E2,Center$)

Stat= GenerateVarName(WsTransformEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("WORKPLANE: Parallel thru point")
Stat=SetWorkplane(Element$)
Trans1= 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
Trans2= ParallelPointTrans(Trans1,Pc)
Stat=SetWorkplane(Trans2)
Stat= NameEntity(Trans2,Name$)
Stat=DeleteEntity(Pc)
RecordFunctionOff
CloseVarRecord
IF IsRulerOn THEN PlotAll
SUBEXIT
Leave:!
Leave_script= 1
SUBEND

SUB Wp_pickworkplane(Dir_type$,Destination$,Leave_script)
DIM Answer$[256]
DIM Ename$[256]

stat= SetInputFormat("n4","")

Pick_ent:E1=PickEntity("PICK ENTITY FOR WORKPLANE")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E1) THEN Pick_ent
Stat= EntityNameCheck(E1,Ename$)

RecordFunctionOn
IF Dir_type$= "F" THEN
	Stat= RecordDescr("WORKPLANE: Existing Workplane")
	Stat= SetWorkplane(Ename$)
ELSE
	Stat= RecordDescr("WORKPLANE: Invert Existing Workplane")
	Work= ReverseZTrans(Ename$)
	Stat= SetWorkplane(Work)
END IF
RecordFunctionOff
CloseVarRecord
IF IsRulerOn THEN PlotAll
SUBEXIT
Leave:!
Leave_script= 1
SUBEND

SUB Wp_plane3pts(Destination$,Leave_script)
DIM Answer$[256]
DIM Point1$[256]
DIM Point2$[256]
DIM Point3$[256]
DIM Name$[256]

StdWorkplane

First_point: E1=PickEntity("PICK FIRST POINT")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E1) THEN First_point
T1= LastPickPara
Pick_return1= CurrPickReturn
Stat= EntityNameCheck(E1,Point1$)
P1= ParameterReturnPt(E1,T1,Pick_return1)

Second_point: E2=PickEntity("PICK SECOND POINT")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E2) THEN Second_point
T2= LastPickPara
Pick_return2= CurrPickReturn
Stat= EntityNameCheck(E2,Point2$)
P2= ParameterReturnPt(E2,T2,Pick_return2)

Third_point: E3=PickEntity("PICK THIRD POINT")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(E3) THEN Third_point
T3= LastPickPara
Pick_return3= CurrPickReturn
Stat= EntityNameCheck(E3,Point3$)
P3= ParameterReturnPt(E3,T3,Pick_return3)

Stat= DeleteEntity(P1)
Stat= DeleteEntity(P2)
Stat= DeleteEntity(P3)

Stat= GenerateVarName(WsTransformEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("WORKPLANE: Three Point Workplane")
Stat=SetWorkplane
SELECT Pick_return1
CASE WsGeometryEnd
        P1= ParameterReturnPt(Point1$,T1,WsGeometryEnd)
CASE WsGeometryMid
        P1= ParameterReturnPt(Point1$,T1,WsGeometryMid)
CASE WsGeomCenter
        P1= ParameterReturnPt(Point1$,T1,WsGeomCenter)
END SELECT
SELECT Pick_return2
CASE WsGeometryEnd
        P2= ParameterReturnPt(Point2$,T2,WsGeometryEnd)
CASE WsGeometryMid
        P2= ParameterReturnPt(Point2$,T2,WsGeometryMid)
CASE WsGeomCenter
        P2= ParameterReturnPt(Point2$,T2,WsGeomCenter)
END SELECT
SELECT Pick_return3
CASE WsGeometryEnd
        P3= ParameterReturnPt(Point3$,T3,WsGeometryEnd)
CASE WsGeometryMid
        P3= ParameterReturnPt(Point3$,T3,WsGeometryMid)
CASE WsGeomCenter
        P3= ParameterReturnPt(Point3$,T3,WsGeomCenter)
END SELECT
Trans1=ThreePtTrans(P1,P2,P3)
Stat=SetWorkplane(Trans1)
Stat= NameEntity(Trans1,Name$)
Stat= DeleteEntity(P1)
Stat= DeleteEntity(P2)
Stat= DeleteEntity(P3)
RecordFunctionOff
CloseVarRecord
IF IsRulerOn THEN PlotAll
SUBEXIT
Leave:!
Leave_script= 1
SUBEND

SUB Wp_vertlineplane(Destination$,Leave_script)
DIM Answer$[256]
DIM Element$[256]
DIM Name$[256]
DIM Center$[256]

Start:!
stat= SetInputFormat("n4","")

Pick_line:L1=PickSpecificEnt("PICK LINE OR WORKPLANE AXIS",WsPickLines)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(L1) THEN Pick_line
Stat= EntityNameCheck(L1,Element$)
T= LastPickPara
IF T< 0.5 THEN
	T1= 0.0
	T2= 1.0
ELSE
	T1= 1.0
	T2= 0.0
END IF

Stat= GenerateVarName(WsTransformEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("WORKPLANE: vertical to a line")
Stat=SetWorkplane
Pc= ParameterPt(Element$,T1)
P1= ParameterPt(Element$,T2)
Pz= PointXYZ(0.0,0.0,1.0)
P2= AddVectors(P1,Pz)
Trans1=ThreePtTrans(Pc,P1,P2)
Stat=SetWorkplane(Trans1)
Stat= NameEntity(Trans1,Name$)
Stat=DeleteEntity(Pc)
Stat=DeleteEntity(P1)
Stat=DeleteEntity(P2)
Stat=DeleteEntity(Pz)
RecordFunctionOff
CloseVarRecord
IF IsRulerOn THEN PlotAll
SUBEXIT
Leave:!
Leave_script= 1
SUBEND

SUB Wp_x_y_or_z_zplane(Descr$,Destination$,Leave_script,Xangle,Yangle,Zangle)
DIM Answer$[256]
DIM Center$[256]
DIM Name$[256]
DIM Descr2$[256]

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

Descr2$= JoinStrings("WORKPLANE: ",Descr$," Workplane")
Stat= GenerateVarName(WsTransformEnt,Name$)
RecordFunctionOn
Stat= RecordDescr(Descr2$)
Stat=SetWorkplane
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
Trans1=AngleTransform(Pc,Xangle,Yangle,Zangle)
Stat=SetWorkplane(Trans1)
Stat= NameEntity(Trans1,Name$)
Stat=DeleteEntity(Pc)
RecordFunctionOff
CloseVarRecord
IF IsRulerOn THEN PlotAll
SUBEXIT
Leave:!
Leave_script= 1
SUBEND

