Book a Demo

Author Topic: Problem with first automation project  (Read 7790 times)

david_wendelken

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
  • excited new user!
    • View Profile
Problem with first automation project
« on: November 06, 2005, 07:40:51 am »
I'm new to both EA and C#.

I've gone thru the C# sample code and gleaned how to iteratively loop thru the contents of my project.

I'm trying to update a sample property for each element that  has it.
For learning purposes, I've picked "Author".

I'm having this problem:

How do I know what type of element is contained in a Package.Element?  

I happen to know the first element returned is supposed to be a Requirement.    How do I change it's properties?
I tried casting it into an object of  EA.RequirementClass, but that didn't seem to work.

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2919
  • Karma: +55/-3
    • View Profile
Re: Problem with first automation project
« Reply #1 on: November 06, 2005, 04:00:04 pm »
Package.Element contains the properties of the package. You probably need to iterate through the Package.Elements collection. Check the Element.Type property for each item in the collection; this should be "Requirement" for your requirement element.

I don't know C#, but VB6 might be something like:

Code: [Select]
   Dim repos As EA.repository
   Set repos = GetObject(, "EA.App").repository
   
   Dim pack As EA.Package
   Set pack = repos.GetTreeSelectedPackage()
   
   Dim elem As EA.element
   For Each elem In pack.Elements
       If elem.Type = "Requirement" Then
           MsgBox "Found It!"
       End If
   Next
The Sparx Team
[email protected]

david_wendelken

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
  • excited new user!
    • View Profile
Re: Problem with first automation project
« Reply #2 on: November 06, 2005, 04:40:40 pm »
Ok.  The Element.Type property lets me know what type of object it is.

In the example project, there is a Requirement called FR1 - Utilize existing hardware.

If I open it in the interface, it says it is a "Requirement".
It has a property called "Author".

Now, I've successfully tested for the element type being equal to "Requirement".  It's displaying the correct message for requirements and non-requirements.
So, if it's a requirement, I'm trying to cast the element to a RequirementClass.  It won't let me.
Ditto for Requirement, which seems to be an interface.
(The sample code casts to Element instead of ElementClass.)

I'm also a bit more confused because casting it to an Element interface allows me to view the Author property.
But the RequirementClass doesn't have an Author property!



???

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2919
  • Karma: +55/-3
    • View Profile
Re: Problem with first automation project
« Reply #3 on: November 06, 2005, 05:20:39 pm »
Quote
So, if it's a requirement, I'm trying to cast the element to a RequirementClass.  It won't let me.


How are you trying to cast it? Can you post up a code snippet  please?

And why are you trying to cast it? What do you hope to achieve?
The Sparx Team
[email protected]

david_wendelken

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
  • excited new user!
    • View Profile
Re: Problem with first automation project
« Reply #4 on: November 06, 2005, 05:45:30 pm »
Ok!

Sorry about the funky indenting in the code below.  It's not the way I entered it, it's just the way it's displaying.

Here's one way that compiles and runs.
Data doesn't get changed.
Syntax gleaned from the sample cs automation file.
Same results with or without the commented out lines:

Code: [Select]

void SetAuthorElements(EA.Package Package)
{
 for( short idx = 0; idx < Package.Elements.Count; idx++ )
 {
    if (((EA.Element)Package.Elements.GetAt(idx)).Type
          == "Requirement"
      )
   {
     ListAdd(((EA.Element)Package.Elements.GetAt(idx)).Name );
     ListAdd(((EA.Element)Package.Elements.GetAt(idx)).Author);
     ((EA.Element)Package.Elements.GetAt(idx)).Author
         = "John Doe";
     // ((EA.Element)Package.Elements.GetAt(idx)).Update();

     // ((EA.Element)Package.Elements.GetAt(idx)).Refresh();

     ListAdd(((EA.Element)Package.Elements.GetAt(idx)).Author);
   }
 }
}


Here's an attempt to cast it into a RequirementClass object.
Why?  If nothing else, once I cast it I don't have to type all the mess above just to reference something!  Plus, the above approach didn't work, so...

Code: [Select]

void SetAuthorElements(EA.Package Package)
{
 // Won't compile.
 //EA.RequirementClass rc = new EA.RequirementClass();
 EA.RequirementClass rc; // will try this instead.
 for( short idx = 0; idx < Package.Elements.Count; idx++ )
 {
   if (((EA.Element)Package.Elements.GetAt(idx)).Type
       == "Requirement")
   {
     ListAdd(((EA.Element)Package.Elements.GetAt(idx)).Name );
     ListAdd(((EA.Element)Package.Elements.GetAt(idx)).Author);
     rc = (EA.RequirementClass)Package.Elements.GetAt(idx);
     ListAdd(Indent + rc.Name);
     //ListAdd(Indent + rc.Author); // Won't compile.
     // Will worry about changing something later!
   }
 }
}

The above blows up with a "Specified cast is not valid." error message.
Oddly, it also blows up if I try to cast it to an ElementClass, which I thought out to work since it can cast to the Element interface.

Now, I'm also new to C#, so it's certainly possible I'm doing a "stupid newbie" trick for God and everybody to laugh at.   :o


david_wendelken

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
  • excited new user!
    • View Profile
Re: Problem with first automation project
« Reply #5 on: November 08, 2005, 05:15:14 pm »
bump.   ???

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2919
  • Karma: +55/-3
    • View Profile
Re: Problem with first automation project
« Reply #6 on: November 08, 2005, 05:44:01 pm »
Sorry not to get back to you...

I've not used automation in C# but I'm guessing that EA.ElementClass and EA.RequirementClass appear with auto-completion and are equivalent of EA.Element and EA.Requirement available in VB. If not, it's a problem; if so, don't try to cast to EA.Requirement! Open the properties dialog of a class element in EA and you will see a "Requirement" tab: EA.Requirement is the type of the internal requirement items created on that tab, and is not the type of external requirement elements. They are of type EA.Element.

So, your first attempt without the typecasting is closer to being correct than your second. I think the reason your first attempt doesn't work is you probably need to do the Update and/or Refresh on the collection as well as the element. Try that.
The Sparx Team
[email protected]

fwoolz

  • EA User
  • **
  • Posts: 435
  • Karma: +0/-0
  • We have met the enemy, and he is us.&lt;Pogo, 1970&gt;
    • View Profile
Re: Problem with first automation project
« Reply #7 on: November 09, 2005, 08:12:42 am »
When retrieving a collection of objects, you can use the ObjectType property (one of the built-in enumerations) to determine the type, then cast the element accordingly. ALWAYS cast to the interface, not the co-class (e.g., EA.Requirement, not EA.RequirementClass); AFAIK, inheritance and COM take care of the rest. I have done this in C# and it works fine.

Cheers,
Fred Woolsey
Fred Woolsey
Interfleet Technology Inc.

Always be ready to laugh at yourself; that way, you beat everyone else to the punch.