
! script name: pksurfaces.d/pkalignnormal
! solids: align sheet body normal vector

!A
!A NAME:
!A
!A 	Sheet Body: Align Normal Vectors
!A
!A DESCRIPTION:
!A
!A 	This script aligns normal vector direction on a sheet body
!A
!B INSTRUCTIONS:
!B
!B 	PICK FACE ON SHEET BODY, Proceed: Pick a face on the sheet body
!B
!B 		P - Proceed may be used to exit the script.
!B
!B 	PICK WHICH SIDE OF BODY: Pick which side of the sheet body the
!B 		normal vectors are to be aligned on.
!B
!B NOTE:
!B
!B	Solid bodies automatically have their normals pointing outward.
!B	Sheet bodies have no standard normal direction, it is determined
!B	by the method of creation. The normals need to be aligned properly
!B	for the faces of the sheet to be machined correctly.
!B
!# see also - htdocs/help.d/parasolids.htm

Stat=ChangeLabelStr("ScriptNameStatus","Function: align normal direction ")

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

ActivateUndo
Stat= GLPlotOn
Normal_len=NamedVarVal("3D Normal Length",Ws3DSystemVars,1.0)
Forward_color=3
Reverse_color=4
Start:!

Surface=WPKPickEntity("PICK FACE ON SHEET BODY ",WsPKPickFace)
IF (ExitScript(Destination$)) THEN Leave
IF NOT WPKIsEntity(Surface) THEN Start

Body= WPKParentBody(Surface)
Body_type= WPKBodyType(Body)
IF Body_type<> WsPKSheetBody THEN Body_loop

UVpoint=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint) THEN Start
U=XPointValue(UVpoint)
V=YPointValue(UVpoint)
Stat=DeleteEntity(UVPoint)


GOSUB Orient_normal
IF Norm_sign= -1 THEN 
	WPKClearErrorFlag
	Stat=WPKReverseNormals(Body)

	ActivateUndo
	Stat= AdvanceUndoNumber("align sheet normal")
	Stat= AddItemToUndo(WsUBPKSessionMark)
	WPKErrorCheck
END IF

GOTO Start

Orient_normal:!
	GOSUB ShowNormalDir
Pick_normal:L1=PickSpecificEnt("PICK WHICH SIDE OF BODY",WsPickLines)
	IF ExitScript(Destination$) THEN Leave
	IF NOT ValidGeometry(L1) THEN Orient_normal
	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:!
	if(IsDirReversed(Surface)=WsFalse)THEN
		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)
	ELSE
		Forward_normal=SurfNormalLine(Surface,U,V,Normal_len,WsReverse)
		Pforward=ParameterPt(Forward_normal,1.0)
		Reverse_normal=SurfNormalLine(Surface,U,V,Normal_len,WsForward)
		Preverse=ParameterPt(Reverse_normal,1.0)
	END IF

	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

Leave:!
Stat=SetPickMask(WsPickAll)
Stat=DeleteEntity(Nline)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat=DeleteEntity(UVPoint)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
