
! script name: vsolids.d/planeparallel
! var_topic: construct plane parallel to an existing plane

!A
!A NAME:
!A
!A 	Plane: Parallel Distance	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script allows a plane to be defined at a specified distance
!A	parallel to an existing plane.
!A
!B INSTRUCTIONS:
!B	
!B	PICK PARALLEL PLANE: Pick the parallel plane.
!B
!B	PARALLEL DISTANCE: Input the parallel distance, +/- switches sides.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick the side the parallel plane will be
!B 		constructed on.
!B
!C NOTES:
!C
!C 	The U contours of a plane are the lines parallel to the length of
!C 	the plane ( parallel to X axis in an unoriented plane ) and the V
!C 	contours are the lines parallel to the width of the plane ( parallel
!C 	to Y axis in an unoriented plane ).
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: plane parallel distance ")

DIM Answer$[256]
DIM Destination$[256]
DIM Dist_var$[256]
DIM Plane$[256]
DIM Name$[256]

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

ActivateUndo
Dist=NamedVarVal("parallel distance",WsSystemVars,.25)
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Forward_color=3
Reverse_color=4

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

Plane_loop:!
Stat=SetInputFormat("n4","")
Plane1=PickEntity("PICK PARALLEL PLANE")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Plane1) THEN Plane_loop
Stat= EntityNameCheck(Plane1,Plane$)
Etype=EntityType(Plane1)
IF NOT ((Etype=WsPlaneSolidEnt)OR(Etype=WsDiskSolidEnt)) THEN Plane_loop

Pt=LastUVSurfPick(Plane1)
IF NOT ValidGeometry(Pt) THEN RETURN
U=XPointValue(Pt)
V=YPointValue(Pt)
Stat=DeleteEntity(Pt)
GOSUB Orient_normal
Perp_dist=Norm_sign*Dist

Stat= GenerateVarName(WsPlaneSolidEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("PLANE: Parallel")
Stat= SetRecDelLastExec
IF No_dist_var THEN
        V1= FreeParameter("Distance",Dist)
ELSE
        V1= FreeParameter("Distance",Dist_var$)
END IF
V2= FreeParameter("Side",Norm_sign)
P1=ParallelPlane(Plane$,V1,V2)
Stat= NameEntity(P1,Name$)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(P1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("plane")
	Stat= AddItemToUndo(WsUBCreateMode,P1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Plane_loop

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(Plane1,U,V,Normal_len,WsForward)
	Pforward=ParameterPt(Forward_normal,1.0)
	Stat=SetColor(Forward_normal,Forward_color)
	Stat=SetColor(Pforward,Forward_color)

	Reverse_normal=SurfNormalLine(Plane1,U,v,Normal_len,WsReverse)
	Preverse=ParameterPt(Reverse_normal,1.0)
	Stat=SetColor(Reverse_normal,Reverse_color)
	Stat=SetColor(Preverse,Reverse_color)
RETURN

Leave:!
Stat=DeleteEntity(P1)
Stat=DeleteEntity(P2)
Stat=DeleteEntity(P3)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Vdist=CreateVariable("parallel distance",WsSystemVars,Dist)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
