
! script name: solids.d/planeperp
! 3dtopic: construct plane perpendicular to an existing plane

!A
!A NAME:
!A
!A 	Plane: Perpendicular
!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
!B 	PICK WHICH SIDE OF SURFACE: Pick the side of the plane the
!B 		perpendicular plane will be 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
!# see also - points.d/xyzinput.hlp

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

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

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 ValidGeometry(plane1) THEN Plane_loop
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

Angle= 90.0
Plane2=PlaneAnglePlane(Plane1,Xlength,Ylength,Angle,Norm_sign,Surf_edge)

ActivateUndo
Stat= AdvanceUndoNumber("plane")
Stat= AddItemToUndo(WsUBCreateMode,Plane2)
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
