Matlab - Catia连接错误

时间:2014-03-17 13:34:05

标签: vb.net matlab catia

我需要在Catia和Matlab之间建立实时连接,这样我就可以将参数值发送到我在Catia的参数化设计中,并阅读其他一些参数和度量。

这是我的解决方案:

首先我创造:
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

然后,在Matlab中我有一个包围这个* dll的类:

Matlab课程:

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

所以在我的程序中,我通过Catia = CatiaLink创建Catia对象 而且我使用10000次甚至更多次来设置和获取参数。

一切都可以正常工作几千次而且我得到错误:

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

有人可以解释发生了什么吗?以及如何防止这种情况?

1 个答案:

答案 0 :(得分:0)

看起来你永远不会打电话给Catia.Quit()