Matlab - Catia connection error

4

I need to set up live connection between Catia and Matlab so I can send parameters values to my parametric design in Catia and read some other parameters and measures.

This is my sollution:

First I create:
VB NET (*.dll)

Public Class CatiaLinkLibrary
    Dim CATIA As Object
    Dim rootproduct
    Sub StartCatia()
        CATIA = CreateObject("CATIA.Application")
    End Sub

Sub CloseCatia()
    CATIA.Quit()
End Sub

Sub Visible(ByRef mode As Integer)
    If mode = 1 Or mode = 0 Then
        CATIA.Visible = mode
    End If
End Sub

Sub OpenFile(ByRef filename As String)
    CATIA.Documents.Open(filename)
    rootproduct = CATIA.ActiveDocument.Product()
End Sub

Function GetMass() As Double
    Return rootproduct.Analyze.Mass()
End Function

Function GetVolume() As Double
    Return rootproduct.Analyze.Volume()
End Function

Function GetArea() As Double
    Return rootproduct.Analyze.WetArea()
End Function

Function GetGravityCenter()
    Dim gravitycenter(2)
    rootproduct.Analyze.GetGravityCenter(gravitycenter)
    GetGravityCenter = gravitycenter
End Function

Function GetIntertia()
    Dim inertia(8)
    rootproduct.Analyze.GetInertia(inertia)
    GetIntertia = inertia
End Function

Sub ChangeParameter(ByRef parameterName As String, ByRef Value As Double)
    Dim pd As Object
    Dim part As Object
    Dim parameters As Object
    Dim length As Object
    pd = CATIA.ActiveDocument
    part = pd.Part
    parameters = part.Parameters
    length = parameters.Item(parameterName)
    length.Value = Value
    part.Update()
End Sub

Function GetParameter(ByRef parameterName As String) As Double
    Dim pd As Object
    Dim part As Object
    Dim parameters As Object
    Dim length As Object
    pd = CATIA.ActiveDocument
    part = pd.Part
    parameters = part.Parameters
    length = parameters.Item(parameterName)
    Return length.Value()
End Function

Sub closeDoc(ByRef name As String)
    Dim windows As Object
    Dim window As Object
    Dim doc As Object
    windows = CATIA.Windows
    window = windows.Item(name)
    window.Activate()
    window.Close()
    doc = CATIA.ActiveDocument
    doc.Close()
End Sub

Sub activeDoc(ByRef name As String)
    Dim windows As Object
    Dim window As Object
    Dim doc As Object
    windows = CATIA.Windows
    window = windows.Item(name)
    window.Activate()
    doc = CATIA.ActiveDocument
End Sub

Function GetArea2() As Double
    Dim pd As Object
    Dim part As Object
    Dim bodys As Object
    Dim body As Object
    Dim spabench As Object
    Dim mymeas As Object

    pd = CATIA.ActiveDocument
    part = pd.Part
    bodys = part.Bodies
    body = bodys.Item("PartBody")
    spabench = pd.GetWorkbench("SPAWorkbench")
    mymeas = spabench.GetMeasurable(body)
    Return mymeas.Area

End Function

End Class

Then, in Matlab I have class that wraps around this *dll:

Matlab class:

classdef CatiaLink < handle
      properties
          catia;
      end
      methods
          function obj = CatiaLink()
              %modify this path to your .NET DLL
              NET.addAssembly('C:\DOKTORAT\Modele Geometryczne\CatiaLinkLibrary\CatiaLinkLibrary\bin\Debug\CatiaLinkLibrary.dll');
              obj.catia = CatiaLinkLibrary.CatiaLinkLibrary;
              obj.catia.StartCatia;
              disp('Catia started')
          end
          function Visible(obj,mode)
              obj.catia.Visible(mode);
          end
          function Quit(obj)
              obj.catia.CloseCatia;
          end
          function Open(obj,filename)
              obj.catia.OpenFile(filename);
          end
          function mass = GetMass(obj)
              mass = obj.catia.GetMass;
          end
          function vol = GetVolume(obj)
              vol = obj.catia.GetVolume;
          end
          function area = GetArea(obj)
              area = obj.catia.GetArea;
          end
          function cog = GetCenterOfGravity(obj)
              tmp = obj.catia.GetGravityCenter;
              cog = [tmp(1),tmp(2),tmp(3)];
          end
          function inertia = GetInertia(obj)
              tmp = obj.catia.GetIntertia;
              inertia = [tmp(1), tmp(2), tmp(3); ...
                         tmp(4), tmp(5), tmp(6); ...
                         tmp(7), tmp(8), tmp(9)];
          end
          function setParameter(obj, parameterName, Value)
              obj.catia.ChangeParameter(parameterName, Value); 
          end
          function val = getParameter(obj, parameterName)
              val = obj.catia.GetParameter(parameterName);
          end
          function closeDoc(obj, name)
              obj.catia.closeDoc(name);
          end
          function activeDoc(obj, name)
              obj.catia.activeDoc(name);
          end
          function area = getArea2(obj)
              area = obj.catia.GetArea2;
          end

      end
end

So in my program I create Catia object by Catia = CatiaLink.
And than I use it like 10000 or even more times to set and get parameters.

Everything works just fine up to around couple thousand times and than I get error:

Error using CatiaLink/setParameter (line 42)
Message: No more threads can be created in the system. (Exception from
HRESULT: 0x800700A4)
Source: mscorlib
HelpLink:

Can someone explain what is happening? And how to prevent this?

vb.net
matlab
catia
asked on Stack Overflow Mar 17, 2014 by vbar • edited Mar 17, 2014 by Schorsch

1 Answer

0

It looks like you're never calling Catia.Quit()

answered on Stack Overflow May 29, 2014 by MattyB

User contributions licensed under CC BY-SA 3.0