combine two stl into one

25 ビュー (過去 30 日間)
Alberto Acri
Alberto Acri 2024 年 6 月 22 日
編集済み: DGM 2025 年 7 月 19 日
I have two 3D geometries ('sol_1.stl' and 'sol_2.stl') and their corresponding nodes and faces.
I would like to combine the two STL geometries into a single geometry, or merge the nodes and faces of the two geometries into one.
  • sol_1.st + sol_2.st -> sol_fin.stl
  • nodes_sol_1 + nodes_sol_2 -> nodes_sol_fin (simple)
  • faces_sol_1 + faces_sol_2 -> faces_sol_fin

採用された回答

Avni Agrawal
Avni Agrawal 2024 年 6 月 28 日
I understand that you are trying to combine two STL geometries into a single geometry by merging their nodes and faces in MATLAB, you can follow these steps:
  1. Read the STL files to get the nodes and faces.
  2. Merge the nodes and faces from both geometries.
  3. Write the combined geometry to a new STL file.
Below is a MATLAB script that demonstrates how to do this:
% Read the STL files
fv1 = stlread('../sol/sol_1.stl');
fv2 = stlread('../sol/sol_2.stl');
% Extract nodes (vertices) and faces from the first geometry
nodes1 = fv1.Points;
faces1 = fv1.ConnectivityList;
% Extract nodes (vertices) and faces from the second geometry
nodes2 = fv2.Points;
faces2 = fv2.ConnectivityList;
% Merge nodes
% Note: We need to adjust the face indices of the second geometry
% by adding the number of nodes in the first geometry
% Combine nodes
combinedNodes = [nodes1; nodes2];
% Adjust face indices for the second geometry
adjustedFaces2 = faces2 + size(nodes1, 1);
% Combine faces
combinedFaces = [faces1; adjustedFaces2];
% Create a new triangulation object for the combined geometry
combinedGeometry = triangulation(combinedFaces, combinedNodes);
% Write the combined geometry to a new STL file
stlwrite(combinedGeometry, 'combined_geometry.stl');
I hope this helps.
  1 件のコメント
DGM
DGM 2025 年 3 月 31 日
Note that this doesn't actually do any CSG to combine the geometry (e.g. the union). This just creates a model with two independent connected components.
If the objects intersect, there will be no new vertices or edges formed at their intersection, and the model will have interior and/or tangential faces and edges.

サインインしてコメントする。

その他の回答 (1 件)

DGM
DGM 2025 年 7 月 17 日
編集済み: DGM 2025 年 7 月 19 日
Perhaps this will be easier to use. Say we have multiple files or other representations of triangulated surfaces:
% if you have files or other things, just throw them into a cell array
unzip stlpile.zip % prepare things for the forum
% model 1 (a triangulation object)
Tc{1} = stlread('stlpile/stepholecube.stl');
% model 2 (F,V lists)
Tc{2} = stlread('stlpile/sphere_20.stl');
[F V] = t2fv(Tc{2});
V = V/25;
V(:,1) = V(:,1) + 1.5;
Tc{2} = {F V};
% model 3 (an FV struct)
[x y z] = peaks(21);
Tc{3} = surf2patch(x/2,y/2 + 3,z/6,'triangles');
% model 4 (a TriRep object)
% pretend for a moment we're using an old version
[F V] = stlread_2011('stlpile/btpyramid.stl'); % prior to R2018b (FEX #22409)
V = bsxfun(@minus,V,[2 0.5 0.5]); % prior to R2016b
Tc{4} = TriRep(F,V); %#ok<DTRIREP> % prior to R2013a
At this point, we have a cell array of four models in different formats. Combining them is easy.
% concatenate the models and write to a file
T = tricat(Tc); % attached
stlwrite(T,'test.stl')
% display the composite model
trisurf(T,'facecolor','w','edgecolor','k')
view(3); view(-45,25); camlight;
axis equal; grid on
As before, this does not do any CSG on the models. It just concatenates and does index offsets and some cleanup.
The input to tricat() is a cell array containing surface data in any of the following formats:
  • triangulation objects
  • delaunayTriangulation objects
  • legacy TriRep or DelaunayTri objects
  • FV structs
  • cell arrays containing F,V lists
The combined data is pruned and returned as either a triangulation object or as F,V lists.
While tricat() itself should run on older versions, the rest of this example isn't version-agnostic. If we were truly running an old version, as in example 4, T would be a TriRep object, and we would need to use a third-party encoder.
See also:

カテゴリ

Help Center および File ExchangeSTL (STereoLithography) についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by