Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MintJulep on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interfacing with CATIA in C#

Status
Not open for further replies.

lharrison

New member
Joined
Oct 19, 2011
Messages
52
Location
US
I am writing a small program to automate creating Jpgs from CATIA part captures. I had previously written a similar program in VB.net, but we are now transitioning to C#. I seem to be having a lot of trouble simply getting objects from many functions, and it seems like my program isn't fully attached to the CATIA process. When coding in VB.net, the main windows would display the functions I was calling, such as opening a part file. C# does not seem to do this, and many results simply return null, or errors such as base {System.Runtime.InteropServices.ExternalException} = {"Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))"} Here is a small except of my program as an example. This is after a part file has been opened through code.

Code:
        INFITF.Application CatiaInstance;
        private void AttachToCatia()
        {
            CatiaInstance = (INFITF.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Catia.Application");
        }

        private void WorkWithAnnotationSets()
        {
            MECMOD.PartDocument ActivePartDocument = (MECMOD.PartDocument)CatiaInstance.ActiveDocument;
            MECMOD.Part ActivePart = ActivePartDocument.Part;

            if (ActivePart.AnnotationSets.Count != 0)
            {
                AnnotationTypeLib.AnnotationSets AnnotationSets = (AnnotationTypeLib.AnnotationSets)ActivePart.AnnotationSets;

                ///Loop through all annotation sets
                for (int i = 0; i < AnnotationSets.Count; i++)
                {
                   AnnotationTypeLib.AnnotationSet AnnotationSet = (AnnotationTypeLib.AnnotationSet)AnnotationSets.Item(i);
               [b]//AnnotationSets items always return a HRESULT: 0x80020009 (DISP_E_EXCEPTION), even though objects exist in the collection[/b]

                    ///Loop through annotations found in a particular annotation set
                    for (int x = 0; x > AnnotationSet.Annotations.Count; x++)
                    {
                        AnnotationTypeLib.Annotation Annotation = (AnnotationTypeLib.Annotation)(AnnotationSet.Annotations as AnnotationTypeLib.Annotations).Item(x);
                    }

                    //loop through captures found in a particular annotation set
                    for (int y = 0; y > AnnotationSet.Captures.Count; y++)
                    {
                        AnnotationTypeLib.Capture Capture = (AnnotationTypeLib.Capture)(AnnotationSet.Captures as AnnotationTypeLib.Captures).Item(y);
                    }
                }
            }
        }

I never had problems like this when coding in VB.net, where things just seemed to 'work'. Anyone know where I am going wrong here???
 
Well I solved on of my problems right after posting. I had forgotten that internally CATIA doesn't always use 0 indexing, so changing the for loops to begin on 1 properly returned the objects. Still can't figure out why you cannot see the functions running in a CATIA window though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top