# How do I create an STL file from nearest neighbour graph edges?

I'm trying to figure out to deal with (and design) .stl files within Mathematica.

As an example, I would like to generate a FCC lattice without the atoms (but connecting all nearest neighbours), and exporting it as an STL file. I've obtained the coordinates from LatticeData, connected the nearest neighbours with NearestNeighbourGraph, and modified the Edge thickness. However, exporting the data as an .stl file only shows the atoms (the vertices), and not the edges.

How can I generate a list of nearest neighbour edges within Mathematica from a lattice structure that can then be successfully exported into the .stl format?

cell = LatticeData["FaceCenteredCubic", "Image"];
coords = Translate[DeleteCases[cell, {_, _, Polygon[_]}, Infinity][[1]], 2 Tuples[Range[1], 3]][[1]][[1]][[1]]
graph = NearestNeighborGraph[coords, VertexSize -> .05,  EdgeShapeFunction -> ({Darker[Black], Thickness[0.04], Line[#1]} &)]
Export["temp.stl",Show[graph]]

• Instead of Line[#1] for the EdgeShapeFunction, try Tube[#1, 1/40]. – LouisB Nov 14 '20 at 8:28

Extract the edges from the graph and convert them to tubes:

gr = EdgeList[graph] /. UndirectedEdge[x_, y_] :> Tube[{x, y}, 0.1] //
Graphics3D


You'll have to add spheres at vertices to make the object manifold, I believe, and good luck with including support structure if you are interested in 3D printing.

• Thanks - I'm interested in using the lattice to hollow out parts with otherwise higher density. They seem to work out well for the ball and bond models, but I haven't tried printing any without atoms yet. Have you tried with just the edges and found them difficult to print without support structures? – Zhao Nov 14 '20 at 13:21
• I've been working on ways to print molecules: Export["<file.stl>", MoleculePlot3D@Molecule["Methane"]] and simple models work fine if the bonds and atoms are similar in size. As I move up to bigger molecules (and therefore want smaller atoms and bonds) the support structure is becoming an issue. – bobthechemist Nov 14 '20 at 19:25