# Extract arguments from MeshRegion created by STL import

I've been trying to extract points and polygons from a MeshRegion expression created by importing an STL file. At first I thought I could use a simple ReplaceAll as shown below:

test = Abc[{a1, b2},  Method -> {method}];
test /. Abc[a_, b_] :> a


This worked. However, the following does not:

test2 = MeshRegion[{{0, 0, 0}, {2, 0, 0}, {2, 2, 0}, {0, 2, 0}, {1, 1,
2}}, Pyramid[{1, 2, 3, 4, 5}], Boxed -> True];

test2 /. MeshRegion[a_, b_] :> a


I get the error message

MeshRegion::bdcoord: The coordinates given at position 1 in MeshRegion[a_,b_] are not a list of real-valued points in the same dimension. >>


Any ideas?

• Look up MeshCoordinates. – ilian Oct 26 '15 at 19:33
• MeshRegion is atomic: AtomQ[test2] returns True. – Chip Hurst Oct 26 '15 at 19:46
• Also, to avoid the message use HoldPattern[MeshRegion][a_, b_] :> a. Your replacement rule is trying to evaluate before being applied. – Chip Hurst Oct 26 '15 at 20:25
• @ChipHurst you are right it is atomic. But why does that stop me extracting the argument? If you test Sin there is no problem: test = Sin[{1, 2, 3}]; test /. Sin[a_] :> a Out[36]= {1, 2, 3} – Robert Oct 27 '15 at 20:44

Import["ExampleData/spikey.stl", #] & /@ {"VertexData", "PolygonData"}

You can also extract vertex positions and polygons from the MeshRegion using the functions MeshCoordinates and MeshCells
• It would be more efficient to do a single import using Import["ExampleData/spikey.stl", {{"VertexData", "PolygonData"}}] – Bob Hanlon Oct 26 '15 at 21:37