
! script name: 3dintersect.d/osintersect
! 3dtopic: generate path of intersection of two offset surfaces

!A
!A NAME:
!A
!A 	Offset Intersect
!A
!A DESCRIPTION:
!A
!A 	This script will generate the intersection of two surfaces
!A 	with an offset applied to each surface.
!A
!B INSTRUCTIONS:
!B
!B 	PICK SURFACE #1: Pick the first surface.
!B
!B	OFFSET FOR SURFACE #1: Enter the offset amount for surface #1.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick the side of surface #1 the offset is
!B 		applied on.  The normal vectors will be displayed to allow the
!B 		side to be picked.
!B
!B 	PICK SURFACE #2: Pick the second surface.
!B
!B	OFFSET FOR SURFACE #2: Enter the offset amount for surface #2.
!B
!B 	PICK WHICH SIDE OF SURFACE: Pick the side of surface #2 the offset is
!B 		applied on.  The normal vectors will be displayed to allow the
!B 		side to be picked.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: offset intersect surfaces ")

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

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

Start:!
Normal_color=3
Direction_color=4

StdWorkplane

Stat=Setup3dCutter(WsNullTool,0.0,0.0,0.0,0.0,0.0)

Intersect2: StdWorkplane

Pick_loop:!

Pick_surf1: Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE #1",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Pick_loop

UVpoint1=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint1) THEN Pick_surf1
U=XPointValue(UVpoint1)
V=YPointValue(UVpoint1)

Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Offset1)
Offset1=GetValue("OFFSET FOR SURFACE #1")
IF ExitScript(Destination$) THEN Leave

norm_sign=1
IF ABS(Offset1)>0.0 THEN
	GOSUB Orient_normal
END IF
Surface1=Surface
Normal_sign1=norm_sign

Pick_surf2: Stat=SetPickMask(WsPickSurfOnly)
Stat=SetInputFormat("n0","")
Surface=PickEntity("PICK SURFACE #2",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Pick_loop

UVpoint2=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint2) THEN Pick_surf2
U=XPointValue(UVpoint2)
V=YPointValue(UVpoint2)

Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Offset2)
Offset2=GetValue("OFFSET FOR SURFACE #2")
IF ExitScript(Destination$) THEN Leave

norm_sign=1
IF ABS(Offset2)>0.0 THEN
	GOSUB Orient_normal
END IF
Surface2=Surface
Normal_sign2=norm_sign

s1=Normal_sign1
s2=Normal_sign2
path_group=IntersectSurfs(Surface1,S1,Surface2,S2,Tol,Offset1,Offset2)

ActivateUndo
Stat= AdvanceUndoNumber("intersect curves")
Stat= AddGroupToUndo(WsUBCreateMode,Path_group,1,WsTrue)

Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)

GOTO Intersect2

Orient_normal:!
	GOSUB ShowNormalDir
	Stat=SetInputFormat("n4","")
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
	IF IsDirReversed(Surface) THEN Norm_sign= -Norm_sign
	Stat=DeleteEntity(Forward_normal)
	Stat=DeleteEntity(Reverse_normal)
	Stat=DeleteEntity(Pt)
	Stat=DeleteEntity(Pforward)
	Stat=DeleteEntity(Preverse)
RETURN

ShowNormalDir:!
	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)
	
	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=SetInputFormat("n4","")
Stat=DeleteEntity(UVpoint1)
Stat=DeleteEntity(UVpoint2)
Stat=DeleteEntity(Forward_normal)
Stat=DeleteEntity(Reverse_normal)
Stat=DeleteEntity(Pt)
Stat=DeleteEntity(Pforward)
Stat=DeleteEntity(Preverse)
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
