Type mismatch error using SolidWorks' API in MATLAB

16 visualizzazioni (ultimi 30 giorni)
Hello everyone,
I am experiencing hard time in trying to solve a problem when using the SolidWorks' API in MATLAB. I want to use SolidWorks as CAD/CAE engine to be called iteratively by a MATLAB function for structural optimization.
I can get the access to the API by using the 'actxsever' instruction and use lots of API's functionalities. However, there are some methods that I cannot use for two main reasons:
Type mismatch
When I try to use methods that require arguments related to errors or warnings. For example, when I want to use the OpenDoc6 method:
% Create a SolidWorks instance
swApp = actxserver('SldWorks.Application');
% Set SolidWorks as visible
invoke(swApp,'Visible', true);
% Set file path
filePath = "C:\Path\to\your\file.sldprt";
% Set arguments for OpenDoc6
swDocPART = 1;
swOpenDocOptions_Silent = 1;
% Arguments for error and warning
Errors = int32(0);
Warnings = int32(0);
% Open the document
PartDoc = invoke(swApp, 'OpenDoc6', filePath, swDocPART, swOpenDocOptions_Silent, '', Errors, Warnings);
I get the following error:
Error using COM.SldWorks_Application/invoke
Error: Type mismatch, argument 5
Error in prova_opendoc6 (line 19)
PartDoc = invoke(swApp, 'OpenDoc6', filePath, swDocPART,swOpenDocOptions_Silent, '', Errors, Warnings);
Looking on the API help about OpenDoc6 method, specifically the C++ syntax, I can see that Errors and Warnings are actually outputs: https://help.solidworks.com/2023/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.isldworks~opendoc6.html
but I don't understand how to manage this within MATLAB.
NULL or NOTHING
When there is the need of using the MATLAB equivalent of NULL or NOTHING. I tried with empty array [], empty string "", NaN, missing, etc. But none of them seems to work. For example, if I want to select something with SelectByID2:
myModelExt = invoke(myModel,'Extension');
boolstatus = invoke(myModelExt,'SelectByID2',"Boss-Extrude1", "SOLIDBODY", 0, 0, 0, true, 0, [], 0);
I get the error:
Error using Interface.SldWorks_Application_31.ActiveDoc.Extension/invoke
Error: Type mismatch, argument 8
but I have no idea how to manage the Callout argument.
I kindly ask you to help me to solve these difficulties. Maybe MATLAB's possibilities to use the SolidWorks' API are just limited and it is not possible using all the capabilities of the API, but I would like to hear about you before switching to other approaches.
Thank you very much.

Risposte (1)

Divyanshu
Divyanshu il 24 Giu 2024
Modificato: Divyanshu il 24 Giu 2024
Hi Mario,
The 8th argument of the method is an object of Callout type. Generally, such objects are represented as VT_DISPATCH, therefore one might need to create an object of type VT_DISPATCH with NULL value and then pass that object as 8th argument.
However, I think this is not supported in COM interface as of now.
So a possible workaround would be to use .NET interface of Solidworks.
If you are familiar with .NET assembly of Solidworks, the following C# code can be converted to MATLAB, :
SldWorks.SldWorks swApp = new SldWorks.SldWorksClass();
SldWorks.ModelDoc2 swDoc = swApp.IActiveDoc2;
swDoc.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
In the .NET Interface of MATLAB, [] would pass null to the object, just like C#.
  3 Commenti
Divyanshu
Divyanshu il 27 Giu 2024
Modificato: Divyanshu il 27 Giu 2024
Hi,
Is this the issue only with method 'OpenDoc6' or for other methods as well? Because if it is only with 'OpenDoc6' then probably some issue with number of arguments or type of arguments passed to the method, ensure they match the definition.
Mario Brandon Russo
Mario Brandon Russo il 28 Giu 2024
Hi,
Unfortunately these two problems happen with all the methods. The first one (Type mismatch) happens for all the methods with arguments collecting error and warning information. The second one (Null or Nothing) happens for all the methods with arguments that can be "nothing". In both cases the error prompted by MATLAB is the type mismatch.

Accedi per commentare.

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by