Bsoft  2.1.0
Bernard's software package
bshow_filament.tcl
Go to the documentation of this file.
1 ##
2 # @file bshow_filament.tcl
3 #
4 # @brief Procedures to create, move and destroy filament selections
5 #
6 # @author Bernard Heymann
7 # @date Created: 20020808
8 # @date Modified: 20180410
9 
10 set show_filaments 1
11 set show_node_labels 1
12 set show_profiles 0
13 set filament_width 40
14 set filament_node_radius 10
15 set boxing_interval 20
16 set helix_rise 0
17 set helix_angle 0
18 set node_selected []
19 set fil_plot_width 350
20 set fil_plot_height 200
21 
22 ## @brief Dialog box to manipulate filaments on the main canvas
23 #
24 
25 proc Filaments { } {
26  global project_item theimg imgtype
27  global helv12
28  global filament_width filament_node_radius boxing_interval
29  global helix_rise helix_angle
30  global fil_plot_width fil_plot_height
31 
32  if ![Bimage exists $theimg] {
33  tk_dialog .dialog "No image in memory!" \
34  {Please read in an appropriate image first.} \
35  info 0 {OK}
36  return
37  }
38 
39  set c [getImageCanvas $theimg]
40  if { $filament_width < 1 } { set filament_width 40 }
41  if { $filament_node_radius < 1 } { set filament_node_radius [expr int($filament_width / 4)] }
42  if { $filament_node_radius < 1 } { set filament_node_radius 3 }
43  if { $boxing_interval < 1 } { set boxing_interval [expr int($filament_width / 2)] }
44  if ![Bmg exists] {
45  createMgParam
46  setMgParam
47  }
48  puts "Launching Particles for $c: $project_item"
49 
50  set w .wfil
51  if ![winfo exists $w] {
52  toplevel $w
53  menu $w.menuBar -tearoff 0
54  $w.menuBar add cascade -menu $w.menuBar.fil -label "Filament" -underline 0
55  menu $w.menuBar.fil -tearoff 0
56  $w configure -menu $w.menuBar
57  $w.menuBar.fil add command -label "Help" -underline 0 \
58  -command { showURL "file://$Bsoft/doc/bshow/bshow_fil.html" }
59 # -command { showHelp bshow_filament.hlp }
60  $w.menuBar.fil add command -label "Write filament profile" \
61  -command { filamentProfileWrite } -underline 0
62  $w.menuBar.fil add command -label "Delete selected filament" \
63  -command { filamentDelete } -underline 0
64  $w.menuBar.fil add command -label "Delete all filaments" \
65  -command { filamentDeleteAll } -underline 0
66  $w.menuBar.fil add command -label "Center nodes" \
67  -command { filamentCenterNodes } -underline 0
68  $w.menuBar.fil add command -label "Extract filaments" \
69  -command { filamentExtract } -underline 0
70  $w.menuBar.fil add command -label "Generate particles" \
71  -command { filamentsToParticles } -underline 0
72  $w.menuBar.fil add command -label "Close" \
73  -command "destroy $w" -underline 0
74 
75  frame $w.fil_width
76  label $w.fil_width.tag -font $helv12 -text "Box width" -width 15 -anchor e
77  entry $w.fil_width.entry -width 10 -validate key -vcmd { string is double %P }
78  $w.fil_width.entry insert 0 $filament_width
79 
80  frame $w.node_radius
81  label $w.node_radius.tag -font $helv12 -text "Node radius" -width 15 -anchor e
82  entry $w.node_radius.entry -width 10 -validate key -vcmd { string is double %P }
83  $w.node_radius.entry insert 0 $filament_node_radius
84 
85  frame $w.boxing_interval
86  label $w.boxing_interval.tag -font $helv12 -text "Boxing interval" -width 15 -anchor e
87  entry $w.boxing_interval.entry -width 10 -validate key -vcmd { string is double %P }
88  $w.boxing_interval.entry insert 0 $boxing_interval
89 
90  frame $w.helix_rise
91  label $w.helix_rise.tag -font $helv12 -text "Helix rise per unit" -width 15 -anchor e
92  entry $w.helix_rise.entry -width 10 -validate key -vcmd { string is double %P }
93  $w.helix_rise.entry insert 0 $helix_rise
94 
95  frame $w.helix_angle
96  label $w.helix_angle.tag -font $helv12 -text "Helix angle per unit" -width 15 -anchor e
97  entry $w.helix_angle.entry -width 10 -validate key \
98  -vcmd { expr {[string match {[-+]} %P] || [string is double %P]} }
99  $w.helix_angle.entry insert 0 $helix_angle
100 
101  frame $w.fil
102  label $w.fil.tag -font $helv12 -text "Filaments" -width 15 -anchor e
103  label $w.fil.num -width 10 -relief sunken -bd 1 -font $helv12 -anchor w -text "0"
104 
105  frame $w.node
106  label $w.node.tag -font $helv12 -text "Nodes" -width 15 -anchor e
107  label $w.node.num -width 10 -relief sunken -bd 1 -font $helv12 -anchor w -text "0"
108 
109  frame $w.fil_selected
110  label $w.fil_selected.tag -font $helv12 -text "Selected filament" -width 15 -anchor e
111  label $w.fil_selected.num -width 10 -relief sunken -bd 1 -font $helv12 -anchor w
112 
113  frame $w.node_selected
114  label $w.node_selected.tag -font $helv12 -text "Selected node" -width 15 -anchor e
115  label $w.node_selected.num -width 10 -relief sunken -bd 1 -font $helv12 -anchor w
116 
117  setupMicrographButtons $w.neighbor "micrograph"
118 
119  set g $w.plot
120  canvas $g -width [expr $fil_plot_width + 10] -height [expr $fil_plot_height + 10] \
121  -background white -relief sunken -borderwidth 2
122 
123  frame $w.switches
124  checkbutton $w.switches.filaments -text "Show filaments" -variable show_filaments \
125  -command { filamentsDrawAll }
126  checkbutton $w.switches.labels -text "Show labels" -variable show_node_labels \
127  -command { filamentsDrawAll }
128  checkbutton $w.switches.profiles -text "Show profiles" -variable show_profiles
129 
130  pack $w.node_radius.tag $w.node_radius.entry -side left -pady 5 -padx 10
131  pack $w.fil_width.tag $w.fil_width.entry -side left -pady 5 -padx 10
132  pack $w.boxing_interval.tag $w.boxing_interval.entry -side left -pady 5 -padx 10
133  pack $w.helix_rise.tag $w.helix_rise.entry -side left -pady 5 -padx 10
134  pack $w.helix_angle.tag $w.helix_angle.entry -side left -pady 5 -padx 10
135  pack $w.fil.tag $w.fil.num -side left -pady 5 -padx 10
136  pack $w.node.tag $w.node.num -side left -pady 5 -padx 10
137  pack $w.fil_selected.tag $w.fil_selected.num -side left -pady 5 -padx 10
138  pack $w.node_selected.tag $w.node_selected.num -side left -pady 5 -padx 10
139 # pack $w.neighbor.prev $w.neighbor.next \
140 # -side left -pady 5 -padx 5 -ipadx 5 -ipady 5
141  pack $w.switches.filaments $w.switches.labels $w.switches.profiles \
142  -side left -pady 5 -padx 5
143  pack $w.fil_width $w.node_radius $w.boxing_interval $w.helix_rise \
144  $w.helix_angle $w.fil $w.node \
145  $w.fil_selected $w.node_selected $w.neighbor $w.plot $w.switches -side top -fill x -pady 2
146 
147  } else {
148  wm deiconify $w
149  raise $w
150  }
151  wm title $w "Filaments"
152  wm iconname $w "Filaments"
153  .menuBar.window entryconfigure "Filaments" -state normal
154  bind $c <1> "nodeCreateSelect %x %y"
155  bind $c <2> "filamentCreate %x %y"
156  bind $c <Command-1> "filamentCreate %x %y"
157  bind $c <Control-1> "filamentCreate %x %y"
158  bind $c <Shift-1> "nodeDelete %x %y"
159  bind $c <Shift-2> "nodeDelete %x %y"
160  bind $c <B1-Motion> "nodeMove %x %y"
161  bind $c <B2-Motion> "nodeMove %x %y"
162 # bind $c <ButtonRelease> "filamentsDrawAll"
163 # bind $c <ButtonRelease> "drawSpline %W"
164  bind $w <Return> "Update 0"
165  bind $w <Control-w> { destroy .wfil }
166  bind $w <Command-w> { destroy .wfil }
167 # updateNodeParam
168  filamentsDrawAll
169 }
170 
171 ## @brief Sets the node radius from the dialog box.
172 
173 proc setNodeRadius { } {
174  if ![winfo exists .wfil] { return }
175  global filament_node_radius project_item
176  set filament_node_radius [.wfil.node_radius.entry get]
177  Bmg set $project_item filament_node_radius $filament_node_radius
178 }
179 
180 ## @brief Starts a new filament
181 #
182 # @param x
183 # @param y First node position in canvas coordinates.
184 
185 proc filamentCreate { x y } {
186  global node_selected
187  set node_selected []
188  nodeCreateSelect $x $y
189 }
190 
191 ## @brief Creates or selects a node.
192 #
193 # @param x
194 # @param y Node position in canvas coordinates.
195 
196 proc nodeCreateSelect { x y } {
197  if ![winfo exists .wfil] { return }
198  global theimg
199  global node_selected
200  global tool
201  set snid $node_selected
202  set node_selected [nodeSelect $x $y]
203  if { [llength $node_selected] > 0 } { return }
204  if { $tool != "filament" } { return }
205  set wc [getControlWindow $theimg]
206  setNodeRadius
207  set n [$wc.image.scale get]
208  set mg_item [micrographItem $n]
209  set loc [imageCoordinatesFromCanvas $x $y]
210  set id [Bmg node $mg_item create [lindex $loc 0] [lindex $loc 1] [lindex $loc 2] [lindex $snid 0] [lindex $snid 1]]
211  if { [llength $id] > 0 } {
212  nodeDraw [lindex $id 0] [lindex $id 1]
213 # puts "created ID = $id"
214  }
215  set node_selected $id
216  filamentUpdateSelected
217  filamentsDrawAll
218 # filamentDraw [lindex $node_selected 0]
219 }
220 
221 ## @brief Selects a node on the canvas.
222 #
223 # @param x
224 # @param y Node position in window coordinates.
225 #
226 # @returns node_selected Index of the selected node, 0 if none selected.
227 
228 proc nodeSelect { x y } {
229  if ![winfo exists .wfil] { return }
230  global theimg
231  global node_selected
232  set wc [getControlWindow $theimg]
233  setNodeRadius
234  set n [$wc.image.scale get]
235  set mg_item [micrographItem $n]
236  set loc [imageCoordinatesFromCanvas $x $y]
237 # puts $loc
238  set id [Bmg node $mg_item select [lindex $loc 0] [lindex $loc 1] [lindex $loc 2]]
239  if { [llength $id] > 0 } {
240  nodeDraw [lindex $id 0] [lindex $id 1]
241 # puts "selected ID = $id"
242  }
243  set node_selected $id
244  filamentUpdateSelected
245 # drawSelectedNode $c
246  return $node_selected
247 }
248 
249 ## @brief Moves the selected node
250 #
251 # @param x
252 # @param y Node position in canvas coordinates.
253 
254 proc nodeMove { x y } {
255  global theimg
256  global node_selected
257  global px py
258  if { [llength $node_selected] < 1 } { return }
259  set c [getImageCanvas $theimg]
260  set wc [getControlWindow $theimg]
261  set scale [$wc.scale.scale get]
262  set n [$wc.image.scale get]
263  set ix [expr [$c canvasx $x] / $scale ]
264  set iy [expr [$c canvasy $y] / $scale ]
265  set dx [expr $ix - $px]
266  set dy [expr $py - $iy]
267  set px $ix
268  set py $iy
269 # puts "Moving $dx $dy"
270  set mg_item [micrographItem $n]
271  Bmg node $mg_item move [lindex $node_selected 0] [lindex $node_selected 1] $dx $dy
272  filamentsDrawAll
273 }
274 
275 ## @brief Draws a circle for a node on the canvas.
276 #
277 # @param fid Filament identifier.
278 # @param nid Node identifier.
279 
280 proc nodeDraw { fid nid } {
281  global theimg
282  global node_selected show_node_labels
283  global filament_color filament_first_color filament_select_color
284  set c [getImageCanvas $theimg]
285  set wc [getControlWindow $theimg]
286  set n [$wc.image.scale get]
287  set z [$wc.slice.scale get]
288  set mg_item [micrographItem $n]
289  set loc [Bmg node $mg_item location $fid $nid]
290 # puts "drawing $fid $nid: $loc"
291  set scale [$wc.scale.scale get]
292  set x [expr [lindex $loc 0] * $scale ]
293  set y [expr [yFlip [lindex $loc 1]] * $scale ]
294  set zr [expr abs([lindex $loc 2] - $z) ]
295  set radius [expr [.wfil.node_radius.entry get] * $scale]
296  set xmin [expr $x - $radius ]
297  set xmax [expr $x + $radius + $scale - 1 ]
298  set ymin [expr $y - $radius ]
299  set ymax [expr $y + $radius + $scale - 1 ]
300  set color $filament_color
301  if { $nid == 1 } {
302  set color $filament_first_color
303  $c create text $xmin $y -text $fid -fill blue -anchor e -tags node
304  }
305  if { $fid == [lindex $node_selected 0] && $nid == [lindex $node_selected 1] } {
306  set color $filament_select_color
307  }
308  $c create oval $xmin $ymin $xmax $ymax -outline $color -width 1 -tags node
309  if { $show_node_labels } {
310  $c create text $xmax $y -text $nid -fill $color -anchor w -tags node
311  }
312 }
313 
314 ## @brief Draws a line between two nodes on the canvas.
315 #
316 # @param fid Filament identifier.
317 # @param nid1 Node 1 identifier.
318 # @param nid2 Node 2 identifier.
319 
320 proc nodeLinkDraw { fid nid1 nid2 } {
321  global theimg
322  global filament_color
323  set c [getImageCanvas $theimg]
324  set wc [getControlWindow $theimg]
325  set n [$wc.image.scale get]
326  set z [$wc.slice.scale get]
327  set mg_item [micrographItem $n]
328  set loc1 [Bmg node $mg_item location $fid $nid1]
329  set loc2 [Bmg node $mg_item location $fid $nid2]
330  set scale [$wc.scale.scale get]
331  set x1 [expr [lindex $loc1 0] * $scale ]
332  set y1 [expr [yFlip [lindex $loc1 1]] * $scale ]
333  set x2 [expr [lindex $loc2 0] * $scale ]
334  set y2 [expr [yFlip [lindex $loc2 1]] * $scale ]
335  set fz -1.0
336  set dz [expr [lindex $loc2 2] - [lindex $loc1 2]]
337  if { $dz == 0 } {
338  set fz [expr [lindex $loc2 2] - $z + 0.5]
339  } else {
340  set fz [expr ([lindex $loc2 2] - $z)/($dz*1.0)]
341  }
342  if { $fz >= 0.0 && $fz <= 1.0 } {
343  $c create line $x1 $y1 $x2 $y2 -fill $filament_color -width 1 -tags fil
344  }
345 }
346 
347 ## @brief Draws filament nodes and lines connecting nodes on the canvas
348 #
349 
350 proc filamentsDrawAll { } {
351  global theimg
352  global show_filaments
353  set c [getImageCanvas $theimg]
354  set wc [getControlWindow $theimg]
355  $c delete fil node selnode
356  if { $show_filaments < 1 } { return }
357  set n [$wc.image.scale get]
358  set mg_item [micrographItem $n]
359  set ids [Bmg node $mg_item ids]
360 # puts "Node IDs: $ids"
361  updateNodeParam
362  set pfid 0
363  set pnid 0
364  foreach {fid nid} $ids {
365  nodeDraw $fid $nid
366  if { $pfid == $fid } {
367  nodeLinkDraw $fid $pnid $nid
368  }
369  set pfid $fid
370  set pnid $nid
371  }
372 # updateNodeParam
373 }
374 
375 ## @brief Updates the selected filament and node in the dialog window
376 #
377 
378 proc filamentUpdateSelected { } {
379  if ![winfo exists .wfil] { return }
380  global node_selected project_item
381 # puts "Project item: $project_item"
382  set nfil [Bmg filament $project_item count]
383  set nnode [Bmg node $project_item count]
384  .wfil.fil.num config -text "$nfil"
385  .wfil.node.num config -text "$nnode"
386  if { [llength $node_selected] < 0 } {
387  .wfil.fil_selected.num config -text ""
388  .wfil.node_selected.num config -text ""
389  } else {
390  .wfil.fil_selected.num config -text [lindex $node_selected 0]
391  .wfil.node_selected.num config -text [lindex $node_selected 1]
392  filamentProfilePlot [lindex $node_selected 0] [lindex $node_selected 1]
393  }
394 }
395 
396 ## @brief Deletes the selected node.
397 #
398 # @param x
399 # @param y Node position in window coordinates.
400 
401 proc nodeDelete { x y } {
402  if ![winfo exists .wfil] { return }
403  global theimg node_selected
404  set wc [getControlWindow $theimg]
405  set n [$wc.image.scale get]
406  set mg_item [micrographItem $n]
407  set id [nodeSelect $x $y]
408 # puts "ID to be deleted = $id"
409  if { [llength $id] > 0 } {
410  Bmg node $mg_item delete [lindex $id 0] [lindex $id 1]
411  set node_selected []
412  }
413  filamentsDrawAll
414 }
415 
416 ## @brief Deletes the selected filament.
417 #
418 
419 proc filamentDelete { } {
420  if ![winfo exists .wfil] { return }
421  global theimg node_selected project_item
422  set wc [getControlWindow $theimg]
423  set n [$wc.image.scale get]
424  set mg_item [micrographItem $n]
425  if { [llength $node_selected] > 0 } {
426  Bmg filament $mg_item delete [lindex $node_selected 0]
427  set node_selected []
428  }
429  filamentsDrawAll
430 }
431 
432 ## @brief Deletes all filaments.
433 #
434 
435 proc filamentDeleteAll { } {
436  global theimg project_item
437  set c [getImageCanvas $theimg]
438  Bmg filament $project_item delete all
439  $c delete node selnode
440  $c delete spline
441  $c delete fil
442  filamentUpdateSelected
443 }
444 
445 ## @brief Centers filament nodes.
446 #
447 
448 proc filamentCenterNodes { } {
449  global filament_width project_item
450  set filament_width [.wfil.fil_width.entry get]
451  puts $project_item
452  Bmg filament $project_item center $filament_width
453  filamentsDrawAll
454 }
455 
456 ## @brief Extracts filaments defined in the micrograph parameters.
457 #
458 
459 proc filamentExtract { } {
460  global helv12
461  global filament_width project_item
462 
463  if { [Bmg node $project_item count] < 1 } {
464  tk_messageBox -icon error -type ok -title "Error" -message \
465  "No filaments picked!"
466  return
467  }
468 
469  set w .wfext
470  catch {destroy $w}
471  toplevel $w
472  wm title $w "Extracting filaments"
473  wm iconname $w "ExtFil"
474 
475  label $w.msg -font $helv12 -wraplength 4i -justify left \
476  -text {Select parameters for filament extraction}
477 
478  frame $w.panel
479 
480  frame $w.panel.fil_width
481  label $w.panel.fil_width.tag -font $helv12 -text "Filament width" -width 30 -anchor w
482  entry $w.panel.fil_width.entry -width 10 -relief sunken \
483  -validate key -vcmd { string is double %P }
484  $w.panel.fil_width.entry insert 0 $filament_width
485 
486  frame $w.panel.fil_axis
487  label $w.panel.fil_axis.tag -font $helv12 -text "Filament axis" -width 30 -anchor w
488  radiobutton $w.panel.fil_axis.x -text "x" -variable fil_axis -value 1
489  radiobutton $w.panel.fil_axis.y -text "y" -variable fil_axis -value 2
490  radiobutton $w.panel.fil_axis.z -text "z" -variable fil_axis -value 3
491 
492  frame $w.panel.path
493  label $w.panel.path.tag -font $helv12 -text "Filament path" -width 10 -anchor w
494  entry $w.panel.path.entry -width 50 -relief sunken
495  $w.panel.path.entry insert 0 ""
496 
497  frame $w.check
498  checkbutton $w.check.splitbutton -text "Individual output files" \
499  -width 20 -anchor w -variable fil_split
500 
501  frame $w.buttons
502  button $w.buttons.ok -text OK -command "filamentExtract_and_DestroyWindow $w"
503  button $w.buttons.cancel -text Cancel -command "destroy $w"
504 
505  pack $w.msg -side top -padx 5 -pady 5
506 
507  pack $w.panel.fil_width.tag $w.panel.fil_width.entry -side left -padx 5 -pady 5
508  pack $w.panel.fil_axis.tag $w.panel.fil_axis.x $w.panel.fil_axis.y \
509  $w.panel.fil_axis.z -side left -padx 5 -pady 5
510  pack $w.panel.path.tag $w.panel.path.entry -side left -padx 5 -pady 5
511  pack $w.panel.fil_width $w.panel.fil_axis $w.panel.path -side top
512  pack $w.panel -side top -fill both
513 
514  pack $w.check.splitbutton -side left -pady 5 -padx 5
515  pack $w.check -side top -pady 2m
516 
517  pack $w.buttons.ok $w.buttons.cancel -side left -padx 5
518  pack $w.buttons -side bottom -pady 2m
519 
520  bind $w <Return> "filamentExtract_and_DestroyWindow $w"
521 }
522 
523 ## @brief Extracts filaments from the current set of node coordinates.
524 #
525 # @param w Dialog window.
526 
527 proc filamentExtract_and_DestroyWindow { w } {
528  global filetypes
529  global filename project_item
530  global filament_width
531  global fil_axis fil_split
532 
533  set xname [file root [file tail $filename]]
534  append xname "_fil.mrc"
535  set xname [tk_getSaveFile -filetypes $filetypes \
536  -initialfile $xname -defaultextension .mrc]
537  if { [string length $xname] < 1 } { return }
538 # puts "Extract from $filename to $xname"
539  set pathname [$w.panel.path.entry get]
540  set filament_width [$w.panel.fil_width.entry get]
541 # set fil_axis 0
542 # puts "Filament axis = $fil_axis split = $fil_split"
543  .wfil.fil_width.entry delete 0 end
544  .wfil.fil_width.entry insert 0 $filament_width
545  Bmg set $project_item pixel_size [getPixelSizeEntry]
546  setMgParam
547  Bmg filament $project_item extract $xname $filament_width $fil_axis $fil_split $pathname
548  destroy $w
549 }
550 
551 ## @brief Creates particles on filaments.
552 #
553 
554 proc filamentsToParticles { } {
555  global filament_width filament_node_radius boxing_interval
556  global helix_rise helix_angle project_item
557 # puts "toParticles"
558  set filament_width [.wfil.fil_width.entry get]
559  set filament_node_radius [.wfil.node_radius.entry get]
560  set boxing_interval [.wfil.boxing_interval.entry get]
561  set helix_rise [.wfil.helix_rise.entry get]
562  set helix_angle [.wfil.helix_angle.entry get]
563  setMgParam
564  Bmg set $project_item filament_width $filament_width
565  Bmg set $project_item filament_node_radius $filament_node_radius
566 # puts "$filament_width $boxing_interval $helix_rise $helix_angle"
567  set np [Bmg filament $project_item toparticles $filament_width $boxing_interval $helix_rise $helix_angle]
568 # puts "$np particles generated"
569  Particles
570 }
571 
572 ## @brief Updates node parameters from the micrograph parameters in memory.
573 #
574 
575 proc updateNodeParam { } {
576  global project_item imgtype
577  global filament_width filament_node_radius
578 
579  set w .wfil
580  if ![winfo exists $w] { return }
581 # puts "Updating filament parameters"
582  if ![Bmg exists] { createMgParam }
583  set filename [Bmg get $project_item filename $imgtype]
584 # puts "File: $filename"
585  wm title $w [list "Filaments:" $filename]
586  updatePixelsize
587  set filament_width [$w.fil_width.entry get]
588  set filament_node_radius [$w.node_radius.entry get]
589  Bmg set $project_item filament_width $filament_width
590  Bmg set $project_item filament_node_radius $filament_node_radius
591 # puts "$filament_width $filament_node_radius"
592  $w.fil_width.entry delete 0 end
593  $w.fil_width.entry insert 0 $filament_width
594  $w.node_radius.entry delete 0 end
595  $w.node_radius.entry insert 0 $filament_node_radius
596  filamentUpdateSelected
597 }
598 
599 ## @brief Plots the selected filament profile in the plot canvas
600 #
601 # @param fid filament identifier.
602 # @param nid node identifier.
603 
604 proc filamentProfilePlot { fid nid } {
605  global theimg project_item
606  global filament_width
607  global fil_plot_width fil_plot_height
608  global show_profiles
609 
610  if ![winfo exists .wfil] { return }
611  if { $fid < 1 } { return }
612  if { $nid < 1 } { return }
613 
614  set g .wfil.plot
615  $g delete filplot
616 
617  if !$show_profiles { return }
618 
619  set prof [Bmg filament $project_item profile $fid $nid $filament_width]
620 # puts $prof
621  set len [lindex $prof 0]
622  if { $len < 1 } { return }
623 
624  set min [lindex $prof 1]
625  set max [lindex $prof 1]
626  for {set i 1} {$i <= $len} {incr i 1} {
627  set v [lindex $prof $i]
628  if { $min > $v } { set min $v }
629  if { $max < $v } { set max $v }
630  }
631  if { $max <= $min } { set max [expr $min + 1] }
632 # puts "profile: $min $max"
633  set xoff 5
634  set yoff [expr $fil_plot_height * ( (3*$max - $min) * 0.25 / ($max - $min)) + 5]
635  set xscale [expr $fil_plot_width*1.0/$len]
636  set yscale [expr $fil_plot_height*0.5/($max - $min)]
637 
638 # puts "$yoff $yscale"
639 
640  set fp ""
641  set fps ""
642  for {set i 1} {$i <= $len} {incr i 1} {
643  set j [expr $i + $len]
644  set x [expr ($i-1)*$xscale + $xoff]
645  set y [expr $yoff - [lindex $prof $i] * $yscale]
646  set ys [expr $yoff - [lindex $prof $j] * $yscale]
647  append fp " " $x " " $y
648  append fps " " $x " " $ys
649  }
650  $g create line $fp -fill blue -smooth yes -width 1 -tags filplot
651  $g create line $fps -fill red -smooth yes -width 1 -tags filplot
652  set orix [expr $fil_plot_width/2 + $xoff]
653  set oriy $yoff
654  $g create line $orix 0 $orix [expr 2*$yoff + $fil_plot_height] \
655  -fill black -smooth yes -width 1 -tags filplot
656  $g create line 0 $oriy [expr 2*$xoff + $fil_plot_width] $oriy \
657  -fill black -smooth yes -width 1 -tags filplot
658 }
659 
660 ## @brief Writes the selected filament profile to a file
661 #
662 
663 proc filamentProfileWrite { } {
664  global theimg project_item
665  global node_selected
666  global filament_width
667  if { [llength $node_selected] < 0 } {
668  tk_dialog .dialog "No filament selected!" \
669  {Please select a filament first.} \
670  info 0 {OK}
671  return
672  }
673  set filename [Bimage get $theimg filename]
674  set fprfile "[file rootname [file tail $filename]].txt"
675  set fprfile [tk_getSaveFile -initialfile $fprfile -defaultextension .txt]
676  if { [string length $fprfile] < 1 } { return }
677  set wc [getControlWindow $theimg]
678  set pixel_size [$wc.pixel_size.x get]
679  set fid [lindex $node_selected 0]
680  set nid [lindex $node_selected 1]
681  set prof [Bmg filament $project_item profile $fid $nid $filament_width]
682  set len [lindex $prof 0]
683  if { $len < 1 } { return }
684  set ffpr [open $fprfile w]
685  puts $ffpr "File name: $filename"
686  puts $ffpr "Filament: $fid"
687  puts $ffpr "Pixel size: $pixel_size angstrom/pixel"
688  puts $ffpr " "
689  puts $ffpr "Pixel Density"
690  for {set i 1} {$i <= $len} {incr i 1} {
691  set v [lindex $prof $i]
692  puts $ffpr "$i $v"
693  }
694  puts $ffpr " "
695  close $ffpr
696 }
697