
! script name: vsplines.d/beziercurve
! var_topic: construct bezier curve through group of points

!A
!A NAME:
!A
!A 	Bezier Curve	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script constructs a bezier curve using a group of points
!A 	as control points.
!A
!A 	A bezier curve is a polynomial, the degree of the polynomial is
!A 	equal to the number of points minus one, and is tangent at both
!A 	ends of the curve, and is influenced by the weight of the other
!A 	points.
!A
!A~ image location - images/beziercurve.jpg
!B INSTRUCTIONS:
!B
!B 	CONTROL POINT #  ABS X,Y: Enter the X,Y
!B 		coordinates of the point.  Remember, the order of
!B 		the points is important.  NOTE: See XYZ INPUT OPTIONS.
!B
!C NOTES:
!C
!C 	Enter the last point twice to end the point input mode.
!C
!# see also - points.d/xyzinput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: bezier curve ")

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

Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

ActivateUndo
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("BEZIER: bezier curve ")
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=BSplinePtGrp(Egroup,0,3)
Stat= NameEntity(S1,Name$)
Stat=DeleteEntity(Egroup)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(S1) THEN
	DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("bezier curve")
	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
