Book a Demo

Author Topic: Preserve Add-In Objects  (Read 6732 times)

Kai H.

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • Where is my mind?
    • View Profile
Preserve Add-In Objects
« on: March 24, 2011, 07:43:15 pm »
Good morning,

I searched the forum and also asked some colleaugues but noone came up with an answer to this problem:

During the control flow of my Add-In, I create a large object from file. After the Add-In has executed, I loose the control flow and the object gets destroyed. This results in creating the same object over and over again, every time the user selects other actions from my Add-In. so here is my question:


How can I preserve objects that an Add-In created for later use?


Cheers and thanks for your reply  ;)

Kai

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Preserve Add-In Objects
« Reply #1 on: March 24, 2011, 10:02:29 pm »
I guess this is called serialization. Write it to a file or tweak the EA database on your own risk

q.

Kai H.

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • Where is my mind?
    • View Profile
Re: Preserve Add-In Objects
« Reply #2 on: March 24, 2011, 11:33:12 pm »
Thanks for your answer. Unfortunately, this sounds equally bad  :(

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Preserve Add-In Objects
« Reply #3 on: March 25, 2011, 06:17:50 am »
Paolo (AFAIR) had asked for a way store user data safely in the EA database. Maybe you can search for the thread here and join him with a feature request.

q.

Frank Horn

  • EA User
  • **
  • Posts: 535
  • Karma: +1/-0
    • View Profile
Re: Preserve Add-In Objects
« Reply #4 on: March 25, 2011, 08:10:18 am »
Kai,
I don't think you need serialization in this scenario. A static variable should do.

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Preserve Add-In Objects
« Reply #5 on: March 25, 2011, 10:59:03 am »
Quote
Paolo (AFAIR) had asked for a way store user data safely in the EA database. Maybe you can search for the thread here and join him with a feature request.

q.
AFAIR it wasn't me but I DID support the suggestion made by someone else...

Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Kai H.

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • Where is my mind?
    • View Profile
Re: Preserve Add-In Objects
« Reply #6 on: March 25, 2011, 05:45:14 pm »
Thank you for your replies, I think I will use serialization, since then I have the required object available after exit. For future forum searches, below are two nice howto links

to store C# objects to XML file using serialization:
http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file
http://www.devx.com/tips/Tip/39593

Kai

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Preserve Add-In Objects
« Reply #7 on: March 27, 2011, 04:45:04 pm »
I don't think you need to serialize your object to keep while EA is running.
If define a variable in your Addin-class and you initialize that in the EA_Connect, that should remain available until EA itself is closed. (I don't remember if I had to use a static or variable or not).
I've used that technique before to start a file system monitor which keeps running the whole time EA is open.

Geert

Kai H.

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • Where is my mind?
    • View Profile
Re: Preserve Add-In Objects
« Reply #8 on: March 29, 2011, 06:57:07 pm »
Thanks Geert,

i tried it and it's working fine, here is my code:

Code: [Select]
using System;
using System.Windows.Forms;

namespace EA_Banana_AddIn
{
      public class Main
      {
            private bool m_ShowFullMenus = false;
            private bool banana = false;

            public String EA_Connect(EA.Repository Repository)
            {
                        this.banana = true;
                  
                  //No special processing required.
                  return "a string";
            }

            //Called when user Click Add-Ins Menu item from within EA.
            //Populates the Menu with our desired selections.
            public object EA_GetMenuItems(EA.Repository Repository, string Location, string MenuName)
            {
                  EA.Package aPackage = Repository.GetTreeSelectedPackage();
                  switch( MenuName )
                  {
                        case "":
                              return "-&Banana";
                        case "-&Banana":
                    string[] ar = {"Banana Test"};
                              return ar;
                  }
                  return "";
            }
            
            //Sets the state of the menu depending if there is an active project or not
            bool IsProjectOpen(EA.Repository Repository)
            {
                  try
                  {
                        EA.Collection c = Repository.Models;
                        return true;
                  }
                  catch
                  {
                        return false;
                  }
            }

            //Called once Menu has been opened to see what menu items are active.
            public void EA_GetMenuState(EA.Repository Repository, string Location, string MenuName, string ItemName, ref bool IsEnabled, ref bool IsChecked)
            {
                  if( IsProjectOpen(Repository) )
                  {
                        if( ItemName == "Menu1" )
                              IsChecked = m_ShowFullMenus;
                        else if( ItemName == "Menu2")
                              IsEnabled = m_ShowFullMenus;
                  }
                  else
                        // If no open project, disable all menu options
                        IsEnabled = false;
            }

            //Called when user makes a selection in the menu.
            //This is your main exit point to the rest of your Add-in
            public void EA_MenuClick(EA.Repository Repository, string Location, string MenuName, string ItemName)
            {
            switch (ItemName)
            {
                case "Banana Test":
                    MessageBox.Show("It's "+this.banana+" that bananas are tasteful.", "Hint", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    break;
            }
            }
      }
}

Cheers

Kai