Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: marine1981 on November 16, 2010, 12:29:02 am
-
I search the available diagram. How I get this diagram? I select the Package and then I create a “New Package” with a “New Diagram”. Now, I want insert the New Diagram in the available diagram”. I use the OnPostNewPackage-Event with C#
My tree:
+ Package
++ Diagram
++ New Element for the “New Diagram”
++ NewPackage
+++ New Diagram
EA.Package ParentPackage = Repository.GetPackageByID(NewPackage.ParentID)
…
for(short i=0; i < ParentPackage.Diagrams.Count-1; i++)
{
MessageBox.Show(“Do somethinkg” + i);
}
-
Sven,
First of all, you can use foreach on EA.Collection, that is a bit more elegant then your for loop.
Further you need to add Something to the Diagram.DiagramObjects collection to put something on the diagram.
Geert
-
I have already inserted a Element into the Diagram. Later, I want connect the both "Elements".
-
To Connect the elements use Element.Connectors on the source and fill in the ID of the target on the new Connector before saving it.
Geert
-
How I can use the EA.Collection with foreach?
EA.Collection DiagramSearch = ParentPackage.Diagrams. ....;//?
foreach(short i in DiagramSearch)
{
MessageBox.Show("Test: " + Convert.ToString(i));
}
-
foreach (EA.Diagram actDiagram in ParentPackage.Diagrams) {
actDiagram...();
}
-
I have got under a selected Package a Diagram in the Project Browser. How I can get the Diagram? It exists only one Diagram. How I can do this one?
-
If you are sure there is only one diagram you can do
Package.Diagrams.GetAt(0)
But you better put a test before that, so something like
if (myPackage.Diagrams.Count > 0)
{
myDiagram = (EA.Diagram)Package.Diagrams.GetAt(0);
}
Geert
-
It does work well. Now, I want conntect both elements.
I have already the ID vom the new element. Now, I need the ElementID from the Element with Stereotype "MyStereotypeName". How I can search the right element in the Diagram?
-
My Idea:
Not much of an idea then ;D
I think you best loop the DiagramObjects of your diagram, get the ElementID of the diagramObject, get the element using Repository.GetElementByID and check the stereotype.
Geert
-
It's late ;)
My Idea is:
foreach(EA.Element ParentElement in ParentDiagramObject)
{
ParentElement = Repository.GetElementByID(ParentElement.ElementID);
if(ParentElement.Stereotype == "StereotypeName");
{
//Conntect the elements
}
}
-
My problem is very simple. I don't kow, how i can get the upper element from the diagram(not Project Browser). The upper element has got a clear stereotyp. The other elements in the diagram has got the same stereotyp. How I can get the the element with the clear stereotype?
My idea was:
foreach(EA.Element ParentElement in ParentDiagramObject)
{
//a sample code
MessageBox.Show(ParentElement.Stereotype);
}
But it does not work :(
-
So do you need the element that owns the diagram, or an element that is shown on the diagram?
Geert
-
I need the first element(main element) from the diagram. Is the first element the owner of the diagram?
-
How do you determine the "first" or "main" object in a diagram?
In a diagram all objects are created equal ;)
The owning element is the element which owns the diagram, which means the diagram is nested under the element in the project browser like
Package -> Onwer of element
- Element -> Owner of diagram
- Diagram
Geert
-
The first element is the first element which was created on the diagram. The other elements which later was inserted on the diagram should connected to the first element. My structure Diagram, Package and Element Structure in the Project Browser is not normal.
+ 1. Layer
++ 2. Layer
+++ 3. Layer
My structure:
+Select Package
+First Element
++ Diagram
++ New Package
++ New first Element for the 2. Diagram
+++ 2. Diagram
+++ ...
-
EA doesn't record the order in which elements are created on diagrams, but in your case it seems that you can work with the owner of the diagram.
So if you want to connect all elements shown on the diagram to the owner element I would do something like:
EA.Element ownerElement = myRepository.GetElementByID (myDiagram.ParentID);
foreach (EA.DiagramObject myDiagramObject in myDiagram.DiagramObjects)
{
if (myDiagramObject.ElementID != ownerElement.ElementID)
{
EA.Connector newCon = ownerElement.Connectors.AddNew("","Association");
newCon.SupplierID = myDiagramObject.ElementID;
newCon.Update();
}
}
Geert
-
Thanks. How I define the owner element on the diagram? Does EA define the owner element automatically?
-
If you create the new diagram under the Element then its ElementID is stored in the Diagram.ParentID.
Geert
-
I get a error message:
EA_OnPostNewPackage: Can't find matching ID
mm...
-
What's in the Diagram.ParentID?
Geert
-
Here my code:
//Insert new element in upper Diagram
if(ParentPackage.Diagrams > 0)
EA.Diagram ParentDiagram = (EA.Diagram)ParentPackage.Diagrams.GetAt(0);
EA.DiagramObject ParentDiagramObject = (EA.DiagramObject)ParentDiagram.DiagramObjects.AddNew("","");
ParentDiagramObject.ElementID = NewElement.ElementID;
ParentDiagramObject.Update();
//Connect both elements
EA.Element ownerElement = Repository.GetElementByID(ParentDiagram.ParentID);
foreach(EA.DiagramObject myDiagramObject in ParentDiagram.DiagramObjects)
{
if(myDiagramObject.ElementID != ownerElement.ElementID)
{
EA.Connector newCon = [color=#ff00ff]ownerElement.Connectors.AddNew("","Associtaion");[/color]
newCon.SupplierID = myDiagramObjecg.ElementID;
newCon.Update();
}
}
The error message:
The Type "object" can not implicit convert in "EA.Connector". It is already exist an explicit connversation.
Casting ist not helpful.
Do you have got any idea?
Thanks
Sven
-
Yeah, you have to remember to always cast the objects returned by Collection.AddNew()
so
EA.Connector newCon = (EA.Connector)ownerElement.Connectors.AddNew("","Association");
should work
Geert
-
Hello,
I have tried it before but it does not work. I get the error message:
EA_OnPostNewPackage: Can't find matching ID
Do you have got any idea?
-
On which line?
Are you sure the diagram is nested under the element? (what does Diagram.ParentID have as value?)
Has the element been saved?
Geert
-
Element saved?
What does it mean?
I get the error message in EA not in Visual C#
-
In the project browser, is the direct parent of the diagram an element or a package?
This should be reflected by the Diagram.ParentID. If it is owned by a package then the ParentID will be 0.
I'm sure you get the error in EA, but you'll need to debug to figure out which line is causing the problem.
Geert
-
Now, I have insered a MessageBox.Show(Convert.ToString(ParentDiagram.ParentID));
And I get a 0 in the MessageBox.
I unfortunately use Visual C# 2008 Express. So debug does not work. :(
My structure:
+ 1. Layer
++ 2. Layer
+++ 3. Layer
+Select ParentPackage
+First (Parent)Element (to connect)
++ Parent Diagram (to connect both elements in this diagram)
++ New Package
++ New first Element for the 2. Diagram and the Parent Diagram (to connect)+++ 2. Diagram
+++ ...
-
Sven,
I know you cannot connect your debugger to EA.exe, but you can debug your code if you make a small test application that connects to the running EA instance and runs your code (or the part that matters)
this code will connect to the open instance of EA.
using System.Runtime.InteropServices;
...
//connect to the currently running instance
//if multiple instances are running it will connect to the instance that was first started.
object obj = Marshal.GetActiveObject("EA.App");
EA.App eaApp = obj as EA.App;
wrappedModel = eaApp.Repository;
Geert
-
Do you mean a seperate program like command-program(DOS)?
Or a integration in the Add-In?
object obj = Marshal.GetActiveObject("EA.App");
EA.App eaApp = obj as EA.App;
wrappedModel = eaApp.Repository;
How I define/create the object wrappedModel because Visual don't know the object "wrappedModel".
-
Just a regular Windows Forms application with a button or something to start your test.
That's how I usually test my addins (avoids having to restart EA each time you recompile)
Geert
-
How I can define/create the object wrappedModel becaus Visual doesn't know it.
-
wrappedModel is a variable I defined of type EA.Repository.
The snippet is part of my wrapper class for EA.Repository.
Geert
-
I have created the application.
How I use the application to debug, now?
-
Start it and put a breakpoint in your operation, then you can go step by step.
Geert
-
How I set under Debuggen=> Startoptions the command/arguments? Which folder does it need?
-
Sven, if you have created a windows forms application you should be able to just start it by clicking the green |> icon.
Geert
-
Hello,
it is possible to start but i get a message "A Project with the Type "ClassLibery" cannot start directly".
-
That's because you have to start of project of type "Windows Forms Application"
Geert
-
Yes, of course. Thats my problem so i cannot debug.