proc ParametricLine { } {    
    global ParametricPriv 
    set w .gid.parametric
    #Helice cónica, 3 vueltas completas, de radio 4 a 1, altura=5
    #set ParametricPriv(xt) "(4*(1-t)+1*t)*sin(3*6.2832*t)"
    #set ParametricPriv(yt) "(4*(1-t)+1*t)*cos(3*6.2832*t)"
    #set ParametricPriv(zt) "5*t"
    set ParametricPriv(xt) ""
    set ParametricPriv(yt) ""
    set ParametricPriv(zt) ""    
    set ParametricPriv(NumberPointsT) 50
    set ParametricPriv(t0) 0.0
    set ParametricPriv(t1) 1.0  
    set ParametricPriv(OnlyPoints) 0  
    #creación de la ventana principal
    InitWindow $w [_ "Create Parametric Line"] ParametricWindowGeom
    #creación de los witgets
    frame $w.frmEquations
    label $w.frmEquations.lx -text "X(t)"
    entry $w.frmEquations.ex -relief sunken -width 15 -textvariable ParametricPriv(xt)
    label $w.frmEquations.ly -text "Y(t)"
    entry $w.frmEquations.ey -relief sunken -width 15 -textvariable ParametricPriv(yt)
    label $w.frmEquations.lz -text "Z(t)"
    entry $w.frmEquations.ez -relief sunken -width 15 -textvariable ParametricPriv(zt)
    GidHelp "$w.frmEquations " \
	[_ "Parametric equations, valid all tcl funcions:\
           + - * / %%\
           abs cosh log sqrt acos double log10 srand asin exp pow\
           tan atan floor rand tanh atan2 fmod round ceil hypot sin\
           cos int sinh."]
    frame $w.frmParams
    label $w.frmParams.lt0 -text "t0"
    entry $w.frmParams.et0 -relief sunken -width 6 -textvariable ParametricPriv(t0)
    label $w.frmParams.lt1 -text "t1"
    entry $w.frmParams.et1 -relief sunken -width 6 -textvariable ParametricPriv(t1)
    
    frame $w.frmDivisions
    label $w.frmDivisions.lnpt -text [_ "Nºpoints"]
    UpDownEntry $w.frmDivisions.udnpt -variable ParametricPriv(NumberPointsT)\
	    -entry-options "-relief sunken -width 4" \
	    -up-command "IncrementNPT" -down-command "DecrementNPT" \
	    -button-options "-bd 1" \
	    -up-color blue -down-color blue
    label $w.frmDivisions.lOnlyPoints -text [_ "Create only points"]
    checkbutton $w.frmDivisions.chkOnlyPoints -variable ParametricPriv(OnlyPoints)
    
    set def_back [$w cget -background]
    frame $w.frmButtons -bg [CCColorActivo $def_back]
    button $w.frmButtons.btnApply -text [_ "Apply"] \
	    -command "CreateParametricLine $w" \
	    -underline 0 -width 6
    button $w.frmButtons.btnclose -text [_ "Close"] \
	    -command "destroy $w" \
	    -underline 0 -width 6
    #empaquetamiento de los widgets
    grid $w.frmEquations -padx 5 -pady 5 -sticky ew    
    grid $w.frmEquations.lx $w.frmEquations.ex -sticky e
    grid $w.frmEquations.ly $w.frmEquations.ey -sticky e
    grid $w.frmEquations.lz $w.frmEquations.ez -sticky e
    grid conf $w.frmEquations.ex $w.frmEquations.ey $w.frmEquations.ez -sticky ew
    grid columnconf $w.frmEquations 1 -weight 1

    grid $w.frmParams -padx 5 -pady 5 -sticky ew   
    grid $w.frmParams.lt0 $w.frmParams.et0 $w.frmParams.lt1 $w.frmParams.et1 -sticky e
    grid conf $w.frmParams.et0  $w.frmParams.et1 -sticky ew
    grid columnconf $w.frmParams "1 3" -minsize 6 -weight 1

    grid $w.frmDivisions -padx 5 -pady 5 -sticky w    
    grid $w.frmDivisions.lnpt $w.frmDivisions.udnpt -sticky w
    grid $w.frmDivisions.lOnlyPoints $w.frmDivisions.chkOnlyPoints -sticky w
    grid columnconf $w.frmDivisions 0 -weight 1
    
    grid $w.frmButtons -sticky ews -columnspan 7
    grid $w.frmButtons.btnApply $w.frmButtons.btnclose -padx 5 -pady 6
    grid columnconf $w "0" -weight 1
    grid rowconfigure $w "3" -weight 1
    
    focus $w.frmButtons.btnApply
    
    bind $w <Alt-c> "tkButtonInvoke $w.frmButtons.btnclose"
    bind $w <Escape> "tkButtonInvoke $w.frmButtons.btnclose"
    bind $w <Return> "tkButtonInvoke $w.frmButtons.btnApply"

    wm minsize $w 140 175
}

