
! script name: 3dintersect.d/edgecurve
! 3dtopic: generates a curve along a surface edge

!A
!A NAME:
!A
!A 	Intersect: Edge Curve
!A
!A DESCRIPTION:
!A
!A 	This script generates a curve along the edge of a surface.
!A
!B INSTRUCTIONS:
!B
!B 	PICK SURFACE: Pick the surface along the edge desired.
!B
!C NOTES:
!C
!C 	When selecting the surface edge position the mouse slightly toward the
!C 	interior of the surface.  Make sure that the edge pick is clearly along
!C 	the edge desired. Avoid selecting near the corners.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: edge curve ")

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

Tol=NamedVarVal("3D Intersect Tol",Ws3DSystemVars,0.005)

ActivateUndo
Start:!
StdWorkplane
Stat=Setup3dCutter(WsNullTool,0.0,0.0,0.0,0.0,0.0)

Pick_loop:!

Intersect2: StdWorkplane

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

GOSUB Which_edge
IF NOT Surf_edge THEN Pick_loop
Which1=Surf_edge
Direction=Edge_dir

Stat=SetPickMask(WsPickGeometry)
Stat=SetInputFormat("n0","")

Path=EdgeCurve(surface,Which1,tol,Direction)

ActivateUndo
Stat= AdvanceUndoNumber("edge curve")
Stat= AddGroupToUndo(WsUBCreateMode,Path,1,WsTrue)
GOTO Intersect2

Which_edge: Surf_edge=0
Edge_dir=WsForward
Pt=LastUVSurfPick(Surface)
IF NOT ValidGeometry(Pt) THEN RETURN
U=XPointValue(Pt)
V=YPointValue(Pt)
Stat=DeleteEntity(Pt)
Surf_edge=ClosestSurfEdge(Surface,U,V)

SELECT Surf_edge
    CASE 1,3
	Edge_dir=WsForward
	IF U>0.5 THEN Edge_dir=WsReverse
    CASE 2,4
	Edge_dir=WsForward
	IF V>0.5 THEN Edge_dir=WsReverse
END SELECT
RETURN

Leave:!
Stat=SetInputFormat("n0","")
Stat=SetPickMask(WsPickGeometry)
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
