
! script name: solids.d/planeangle
! 3dtopic: construct plane at angle to an existing plane

!A
!A NAME:
!A
!A 	Plane: Angle with Plane
!A
!A DESCRIPTION:
!A
!A 	This script allows a plane to be defined at an angle to an existing
!A 	plane.
!A
!B INSTRUCTIONS:
!B	
!B 	ANGLE: Enter the angle the plane is to be constructed at,
!B 		relative to the existing plane.
!B
!B 	PICK PLANE: Pick the plane.  The edge of the plane
!B 		closest to the pick point will be used as a
!B 		side for the plane to be constructed.
!B
!B 	PICK WHICH SIDE OF PLANE: Pick the side of the plane
!B 		for the construction.
!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 angle ")

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

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

Stat=SetInputFormat("n4","")
Input_angle:!
Angle=InputAngle("ANGLE ")
IF ExitScript(Destination$) THEN Leave

Plane_loop:!

Stat=SetInputFormat("n4","")
Plane1=PickEntity("PICK 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

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 PLANE",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
