- Ensure Proper Release of COM Objects: Make sure that all COM objects are properly released. This includes not only ‘swApp’ and ‘swModel’ but also any other intermediate COM objects you create.
- Error Handling: Implement error handling to ensure that resources are released even if an error occurs. This can be done using a try-catch block. Refer to the following documentation to learn more about using a try-catch block within the code: https://www.mathworks.com/help/matlab/ref/try.html
- Debugging: Add debug statements to identify where the code gets stuck. “disp” or “fprintf” statements can be added to print intermediate values and track the execution flow.
- Close Documents Explicitly: Ensure that the opened document is closed before releasing the application.
- Use the recommended MATLAB syntax for COM objects: Refer to the following link to learn about the recommended syntax for creating variables for COM objects: https://www.mathworks.com/help/matlab/matlab_external/handling-com-data-in-matlab-software.html#:~:text=Use%20one%20of,invoke(h%2C%27TestMeth1%27)%3B
MATLAB Function for Evaluating Body Position in SolidWorks Stuck in Busy Mode
6 views (last 30 days)
Show older comments
I'm working on a MATLAB function that interacts with SolidWorks to evaluate the position of a body within a SolidWorks part. The function performs the evaluation correctly, but I'm encountering an issue where it gets stuck in a busy mode and doesn't exit. This results in MATLAB becoming unresponsive indefinitely.
function position = getposition(path, bodyname, N)
% Connect to SolidWorks
% Crea un'istanza dell'applicazione SolidWorks
swApp = actxserver('SldWorks.Application');
% Imposta l'applicazione SolidWorks come visibile
set(swApp, 'Visible', true);
% Apri il documento specificato
% 'path' deve essere il percorso completo del documento da aprire
swModel = invoke(swApp, 'OpenDoc', path, 1); % 1 per parti, 2 per assiemi, ecc.
% Ottieni il gestore delle configurazioni del modello aperto
configMgr = invoke(swModel, 'ConfigurationManager');
% Ottieni la configurazione attiva
config = invoke(configMgr, 'ActiveConfiguration');
% Ottieni i corpi solidi nella part
bodies = invoke(swModel, 'GetBodies2', 0, true); % 0 per corpi solidi
% swModel = [];
numBodies = length(bodies);
if bodyname == "Grain"
if N > 1
if mod(N, 2) == 0
bodyname = ["Grain_rep["+num2str(N/2)+"]","Grain_rep["+num2str(N/2+1)+"]"];
else
bodyname = "Grain_rep["+num2str(round(N/2))+"]";
end
end
end
position = zeros(length(bodyname),3);
j = 1;
% Loop through each body and get its name
for i = 1:numBodies
body = bodies{i}; % SolidWorks uses 0-based indexing
if length(bodyname)>1
if contains(body.Name, bodyname)
boundingBox = invoke(body, 'GetBodyBox');
position(j,:) = [mean([boundingBox(1:3); boundingBox(4:6)])];
j = j+1;
end
else
if body.Name == bodyname
boundingBox = invoke(body, 'GetBodyBox');
position = mean([boundingBox(1:3); boundingBox(4:6)]);
break
end
end
bodies{i} = [];
end
position = position.*[-1 1 1];
release(swApp)
release(swModel)
end
Despite checking for any infinite loops or blocking calls, I haven't been able to identify the cause of the issue.
Has anyone faced a similar problem or can suggest a potential solution?
Thank you in advance for your assistance!
0 Comments
Answers (1)
Saurav
on 24 Jul 2024
Hi Matteo,
I understand that your code becomes unresponsive when evaluating the position of a body in SolidWorks using MATLAB.
Your MATLAB script might be having trouble properly releasing resources or handling the SolidWorks COM object.
Here are a few suggestions to help you debug and potentially resolve the issue:
I hope this helps!
0 Comments
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!