Type mismatch error using SolidWorks' API in MATLAB
16 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Divyanshu
2024-6-24
编辑:Divyanshu
2024-6-24
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!