Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started 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)
-
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.
-
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)
-
What do you get for print SelectedPackage ?
q.
-
Hi,
I get this
(http://i62.tinypic.com/2ev52yc.jpg)
-
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
-
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()
:)