
! script name: csplines.d/csplinecurve
! topic: construct cubic spline through a group of points

!A
!A NAME:
!A
!A 	Cubic Spline
!A
!A DESCRIPTION:
!A
!A 	This script constructs a cubic spline through a group of points.
!A 	A cubic spline maintains tangency between the points and approximate
!A 	continuity of the curvature.
!A
!A~ image location - images/csplinecurve.jpg
!A
!B INSTRUCTIONS:
!B
!B 	POINT #  ABS X,Y: Enter the X,Y coordinates of the point.  Remember,
!B 		the order of the points is important.
!B 		NOTE: See XYZ INPUT OPTIONS.
!B
!B NOTE:
!B
!B 	Enter the coordinates of the last point twice to generate the cubic
!B 	spline through the points.
!B
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: cubic spline ")

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

ActivateUndo
Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

Group_loop:!
Group=CreateGroup("TEMPGROUP")
Pt_count=1

Point_loop:!

Ploop:Pt=XyzInput("POINT #"&VAL$(Pt_count)&" ",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave

IF Pt_count>1 THEN
	Dist=DistTwoPts(Pt,Plast)
	IF (Dist>0.0001) THEN
		Stat=AddToGroup(Group,Pt)
		Stat=SetLastPoint(Pt,WsXyzInput)
		Stat= SetSelectedFlag(Pt,WsTrue)
        	Stat= PlotEntity(Pt)
		Plast=Pt
		Pend=Pt
		Pt_count=Pt_count+1
		Stat= DeleteEntity(Spline_ent)
		Spline_ent=CSCurvePtGrp(Group,Pstart,0,Pend,0)
		GOTO Point_loop
	END IF
ELSE
	Stat=AddToGroup(Group,Pt)
	Stat=SetLastPoint(Pt,WsXyzInput)
	Stat= SetSelectedFlag(Pt,WsTrue)
        Stat= PlotEntity(Pt)
	Plast=Pt
	Pstart=Pt
	Pt_count=Pt_count+1
	GOTO Point_loop
END IF


Stat= DeleteEntity(Spline_ent)
Spline_ent=CSCurvePtGrp(Group,Pstart,0,Pend,0)
Stat=DeleteGroup(Group)
Stat=DeleteEntity(Pt)

ActivateUndo
Stat= AdvanceUndoNumber("spline curve")
Stat= AddItemToUndo(WsUBCreateMode,Spline_ent)
LOAD "csplines.d/csplinecurve"
GOTO Group_loop

Leave:!
IF ValidGeometry(Spline_ent) THEN
	ActivateUndo
	Stat= AdvanceUndoNumber("spline curve")
	Stat= AddItemToUndo(WsUBCreateMode,Spline_ent)
END IF
Stat=DeleteEntity(Pt)
Stat=DeleteGroup(Group)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END

