
! script name: vsolids.d/planeperp
! var_topic: construct plane perpendicular to an existing plane

!A
!A NAME:
!A
!A 	Plane: Perpendicular	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script allows a plane to be defined 
!A	perpendicular to an existing plane.
!A
!B INSTRUCTIONS:
!B	
!B 	PICK PERPENDICULAR PLANE: Pick the perpendicular plane.
!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
!# see also - points.d/xyzinput.hlp

Stat=ChangeLabelStr("ScriptNameStatus","Function: plane perpendicular ")

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

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


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

Plane_loop:!

Stat=SetInputFormat("n4","")
Plane1=PickEntity("PICK PERPENDICULAR PLANE")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Plane1) THEN Plane_loop
Stat= EntityNameCheck(Plane1,Plane$)
Etype=EntityType(Plane1)
IF Etype<>WsPlaneSolidEnt THEN Plane_loop

XLength=SolidLength(Plane1,1)
YLength=SolidLength(Plane1,2)
GOSUB Which_edge
IF Surf_edge=0 THEN Plane_loop
GOSUB Orient_normal

Stat= GenerateVarName(WsPlaneSolidEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("PLANE: Perpendicular")
Stat= SetRecDelLastExec
V4= FreeParameter("Xlength",Xlength)
V5= FreeParameter("Ylength",Ylength)
V6= FreeParameter("Side",Norm_sign)
V7= FreeParameter("Which edge",Surf_edge)
Plane2=PlaneAnglePlane(Plane$,V4,V5,90.0,V6,V7)
Stat= NameEntity(Plane2,Name$)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(Plane2) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("plane")
	Stat= AddItemToUndo(WsUBCreateMode,Plane2)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
GOTO Plane_loop

Which_edge: Surf_edge=0
Pt=LastUVSurfPick(Plane1)
IF NOT ValidGeometry(Pt) THEN RETURN
U=XPointValue(Pt)
V=YPointValue(Pt)
Stat=DeleteEntity(Pt)
Surf_edge=ClosestSurfEdge(Plane1,U,V)

SELECT Surf_edge
CASE 2,4
	A=Xlength
	Xlength=Ylength
	Ylength=A
END SELECT
RETURN

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)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
