Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: marine1981 on October 15, 2010, 08:17:52 pm
-
Hello,
i have problem, because this is my first add-in. I write this Add-In in C#.
My problem:
After then I create a new "Package" in the "Project Browser". I want automatically create a new Class Diagram. Someone have any idea?
I think, i need a Post-Event. But I don't know right the class or the .Net Object.
How I get the ID by the new Package?
Thanks
Sven
-
Sven,
Try EA_OnPostNewPackage
The info parameter contains the package id.
Geert
-
Thanks Geert. But I have a further question.
How I can create an new "Class Diagram"?
The example: Add and Manage Diagrams
...
package = m_Repository.GetPackageByID(5);
diagram = package.Diagrams.Add("Logical Diagram", "Logical");
...
I don't know the Class from the Object "package"? How I can create the Object package in C#?
Thanks
-
The example seems valid to me.
What do you mean by "I don't know the Class from the Object "package"?
Just look at the operation GetPackageByID. There it says that it will return an EA.Package object, so your variable "package" will need to be of the type EA.Package.
Geert
-
My Example:
public virtual bool EA_OnPostNewPackage(EA.Repository Repository, EA.EventProperties Info)
{
EA.Repository Repo1;
EA.Repository TestDiagram;
Repo1 = Repository.GetPackageByID(Info)
TestDiagram = Repo1.Diagrams.AddNew("Logical Diagram","Logical");
}
I can't create a new Diagram because IntelliSense can't find "Diagrams". Where is my logical mistake?
Thanks
-
My Example:
public virtual bool EA_OnPostNewPackage(EA.Repository Repository, EA.EventProperties Info)
{
EA.Package Repo1;
EA.Diagram TestDiagram;
Repo1 = Repository.GetPackageByID(Info)
TestDiagram = Repo1.Diagrams.AddNew("Logical Diagram","Logical");
}
I can't create a new Diagram because IntelliSense can't find "Diagrams". Where is my logical mistake?
Thanks
FTFY
Geert
-
Thanks, thats helpful!
Where I can find the definitions of EA.Package, EA.Diagram ...?
-
in the help file.
Geert
-
Which helpfile do you mean?
-
the EA help, as in RTFM ;)
Geert
-
EA Help = SDK?
I never find the EA.Package, EA.Diagram, ... there(I used the search from Acrobat Reader).
I search a Overview with all Classes und a small description on one letter. Do you know somethink like this one?
Thanks and excause me but this is my first Micro-Add-In.
-
Yep,
Search for "Repository" and select the second result. (Repository) If you then go to the Contents tab you'll see all the API classes and their documentation.
Geert
-
Overview on Page 189 in the SDK. :)
But now I have other question.
How I get the ID from the new Package.
public virtual bool EA_OnPostNewPackage(EA.Repository Repository, EA.EventProperties Info)
Which function do I need? My Idea was:
public virtual bool EA_OnPostNewPackage(EA.Repository Repository, EA.EventProperties Info)
{
EA.Package Repo1;
EA.Diagram TestDiagram;
Repo1 = Repository.GetPackageByID(Info/*It's false, because a object*/)
TestDiagram = Repo1.Diagrams.AddNew("Logical Diagram","Logical");
}
-
From TFM on EA_OnPostNewPackage
Info
EA.EventProperties
IN
Contains the following EventProperty objects for the new package:
PackageID: A long value corresponding to Package.PackageID.
From TFM on EventProperties:
Get (object Index)
EventProperty
Read only. Returns an EventProperty in the list, raising an error if Index is out of range.
Parameters:
Index: Variant - can either be a number representing a zero-based index into the array, or a string representing the name of the EventProperty. For example, Props.Get(3) or Props.Get("ObjectID").
So I guess you'll have to do something like
info.Get("PackageID")
to get the packageID.
Geert
-
Thats right!
But Info.Get("PackageID") does not work.
I have got a understanding problem with the method get(object Index).
How I use the method Get()?
The example Probs.Get(3) or Probs.Get("Objetct ID").
Is the number 3 equivalent with "ObjectID"?
Is ObjectID a variable, string or an enumeration?
-
Index is of type "object", so it can be any type, including string or integers.
I think the idea is to allow us to use both the index as the name of the property.
So it seems to be both Get("PackageID") as Get(0) (or Get(1), i don't know if it is zero- or one-based) should work.
Geert
-
I'm not really progressing. I've got a problem. With ...
diagram1 = package1.Diagrams.AddNew("Logical Diagram", "Logical");
it generates a new diagram. So far so good.
But at first I must set the pointe to the package1 with:
package1 = Repository.GetPackageByID(intPackageID)
How I get the intPackgeID? I have used Info.Get(object index) but without any success. Because I don't find the right parameter for this method. Do you haven any idea?
Thanks
Sven
-
Sven,
If I find the time later on I'll try it out, but in the meantime you can try to ask Sparx support using the link (http://www.sparxsystems.com/support/bug_report.html) on the bottom of the page.
They are usually quite responsive, but keep in mind that they live upside-down, and it's the middle of the night for them at the moment.
Geert
-
Oh, thanks. That's very nice.
-
Sven,
I did a little test, and this code works:
public void EA_OnPostNewPackage(EA.Repository Repository, EA.EventProperties Info)
{
EA.EventProperty packageID = Info.Get("PackageID");
EA.Package newPackage = Repository.GetPackageByID(int.Parse((string)packageID.Value));
string test = newPackage.Name;
}
Geert
-
Your code is very helpful. It generates a new diagram now but in the false level. I need the diagram under the package folder.
-
Sven,
Where it it creating the diagram then?
Post the code you have now, I'll have a look.
Geert
-
Hello Geert,
i think, i have got a update/refresh problem.
public virtual bool EA_OnPostNewPackage(EA.Repository Repository, EA.EventProperties Info)
{
EA.EventProperty PackageID = Info.Get("PackageID");
EA.Package NewPackage = Repository.GetPackageByID(int.Parse((string)PackageID.Value));
...
//------------------------------------------------------------
//Zuweisung des Packagenamens auf den Diagramnamen
//------------------------------------------------------------
//Trim() entfernt überflüssige Leerzeichen am Stringanfang und -ende
packageName = packageName.Trim();
EA.Diagram NewDiagram = (EA.Diagram)NewPackage.Diagrams.AddNew(Convert.ToString(packageName), "Logical");
NewPackage.Update();
NewDiagram.Update();
//------------------------------------------------------------
//Zuweisung elementName
//------------------------------------------------------------
// Werte für Substring(StartWert, Länge) werden berechnet
// Berechnung der Startposition am String
startWertStringPosition = packageName.IndexOf('-') + 1;
// Berechnung der Stringlänge
zeichenLaengeString = packageName.IndexOf('(') - startWertStringPosition;
elementName = packageName.Substring(startWertStringPosition, zeichenLaengeString);
//Trim() entfernt überflüssige Leerzeichen am Stringanfang und -ende
elementName = elementName.Trim();
EA.Element NewElement = (EA.Element)NewPackage.Elements.AddNew(Convert.ToString(elementName), "UseCase");
//------------------------------------------------------------
//Zuweisung des elementAlias
//------------------------------------------------------------
// Werte für Substring(StartWert, Länge) werden berechnet
// Berechnung der Startposition am String
startWertStringPosition = 0;
// Berechnung der Stringlänge
zeichenLaengeString = packageName.IndexOf('-');
elementAlias = packageName.Substring(startWertStringPosition, zeichenLaengeString);
//Trim() entfernt überflüssige Leerzeichen am Stringanfang und -ende
NewElement.Alias = elementAlias.Trim();
//------------------------------------------------------------
//Zuweisung elementStereotype
//------------------------------------------------------------
// Werte für Substring(StartWert, Länge) werden berechnet
// Berechnung der Startposition am String
startWertStringPosition = packageName.IndexOf("(Ebene") + 6;//Startwert
// Berechnung der Stringlänge
zeichenLaengeString = packageName.IndexOf(")") - startWertStringPosition;
elementStereotype = packageName.Substring(startWertStringPosition, zeichenLaengeString);
//Trim() entfernt überflüssige Leerzeichen am Stringanfang und -ende
NewElement.Stereotype = elementStereotype.Trim();
NewElement.Update();
NewPackage.Update();
NewDiagram.Update();
NewPackage.Elements.Refresh();
-
Seems OK to me, where is it creating the diagram?
Or do you mean that you don't see the diagram and the new element in the project browser immediately?
In that case Repository.AdviseElementChange (NewPackage.Element.ElementID) should fix that. (although I had some issues with that operation in the past, if it isn't working as expected try Repository.RefreshModelView (NewPackage.PackageID))
Geert
-
Yes, I don't see the diagram and the new element in the project browser immediately.
I think he need a update like "reload project".
After then I have got the right view.
When and Where it needs a update?
-
That is "normal". Apparently the GUI doesn't know about updates performed by the API, so you have to tell the GUI to update itself.
Geert
-
I think he need a update like "reload project".
After then I have got the right view.
When and Where it needs a update?
-
Or do you mean that you don't see the diagram and the new element in the project browser immediately?
In that case Repository.AdviseElementChange (NewPackage.Element.ElementID) should fix that. (although I had some issues with that operation in the past, if it isn't working as expected try Repository.RefreshModelView (NewPackage.PackageID))
Geert
-
I have fixed that but i does not work with one of the both fixes.
Repository.AdviceElementChange(NewPackage.Element.ElementID);
or
Repository.RefreshModelView(NewPackage.PackageID);
-
RefreshModelView(0) should reload the whole model, so I would try that first.
If all else fails you can still use Repository.OpenFile (string Filename) to completely reload the model.
Geert
-
Hello,
thank you for your helpful answer. RefreshModelView(0) does work, but it closes the folder tree/folder structure and I don't want this kind of behavior.
I have inserted the package ID(RefreshModelView(PackageID)), too. But this way is not better. Do you have any idea?
-
Yes, you can select your package (or the new diagram) again, so it will open the tree again.
Look at Repository.ShowInProjectView (object Item)
Geert
-
THANKS! It does work, now! :)
My solution:
//Reload Project Browser
Repository.RefreshModelView(0);
//Select new Diagram in the Prjoject Browser
Repository.ShowInProjectView(NewDiagram);
:)
-
Now, I have got a further problem with the new element in the Project Browser. I create the element with:
EA.Element NewElement = (EA.Element)NewPackage.Elements.Add(...);
The program creates the element in th new Package in the Project Browser. But I need the element in the same level like the new package and not under the package. How I can move the element and How I can set the position at the creation in the tree? I have tested with PosTree but without any success. Do you have got any idea?
Thanks
Sven
-
Sven,
In that case you'll have to add the diagram on the parentPackage of the new package.
So you'll have to do something like
EA.Package myParentPackage = myRepository.GetPackageByID(myNewPackage.ParentID);
myParentPackage.Diagrams.AddNew(...
Geert
-
Your information was helpful.
I modified you example for a element:
EA.Package ParentPackage = Repository. GetPackageByID(NewPackage.ParentID);
EA.Element NewElement = (EA.Element)ParentPackage.AddNew(Convert.ToString(elementName), "UseCase");
NewElement.... = "...";
An it does work!
Thanks
Sven
-
I see a mistake, now. I don't can manual delete the new element in the Project Browser.
Do you have got any reason for this manner?
-
No, try reloading, that might help.
Geert
-
Reload without a newstart does not work. First after a new start I can delete the elements. Why?
-
No idea :-/
Geert
-
How I can create a Composition? With:
NewConnector = (EA.Connector)NewElement.Connectors.AddnNew("","Composition");
I get with this code only a aggreation and not a Composition.
Thanks
Sven
-
Sven,
You have to set the ConnectorEnd.Aggregation to the value "2" to make it composite.
(by the way, you might want to use regular associations iso aggregations, EA treats them a little different, although in UML the only difference is in the AggregationKind)
Geert
-
Thanks! It does work!