How do I set up MATLAB to communicate with Autodesk Inventor's Apprentice (COM API)?

5 Ansichten (letzte 30 Tage)
Hello all,
Trying to write some MATLAB code to access iProperty information from Autodesk Inventor files through the Apprentice COM Component.... There are lots of examples of how to do this in Excel VBA but can't seem to find any examples with MATLAB?
I can create a COM server for Inventor.Application using:
x = actxserver('Inventor.Application')
but it won't let me do the same for:
x = actxserver('Inventor.ApprenticeServerComponent')
Does anyone have any experience with this?
Thanks,
Greg

Antworten (3)

Fabian
Fabian am 14 Dez. 2023
Hi I have now written funktion in C# and matlab wich are able to change parameters
C#:
// CInventor starts up and closes down Inventor
// gets the Modell parameter and prints them in a txt
// 2019-07-02 : Started.
// by Fabibause
// information on this is at http://www.ransensoftware.com/Inventor-CPP/
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using Inventor;
using System.Threading;
using Path = System.IO.Path;
using File = System.IO.File;
namespace SAClassif
{
static class CInventor
{
private static Inventor.Application m_Inventor;
private static bool m_bWasAlreadyRunning = false;
public static string sUserParamName;
public static string sUserParamNameOUT;
public static string sFullDocName;
[STAThread]
static void Main(string[] args)
{
ConnectToInventor();
GetsFullDocNam();
WriteToTxtUserParameter();
Inventor.Document PartDoc = (Inventor.Document)m_Inventor.Documents.Open(sFullDocName);
PartComponentDefinition PartDef = ((Inventor.PartDocument)PartDoc).ComponentDefinition;
ModelParameters ModParams = PartDef.Parameters.ModelParameters;
PartDoc.Update();
PartDoc.Save();
Console.Out.WriteLine("done");
DisconnectFromInventor();
}
public static void GetsFullDocNam()
{
string fileName = "Matlab2CS.txt";
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
StreamReader sr = new StreamReader(filePath);
sFullDocName = sr.ReadLine();
Console.Out.WriteLine(sFullDocName);
sr.Close();
}
public static bool WriteToTxtUserParameter()
{
// Pfad zur Textdatei
string fileName = "CS2Matlab.txt";
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
StreamWriter sw = new StreamWriter(filePath);
Inventor.Document PartDoc = (Inventor.Document)m_Inventor.Documents.Open(sFullDocName);
PartComponentDefinition PartDef = ((Inventor.PartDocument)PartDoc).ComponentDefinition;
UserParameters UserParams = PartDef.Parameters.UserParameters;
int iNumUsrParms = UserParams.Count;
for (int mp = 1; mp <= iNumUsrParms; mp++)
{
UserParameter UserParam = UserParams[mp];
sw.WriteLine(UserParam.Name);
sw.WriteLine(UserParam.Value);
//sw.WriteLine();
}
sw.Close();
return true;
}
public static bool ConnectToInventor()
{
Debug.WriteLine("Connecting to inventor...");
try
{
// See if Inventor is already running...
m_Inventor = (Inventor.Application)Marshal.GetActiveObject("Inventor.Application");
m_bWasAlreadyRunning = true;
Debug.WriteLine("Inventor already running...");
}
catch (Exception ex1)
{
Debug.WriteLine("Not running, will start it...");
m_bWasAlreadyRunning = false;
try
{
Type invAppType = Type.GetTypeFromProgID("Inventor.Application");
m_Inventor = (Inventor.Application)System.Activator.CreateInstance(invAppType);
m_Inventor.Visible = true;
Debug.WriteLine("Started Inventor");
}
catch (Exception ex2)
{
MessageBox.Show(ex2.ToString());
MessageBox.Show("Unable to start Inventor");
return false;
}
}
return m_Inventor != null;
}
public static void DisconnectFromInventor()
{
if (m_Inventor == null)
{
return;
}
if (m_bWasAlreadyRunning)
{
// I did not start Inventro so I shoulkd not close it...
return;
}
// Inventor was not running, you started it so you should shut it down
m_Inventor.Quit();
m_Inventor = null;
}
public static bool MissingComponent(ComponentOccurrence ThisOcc)
{
if (ThisOcc.ReferencedDocumentDescriptor != null)
{
if (ThisOcc.ReferencedDocumentDescriptor.ReferenceMissing)
{
return true;
}
}
return false;
}
static string GetUserParam(Inventor.AssemblyDocument InvIAMDoc, string sUserParamName)
{
UserParameters UsrParams = InvIAMDoc.ComponentDefinition.Parameters.UserParameters;
int iNumUsrParms = UsrParams.Count;
for (int mp = 1; mp <= iNumUsrParms; mp++)
{
// Get this user parameter
UserParameter UsrParam = UsrParams[mp];
// Compare its name to what we are looking for
bool bIsMyParam = (string.Compare(UsrParam.Name, sUserParamName, StringComparison.OrdinalIgnoreCase) == 0);
// Return the value if it is the one...
if (bIsMyParam)
{
string sMsg = string.Format("Found {0} with value {1} ", UsrParam.Name.ToString(), UsrParam.Value.ToString());
Debug.WriteLine(sMsg);
return UsrParam.Value;
}
}
return "";
}
}
}

