Author Topic: Interop'ing' with C#  (Read 4907 times)

richdenis

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Interop'ing' with C#
« on: April 14, 2004, 10:34:45 am »
Hello,
 I am trying to use the sample code that is provided in the help file to no avail.  I am sure there is something I am not doing right but I am not sure what it could be.  While I can connect to the repository just fine and see that there are 2 models in the repository, when I try to access the model by doing a repository.Models.GetAt(i), I get an exception thrown that simply says "The server threw an exception".  I can not figure out what I could be doing wrong so I am including the code to see if anyone else can get any further.  Any help would be greatly appreciated:

private EA.Repository LoadRepository(string repositoryPath)
           {
                 EA.Repository _repository = new RepositoryClass();
                 try
                 {
                       _repository.OpenFile(repositoryPath);
                 }
                 catch( Exception e )
                 {
                       MessageBox.Show( e.StackTrace );
                 }
                 return _repository;
           }

           private void DumpModel(EA.Repository repository)
           {
                 for(short i=0;i< repository.Models.Count;i++)
                 {
                       try
                       {
                             object pack = ((Collection)repository.Models).GetAt(i);
                             DumpPackage((IPackage)repository.Models.GetAt(i), "");      
                       }
                       catch( Exception e )
                       {
                             MessageBox.Show( e.StackTrace );
                       }
                 }
           }

           private void DumpPackage(IPackage package, string indent)
           {
                 output.Append(indent + package.Name);
                 DumpElements(package, indent + "     ");
                 
                 for(short i=0;i<((Collection)package.Packages).Count;i++)
                 {
                       DumpPackage((IPackage)((Collection)package.Packages).GetAt(i) , indent + "    ");                        
                 }
           }

           private void DumpElements(IPackage package, string indent)
           {
                 for(short i=0;i<((Collection)package.Elements).Count;i++)
                 {
                       output.Append(indent + "::" + ((IPackage)((Collection)package.Elements).GetAt(i)).Name);
                 }
           }

Please look it over and give any suggestions you can.  I am attaching the entire form class file so you can implement it easier.



***BEGIN***
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using EA;
using System.Text;

namespace EATestHarness
{
     /// <summary>
     /// Summary description for Form1.
     /// </summary>
     public class Form1 : System.Windows.Forms.Form
     {
           private System.Windows.Forms.OpenFileDialog openFileDialog1;
           private System.Windows.Forms.Button btnClickMe;

           private StringBuilder output = new StringBuilder();
           /// <summary>
           /// Required designer variable.
           /// </summary>
           private System.ComponentModel.Container components = null;

           public Form1()
           {
                 //
                 // Required for Windows Form Designer support
                 //
                 InitializeComponent();

                 //
                 // TODO: Add any constructor code after InitializeComponent call
                 //
           }

           /// <summary>
           /// Clean up any resources being used.
           /// </summary>
           protected override void Dispose( bool disposing )
           {
                 if( disposing )
                 {
                       if (components != null)
                       {
                             components.Dispose();
                       }
                 }
                 base.Dispose( disposing );
           }

           #region Windows Form Designer generated code
           /// <summary>
           /// Required method for Designer support - do not modify
           /// the contents of this method with the code editor.
           /// </summary>
           private void InitializeComponent()
           {
                 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
                 this.btnClickMe = new System.Windows.Forms.Button();
                 this.SuspendLayout();
                 //
                 // openFileDialog1
                 //
                 this.openFileDialog1.Filter = "\"EAP Files|*.EAP\"";
                 this.openFileDialog1.FileOk += new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
                 //
                 // btnClickMe
                 //
                 this.btnClickMe.Location = new System.Drawing.Point(176, 24);
                 this.btnClickMe.Name = "btnClickMe";
                 this.btnClickMe.Size = new System.Drawing.Size(72, 32);
                 this.btnClickMe.TabIndex = 0;
                 this.btnClickMe.Text = "ClickMe!";
                 this.btnClickMe.Click += new System.EventHandler(this.btnClickMe_Click);
                 //
                 // Form1
                 //
                 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                 this.ClientSize = new System.Drawing.Size(292, 273);
                 this.Controls.Add(this.btnClickMe);
                 this.Name = "Form1";
                 this.Text = "Form1";
                 this.ResumeLayout(false);

           }
           #endregion

           /// <summary>
           /// The main entry point for the application.
           /// </summary>
           [STAThread]
           static void Main()
           {
                 Application.Run(new Form1());
           }

           private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
           {
           
           }

           private EA.Repository LoadRepository(string repositoryPath)
           {
                 EA.Repository _repository = new RepositoryClass();
                 try
                 {
                       _repository.OpenFile(repositoryPath);
                 }
                 catch( Exception e )
                 {
                       MessageBox.Show( e.StackTrace );
                 }
                 return _repository;
           }

           private void DumpModel(EA.Repository repository)
           {
                 for(short i=0;i< repository.Models.Count;i++)
                 {
                       try
                       {
                             object pack = ((Collection)repository.Models).GetAt(i);
                             DumpPackage((IPackage)repository.Models.GetAt(i), "");      
                       }
                       catch( Exception e )
                       {
                             MessageBox.Show( e.StackTrace );
                       }
                 }
           }

           private void DumpPackage(IPackage package, string indent)
           {
                 output.Append(indent + package.Name);
                 DumpElements(package, indent + "     ");
                 
                 for(short i=0;i<((Collection)package.Packages).Count;i++)
                 {
                       DumpPackage((IPackage)((Collection)package.Packages).GetAt(i) , indent + "    ");                        
                 }
           }

           private void DumpElements(IPackage package, string indent)
           {
                 for(short i=0;i<((Collection)package.Elements).Count;i++)
                 {
                       output.Append(indent + "::" + ((IPackage)((Collection)package.Elements).GetAt(i)).Name);
                 }
           }


           private void btnClickMe_Click(object sender, System.EventArgs e)
           {
                 EA.Repository _repository = LoadRepository(@"C:\\Development\\Diagrams\\UseCase\\UseCase.EAP");
                 DumpModel(_repository);
                 MessageBox.Show(output.ToString());
           }
     }
}
***END***

barny451

  • EA Novice
  • *
  • Posts: 19
  • Karma: +0/-0
    • View Profile
Re: Interop'ing' with C#
« Reply #1 on: April 15, 2004, 02:04:09 am »
Only obvious thing I can spot is that your
for(short i=0;i< repository.Models.Count;i++)

should (must) use repository.Models.Count-1 as the upper limit, and the same for all your other iterators.

Can't you use
 for each m in repository.Models
style of iteration? it's both cleaner and clearer

Other than that, solve the problem a bit at a time, make the first loop just print pack.Name (to show it is correctly iterating over all the models) or something equally simple, then make the dumppackage print the names of all the packages it encorunters, etc.

HTH
Ian