Author Topic: Trying to access EA Project from outside the EA Process with ("EA.App")  (Read 7084 times)

sarathipriyan

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Hi Geert and Uffe,

Using C# code we’re trying to access EA Project from outside the EA Process with something like GetActiveObject("EA.App") object. Is there any code sample (c#) that I use? We have a Enterprise Architect v15.2.1554(Trial Edition Free 30 day unlimited) and I'm using VS2019 community.


can you please share the sample c# code for access (create and get) EA Project from outside the EA .






Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13400
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Trying to access EA Project from outside the EA Process with ("EA.App")
« Reply #1 on: September 30, 2020, 10:21:47 pm »
This is the code I use to connect to the running instance on my model wrapper class https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework/blob/master/EAAddinFramework/EAWrappers/Model.cs


Code: [Select]
        public Model()
        {
            object obj = Marshal.GetActiveObject("EA.App");
            global::EA.App eaApp = obj as global::EA.App;
            this.initialize(eaApp.Repository);
        }

this code (vbscript) creates a  new EA instance
Code: [Select]
dim repository
set repository = CreateObject("EA.Repository")

Geert

sarathipriyan

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Trying to access EA Project from outside the EA Process with ("EA.App")
« Reply #2 on: October 01, 2020, 07:44:10 pm »
This is the code I use to connect to the running instance on my model wrapper class https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework/blob/master/EAAddinFramework/EAWrappers/Model.cs


Code: [Select]
        public Model()
        {
            object obj = Marshal.GetActiveObject("EA.App");
            global::EA.App eaApp = obj as global::EA.App;
            this.initialize(eaApp.Repository);
        }

this code (vbscript) creates a  new EA instance
Code: [Select]
dim repository
set repository = CreateObject("EA.Repository")

Geert



Thanks for the update. I will check and update you

sarathipriyan

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Trying to access EA Project from outside the EA Process with ("EA.App")
« Reply #3 on: October 01, 2020, 07:48:43 pm »
Hi Geert,

here's a copy of the code I'm using to connect with the EA repo externally.


   
Quote
static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        private static global::EA.Repository _wrappedModel;

        [STAThread]
        static void Main()
        {

            Model();
            Console.WriteLine("Hooked upto repository");
            DumpModel();     
        }

        static public  void DumpModel()
        {
            for(short i=0; i < _wrappedModel.Models.Count; i ++)
            {
                DumpPackage("", (EA.Package)_wrappedModel.Models.GetAt(i));
            }
        }

        static public  void DumpPackage(string s, EA.Package package)
        {
            Console.WriteLine("Package Name = " + package.Name);
            DumpElements(s, package);
        }

        static public void DumpElements(string s, EA.Package package)
        {
           
            for (short j = 0 ; j  < package.Elements.Count; j ++)
            {
                EA.Element element = (EA.Element)package.Elements.GetAt(j);
                Console.WriteLine(":: " + element.Name);
            }
        }

        static public void Model()
        {
            object obj = Marshal.GetActiveObject("EA.App");
            global::EA.App eaApp = obj as global::EA.App;
            initialize(eaApp.Repository);
        }

        static public void initialize(global::EA.Repository eaRepository)
        {
            _wrappedModel = eaRepository;
        }


    }
}

I have also created a project, Requirement, package, and elements from EA console.
but I'm not able to get the elements externally. The count is zero(package.Elements.Count;)

how do I get the elements from EA?

Enterprise Architect v15.2.1554(Trial Edition Free 30 day unlimited) and I'm using VS2019 community.
« Last Edit: October 01, 2020, 09:09:44 pm by sarathipriyan »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13400
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Trying to access EA Project from outside the EA Process with ("EA.App")
« Reply #4 on: October 01, 2020, 08:00:24 pm »
I don't see the code you are using to start EA, nor the code you are using to open a model.

But more importantly, you are only dumping elements, not packages.
Usually there are no elements directly under the root packages, only packages.

Geert

sarathipriyan

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Trying to access EA Project from outside the EA Process with ("EA.App")
« Reply #5 on: October 01, 2020, 10:16:29 pm »
I don't see the code you are using to start EA, nor the code you are using to open a model.

But more importantly, you are only dumping elements, not packages.
Usually there are no elements directly under the root packages, only packages.

Geert



Yes you're right in that code first we are opening the package and than we print the elements inside the package we wrote.

About opening the model, Sorry I thought object obj = Marshal.GetActiveObject("EA.App") would open the model. Can you suggest which call we need to use to open a model.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13400
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Trying to access EA Project from outside the EA Process with ("EA.App")
« Reply #6 on: October 01, 2020, 10:34:01 pm »
Marshal.GetActiveObject("EA.App") gets you the running instance of EA, with or without model loaded.

If you want to load another model, use Repository.OpenFile or Repository.OpenFile2

Geert

sarathipriyan

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Trying to access EA Project from outside the EA Process with ("EA.App")
« Reply #7 on: October 07, 2020, 01:43:17 am »
Marshal.GetActiveObject("EA.App") gets you the running instance of EA, with or without model loaded.

If you want to load another model, use Repository.OpenFile or Repository.OpenFile2

Geert

Thank you for your suggestions Geert, now I'm able to create and modify packages and elements. I can see the package and elements in EA dashboard. Is there a quick way to visualize that relationship as model diagram in EA?

I  have tried to use package diagram to view the elements but it's not working.