Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: marine1981 on October 15, 2010, 08:17:52 pm

Title: Create autom. a Class Diagram after a new Package
Post 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

Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 15, 2010, 10:10:34 pm
Sven,

Try EA_OnPostNewPackage
The info parameter contains the package id.

Geert
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 18, 2010, 05:15:58 pm
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 18, 2010, 05:34:19 pm
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 18, 2010, 05:59:27 pm
My Example:
Code: [Select]
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 18, 2010, 06:09:16 pm
Quote
My Example:
Code: [Select]
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 18, 2010, 06:30:03 pm
Thanks, thats helpful!

Where I can find the definitions of EA.Package, EA.Diagram ...?
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 18, 2010, 06:38:14 pm
in the help file.

Geert
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 18, 2010, 10:09:28 pm
Which helpfile do you mean?
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 18, 2010, 10:53:02 pm
the EA help, as in RTFM  ;)

Geert
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 18, 2010, 11:44:45 pm
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.
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 18, 2010, 11:59:48 pm
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 20, 2010, 06:25:19 pm
Overview on Page 189 in the SDK. :)

But now I have other question.
How I get the ID from the new Package.
Quote
public virtual bool EA_OnPostNewPackage(EA.Repository Repository, EA.EventProperties Info)
Which function do I need? My Idea was:
Code: [Select]
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");
}
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 20, 2010, 06:39:42 pm
From TFM on EA_OnPostNewPackage
Quote
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:
Quote
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
Code: [Select]
info.Get("PackageID")to get the packageID.

Geert
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 21, 2010, 05:57:47 pm
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?
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 21, 2010, 07:10:15 pm
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 25, 2010, 10:15:53 pm
I'm not really progressing. I've got a problem. With ...
Code: [Select]
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:
Code: [Select]
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

Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 25, 2010, 11:17:41 pm
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 26, 2010, 01:11:53 am
Oh, thanks. That's very nice.
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 27, 2010, 05:18:14 pm
Sven,

I did a little test, and this code works:
Code: [Select]
       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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 29, 2010, 05:12:03 pm
Your code is very helpful. It generates a new diagram now but in the false level. I need the diagram under the package folder.
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 29, 2010, 05:19:04 pm
Sven,

Where it it creating the diagram then?
Post the code you have now, I'll have a look.

Geert
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 29, 2010, 08:56:38 pm
Hello Geert,
i think, i have got a update/refresh problem.

Code: [Select]
    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();

Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 29, 2010, 09:40:51 pm
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 29, 2010, 09:50:58 pm
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?
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 29, 2010, 09:52:47 pm
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on October 29, 2010, 10:01:20 pm
I think he need a update like "reload project".
After then I have got the right view.
When and Where it needs a update?
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on October 29, 2010, 10:26:16 pm
Quote
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on November 01, 2010, 04:52:26 pm
I have fixed that but i does not work with one of the both fixes.

Code: [Select]
Repository.AdviceElementChange(NewPackage.Element.ElementID);or
Code: [Select]
Repository.RefreshModelView(NewPackage.PackageID);
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on November 03, 2010, 05:38:26 pm
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on November 05, 2010, 12:03:31 am
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?
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on November 05, 2010, 12:43:40 am
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on November 05, 2010, 01:26:41 am
THANKS! It does work, now! :)
My solution:
Quote
//Reload Project Browser
Repository.RefreshModelView(0);
//Select new Diagram in the Prjoject Browser
Repository.ShowInProjectView(NewDiagram);
:)
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on November 09, 2010, 11:47:44 pm
Now, I have got a further problem with the new element in the Project Browser. I create the element with:

Code: [Select]
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on November 09, 2010, 11:56:34 pm
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
Code: [Select]
EA.Package myParentPackage = myRepository.GetPackageByID(myNewPackage.ParentID);
myParentPackage.Diagrams.AddNew(...


Geert
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on November 10, 2010, 12:23:02 am
Your information was helpful.

I modified you example for a element:
Code: [Select]
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on November 10, 2010, 12:33:54 am
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?
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on November 10, 2010, 12:38:59 am
No, try reloading, that might help.

Geert
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on November 10, 2010, 12:42:31 am
Reload without a newstart does not work. First after a new start I can delete the elements. Why?
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on November 10, 2010, 12:48:34 am
No idea :-/

Geert
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on November 29, 2010, 08:18:35 pm
How I can create a Composition? With:
Code: [Select]
NewConnector = (EA.Connector)NewElement.Connectors.AddnNew("","Composition");
I get with this code only a aggreation and not a Composition.

Thanks


Sven
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: Geert Bellekens on November 29, 2010, 08:27:28 pm
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
Title: Re: Create autom. a Class Diagram after a new Pack
Post by: marine1981 on November 29, 2010, 08:29:33 pm
Thanks! It does work!