
! script name: vzplanes.d/flatsurfwkpln
! var_topic: define workplane parallel to to a planar surface

!A
!A NAME:
!A
!A 	Workplane: Pt: parallel to a Planar Surface  VARIATIONAL
!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
!B 		coordinates of the point to be used as the
!B 		reference point for making the new workplane.
!B 		NOTE: See XYZ INPUT OPTIONS.
!B
!B 	PICK PLANAR SURFACE: pick the surface that
!B 		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]
DIM Surface$[256]
DIM Name$[256]
DIM Center$[256]

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

DeActivateUndo
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)
GOSUB Orient_normal
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= PlotEntity(Center$)
IF IsRulerOn THEN PlotAll

Leave:!
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 (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

