Accessing CATIA V5 using C#

Abhishek Punia

Accessing CATIA V5 using C#

Hello everyone,

I am required to create an application in catia v5 (version 28) with C# and visual studio 2019 (.NET Framework 4.7.2) at my work. To test whether I can access CATIA, I created a console app and added the required references.  I added an additional class "catia_helper" which tries to call catia and the code is as follows:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using INFITF;
using MECMOD;

namespace CATIAConApp
{
    public static class catia_helper
    {
        public static INFITF.Application CATIA;
        public static bool isCatiaSet = false;

        public static bool SetCatia()
        {
            try
            {
                CATIA = (INFITF.Application)Marshal.GetActiveObject("CATIA.Application");
                isCatiaSet = true;
            }
            catch
            {
                isCatiaSet = false;
                throw new COMException("Error resolving CATIA instance.");
            }
            return isCatiaSet;
        }
    }
}

I then call the method in the main program as follows:

 

namespace CATIAConApp
{
    class Program
    {
        static void Main(string[] args)
        {
            catia_helper.SetCatia();
        }
    }
}

 

However, the error declared in the catch statement is thrown, and I am not able to figure out why. The CATIA V5 we use at work is started from a server and there is no local installation of the same. The folders CATEnv and win_b64 are located on the D drive of my computer. The same code works perfectly with the CATIA V5 student version I have on my personal laptop (local version installed in drive C).

Kindly let me know if any of you has any solution as I am unable to proceed further.

Best regards,
Abhi

Abhishek Punia

RE: Accessing CATIA V5 using C#
(in response to Abhishek Punia)

Ok so the problem seemed to be with the CATIA installation. Reinstalling CATIA made the code work.

 

Cliff Johnson, Cliff

RE: Accessing CATIA V5 using C#
(in response to Abhishek Punia)

Most likely you shouldn't have needed to re-install.

Probably the CATIA automation libraries were not registered. 

This can happen if you have two versions installed and remove one, and other ways. 

To fix it you just have run the command "CNEXT.exe -regserver" using the CNEXT executable of the version you want to use. 

How to ask questions 

Abhishek Punia

RE: Accessing CATIA V5 using C#
(in response to Cliff Johnson, Cliff)

Thank you, Cliff!

I'll keep that in mind for the next time.