1. A chemical engineering ideal stage calculator
- Can read and save data to an ascii file
- Can add points with pointer or input location
- Can input an equation to plot as data
- Can clear data and start again
- Location of mouse pointer is displayed on screen
require 5.004;
use Tk 800.023;
use Tk qw/:eventtypes/;
use Tk::FileSelect;
use Tk::toplevel;
use Tk::Dialog;
use Cwd;
# ////////////////////////////////// Fundamentals
my(@menu_button_list, $quit_flag, $quit_code, $add_code);
my($scale,$screen_bump, $sb, $screen_sizex, $ssx, $beed_size, $bs);
$scale = 1;
$screen_bump = 20;
$sb = $screen_bump;
$screen_sizey = 400;
$ssy = $screen_sizey;
$screen_sizex = 400;
$ssx = $screen_sizex;
$beed_size = 1;
$bs = $beed_size;
2. @glines = [0,0,0,0];
@dlines = [0];
@pointsv = [0,0];
@pointsl = [0,0];
$dataldd = 0;
# ////////////////////////////////// Main Frame
$m->destroy if Exists($m);
$m = Tk::MainWindow->new;
$m->title('Distillation Simulator');
$m->iconname('sim');
$quit_flag = 0;
$quit_code = sub {$quit_flag = 1};
$m->protocol('WM_DELETE_WINDOW' => $quit_code);
$fsel = $m->FileSelect
(-directory => Cwd::cwd(), # Alias: -initialdir
-initialfile => "data.dat",
-filter => "*.pl",
-regexp => '*.*', #' does not work (?)
-filelabel => "Datei",
-filelistlabel => "Dateien",
-dirlabel => "Verzeichnis",
-dirlistlabel => "Verzeichnisse",
-verify => ['-T'], # accept only text files
);
# ////////////////////////////////// Menu
9. $m->bind($c, '<Button-1>' => &AddVCoord);
$m->bind($c, '<Button-3>' => &AddLCoord);
$m->bind($c, '<Leave>' => &DeleteCoord);
my $otm, $ott;
sub ShowCoord{
$c->delete($otm);
$c->delete($ott);
my $e = $c->XEvent;
# Note: Cap X returns global x coord, Low Case x returns Canvas local coord.
my $xm = $e->x;
my $ym = $e->y;
my $xmt = ($xm-$sb)*$scale/$ssx;
my $ymt = ($sb+$ssy-$ym)*$scale/$ssy;
$otm = $c->createOval($xm-$beed_size,$ym-$beed_size,$xm+$beed_size,$ym+$beed_size);
$ott = $c->createText($xm,$ym-9,-text => $xmt.",".$ymt);
# print "x ".$xm." y ".$ym."n";
};
sub DeleteCoord{
$c->delete($otm);
$c->delete($ott);
}
sub AddVCoord{
my $e = $c->XEvent;
my $xm = ($e->x-$sb)*$scale/$ssx;
my $ym = ($sb+$ssy-$e->y)*$scale/$ssy;
10. add_vapor($xm,$ym);
print "Adding point : X ".$xm." Y ".$ym."n";
};
sub AddLCoord{
my $e = $c->XEvent;
my $xm = ($e->x-$sb)*$scale/$ssx;
my $ym = ($sb+$ssy-$e->y)*$scale/$ssy;
add_liquid($xm,$ym);
print "Adding point : X ".$xm." Y ".$ym."n";
};
# ////////////////////////////////// Get Data and Plot
sub clear_data{
del_vapor(0,0);
del_liquid(0,0);
}
sub load_data{
if($dataldd>0){
print "Deleting previous datan";
del_vapor(0,0);
del_liquid(0,0);
}
$datafile = $fsel->Show;
open(DATA, "$datafile") || die "Can't open $datafile: $!n";
$vapor = 1;
while(<DATA>)
24. # foreach (@templ) { print "Temp points 0 : ".@{$_}[0]." 1 : ".@{$_}[1]."n"; };
foreach (@templ) {
$point = [@{$_},0];
($xc,$yc) = (${$point}[0],${$point}[1]);
$xc = $xc*$ssx/$scale+$sb;
$yc = $yc*$ssy/$scale+$sb;
${$point}[2] = $c->createOval($xc-$bs,$sb+$sb+$ssy+$bs-$yc,$xc+$bs,$sb+$sb+$ssy-$bs-$yc);
push @pointsl, $point;
};
foreach (@pointsl) { print "New liquid points 0 : ".@{$_}[0]." 1 : ".@{$_}[1]."n"; };
};
# //////////////////////////////////////////////////////////////////////////////
# // Menu Bar Code
# //////////////////////////////////////////////////////////////////////////////
sub mkmb {
# (Ripped from nTk examples)
# Make a Menubutton widget; note that the menu is automatically created.
# We maintain a list of the Menubutton references since some callers
# need to refer to the Menubutton, as well as to suppress stray name
# warnings with Perl -w.
my($mb0, $mb_label, $mb_label_underline, $mb_msg, $mb_list_ref) = @_;
my $mb = $mb0->Menubutton(
-text => $mb_label,
-underline => $mb_label_underline,
-background => 'DarkGreen',