Author Topic: Adding a class or activity using python  (Read 5677 times)

sandeep bhat

  • EA User
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
Adding a class or activity using python
« on: October 29, 2014, 08:27:10 pm »
Hi,
I have written an Addin using python as a scripting language. I would like to add an class or activity to an existing package using the script.
I did try doing this but i would get an exception.

Collection = m_Repository.GetTreeSelectedElements()
Collection = Collection.GetAt(0)

NewColl=Collection.AddNew('test','4')
NewColl.Update()
Collection.Refresh()


qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Adding a class or activity using python
« Reply #1 on: October 29, 2014, 08:32:44 pm »
Depends on what you have selected. Does GetTreeSelectedElements return a result set at all? I remember some limitations with scripting languages. GetTreeSelectedObject works for sure. If you got a package you would need to change the '4' to 'Class'  so it creates a class named 'test'.

The return value of addNew is not a collection but the Element (or whatever is appropriate for the selected element/package).

Ah. And the Refresh is not needed at all.

q.
« Last Edit: October 29, 2014, 08:35:53 pm by qwerty »

sandeep bhat

  • EA User
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
Re: Adding a class or activity using python
« Reply #2 on: October 29, 2014, 08:53:33 pm »
Hi qwerty,
Thanks for your reply. Well GetTreeSelectedElements returns an Collection object. But as per your suggestion i have made the changes but i still get the exception.
I get an exception immediately after the addnew line

SelectedPackage = m_Repository.GetTreeSelectedObject()
        try:            
            obj=SelectedPackage.AddNew('test','Class')
            obj.Update()
            return
        except Exception:
            MessageBox(None, str(traceback.format_exc()), 'EA2Simulink',0)



qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Adding a class or activity using python
« Reply #3 on: October 29, 2014, 09:49:37 pm »
What do you get for print SelectedPackage ?

q.

sandeep bhat

  • EA User
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
Re: Adding a class or activity using python
« Reply #4 on: October 29, 2014, 11:22:14 pm »
Hi,
I get this


sandeep bhat

  • EA User
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
Re: Adding a class or activity using python
« Reply #5 on: October 29, 2014, 11:43:54 pm »
I did try this piece of code and it worked like a charm

root2 = m_Repository.Models.AddNew ("A New Root", "");
root2.Update ();
m_Repository.Models.Refresh ();

but i am not sure how do i do it if at all i want to add elements like class or activity
« Last Edit: October 29, 2014, 11:44:11 pm by freakosandy »

sandeep bhat

  • EA User
  • **
  • Posts: 36
  • Karma: +0/-0
    • View Profile
Re: Adding a class or activity using python
« Reply #6 on: October 29, 2014, 11:57:34 pm »
Hi,
I solved it. I was trying to add a new class to a collection which is not possible  so here is what i did.

SelectedPackage = m_Repository.GetTreeSelectedPackage()      
obj=SelectedPackage.Elements.AddNew('test','Class')
obj.Update()


 :)