Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: sandeep bhat on October 29, 2014, 08:27:10 pm

Title: Adding a class or activity using python
Post by: sandeep bhat 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()

(http://i60.tinypic.com/jt42tu.jpg)
Title: Re: Adding a class or activity using python
Post by: qwerty 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.
Title: Re: Adding a class or activity using python
Post by: sandeep bhat 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)


(http://i62.tinypic.com/nv4mxg.jpg)
Title: Re: Adding a class or activity using python
Post by: qwerty on October 29, 2014, 09:49:37 pm
What do you get for print SelectedPackage ?

q.
Title: Re: Adding a class or activity using python
Post by: sandeep bhat on October 29, 2014, 11:22:14 pm
Hi,
I get this

(http://i62.tinypic.com/2ev52yc.jpg)
Title: Re: Adding a class or activity using python
Post by: sandeep bhat 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
Title: Re: Adding a class or activity using python
Post by: sandeep bhat 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()


 :)