StandardCommands.tcl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. # Copyright (c) 1999-2014 OPEN CASCADE SAS
  2. #
  3. # This file is part of Open CASCADE Technology software library.
  4. #
  5. # This library is free software; you can redistribute it and/or modify it under
  6. # the terms of the GNU Lesser General Public License version 2.1 as published
  7. # by the Free Software Foundation, with special exception defined in the file
  8. # OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
  9. # distribution for complete text of the license and disclaimer of any warranty.
  10. #
  11. # Alternatively, this file may be used under the terms of Open CASCADE
  12. # commercial license or contractual agreement.
  13. #
  14. # Draw standard initialisation
  15. #
  16. #################################################
  17. # prompts
  18. #################################################
  19. set Draw_CmdIndex 0
  20. set tcl_prompt1 {
  21. incr Draw_CmdIndex
  22. puts -nonewline "Draw\[$Draw_CmdIndex\]> "
  23. }
  24. set tcl_prompt2 {puts -nonewline "> "}
  25. #################################################
  26. # the help command in TCL
  27. #################################################
  28. proc help {{command ""} {helpstring ""} {group "Procedures"}} {
  29. global Draw_Helps Draw_Groups
  30. if {$command == ""} {
  31. # help general
  32. foreach h [lsort [array names Draw_Groups]] {
  33. dputs -intense "\n\n$h"
  34. set i 0
  35. foreach f [lsort $Draw_Groups($h)] {
  36. if {$i == 0} {
  37. puts ""
  38. puts -nonewline " "
  39. }
  40. puts -nonewline $f
  41. for {set j [string length $f]} {$j < 15} {incr j} {
  42. puts -nonewline " "
  43. }
  44. incr i
  45. if {$i == 4} {set i 0}
  46. }
  47. puts ""
  48. }
  49. } elseif {$helpstring == ""} {
  50. # help function
  51. set isfound 0
  52. foreach f [lsort [array names Draw_Helps]] {
  53. if {[string match $command $f]} {
  54. dputs -nonewline -intense $f
  55. for {set j [string length $f]} {$j < 15} {incr j} {
  56. puts -nonewline " "
  57. }
  58. puts " : $Draw_Helps($f)"
  59. set isfound 1
  60. }
  61. }
  62. if {!$isfound} {
  63. if {[string first * $command] != -1} {
  64. puts "No matching commands found!"
  65. } else {
  66. puts "No help found for '$command'! Please try 'help $command*' to find matching commands."
  67. }
  68. }
  69. } else {
  70. # set help
  71. lappend Draw_Groups($group) $command
  72. set Draw_Helps($command) $helpstring
  73. }
  74. flush stdout
  75. }
  76. help help {help pattern, or help command string group, to set help} {DRAW General Commands}
  77. #################################################
  78. # the getsourcefile command in TCL
  79. #################################################
  80. help getsourcefile {getsourcefile, or getsourcefile command } {DRAW General Commands}
  81. proc getsourcefile {{command ""}} {
  82. global Draw_Helps Draw_Groups Draw_Files
  83. set out {}
  84. if {$command == ""} {
  85. # help general
  86. foreach h [lsort [array names Draw_Groups]] {
  87. lappend out "" "" "$h"
  88. set i 0
  89. foreach f [lsort $Draw_Groups($h)] {
  90. if {$i == 0} {
  91. lappend out ""
  92. }
  93. incr i
  94. #
  95. # check that the command has its source file set
  96. #
  97. foreach command_that_has_file [array names Draw_Files] {
  98. if {($command_that_has_file == $f)} {
  99. lappend out [format {%-20s %s} $f $Draw_Files($f)]
  100. }
  101. }
  102. }
  103. }
  104. } else {
  105. # getsourcefile fonction
  106. append command "*"
  107. foreach f [lsort [array names Draw_Files]] {
  108. if {[string match $command $f]} {
  109. lappend out [format {%-20s %s} $f $Draw_Files($f)]
  110. }
  111. }
  112. }
  113. return [join $out "\n"]
  114. }
  115. #################################################
  116. # whatis
  117. #################################################
  118. #proc gwhatis {aVarName} {
  119. # global $aVarName
  120. # puts -nonewline $aVarName; puts -nonewline " is a "; puts [dtyp ${aVarName}]
  121. #}
  122. help whatis "whatis object1 object2 ..."
  123. proc whatis args {
  124. set __out_string ""
  125. foreach i $args {
  126. if {$i == "."} {set i [dname $i]}
  127. #gwhatis $i
  128. global $i
  129. set __tmp_string "$i is a [dtyp $i]\n"
  130. set __out_string "${__out_string}${__tmp_string}"
  131. }
  132. return ${__out_string}
  133. }
  134. #################################################
  135. # library, lsource
  136. #################################################
  137. proc library lib {
  138. global auto_path
  139. set auto_path [linsert $auto_path 0 $lib]
  140. if [file readable $lib/LibraryInit] {
  141. puts "Loading $lib/LibraryInit"
  142. uplevel "source $lib/LibraryInit"
  143. }
  144. }
  145. proc lsource file {
  146. if [file readable $file] {source $file} else {
  147. global auto_path
  148. foreach dir $auto_path {
  149. if [file readable $dir/$file] {
  150. uplevel #0 "source $dir/$file"
  151. break
  152. }
  153. }
  154. }
  155. }
  156. #################################################
  157. # directory
  158. #################################################
  159. proc isgdraw {var} {
  160. global $var
  161. return [isdraw $var]
  162. }
  163. help directory {directory [pattern], list draw variables} {DRAW Variables management}
  164. proc directory {{joker *}} {
  165. set res ""
  166. foreach var [info globals $joker] {
  167. if [isgdraw $var] {lappend res $var}
  168. }
  169. return $res
  170. }
  171. proc lsd {} { exec ls [datadir] }
  172. proc dall {} {
  173. set schmurtz ""
  174. foreach var [info globals] {
  175. global $var
  176. if [isdraw $var] {
  177. if ![isprot $var] {
  178. lappend schmurtz $var; unset $var
  179. }
  180. }
  181. }
  182. return $schmurtz
  183. }
  184. #################################################
  185. # repeat, do
  186. #################################################
  187. proc repeat {val script} {
  188. for {set i 1} {$i <= $val} {incr i} {uplevel $script}
  189. }
  190. proc do {var start end args} {
  191. global errorInfo errorCode
  192. if {[llength args] == 1} {
  193. set incr 1
  194. set body args
  195. } else {
  196. set incr [lindex 1 args]
  197. set body [lindex 2 args]
  198. }
  199. upvar $var v
  200. if {[dval $incr] < 0} {set rel >=} else {set rel <=}
  201. for {dset v $start} {[dval v] $rel [dval end]} {dset v [dval v+($incr)]} {
  202. set code [catch {uplevel $body} string]
  203. if {$code == 1} {
  204. return -code error -errorInfo $errorInfo -errorcode $errorCode $string
  205. } elseif {$code == 2} {
  206. return -code return $string
  207. }elseif {$code == 3} {
  208. return
  209. } elseif {$code > 4} {
  210. return -code $code $string
  211. }
  212. }
  213. }
  214. #################################################
  215. # datadir, save, restore
  216. #################################################
  217. set Draw_DataDir "."
  218. help datadir {datadir [directory]} "DRAW Variables management"
  219. proc datadir {{dir ""}} {
  220. global Draw_DataDir
  221. if {$dir != ""} {
  222. if {![file isdirectory $dir]} {
  223. error "datadir : $dir is not a directory"
  224. } else {
  225. set Draw_DataDir $dir
  226. }
  227. }
  228. return $Draw_DataDir
  229. }
  230. help save {save variable [filename]} "DRAW Variables management"
  231. proc save {name {file ""}} {
  232. if {$file == ""} {set file $name}
  233. upvar $name n
  234. if {![isdraw n]} {error "save : $name is not a Draw variable"}
  235. global Draw_DataDir
  236. bsave n [file join $Draw_DataDir $file]
  237. return [file join $Draw_DataDir $file]
  238. }
  239. help restore {restore filename [variablename]} "DRAW Variables management"
  240. proc restore {file {name ""}} {
  241. if {$name == ""} {
  242. # if name is not given explicitly, use name of the file w/o extension
  243. set name [file rootname [file tail $file]]
  244. }
  245. global Draw_DataDir
  246. upvar $name n
  247. brestore [file join $Draw_DataDir $file ] n
  248. return $name
  249. }
  250. #################################################
  251. # misc...
  252. #################################################
  253. proc ppcurve {a} {
  254. 2dclear;
  255. uplevel pcurve $a;
  256. 2dfit;
  257. }
  258. #################################################
  259. # display and donly with jokers
  260. #################################################
  261. help disp {display variables matched by glob pattern} "DRAW Variables management"
  262. proc disp { args } {
  263. set res ""
  264. foreach joker $args {
  265. if { $joker == "." } {
  266. dtyp .
  267. set joker [lastrep id x y b]
  268. }
  269. foreach var [info globals $joker] {
  270. if { $var == "." } {
  271. dtyp .
  272. set var [lastrep id x y b]
  273. }
  274. if [isgdraw $var] {lappend res $var}
  275. }
  276. }
  277. uplevel #0 eval display $res
  278. return $res
  279. }
  280. help don {display only variables matched by glob pattern} "DRAW Variables management"
  281. proc don { args } {
  282. set res ""
  283. foreach joker $args {
  284. if { $joker == "." } {
  285. dtyp .
  286. set joker [lastrep id x y b]
  287. }
  288. foreach var [info globals $joker] {
  289. if { $var == "." } {
  290. dtyp .
  291. set var [lastrep id x y b]
  292. }
  293. if [isgdraw $var] {lappend res $var}
  294. }
  295. }
  296. uplevel #0 eval donly $res
  297. return $res
  298. }
  299. help del {unset (remove) variables matched by glob pattern} "DRAW Variables management"
  300. proc del args {
  301. set res ""
  302. foreach joker [eval concat $args] {
  303. if { $joker == "." } {
  304. dtyp .
  305. set joker [lastrep id x y b]
  306. }
  307. foreach var [directory $joker] {
  308. global $var
  309. if ![isprot $var] {
  310. lappend res $var; unset $var
  311. }
  312. }
  313. }
  314. return $res
  315. }
  316. help era {erase variables matched by glob pattern} "DRAW Variables management"
  317. proc era args {
  318. set res ""
  319. foreach joker [eval concat $args] {
  320. if { $joker == "." } {
  321. dtyp .
  322. set joker [lastrep id x y b]
  323. }
  324. eval lappend res [directory $joker]
  325. }
  326. if [llength $res] {
  327. uplevel \#0 eval erase $res
  328. }
  329. }
  330. # The following commands (definitions are surrounded by if) are
  331. # available in extended Tcl (Tclx).
  332. # These procedures are added just to make full-working simulations of them.
  333. if {[info commands lvarpop] == ""} {
  334. proc lvarpop args {
  335. upvar [lindex $args 0] lvar
  336. set index 0
  337. set len [llength $lvar]
  338. if {[llength $args] > 1} {
  339. set ind [lindex $args 1]
  340. if [regexp "^end" $ind] {
  341. set index [expr $len-1]
  342. } elseif [regexp "^len" $ind] {
  343. set index $len
  344. } else {set index $ind}
  345. }
  346. set el [lindex $lvar $index]
  347. set newlvar {}
  348. for {set i 0} {$i < $index} {incr i} {
  349. lappend newlvar [lindex $lvar $i]
  350. }
  351. if {[llength $args] > 2} {
  352. lappend newlvar [lindex $args 2]
  353. }
  354. for {set i [expr $index+1]} {$i < $len} {incr i} {
  355. lappend newlvar [lindex $lvar $i]
  356. }
  357. set lvar $newlvar
  358. return $el
  359. }
  360. }
  361. if {[info commands lmatch] == ""} {
  362. proc lmatch args {
  363. set mode [switch -- [lindex $args 0] {
  364. -exact {format 0}
  365. -glob {format 1}
  366. -regexp {format 2}}]
  367. if {$mode == ""} {set mode 1} else {lvarpop args}
  368. if {[llength $args] < 2} {puts "usage: lmatch ?mode? list pattern";return}
  369. set list [lindex $args 0]
  370. set pattern [lindex $args 1]
  371. set res {}
  372. foreach a $list {
  373. if [switch $mode {
  374. 0 {expr [string compare $a $pattern] == 0}
  375. 1 {string match $pattern $a}
  376. 2 {regexp $pattern $a}}] {lappend res $a}
  377. }
  378. return $res
  379. }
  380. }