Type mismatch error using SolidWorks’ API in MATLAB
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:Pathtoyourfile.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.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:Pathtoyourfile.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. 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:Pathtoyourfile.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. api, actxserver, com, solidworks MATLAB Answers — New Questions