Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: thomaskilian on December 02, 2004, 03:02:06 am
-
Hi PERLies,
I wonder if someone comes up with a better solution? To call Perl automation from within EA I use the following construct:
Command: C:\Programme\Sparx Systems\EA\Code Samples\PERL\test.bat
Arguments: <perlfile>, "package=$p", "diagram=$d", "element=$e", "file=$f"
where <perlfile> is the name of the perl file containing my code.
test.bat looks like
@echo off
perl "C:\Programme\Sparx Systems\EA\Code Samples\PERL\%1.pl" "%2" "%3" "%4" "%5" "%6" "%7" "%8" %9"
pause
In my called perl files I use a class EAparms.pm
package EAparms;
use strict;
use Win32::OLE;
sub new {
my $self = {};
my $parms = {};
my $atFile = 0;
foreach my $v (@ARGV) {
# print $v, "\n";
if ($atFile) {
if ($v) { $parms->{$1} .= " " . $v;}
} elsif ($v =~ /(.*)=(.*)/) {
$parms->{$1} = $2;
$atFile = ($1 eq 'file');
}
}
$self->{MODEL} = Win32::OLE->new('EA.Repository', \&OleQuit) or die "oops\n";
$self->{MODEL}->OpenFile($parms->{file}) or die "File not found";
$self->{DIAGRAM} = $self->{MODEL}->GetDiagramByID ($parms->{diagram})
if (defined ($parms->{diagram}));
$self->{PACKAGE} = $self->{MODEL}->GetPackageByID ($parms->{package})
if (defined ($parms->{package}));
my $i = 0;
foreach my $e (split /,/, $parms->{element}) {
$self->{ELEMENT}->[$i++] = $self->{MODEL}->GetElementByID ($e);
}
$self->{ELEMENTS} = $i;
# print "'", $parms->{file}, "'";
bless $self;
}
#########################################################
# some cleanup for EA
#
sub OleQuit {
my $self = shift;
$self->Exit();
}
1;
which returns the opened ea file and possible diagrams/elements. My main problem was getting the filename right. This version works with filenames that have at highest single blanks embedded. I would not get it to work with a filename containg double blanks.
Any comment appreciated :)
-
I tried it and it works wonderfully, thanks for the tip
Just remove the double quotes in your bat file and you are set for the file name (you already have the quotes in the EA tools configuration and it seems that the double double quotes kills each other
ie use
perl "C:\Programme\Sparx Systems\EA\Code Samples\PERL\%1.pl" %2 %3 %4 %5 %6 %7 %8 %9
:D
-
Thomas,
The problem comes from the communication between DOS and Perl.
Any parameter with one or more blanks is split in two or more ARGV's.
I've been playing with your perl-script and the following setup works:
EA tools:
command: C:\test.pl
arguments: "file=$f" "package=$p" "diagram=$d" "element=$e"
(I have ".pl" files associated with perl so I can directly invoke a script)
Test.pl just creates a new EAparms-object. The script for EAparms looks like:
package EAparms;
use strict;
use Win32::OLE;
sub new {
my $self = {};
my $parms = {};
foreach my $v (@ARGV) {
# print ">", $v , "<\n";
if ($v =~ /(.*)=(\w.*)/) {
$parms->{$1} = $2;
}
}
# print "file: '", $parms->{file}, "'\n" if (defined ($parms->{file}));
# print "package: '", $parms->{package}, "'\n" if (defined ($parms->{package}));
# print "diagram: '", $parms->{diagram}, "'\n" if (defined ($parms->{diagram}));
# print "element(s): '", $parms->{element}, "'\n" if (defined ($parms->{element}));
$self->{MODEL} = Win32::OLE->new('EA.Repository', \&OleQuit) or die "Oops\n";
$self->{MODEL}->OpenFile($parms->{file}) or die "File not found";
$self->{DIAGRAM} = $self->{MODEL}->GetDiagramByID ($parms->{diagram})
if (defined ($parms->{diagram}));
$self->{PACKAGE} = $self->{MODEL}->GetPackageByID ($parms->{package})
if (defined ($parms->{package}));
my $i = 0;
foreach my $e (split /,/, $parms->{element}) {
$self->{ELEMENT}->[$i++] = $self->{MODEL}->GetElementByID ($e);
}
$self->{ELEMENTS} = $i;
bless $self;
}
#########################################################
# some cleanup for EA
#
sub OleQuit {
my $self = shift;
$self->Exit();
}
1;
If you want the <prevfile>-functionality you could replace test.bat with a test.pl that invokes the wanted perl-file.
HTH,
Oscar
ps
I'm no "Perlie". my last use of Perl was way back in the previous century.
-
fugazy, thanks for the hint. I was probably thinking too complicated ;)
Oscar,
what you suggested I tried at first. But unfortunately "test.pl parm1" doesn't pass "parm1" to @ARGV :P. Instead I had to call "perl test.pl parm1" (which you can't easily do with the tools menu) in order to access "parm1". So I worked out the above solution. I use W2K/ActiveState Perl. (I would also not vote for being called a "Perlie" but I have to admit that it has some advantages.)