Fabian
Fabian am 14 Dez. 2023
the matlab funktion using this needs t know were the file is at :

Fabian
Fabian am 14 Dez. 2023
The matlab code loks like this :
function Parameter = mf01_GetModelParameter(NameofModel)
if ~isfolder([pwd '\CS_exe_files\GetModelParameters'])
Parameter = -1;
return
end
% if ~isstring(NameofModel)
% Parameter = -1;
% return
% end
% NameofModel = '\\10.76.32.5\Users\fbammes\HIWI\Coding\C#\00_MatlabFunktions\Big_yoke.ipt';
Mainpath = pwd;
exepath = '\CS_exe_files\GetModelParameters\GetModelParameters\bin\Debug\';
exefile = 'GetModelParameters.exe';
txtfileOut = 'Matlab2CS.txt';
txtfileIn = 'CS2Matlab.txt';
file_path_In = [Mainpath exepath txtfileIn ];
file_path_Out = [Mainpath exepath txtfileOut];
% Überprüfen, ob der Befehl erfolgreich ausgeführt wurde
% give CS the information which modell to choose
IntiCS(file_path_Out,NameofModel);
% create parameter list
status = system([Mainpath exepath exefile]);
CheckSystem(status)
% get parameter of that Modell
Parameter = ReadinParamters(file_path_In);
end
function IntiCS(file_path_Out,NameofModel)
% Öffnen Sie die Datei zum Schreiben (wird erstellt, falls sie nicht existiert)
fileID = fopen(file_path_Out, 'w');
% Überprüfen, ob die Datei erfolgreich geöffnet wurde
if fileID == -1
error('Die Datei konnte nicht geöffnet werden.');
end
% Schreiben Sie die Daten in die Datei
fprintf(fileID, '%s\n', NameofModel);
% Schließen Sie die Datei
fclose(fileID);
disp('Daten wurden in die Datei geschrieben.');
end
function Parameter = ReadinParamters(file_path_In)
numOfpara = 1;
% Textdatei öffnen
fileID = fopen(file_path_In, 'r');
% Textdatei zeilenweise lesen und verarbeiten
while ~feof(fileID)
% Einlesen der Zeilen
name = fgetl(fileID);
valueS = strrep(fgetl(fileID), ',', '.');
value = str2double(valueS);
% Überprüfen, ob der Wert gültig ist
if ~isnan(value)
% Arrays aktualisieren
Parameter(numOfpara).names = name;
Parameter(numOfpara).values = value;
Parameter(numOfpara).value_old = value;
numOfpara = numOfpara + 1;
end
end
% Textdatei schließen
fclose(fileID);
end
function CheckSystem(status)
if status == 0
disp('Der Befehl wurde erfolgreich ausgeführt.');
else
disp('Fehler beim Ausführen des Befehls.');
end
end

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by