1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
|
#!/usr/bin/wish
#
# Copyright: 2009-2012
# Author: Dewey Garrett <dgarrett@panix.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# As standalone tool table editor
# Usage:
# tooleditor.tcl filename
# tooleditor.tcl [column_1 ... column_n] filename
#
# This file can also be sourced and included in tcl scripts
# The namespace ::tooledit exports a single function:
# ::tooledit::tooledit
# A single global array ::te() stores private data
# Supports tool offsets along all axes (x y z a b c u v w)
# Older tool table formats are not supported
# No distinction is made between mill or lathe tools
# Text on a line following a semicolon (;) is treated as comment
# Comment-only lines are preserved
package require BWidget ;# for ScrolledWindow, ScrollableFrame
namespace eval ::tooledit {
namespace export tooledit ;# public interface
}
#-----------------------------------------------------------------------
# Internationalization
# use the tcl-package named Emc to set up I18n support
if [catch {package require Linuxcnc} msg] {
# if user is trying to use as standalone in an unconfigured (non-Emc)
# environment, just continue without internationalization
puts stderr "Internationalization not available: <$msg>"
}
# use a command or proc named "_" for ::msgcat::mc
# when embedded in axis, a command named "_" is predefined,
# since "_" is not defined for standalone usage, make a proc named "_"
if {"" == [info command "_"]} {
package require msgcat
proc _ {s} {
return [::msgcat::mc $s]
}
}
#-----------------------------------------------------------------------
proc ::tooledit::init { {columns ""} } {
if [file readable ~/.tooleditrc] {
if [catch {source ~/.tooleditrc} msg] {
puts stderr "[_ "Problem reading ~/.tooleditrc"]:\n$msg"
}
if [info exists geometry] {
set ::te(top,restore,geometry) $geometry
}
}
if {"$columns" == ""} {
set columns $::te(allcolumns)
} else {
set columns [string tolower $columns]
set ::te(user_specified_columns) $columns
}
# disallow duplicate columns (order according to first occurrence)
set checked_columns {}
foreach cname $columns {
if {[lsearch $checked_columns $cname] >= 0} {
puts stderr [format [_ "Note: Ignoring duplicate column name: %s"] $cname]
continue ;# duplicate col name
}
lappend checked_columns $cname
}
# include only allowed column names:
foreach cname $checked_columns {
if {[lsearch $::te(allcolumns) $cname] >= 0} {
lappend ::te(columns) $cname
} else {
puts stderr [format [_ "Unknown column: %s"] $cname]
}
}
set ::te(filemod) 0
set ::te(fmt,int) %d
set ::te(fmt,real) %.9g
set ::te(fmt,angle) %f
set ::te(msg,last) ""
set ::te(pollms) 2000
set ::te(initial,width) 0 ;# initial width as reqd
set ::te(initial,height) 110 ;# initial height limit here
set ::te(hincr) 1 ;# height increment to bump scrollable size
set ::te(autocolumns) {tool poc}
set ::te(header) [concat $::te(autocolumns) $::te(columns) comment]
foreach item $::te(header) { set ::te(type,$item) real ;# default }
set ::te(type,tool) integer
set ::te(type,poc) integer
set ::te(type,orien) integer
set ::te(type,comment) ascii
set ::te(tool_changer) unknown
# include values for each (header) item:
set ::te(tool,width) 5; set ::te(tool,tag) T
set ::te(poc,width) 5; set ::te(poc,tag) P
set ::te(x,width) 7; set ::te(x,tag) X
set ::te(y,width) 7; set ::te(y,tag) Y
set ::te(z,width) 7; set ::te(z,tag) Z
set ::te(a,width) 7; set ::te(a,tag) A
set ::te(b,width) 7; set ::te(b,tag) B
set ::te(c,width) 7; set ::te(c,tag) C
set ::te(u,width) 7; set ::te(u,tag) U
set ::te(v,width) 7; set ::te(v,tag) V
set ::te(w,width) 7; set ::te(w,tag) W
set ::te(diam,width) 7; set ::te(diam,tag) D
set ::te(front,width) 7; set ::te(front,tag) I
set ::te(back,width) 7; set ::te(back,tag) J
set ::te(orien,width) 6; set ::te(orien,tag) Q
set ::te(comment,width) 20; set ::te(comment,tag) \;
# note: width 0 expands with text in entry widget
# when using Bwidget scrollable frame
} ;# init
proc ::tooledit::validangle {v} {
if {[string trim $v] == ""} {return 1} ;# allow null value
if {$v <= 360 && $v >= -360} {return 1}
return 0
} ;# validangle
proc ::tooledit::isnegative {v} {
if {$v < 0} {return 1}
if {[string first - $v] >=0} {return 1} ;# this gets -0
return 0
} ;# ispositive
proc ::tooledit::isinteger {v} {
if ![isnumber $v] {return 0}
if {[string first . $v] >=0} {return 0}
if {[string first e [string tolower $v]] >= 0} {return 0}
return 1
} ;# isinteger
proc ::tooledit::isnumber {v} {
if {[string trim $v] == ""} {return 1} ;# allow null value
if [catch {format %f $v}] {
return 0
} else {
return 1
}
} ;# isnumber
proc ::tooledit::qid {} {
# generate unique id
if { ![info exists ::te(qid)]} {
set ::te(qid) 0
}
incr ::te(qid)
return q$::te(qid)
} ;# qid
proc ::tooledit::ventry {f validatenumber tvar \
{twidth 12} {expand 0} {justify left} {fill x}} {
if {$validatenumber} {
set e [entry $f.[qid] \
-width $twidth -relief sunken -justify $justify \
-textvariable $tvar \
-bg white \
-validate all \
-validatecommand [list ::tooledit::validateNumber $tvar %W %s %P] \
-invalidcommand [list ::tooledit::invalidNumber $tvar %W] \
]
pack $e -side left -expand $expand -fill $fill
} else {
set e [entry $f.[qid] \
-width $twidth -relief sunken -justify $justify\
-textvariable $tvar \
-bg white \
-validate all \
-validatecommand [list ::tooledit::validateOther $tvar %W %s %P] \
]
pack $e -side left -expand $expand -fill $fill
}
return $e
} ;# ventry
proc ::tooledit::validateOther {varname widget current new} {
if {"$current" != $new} {
incr ::te(filemod)
}
return 1 ;# 1==>ok
} ;# validateOther
proc ::tooledit::validateNumber {varname widget current new} {
if ![info exists $varname] {return 1}
if {"$new" == ""} {return 1}
if {"$new" == "[_ "NEW"]"} {
return 1 ;# 1==>ok dont flag items tagged "NEW"
}
if {"$current" == "[_ "NEW"]"} {
$widget configure -selectbackground $::te(restore,selectbackground)
$widget configure -selectforeground $::te(restore,selectforeground)
}
if [catch {format %f $new} ] {
$widget configure -fg red
message verror
return 1 ;# problem but return ok (just change color)
} else {
if {"$current" != "$new"} {message modified}
$widget configure -fg black
incr ::te(filemod)
return 1 ;# 1==>ok
}
} ;# validateNumber
proc ::tooledit::invalidNumber {varname widget} {
tk_messageBox \
-title [_ Problem] \
-message [_ "Bad Entry"] \
-detail [_ "(must be a number)"] \
-type ok
$widget configure -validate all ;# restore validation
} ;# invalidNumber
proc ::tooledit::readfile {filename} {
set filename [file normalize $filename]
if {[file exists $filename] && ![file readable $filename]} {
lappend msg [format [_ "filename: <%s> not readable"] $filename]
}
if [file exists $filename] {
if ![file writable $filename] {
lappend msg "[format [_ "filename: <%s> not writable"] $filename]"
}
} else {
set new 1
if ![file writable [file dirname $filename]] {
lappend msg "[format [_ "directory: <%s> not writable"] $filename]"
}
}
if [info exists msg] {return -code error $msg}
if [info exists new] {
makeline new
incr ::te(filemod)
message newfile
return
}
set tooledit_pids ""
# count howmany tooledits (exclusive of this one) are running
# Note: pidof ignores defunct processes
# catch required since child error returned if no other pids:
catch { set tooledit_pids [eval exec pidof -x -o[pid] tooledit]} emsg
# determine if axis is running with same filename
catch {
sendaxis ping
set axis_is_running [sendaxis tool_table_filename]
if $axis_is_running {
set tchanger nonrandom ;# default if not specified
if [catch {set tchanger [send axis inifindall EMCIO RANDOM_TOOLCHANGER]} msg ] {
puts "UNEXPECTED: $msg"
}
switch "$tchanger" {
1 {set ::te(tool_changer) random}
default {set ::te(tool_changer) nonrandom}
}
}
set tooledit_pid_ct [llength $tooledit_pids]
if {$axis_is_running && $tooledit_pid_ct > 0} {
set msg [_ "Current file:"]
set detail "$filename"
if {"$::te(axis,filename)" == "$filename"} {
tk_messageBox \
-title [_ "Tooledit already running"] \
-message $msg \
-detail $detail \
-type ok
# try to raise it:
catch {send $::te(axis,tooledit) [list wm deiconify $::te(top)]}
exit 1
}
}
} emsg
#if {$emsg != ""} {puts emsg=$emsg}
set fd [open $filename r]
set bct 0
set lno 0
while {1} {
gets $fd newline
incr lno ;# starts at 1
if [eof $fd] break
foreach item {t p x y z a b c u v w d i j q comment} {
set u($item) ""
}
# extract the comment as is (without converting it to lower case)
set i1 [string first \; $newline]
if {$i1 >= 0} {
set u(comment) [string range $newline [expr $i1 +1] end]
set u(comment) [string trim $u(comment)]
set newline [string range $newline 0 [expr -1 + $i1]]
set newline [string trim $newline]
}
set newline [string tolower $newline]
if {"$newline" == ""} {
lappend ::te(global,comments) $u(comment)
continue
}
set bogus 0
foreach tagvalue [split [string trim $newline]] {
set tagvalue [string trim $tagvalue]
if {"$tagvalue" == ""} continue
set tag [string range $tagvalue 0 0 ]
set value [string range $tagvalue 1 end ]
if ![isnumber $value] {
puts stderr [format [_ "Skipping linenumber %d for tag %s, value <%s> is not a number"] \
$lno $tag $value]
incr bct; set bogus 1
}
switch $tag {
t - p - q { if ![isinteger $value] {
puts stderr [format [_ "Skipping linenumber %d for tag %s, expected integer not <%s>"] \
$lno $tag $value]
incr bct; set bogus 1
}
}
}
# catch errors since format is already checked
# (line will not be displayed)
# this allows all errors on a line to be flagged in one pass
switch $tag {
t - p - q {catch {set u($tag) [format "$::te(fmt,int)" $value]}}
x - y - z -
a - b - c -
u - v - w -
d {catch {set u($tag) [format "$::te(fmt,real)" $value]}}
i - j {catch {set u($tag) [format "$::te(fmt,angle)" $value]}}
default {puts stderr [format [_ "At linenumber %d, Unknown tag <%s>"] \
$lno $tag]
incr bct; set bogus 1
}
}
}
if $bogus continue
makeline u
::tooledit::repack
} ;# while
close $fd
if {$bct >0} {
# schedule message after message widget is created
after 0 {::tooledit::message bogus}
}
message opened
::tooledit::column_sort $::te(entry,header,tool) tool 1
set ::te(filemod) 0
return
} ;# readfile
proc ::tooledit::watch {args} {
catch {after cancel $::te(afterid)}
if ![file exists $::te(filename)] {
::tooledit::message filegone
set ::te(afterid) [after $::te(pollms) ::tooledit::watch]
return
}
set mtime [file mtime $::te(filename)]
switch $args {
start {
set ::te(mtime) $mtime
set ::te(md5sum) [eval exec md5sum $::te(filename)]
}
stop {return}
default {
if {$mtime > $::te(mtime)} {
set ::te(mtime) $mtime
set md5sum $::te(md5sum)
set ::te(md5sum) [eval exec md5sum $::te(filename)]
# no message if file contents unchanged
if {"$md5sum" != "$::te(md5sum)"} {
::tooledit::message changed
}
}
}
}
# try to clear error display in case user clears error before check
# skip if newtool since error is annoying
# skip if filegone|changed|bogus since important
if { "$::te(msg,last)" != "newtool"
&& "$::te(msg,last)" != "filegone"
&& "$::te(msg,last)" != "changed"
&& "$::te(msg,last)" != "bogus"
&& "$::te(msg,last)" != "isort"
&& "$::te(msg,last)" != "dsort"
} {
::tooledit::toolvalidate silent ;# to clear errors
}
if [info exists ::te(load,button)] {
if ![sendaxis ping] {
# axis disappeared
pack forget $::te(load,button)
unset ::te(load,button)
} else {
if [sendaxis check_for_reload] {
$::te(load,button) configure -state normal
} else {
$::te(load,button) configure -state disabled
}
}
}
set ::te(afterid) [after $::te(pollms) ::tooledit::watch]
} ;# watch
proc ::tooledit::tooledit {filename {columns ""} } {
package require Tk
# name to identify tooledit used by first axis invocation:
set ::te(axis,tooledit) axis_tooledit
if {[package vcompare $::tcl_version 8.5] >= 0} {
# tcl8.5 lsort -indices available
set ::te(enable_column_sorting) 1
} else {
set prog [file tail $::argv0]
puts stderr [format [_ "%s: Column sorting not available with tcl_version==%s"] \
$prog $::tcl_version]
}
::tooledit::init $columns
set ::te(filename) [file normalize $filename]
# allow for translated names for header columns:
foreach h $::te(header) {
switch -exact $h {
tool {set ::te($h,show) [_ "tool"]}
poc {set ::te($h,show) [_ "poc"]}
diam {set ::te($h,show) [_ "diam"]}
front {set ::te($h,show) [_ "front"]}
back {set ::te($h,show) [_ "back"]}
orien {set ::te($h,show) [_ "orien"]}
comment {set ::te($h,show) [_ "comment"]}
default {set ::te($h,show) $h}
}
set ::te($h,show) [string toupper $::te($h,show)]
}
set ::te(top) [toplevel .tooledit]
wm withdraw $::te(top); update
wm resizable $::te(top) 1 1
wm protocol $::te(top) WM_DELETE_WINDOW ::tooledit::bye
wm title $::te(top) "tooledit: $::te(filename)"
if [info exists ::te(tooledit,geometry)] {
wm geometry $::te(top) $::te(tooledit,geometry)
}
# note: never pack ::te(scroll,frame), handled by ScrolledWindow
set ::te(scroll,window) [ScrolledWindow $::te(top).scrolled \
-scrollbar vertical -auto none]
set ::te(scroll,frame) [ScrollableFrame $::te(top).scrolled.sff \
-height $::te(initial,height) -width $::te(initial,width) \
-constrainedwidth 1]
$::te(scroll,window) setwidget $::te(scroll,frame) ;# associates scrollbars
set ::te(main,frame) [$::te(scroll,frame) getframe] ;# this is parent
set ::te(lasti) 0
# header frame -------------------------------------------------
set f [frame $::te(top).header]
set ::te(header,frame) $f
pack $f -side top -expand 1 -fill x -anchor n
pack [label $f.b -text [_ "Del"] -width 3] -side left -expand 0
foreach h $::te(header) {
set e 0;set j center
if {"$h" == "comment"} {set e 1;set j left}
set ey [entry $f.$::te(lasti)$h -justify $j -textvariable ::te($h,show) \
-state disabled -relief groove \
-disabledforeground black \
-width $::te($h,width)]
pack $ey -side left -fill x -expand $e
set ::te(entry,header,$h) $ey
$ey configure -cursor arrow
bind $ey <ButtonRelease-1> "::tooledit::column_sort $ey $h"
}
readfile $::te(filename)
set ::te(filename) [file normalize $::te(filename)]
if [file exists $::te(filename)] {watch start}
pack $::te(scroll,window) -side top -fill x -expand 0 -anchor nw
# button frame -------------------------------------------------
set bf [frame $::te(top).[qid]]
pack $bf -side top -expand 0 -fill both -anchor nw
pack [button $bf.[qid] -text "[_ "Quit"]" \
-command ::tooledit::bye] \
-side right -fill x -expand 1
if { ![info exists ::te(load,button)]
&& [sendaxis ping] && [sendaxis tool_table_filename]
&& "$::te(axis,filename)" == "$::te(filename)"
} {
tk appname $::te(axis,tooledit)
set ::te(load,button) [button $bf.[qid] -text "[_ "ReLoadTable"]" \
-state disabled \
-command [list ::tooledit::sendaxis reload_tool_table]]
pack $::te(load,button) -side right -fill x -expand 1
}
pack [button $bf.[qid] -text "[_ "SaveFile"]" \
-command [list ::tooledit::writefile $::te(filename)]] \
-side right -fill x -expand 1
# pack [button $bf.[qid] -text "[_ "Check Entries"]" \
# -command [list ::tooledit::toolvalidate]] -side right -fill x -expand 1
pack [button $bf.[qid] -text "[_ "ReRead"]" \
-command ::tooledit::toolreread] -side right -fill x -expand 1
pack [button $bf.[qid] -text "[_ "AddTool"]" \
-command {::tooledit::makeline new}] -side right -fill x -expand 1
set bb [button $bf.[qid] -text "[_ "Delete"]"\
-command {::tooledit::deleteline}]
pack $bb -side right -fill x -expand 1
set ::te(deletebutton) $bb
checkdelete
# message frame -------------------------------------------------
set mf [frame $::te(top).[qid]]
pack $mf -side top -expand 0 -fill x
set msg [label $mf.msg -anchor w]
set ::te(msg,widget) $msg
pack $msg -side top -expand 0 -fill x -anchor w
update ;# wait for display before binding Configure events
if [info exists ::te(top,restore,geometry)] {
wm geometry $::te(top) $::te(top,restore,geometry)
unset ::te(top,restore,geometry)
}
set ::te(top,geometry) [wm geometry $::te(top)]
set ::te(top,height) [winfo height $::te(top)]
# set min width so top cannot be disappeared inadvertently
# set min height to initial
wm minsize $::te(top) 100 $::te(top,height)
bind $::te(top) <Configure> {::tooledit::configure %W %w %h}
wm deiconify $::te(top)
} ;# tooledit
proc ::tooledit::configure {W w h} {
if {"$W" != "$::te(top)"} return
if {"$W" == $::te(top) && $::te(top,geometry) != [wm geometry $::te(top)]} {
set ::te(top,geometry) [wm geometry $::te(top)]
set deltah [expr $h - $::te(top,height)]
set fsize [$::te(scroll,frame) cget -height]
if {[expr abs($deltah)] > $::te(hincr)} {
$::te(scroll,frame) configure -height [expr $fsize + $deltah]
set ::te(top,height) $h
}
}
} ;# configure
proc ::tooledit::message {mtype} {
if ![info exists ::te(msg,widget)] return
set w $::te(msg,widget)
set dt [clock format [clock seconds]]
switch $mtype {
opened {$w conf -text "$dt: [format [_ "Opened %s"] $::te(filename)]" -fg darkblue}
newfile {$w conf -text "$dt: [format [_ "Created %s"] $::te(filename)]" -fg darkblue}
write {$w conf -text "$dt: [_ "File updated"]" -fg green4}
modified {$w conf -text "$dt: [_ "File modified"]" -fg darkred}
checke {$w conf -text "$dt: [_ "File check errors"]" -fg red}
checkok {$w conf -text "$dt: [_ "File checked"]" -fg darkgreen}
delete {$w conf -text "$dt: [_ "File items deleted"]" -fg cyan4}
bogus {$w conf -text "$dt: [_ "Bogus lines in file ignored"]" -fg darkorange}
verror {$w conf -text "$dt: [_ "File errors -- Check Entries"]" -fg red}
changed {$w conf -text "$dt: [_ "Warning: File changed by another process"]" -fg red}
filegone {$w conf -text "$dt: [_ "Warning: File deleted by another process"]" -fg red}
newtool {$w conf -text "$dt: [_ "Added Tool"]" -fg green4
update idletasks
$::te(scroll,frame) yview moveto 1.0
}
isort {$w conf -text "$dt: [format [_ "Sorted by %s, increasing"] $::te(lastsort)]" -fg darkgreen}
dsort {$w conf -text "$dt: [format [_ "Sorted by %s, decreasing"] $::te(lastsort)]" -fg darkgreen}
}
set ::te(msg,last) $mtype
} ;# message
proc ::tooledit::deleteline {} {
set dct 0
catch {unset dlines}
foreach item [array names ::te "parm,*,deleteme"] {
if {$::te($item) == 1} {
set i1 [expr 1 + [string first , $item]]
set i2 [expr -1 + [string last , $item]]
lappend dlines [string range $item $i1 $i2]
}
}
if ![info exists dlines] continue
foreach i $dlines {
destroy $::te(entry,$i,frame); unset ::te(entry,$i,frame)
incr dct
if [info exists ::te(items)] {
set idx [lsearch $::te(items) $i]
if {$idx >= 0} {
set ::te(items) [lreplace $::te(items) $idx $idx]
}
if {[string length $::te(items)] == 0} {
unset ::te(items)
}
}
foreach name [array names ::te parm,$i,*] {
unset ::te($name)
}
}
checkdelete
if {$dct >0} { message delete}
incr ::te(filemod)
} ;# deleteline
proc ::tooledit::makeline {ay_name} {
if {"$ay_name" == "new"} {
set new 1
set date "[_ "Added"] [clock format [clock seconds] -format %Y%m%d]"
foreach item {t p x y z a b c u v w d i j q} {
set ay($item) ""
}
set ay(p) [_ "NEW"] ;# support translation of special entry item value
set ay(t) [_ "NEW"] ;# support translation of special entry item value
set ay(comment) "$date"
after 0 {::tooledit::message newtool}
} else {
upvar $ay_name ay
}
set i $::te(lasti)
set f [frame $::te(main,frame).[qid]]
set ::te(entry,$i,frame) $f
if {"$ay_name" == "new"} {
pack $f -side top -expand 1 -fill x -anchor n
} else {
# caller must pack (use ::tooledit::repack)
}
lappend ::te(items) $i
set ::te(parm,$i,tool) $ay(t)
set ::te(parm,$i,poc) $ay(p)
set ::te(parm,$i,x) $ay(x)
set ::te(parm,$i,y) $ay(y)
set ::te(parm,$i,z) $ay(z)
set ::te(parm,$i,a) $ay(a)
set ::te(parm,$i,b) $ay(b)
set ::te(parm,$i,c) $ay(c)
set ::te(parm,$i,u) $ay(u)
set ::te(parm,$i,v) $ay(v)
set ::te(parm,$i,w) $ay(w)
set ::te(parm,$i,diam) $ay(d)
set ::te(parm,$i,front) $ay(i)
set ::te(parm,$i,back) $ay(j)
set ::te(parm,$i,orien) $ay(q)
set ::te(parm,$i,comment) [string trim $ay(comment)]
pack [checkbutton $f.b -variable ::te(parm,$i,deleteme)\
-command "::tooledit::checkdelete"] -side left -expand 0
foreach h $::te(header) {
set e 0;set j right;set v 1
if {"$h" == "comment"} {set e 1; set j left;set v 0}
set ve [ventry $f $v ::te(parm,$i,$h) $::te($h,width) $e $j]
if {[info exists new] && "$h" == "tool"} {set vefocus $ve}
entrybindings $ve $h $i
}
incr ::te(lasti)
if [info exists vefocus] {
set ::te(restore,selectbackground) [$vefocus cget -selectbackground]
set ::te(restore,selectforeground) [$vefocus cget -selectforeground]
$vefocus configure -selectbackground white
$vefocus configure -selectforeground red
$vefocus selection to end
focus $vefocus
}
} ;# makeline
proc ::tooledit::entrybindings {e h i} {
$e conf -takefocus 1
set ::te($i,$h,entry) $e
bind $e <Key-Up> "::tooledit::bindactions $h $i %K"
bind $e <Key-Down> "::tooledit::bindactions $h $i %K"
bind $e <Key-Left> "::tooledit::bindactions $h $i %K"
bind $e <Key-Right> "::tooledit::bindactions $h $i %K"
} ;# entrybindings
proc ::tooledit::bindactions {h i key args} {
set nexth $h;set nexti $i;
switch $key {
Up {
set nexti [expr $i -1]
if {$nexti <0} {
set nexti [expr $::te(lasti) -0]
after 0 [list ::tooledit::bindactions $h $nexti $key]
return
}
}
Down {
set nexti [expr $i + 1]
if {$nexti >= $::te(lasti)} {
set nexti -1
after 0 [list ::tooledit::bindactions $h $nexti $key]
return
}
}
Right {
if {"$h" == "nosuch"} {
set nextidx 0
} else {
set idx [lsearch $::te(header) $h]
set nextidx [expr $idx + 1]
if {$nextidx >= [llength $::te(header)]} {
after 0 [list ::tooledit::bindactions nosuch $nexti $key]
return
}
}
set nexth [lindex $::te(header) $nextidx]
}
Left {
if {"$h" == "nosuch"} {
set nextidx [expr [llength $::te(header)] -1]
} else {
set idx [lsearch $::te(header) $h]
set nextidx [expr $idx + -1]
if {$nextidx < 0} {
after 0 [list ::tooledit::bindactions nosuch $nexti $key]
return
}
}
set nexth [lindex $::te(header) $nextidx]
}
}
if [info exists ::te($nexti,$nexth,entry)] {
$::te($nexti,$nexth,entry) selection to end
focus $::te($nexti,$nexth,entry)
} else {
# frame has been deleted
switch $key {
Up - Down {
set nexti [expr $nexti + 0]
after 0 [list ::tooledit::bindactions $h $nexti $key]
return
}
}
}
} ;# bindactions
proc ::tooledit::checkdelete {} {
set ct 0
foreach name [array names ::te parm,*,deleteme] {
if {$::te($name) == 1} {incr ct}
}
if {$ct > 0} {
$::te(deletebutton) conf -fg red -state normal
} else {
$::te(deletebutton) conf -fg black -state disabled
}
focus $::te(deletebutton)
} ;# checkdelete
proc ::tooledit::toolreread {} {
set ::te(tooledit,geometry) [wm geometry $::te(top)]
for {set i 0} {$i < $::te(lasti)} {incr i} {
catch {
destroy $::te(entry,$i,frame)
unset ::te(entry,$i,frame)
} ;# it may already be gone
}
set ::te(lasti) 0
# can be missing for some prior file open errors
catch {unset ::te(items)}
readfile $::te(filename)
if [file exists $::te(filename)] {watch start}
} ;# toolreread
proc ::tooledit::writefile {filename} {
if [toolvalidate] return ;# failed validation
if [file exists $filename] {
set backup $filename.bak
file rename -force $filename $backup
}
set fd [open $filename w]
if [info exists ::te(global,comments)] {
foreach c $::te(global,comments) {
puts $fd ";$c"
}
}
# write to all populated header items (to preserve values if not displayed)
set allheader [concat $::te(autocolumns) $::te(allcolumns) comment]
foreach i $::te(items) {
set line ""
foreach h $allheader {
set j ""
set w $::te($h,width)
# correct entries with leading zeros
if {$h != "comment" &&
[string first 0 [string trim $::te(parm,$i,$h)]] == 0} {
set ::te(parm,$i,$h) [format %g $::te(parm,$i,$h)]
}
set value [string trim $::te(parm,$i,$h)]
if {"$value" != ""} {
set line "$line $::te($h,tag)$value "
}
}
puts $fd [string trim "$line"]
}
watch stop
close $fd
watch start
message write
set ::te(filemod) 0
} ;# writefile
proc ::tooledit::toolvalidate {args} {
set msg ""
set silent 0
if {"$args" == "silent"} {set silent 1}
if [info exists ::te(items)] {
foreach i $::te(items) {
foreach h $::te(header) {
if {"$h" == "comment"} continue
if ![isnumber $::te(parm,$i,$h)] {
set nextmsg [format [_ "Tool %s, Column %s, parameter %s is not a number"] \
$::te(parm,$i,tool) $h $::te(parm,$i,$h)]
if {[lsearch $msg $nextmsg] >= 0} continue
lappend msg $nextmsg
}
switch -glob $h {
tool* - poc* {
if {![isinteger $::te(parm,$i,$h)] || [isnegative $::te(parm,$i,$h)]} {
lappend msg [format [_ "Tool %s, parameter %s must be nonnegative integer"] \
$::te(parm,$i,tool) $h]
}
}
orien* {
if { "$::te(parm,$i,$h)" != "" \
&& [lsearch {0 1 2 3 4 5 6 7 8 9} $::te(parm,$i,$h)] < 0} {
lappend msg [format [_ "Tool %s: <Orientation> must be 0..9 integer"] \
$::te(parm,$i,tool)]
}
}
front* - back* {
if {![validangle $::te(parm,$i,$h)] } {
lappend msg [format [_ "Tool %s: <%s> must be between -360 and 360"] \
$::te(parm,$i,tool) $h]
}
}
}
}
}
}
# check for multiple uses of a single pocket
if [info exists ::te(items)] {
set pocs ""
foreach i $::te(items) {
set p $::te(parm,$i,poc)
if { "$::te(tool_changer)" == "random" && [lsearch $pocs $p] >= 0 } {
set nextmsg [format [_ "Pocket <%s> specified multiple times"] $p]
if {[lsearch $msg $nextmsg] >= 0} continue
lappend msg $nextmsg
} else {
lappend pocs $p
}
}
}
# check for multiple uses of a single tool
if [info exists ::te(items)] {
set tools ""
foreach i $::te(items) {
set t $::te(parm,$i,tool)
if {[lsearch $tools $t] >= 0} {
set nextmsg [format [_ "Tool <%s> specified multiple times"] $t]
if {[lsearch $msg $nextmsg] >= 0} continue
lappend msg $nextmsg
} else {
lappend tools $t
}
}
}
if {"$msg" != ""} {
if {!$silent} {showerr $msg}
message checke
return 1 ;#fail
}
message checkok
return 0
} ;# toolvalidate
proc ::tooledit::showerr {msg} {
set w .showerr
catch {destroy $w}
set w [toplevel $w]
set l [label $w.l -justify left]
set text ""
set msg [lsort $msg]
foreach item $msg {set text "$text\n$item"}
$l configure -text $text
pack $l -side top
set b [button $w.b -text [_ "Dismiss"] -command "destroy $w"]
pack $b -side top
focus $b
wm withdraw $w
wm title $w [_ "Error"]
update idletasks
set x [expr [winfo screenwidth $w]/2 \
- [winfo reqwidth $w]/2 - [winfo vrootx [winfo parent $w]]]
set y [expr [winfo screenheight $w]/2 \
- [winfo reqheight $w]/2 - [winfo vrooty [winfo parent $w]]]
wm geom $w +$x+$y
wm deiconify $w
} ;# showerr
proc ::tooledit::bye {} {
if $::te(filemod) {
set ans [tk_messageBox \
-title "[_ "File Modified"]" \
-message "[_ "Save Modifications to File?"]" \
-detail $::te(filename) \
-type yesno]
if {$ans == yes} {
::tooledit::writefile $::te(filename)
}
}
catch {after cancel $::te(afterid)}
if ![file exists ~/.tooleditrc] {
# first time use presumed, instruct for configuring columns
set used ""
foreach item $::te(items) {
foreach col $::te(allcolumns) {
if {"$::te(parm,$item,$col)" != ""} {lappend used $col}
}
}
# make used list unique:
foreach item $used { set tmp($item) "" }
set used [array names tmp]
if { ![info exists ::te(user_specified_columns)] \
&& ("$used" != "") \
&& ([llength $used] < [llength $::te(allcolumns)]) } {
set prog [file tail $::argv0]
set msg "[format [_ "Only these columns are currently used:\n\n %s"] $used] \
\n\n[_ "Limit display to these columns by specifying"]:\
\n \[DISPLAY\]TOOL_EDITOR = $prog $used\
\n\n[_ "Format for ini file is"]:\
\n \[DISPLAY\]TOOL_EDITOR = $prog col_1 col_2 ... col_n\
\n\n[_ "For standalone use, invoke as"]:\
\n\n $prog col_1 col_2 ... col_n [_ "tool_table_filename"]
"
catch {destroy .msg}
toplevel .msg
set txt [text .msg.txt -width 80 -height 14]
$txt insert end $msg
pack $txt -side top -fill x -expand 0
pack [button .msg.b -text OK -command {destroy .msg}] -side top
}
update
}
while 1 {
if ![winfo exists .msg] break
after 1000
update
}
if [winfo exists $::te(top)] {
set fd [open ~/.tooleditrc w]
set time [clock format [clock seconds] -format %Y%m%d.%H.%M.%S]
puts $fd "# $time [format [_ "Created by %s"] [file normalize $::argv0]]"
puts $fd "\n# [_ "Saved geometry (updated on program termination)"]:"
puts $fd "set geometry [wm geometry $::te(top)]"
close $fd
destroy $::te(top) ;# for embedded usage
}
set ::tooledit::finis 1 ;# for standalone usage
} ;# bye
proc ::tooledit::sendaxis {cmd} {
# return 1==>ok
switch $cmd {
ping {
# must ping to see if axis is running and get its pwd
if ![catch {set ::te(axis,pwd) [send axis pwd]} msg] {return 1 ;#ok}
}
tool_table_filename {
set prog [file tail $::argv0]
# check that tooledit opened with same filename as axis
if [catch {set f [send axis inifindall EMCIO TOOL_TABLE]} msg] {
return -code error "::tooledit::sendaxis tool_table_filename <$msg>"
}
if {[llength $f] > 1} {
set f [lindex $f 0] ;# use first item specified for compatibility
puts stderr [format [_ "%s: Axis inifile specifies multiple inifile items for: \[EMCIO\]TOOL_TABLE"] $prog]
puts stderr [format [_ "%s: Using: %s"] $prog $f]
}
if {[file pathtype $f] == "relative"} {
set f [file join $::te(axis,pwd) $f]
}
if [info exists ::te(axis,filename)] {return 1} ;# already done
set ::te(axis,filename) [file normalize $f]
if {"$::te(axis,filename)" == [file normalize $::te(filename)]} {
return 1 ;# ok
} else {
set msg [format [_ "%s"] $::te(axis,filename)]
set detail [format [_ "differs from the the ini specified file:\n%s"]\
$::te(filename)]
tk_messageBox \
-title "[_ "Axis is Running"]" \
-message $msg \
-detail $detail \
-type ok
}
}
check_for_reload {
# use same test as axis for disabling:
if [send axis {expr "$::task_state" == "$::STATE_ON"\
&& "$::interp_state" == "$::INTERP_IDLE"}] {
return 1 ;# ok
}
}
reload_tool_table {
if ![sendaxis check_for_reload] {
showerr [list "[_ "Must be On and Idle to reload tool table"]"]
return 0 ;# fail
}
::tooledit::writefile $::te(filename)
if [catch {send axis reload_tool_table} msg] {
return -code error "::tooledit::sendaxis reload_tool_table <$msg>"
}
return 1 ;# ok
}
default {return -code error "::tooledit::sendaxis: unknown cmd <$cmd>"}
}
return 0 ;# fail
} ;# sendaxis
proc ::tooledit::repack { {entryname tool} {mode increasing} } {
if ![info exist ::te(enable_column_sorting)] {
foreach name [array names ::te entry*frame] {
pack $::te($name) -side top -expand 1 -fill x -anchor n
}
return
}
set type $::te(type,$entryname)
foreach item $::te(items) {
set value $::te(parm,$item,$entryname)
if { ( ("$type" == "real") || ("$type" == "integer") )\
&& ( ("$value" == "" ) || ("$value" == "[_ "NEW"]") ) } {
set value 0
}
lappend parms $value
lappend parms_i $item
}
foreach i $::te(items) {
pack forget $::te(entry,$i,frame)
}
set indices [lsort -$type -$mode -indices $parms]
foreach idx $indices {
set i [lindex $parms_i $idx]
pack $::te(entry,$i,frame) -side top -expand 1 -fill x -anchor n
}
} ;# repack
proc ::tooledit::column_sort {e parm {initialize 0} } {
if {$initialize || ![info exists ::te(columnsortorder)]} {
set ::te(columnsortorder) increasing
catch {$::te(lastsort,entry) configure -disabledforeground black}
} else {
if [info exists ::te(lastsort,entry)] {
$::te(lastsort,entry) configure -disabledforeground black
if {"$::te(lastsort,entry)" != "$e"} {
set ::te(columnsortorder) decreasing
}
}
if {"$::te(columnsortorder)" == "increasing"} {
set ::te(columnsortorder) decreasing
} else {
set ::te(columnsortorder) increasing
}
}
::tooledit::repack $parm $::te(columnsortorder)
if ![info exists ::te(enable_column_sorting)] return
set ::te(lastsort,entry) $e
set ::te(lastsort) [string toupper $parm]
switch $::te(columnsortorder) {
increasing {
$e configure -disabledforeground blue
::tooledit::message isort
}
decreasing {
$e configure -disabledforeground violetred
::tooledit::message dsort
}
}
} ;# column_sort
#------------------------------------------------------------------------
set ::te(allcolumns) {x y z a b c u v w diam front back orien}
proc standalone_tooledit {args} {
# configure for standalone usage:
set ::te(standalone) 1
wm withdraw .
if {[lindex $::argv 0] == ""} {
set prog [file tail $::argv0]
puts stderr "\n[_ "Usage"]:"
puts stderr " $prog [_ "filename"]"
puts stderr " $prog \[column_1 ... column_n\] [_ "filename"]"
puts stderr "\n[format [_ "Allowed column_ names are: %s"] $::te(allcolumns)]"
exit 1
}
# start, unless already started (convenient for debug sourcing):
if ![info exists ::te(top)] {
# expect ::argv == [colname colname ...] filename
set columns "" ;# default use all columns
set argct [llength $::argv]
switch $argct {
0 {#notreached
puts stderr "$::argv0: [_ "Missing filename"]";exit 1}
1 {set filename $::argv}
default {set filename [lindex $::argv end]
set columns [lreplace $::argv end end]
}
}
::tooledit::tooledit $filename $columns
tkwait variable ::tooledit::finis
exit 0
}
} ;# standalone_tooledit
if {[info script] == $::argv0} standalone_tooledit
|