proc IncrementNPT { } {
    global ParametricPriv
    set ParametricPriv(NumberPointsT) [expr $ParametricPriv(NumberPointsT)+1]    
}

proc DecrementNPT { } {
    global ParametricPriv
    if { $ParametricPriv(NumberPointsT) > 2} {
	set ParametricPriv(NumberPointsT) [expr $ParametricPriv(NumberPointsT)-1]
    } else {
	bell
    }
}

proc CreateParametricLine { w } {
    global ParametricPriv 

    set fx $ParametricPriv(xt)
    set fy $ParametricPriv(yt)
    set fz $ParametricPriv(zt)

    if { ![string is double -strict $ParametricPriv(t0)] } {
	tk_messageBox -message [_ "Error, t0 must be a real number"] -type ok 
	return
    }
    if { ![string is double -strict $ParametricPriv(t1)] } {
	tk_messageBox -message [_ "Error, t1 must be a real number"] -type ok
	return
    }
    if { ![string is integer -strict $ParametricPriv(NumberPointsT)] } {
	tk_messageBox -message [_ "Error, Nº points must be a positive integer >=2"] -type ok
	return
    }
    if { $ParametricPriv(NumberPointsT) < 2 } {
	tk_messageBox -message [_ "Error, Nº points must be a positive integer >=2"] -type ok
	return
    }
    # sustitución de t por $t en las fórmulas
    foreach {f} {fx fy fz} {
	regsub -all -nocase {\mt\M} [set $f] {$t} $f
    }

    #desactivar el redibujado de cada punto creado y cursor en espera
    $w conf -cursor watch
    .central.s waitstate 1
    update
    .central.s disable graphics 1
    .central.s disable windows 1
    .central.s disable graphinput 1

    if { [ catch {
	set a $ParametricPriv(t0)
	set b [expr  ($ParametricPriv(t1)-$ParametricPriv(t0))/($ParametricPriv(NumberPointsT)-1.0)]
	set oldvalue [.central.s info variables CreateAlwaysNewPoint]
	.central.s process escape escape escape Utilities Variables CreateAlwaysNewPoint  1
	if { $ParametricPriv(OnlyPoints) } {
	    .central.s process escape escape escape Geometry Create Point
	} else {
	    .central.s process escape escape escape Geometry Create NurbsLine
	}
	for { set i 0 } { $i < $ParametricPriv(NumberPointsT) } { incr i} {
	    set t [expr $a+$i*$b]
	    set x [expr $fx]
	    set y [expr $fy]
	    set z [expr $fz]
	    .central.s process $x $y $z
	}
	.central.s process escape
	.central.s process escape escape escape Utilities Variables \
		CreateAlwaysNewPoint $oldvalue
	.central.s process escape escape escape
    } failstring ] } {
	tk_messageBox -message \
		[_ "Error, there is an error in the mathematical expression '%s'" $failstring] \
		-type ok -parent $w
    }

    #reactivar el dibujado y cursor en modo normal    
    .central.s disable graphics 0
    .central.s disable windows 0
    .central.s disable graphinput 0

    .central.s process redraw
    $w conf -cursor ""
    .central.s waitstate 0
}
