g = decsg() multiple objects

10 views (last 30 days)
Bastiaan
Bastiaan on 2 Oct 2024
Commented: Bastiaan on 9 Oct 2024
Hello I am trying to combine multiple objects. however i am not sure if I am doing it right. so back to basics.
what i am trying to do is R1+R2+R3 because all of them are adjecent. and then substract only from the geometry R1 all the holes. but the holes are also on the edges of R1. see attached images.
How do I know the holes are substracted? this is important for steps later in the script, because there are boundary values to it attached.
R1 =[3, 4, rectx, rectx + rect_width, rectx + rect_width, rectx, recty, recty, rect_height, rect_height]';
R2= [3,4,rectx,rectx+rect_width,rectx+rect_width,rectx,recty,recty,rect_height+thickness,rect_height+thickness]';
holes=[];
for j = 0:num_rep_x - 1
for i = 0:num_holes_y - 1
% Calculate the x and y position for each hole
x_center = x_start + j * x_interval;
y_center = y_start - i * y_interval;
% Create a numeric array with circle specification and 6 zeros
hole = [1, x_center, y_center, radius, zeros(1, 6)]'; % Circle specification with 6 zeros
holes =[holes, hole] % Store the hole data
end
end
% %Substract from Rectangles Software Function
num_holes=size(holes,2);
sf1 = 'R1+R2+R3';
for k = 1:num_holes
circleName = ['C', num2str(k)]; % Create circle name
sf1 = [sf1,'-',circleName]; % Append each circle to the formula
end
sfcomb=[sf1]
gm1 = [R1,R2,R3]
gm2 = [holes]
% gmcomb=[gm1,gm2];
gmcomb=[gm1,gm2]
% ns{1}='R1';
% ns{2}='R2';
% ns{3}='R3';
%define ns size
total_elements =num_holes+2; %R1+Cn...+Rectangles
ns = cell(1, total_elements)
% %assign names for rectangles to char
%error shape names in the name space matrix must be valid variable names
ns{1} ='R1';
ns{2} ='R2';
ns{3} ='R3';
for k = 1:num_holes
ns{k+3} =['C', num2str(k)]; % substract from R1-Cn..... dont use %-% in the char.
end
g = decsg(gmcomb,sfcomb,ns_char);
geometryFromEdges(EModel,g);
if I use the easier code i see that the holes are substracted because it is not highlighted anymore by edge and or faces.
R1 =[3, 4, rectx, rectx + rect_width, rectx + rect_width, rectx, recty, recty, rect_height, rect_height]';
R2= [3,4,rectx,rectx+rect_width,rectx+rect_width,rectx,recty,recty,rect_height+thickness,rect_height+thickness]';
holes=[];
for j = 0:num_rep_x - 1
for i = 0:num_holes_y - 1
% Calculate the x and y position for each hole
x_center = x_start + j * x_interval;
y_center = y_start - i * y_interval;
% Create a numeric array with circle specification and 6 zeros
hole = [1, x_center, y_center, radius, zeros(1, 6)]'; % Circle specification with 6 zeros
holes =[holes, hole] % Store the hole data
end
end
% %Substract from Flange left and right
num_holes=size(holes,2);
sf1 = 'R1';
for k = 1:num_holes
circleName = ['C', num2str(k)]; % Create circle name
sf1 = [sf1,'-',circleName]; % Append each circle to the formula
end
sfcomb=[sf1]
gm1 = [R1]
gm2 = [holes]
% gmcomb=[gm1,gm2];
gmcomb=[gm1,gm2]
total_elements =num_holes; %R1+Cn...+Rectangles
ns = cell(1, total_elements)
%
% %assign names for rectangles
%error shape names in the name space matrix must be valid variable names
ns{1} ='R1';
for k = 1:num_holes
ns{k+1} =['C', num2str(k)]; % substract from R1-Cn..... dont use %-% in the char.
end
%%error i get is cell array cant be transformed to char array
%this solves it, dirty trick.
ns=cellfun(@num2str,ns,'uniformOutput',false);
ns_char=char(ns)';
disp(['number of holes:',num2str(num_holes)]);
disp(['Total Elements: ',num2str(total_elements)]);
disp(['Length of gmcomb: ', num2str(length(gm1))]);
disp(['Length of ns: ', num2str(length(ns))]);
disp('Contents of ns:');
disp(ns);
% Check if the number of elements in gmcomb matches the number of names in ns
if length(gmcomb) ~= length(ns_char)
error('The number of elements in gmcomb does not match the number of names in ns.')
end
g = decsg(gmcomb,sfcomb,ns_char);
geometryFromEdges(EModel,g);
generateMesh(EModel,Hmin=0.001);
figure
pdegplot(EModel,FaceLabels="on");
figure
pdegplot(EModel,EdgeLabels="on");

Answers (1)

Ashok
Ashok on 7 Oct 2024
The unexpected result is due to the set formula being passed to the ‘decsg function. The attached example code creates a similar geometry as shown below.
Note that the ‘csgdel function is used to remove face boundaries. You can learn more about this function in the following documentation.
In the attached code, try commenting out the line with the ‘csgdel function and observe the changes in the plots.
Lastly, the ‘pdeviz’ function can be used for better visualization as shown below.
To learn more about the ‘pdeviz’ function, refer to the following documentation page.
I believe this will assist you!

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!