File: example_chemical.tcl

package info (click to toggle)
r-cran-tcltk2 1.2-10-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,356 kB
  • ctags: 1,386
  • sloc: tcl: 37,888; ansic: 792; python: 324; sh: 68; sed: 16; makefile: 1
file content (82 lines) | stat: -rwxr-xr-x 1,640 bytes parent folder | download | duplicates (5)
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
# A small demonstration: drawing chemical structures
#
# NOTE: needs fixing!
#
package require Tk
source ./draw_diagram.tcl
#package require Diagrams
namespace import ::Diagrams::*

pack [canvas .c -width 400 -height 130 -bg white]

#console show
drawin .c

proc ring {} {
   set side 20
   line $side 60 $side 0 $side -60 $side -120 $side 180 $side 120
}
proc benzene {} {
   set item [ring]

   foreach {x1 y1 x2 y2} [lindex $item 2] {break}

   $::Diagrams::state(canvas) create oval \
      [expr {($x1+$x2)/2-12}] [expr {($y1+$y2)/2+12}] \
      [expr {($x1+$x2)/2+12}] [expr {($y1+$y2)/2-12}]

   return $item
}

proc bond { {angle 0} {item {}} } {
   set side 20

   set anchor E
   switch -- $angle {
   "0"   { direction E  ; set anchor E  }
   "60"  { direction NE ; set anchor NE }
   "90"  { direction N  ; set anchor N  }
   "120" { direction NW ; set anchor NW }
   "180" { direction W  ; set anchor W  }
   "240" { direction SW ; set anchor SW }
   "-90" -
   "270" { direction S  ; set anchor S  }
   "-60" -
   "300" { direction SE ; set anchor SE }
   }

   if { $item != {} } {
      currentpos [getpos $anchor $item]
   }

   line $side $angle
}

#
# Very primitive chemical formula
# -- order of direction/currentpos important!
#
direction east
currentpos [position 40 60]
benzene; bond;

set Catom [plaintext C]

bond 90 $Catom
direction north
plaintext C
direction east
plaintext OOH

bond -90 $Catom
direction south
plaintext H

bond 0 $Catom
direction east
benzene
bond; direction east
plaintext NH2  ;# UNICODE \u2082 is subscript 2, but that is not
                # supported in PostScript

#saveps chemical.eps