Type mismatch error using SolidWorks' API in MATLAB
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
for which argument 8 refers to the [ ] brackets. In the API help it is reported https://help.solidworks.com/2023/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~SelectByID2.html
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.
0 commentaires
Réponses (1)
Divyanshu
le 24 Juin 2024
Modifié(e) : Divyanshu
le 24 Juin 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 commentaires
Divyanshu
le 27 Juin 2024
Modifié(e) : Divyanshu
le 27 Juin 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.
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!