
! script name: vsplines.d/cscurveent
! var_topic: construct cubic spline curve ent through a group of points

!A
!A NAME:
!A
!A 	Cubic Spline Curve Ent	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script constructs a cubic spline curve ent through 
!A 	a group of points.
!A
!A 	A cubic spline maintains tangency between the points and approximate
!A 	continuity of the curvature.
!A
!A~ image location - images/csplinecurve.jpg
!B INSTRUCTIONS:
!B
!B 	POINT #  ,Proceed: Pick a point for the cubic spline.
!B 		This question will be repeated until Proceed is selected.
!B
!B 		P - The Proceed option will then construct a cubic spline
!B 		    using the points selected.
!B

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

DIM Answer$[256]
DIM Destination$[256]
DIM Point$[256]
DIM Pstart$[256]
DIM Pend$[256]
DIM Name$[256]
DEG

Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)
Group_loop:!
Group=CreateGroup("TEMPGROUP")
Pt_count=1

Point_loop:!

Stat= SetInputFormat("a1",",P,")
Pick_point:Pt=PickEntity("POINT #"&VAL$(Pt_count)&" , Proceed ",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "P" THEN Make_spline
IF NOT ValidDependency(Pt) THEN Pick_point
Stat= EntityNameCheck(Pt,Point$)
Etype= EntityType(Pt)
IF Etype<> WsPointEnt THEN Point_loop

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
		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

Make_spline:!
Stat= EntityNameCheck(Pstart,Pstart$)
Stat= EntityNameCheck(Pend,Pend$)
Num_elements= GroupCount(Group)

Stat= GenerateVarName(WsCSplineCurveEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("CSPLINE: cubic spline curve ent")
Stat= SetRecDelLastExec
Egroup= CreateGroup("Picked Points")
FOR I=1 TO Num_elements
	RecordFunctionOff
        Pt= GroupElement(Group,I)
        Stat= EntityNameCheck(Pt,Element$)
        IF ValidDependency(Pt) THEN
                RecordFunctionOn
                Stat= AddToGroup(Egroup,Element$)
                RecordFunctionOff
        END IF
NEXT I
RecordFunctionOn
S1=CSCurvePtGrp(Egroup,Pstart$,0,Pend$,0)
Stat= NameEntity(S1,Name$)
Stat=DeleteEntity(Egroup)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(S1) THEN
	DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("cubic spline")
	Stat= AddItemToUndo(WsUBCreateMode,S1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

Stat= SetSelectedFlag(Group,WsFalse)
Stat= PlotEntity(Group)
Stat= DeleteEntity(Group)
GOTO Group_loop

Leave:!
Stat= SetSelectedFlag(Group,WsFalse)
Stat= PlotEntity(Group)
Stat= DeleteEntity(Group)
stat= SetInputFormat("n4","")
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END
