Book a Demo

Author Topic: Copy element to a new package in C#  (Read 8620 times)

jeffc030

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Copy element to a new package in C#
« on: February 03, 2006, 08:49:15 am »
2 issues.

First: I am trying to create a new element as follows:

EA.Package lPack = Repository.Models.AddNew("Pack", "otPackage");
lPack.Update();

EA.Element lElem = lPack.Elements.AddNew("Elem","otElement");
lElem.Update();

The following exception is thrown at lElem.Update() :
"External component has thrown an exception"

Any ideas?

Second :

What would be the easiest way to copy an element, and all of its contents (including embedded elements) to a new package.

Thanks.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Copy element to a new package in C#
« Reply #1 on: February 06, 2006, 06:28:31 am »
I think I've run into this myself recently.

It looks like you are trying to add an element into a package, which is in turn a direct child of the Model element.

[At least recently; it might have been different a while back] EA seems to consider any package that is an immediate child of the Model element to be a View. Views seem not to like having elements as immediate children.

Try creating another package directly under iPack. Then add your element to the second package.

Please let me know if this works; if not I'll see if I still have the code where I ran into something similar, and whether I preserved the solution. My code was in VB.Net 2003, but you should be able to translate without trouble.

David
No, you can't have it!

jeffc030

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Copy element to a new package in C#
« Reply #2 on: February 06, 2006, 09:31:25 am »
Thanks for replying David!

I tried adding another Package to lPack.Packages, and then the element under the returned lPack.Elements, however I get the same exception.

EA.Package lPack = Repository.Models.AddNew("Package", "otPackage");  
lPack.Update();  
 
EA.Package lPack = lPack.Packages.AddNew("Package","otPackage");  
lPack.Update();  

EA.Element lElem = lPack.Elements.AddNew("Elem","otElement");  
lElem.Update();  

I even tried going another 2 levels down and it doesnt work.

Thanks,

Jeff

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Copy element to a new package in C#
« Reply #3 on: February 06, 2006, 09:44:49 am »
Since my last reply I have been working with EA automation, including creating packages. I must confess that I have not been creating both views and packages as we are discussion.

However...

You are creating your packages as type "otPackage" via the second parameter of the AddNew call. Try "Package" instead. Likewise, use "Element" instead of "otElement" for the element call.

David

EDIT:

Just tried out the above solution, using a blank model (no views or anything). Worked like a charm. It also worked perfectly when I added the element directly to the View, so use the above solution and disregard my original assertion.

I was using VB.Net with the Strict option on, so needed to use DirectCast or CType to cast the results of the AddNew methods to EA.Package or EA.Element. You will need to perform the same cast.

Remember that if you want to enumerate the collection or find the package or element via GetByName, you will first have to call the Refresh method on the parent collection.

Let me know how it works.

David
« Last Edit: February 06, 2006, 09:58:23 am by Midnight »
No, you can't have it!

jeffc030

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Copy element to a new package in C#
« Reply #4 on: February 06, 2006, 10:51:37 am »
For some reason I can add to the Elements collection elements of type: "Class" and "Object", but not "Element". Even if I start with an Empty Model.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Copy element to a new package in C#
« Reply #5 on: February 06, 2006, 11:08:19 am »
Oops, mea culpa...

I seem to be monotasking here, at best.

I just realized (while I was programming) that I had mis-stated this. You are correct. You must enter one of the strings listed in the help file as a valid element type.

Of course "Package" is a valid type for the Package.AddNew method. Since I then went on to explain Element.AddNew method my brain just directed me to type "Element" rather than tell you to use the element 'type.'

I only noticed this when I went back into my code and entered "Element" for the element type - and got the same error as you.

Bottom line: you should be OK entering the element type. Try using "State" or "UseCase" or "Activity" for a change. You can find a list of these in the help. Just look for Element, and find the Type attribute. This works for several other sets of string constants.

While we're at it, the ObjectType property will return one of the ot<whatever> enumeration values, and is pretty reliable for finding out what you have when you traverse a collection. It is read-only.

David
No, you can't have it!

jeffc030

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Copy element to a new package in C#
« Reply #6 on: February 06, 2006, 11:46:32 am »
Thanks for the help David!  I was looking at the ObjectType property in the Help thinking that those values are the valid types for this parameter.

Thanks again.

-Jeff