
! script name: zplanes.d/flatsurfwkpln
! topic: define workplane parallel to to a planar surface

!A
!A NAME:
!A
!A 	Workplane: Pt: parallel to a Planar Surface
!A
!A DESCRIPTION:
!A
!A 	This script allows a workplane to be made parallel to a planar surface
!A
!B INSTRUCTIONS:
!B
!B 	WORKPLANE ZERO ABS X,Y,Z: Enter the X,Y,Z coordinates of the point to
!B 		be used as the reference point for making the new workplane.
!B 		NOTE: See XYZ INPUT OPTIONS.
!B
!B 	PICK PLANAR SURFACE: pick the surface that the plane is parallel to.
!B
!C NOTE:
!C
!C	This script will establish a workplane which is parallel
!C	to a planar surface. The planar surface may be a plane, disk, or
!C	a ruled, nurb or bicubic which is flat.
!C
!C	This can be a helpful feature for machining a 3d model with
!C	a rotary or five axis machine tool.
!C

!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: workplane: pt: parallel to surface ")

DIM Answer$[256]
DIM Destination$[256]
DEG

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

StdWorkplane
stat= SetInputFormat("n4","")

Stat=SetPickMask(WsPickNoSurf)
Pzero=XyzInput("WORKPLANE ZERO ",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave

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

UVpoint=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint) THEN Pick_surface
U=XPointValue(UVpoint)
V=YPointValue(UVpoint)

GOSUB Orient_normal
Normal_sign=Norm_sign

PlotOff
L1=SurfNormalLine(Surface,0.5,0.5,1.0,Normal_sign)
T1=LineToVertTrans(L1,Pzero)
Stat=DeleteEntity(Pzero)
Stat=DeleteEntity(L1)
Stat=DeleteEntity(UVPoint)
PlotOn

Stat=SetWorkplane(T1)
IF IsRulerOn THEN PlotAll

WplaneEnt= FindNamedVar("current work area",123456)
IF ValidGeometry(WplaneEnt) THEN Stat= DeleteEntity(WplaneEnt)


Leave:!
Stat=SetPickMask(WsPickGeometry)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat=DeleteEntity(Pzero)
Stat=DeleteEntity(L1)
Stat=DeleteEntity(UVPoint)

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

Orient_normal:!
        GOSUB ShowNormalDir
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)
RETURN

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

