Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Jeremie0 on June 11, 2014, 11:22:56 pm
-
Hello everyone,
I've looked everywhere but I can't find any info on how to copy context references from one usecase to another in C#
Any idea ?
-
What do you mean by "context references"?
q.
-
the actors and systems related to the steps of a scenario
like in the screenshot
(http://s14.postimg.org/v83omphsh/Sans_titre.png)
-
Those are just a summary of elements you find when scanning the connectors collection. So if you create connectors for the copied use case you will automatically find the references.
q.
-
i'm sorry i don't get it...
i've created an element and copied the scenario from the source element to the new one
how do you get "actor" and "CS" for example ?
-
Have a look at the connectors of the source use case. Just copy these connectors and you will see the Context References as well in the copy.
q.
-
i get an error everytime i try to add a connector
foreach (EA.Connector ct in cts)
{
EA.Element tmp = Repository.GetElementByID(ct.ClientID);
ctstarget = ctstarget.AddNew(tmp.Name, ct.Type);
}
and when i do the following, it works but no context references appear in the scenario
foreach (EA.Connector ct in cts)
{
EA.Element tmp = Repository.GetElementByID(ct.ClientID);
EA.Connector con = ctstarget.AddNew(tmp.Name, ct.Type);
//con.Update();
}
-
As long as you comment the Update you will not create a connector.
q.
-
that's precisely where the problem is.
With the update uncommented, everytime i run the addin i get the message
"An extern component encountered an exception"
-
Looking at it a bit more precisely you lack to supply the SupplierID. Maybe you should have a look in my Scripting book.
N.B. The awful indentation makes reading your code not an easy task.
q.
-
it still creates an exception :(
-
foreach (EA.Connector ct in cts)
{
EA.Element tmp = Repository.GetElementByID(ct.ClientID);
EA.Connector con = ctstarget.AddNew(tmp.Name, ct.Type);
con.SupplierID = ct.SupplierID;
if (!con.Update())
{
MessageBox.Show(con.GetLastError());
}
}
sorry for the double post
-
Your code lacks quite some essentials.
useCaseOrg = Repository.GetElementByID(orgID);
useCaseCopy = package.Elements.AddNew(useCaseOrg.Name, "UseCase");
useCaseCopy.Update();
for (EA.Connector con in useCaseOrg.connectors) {
EA.Connector conCopy = useCaseCopy.Connectors.AddNew (con.name, con.type);
conCopy.supplierID = con.SupplierID;
conCopy.Update();
}
I wrote this out of my head with no testing. But it should point you in the right direction.
q.
-
thanks but that's what i wrote already...
-
Too bad I'm not given clairvoyance. So I'm out.
q.
-
the code was missing the following instruction
conCopy.ClientID = con.ClientID;
now the error is gone, but still no context references in the new scenario
-
This is because you do not create the source and client id correctly. When you're done you will see that the connectors for the new UC are not the same as that of the original one. It needs some additional if-then-else to create the connectors correctly. Try using your brain.
q.
-
my $e = $rep->GetTreeSelectedObject();
my $pk = $rep->GetPackageByID ($e->PackageID);
my $uc2 = $pk->Elements->AddNew ($e->Name . "2" , $e->Type);
$uc2->Update();
for my $con (in $e->Connectors) {
my $con2 = $uc2->Connectors->AddNew ($con->Name, $con->Type);
$con2->{SupplierID} = $con->SupplierID;
$con2->Update();
}
works for connectors drawn from UC to other elements, but not vice versa.
q.
-
i basically have the same code, at least i think
EA.Diagram diagram = Repository.GetDiagramByID(droppedID);
EA.Package element2 = Repository.GetPackageByID(diagram.PackageID);
EA.Element a = element2.Elements.AddNew(element.Name, element.Type);
a.Name=a.Name.Replace(a.Name, a.Name + "_tmp");
element2.Elements.Refresh();
a.Update();
EA.Collection targets = a.Scenarios;
foreach (Scenario source in element.Scenarios)
{
EA.Scenario target = targets.AddNew(source.Name, source.Type); //on cree un nouveau scenario du meme nom
var form = new Form1();
var checkedListBox1 = new CheckedListBox();
form.Text = target.Name;
targets.Refresh();
EA.Collection targetsteps = target.Steps;
EA.Collection cts = element.Connectors;
EA.Collection ctstarget = element2.Connectors;
int i = 0;
foreach (ScenarioStep aaa in source.Steps)
{
form.checkedListBox1.Items.Add(aaa.Name);
form.checkedListBox1.SetItemChecked(i, true);
foreach (EA.Connector ct in cts)
{
EA.Connector con = (EA.Connector)ctstarget.AddNew("", ct.Type);
con.SupplierID = ct.SupplierID;
con.ClientID = ct.ClientID;
if(!con.Update())
{
MessageBox.Show(con.GetLastError());
}
}
ctstarget.Refresh();
i++;
element2.Update();
}
anyway thanks for all your help i guess i'll find a workaround
-
You have a similar code but as I pointed out that is not sufficient. Use a debugger to find out why you are not copying the connectors correctly.
q.
-
Furthermore: you are copying the connectors more than once if you have multiple scenarios. Move the connector copy loop outside on the same level as that for the scenarios.
q.
-
ok i figured where the problem was.
The supplierID is the elementID of the new element i created, not a copy of the old one
thanks qwerty for your help
foreach (EA.Connector ct in element.Connectors)
{// ON CHERCHE A COPIER LES CONTEXT ITEMS, PAS LES CONNECTEURS
EA.Connector con = (EA.Connector) element2.Connectors.AddNew("", ct.Type);
con.ClientID = ct.ClientID; // <-- element2 est un package pas un element, trouver l'acteur id
//string prov = Repository.GetConnectorByID(ct.ClientID).Name;
con.SupplierID = a.ElementID;
//prov = Repository.GetConnectorByID(ct.SupplierID).Name;
try
{
con.Update();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
-
although it seems like there's a small problem
not all the actors in the context references are copied in the new use case
and i can't figure out why
any idea ?
-
Compare the Element/Relationships window for the original and the copied use case.
q.
-
yeah i get it, the assocations in the newly created usecase are those linked to the source usecase in the diagram
so do you have an idea
- to create an association in the new usecase ?
- or just to add the missing ones in the context references ?
-
What I said formerly: some of the connectors are probably drawn vice versa. So you need to create them on the actor side, not on the use case.
q.