2
$\begingroup$

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?

$\endgroup$
4
  • 3
    $\begingroup$ Look up MeshCoordinates. $\endgroup$ – ilian Oct 26 '15 at 19:33
  • 2
    $\begingroup$ MeshRegion is atomic: AtomQ[test2] returns True. $\endgroup$ – Chip Hurst Oct 26 '15 at 19:46
  • 1
    $\begingroup$ Also, to avoid the message use HoldPattern[MeshRegion][a_, b_] :> a. Your replacement rule is trying to evaluate before being applied. $\endgroup$ – Chip Hurst Oct 26 '15 at 20:25
  • $\begingroup$ @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} $\endgroup$ – Robert Oct 27 '15 at 20:44
3
$\begingroup$

You can import vertex coordinates and polygons directly from your stl file:

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

You can also extract vertex positions and polygons from the MeshRegion using the functions MeshCoordinates and MeshCells

$\endgroup$
2
  • 2
    $\begingroup$ It would be more efficient to do a single import using Import["ExampleData/spikey.stl", {{"VertexData", "PolygonData"}}] $\endgroup$ – Bob Hanlon Oct 26 '15 at 21:37
  • $\begingroup$ @BobHanlon thanks, the double brackets are not obvious in the help! $\endgroup$ – Robert Oct 27 '15 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.