
! script name: 3dintersect.d/contintsurf
! 3dtopic: contour intersect surface with offset.

!A
!A NAME:
!A
!A 	Contour Intersect Surface with offset
!A
!A DESCRIPTION:
!A
!A 	This script will generate a point at the intersection of a contour and
!A 	a surface with an offset. The pick location will determine 
!A	which intersection is to be  generated.
!A
!B INSTRUCTIONS:
!B
!B 	PICK CONTOUR: Pick the contour.
!B
!B 	PICK SURFACE: Pick the the surface.
!B
!B	OFFSET FOR SURFACE: Enter the offset amount for surface .
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: contour intersect surface ")

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

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
Stat=Setup3dCutter(WsNullTool,0.0,0.0,0.0,0.0,0.0)

Intersect2: StdWorkplane

Pick_contour:!
Stat=SetPickMask(WsPickNoSurf)
Stat=SetInputFormat("n0","")
Contour=PickGroupEnt("PICK CONTOUR ")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Contour) THEN Pick_contour

Pick_surf:!
Stat=SetPickMask(WsPickSurfOnly)
Surface=PickEntity("PICK SURFACE",Answer$)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Surface) THEN Pick_surf
UVpoint=LastUVSurfPick(Surface)
IF NOT ValidGeometry(UVpoint) THEN Pick_surf
U=XPointValue(UVpoint)
V=YPointValue(UVpoint)

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

norm_sign=1
IF ABS(Offset)>0.0 THEN
	GOSUB Orient_normal
END IF
Normal_sign=norm_sign
Stat=DeleteEntity(UVpoint)

Stat=SetPickMask(WsPickGeometry)

Group=ContourIntSurf(Contour,Surface,Tol,Offset,Normal_sign)

ActivateUndo
Stat= AdvanceUndoNumber("intersect")
Stat= AddGroupToUndo(WsUBCreateMode,group,0,WsTrue)

Stat= DeleteEntity(Group)
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
        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(UVpoint)
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
