MATLAB Function for Evaluating Body Position in SolidWorks Stuck in Busy Mode
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!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! 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! solidworks, stuck MATLAB Answers — New Questions