# What is the easiest way to know the minimal distance between 2 mesh (Objects .stl)?

Hi!

So i will start with the code i use to do it :

``````            # Save mesh so we can calculate the smallest distance
mesh_obj = rs.coercemesh(CurentObj)
vertices = mesh_obj.Vertices
normals = mesh_obj.Normals

# Go through all the points and mesure the smallest distance to the mesh
for index, vertex in enumerate(vertices): #

ClosestPoint=rs.MeshClosestPoint(Lastobj,[vertex.X,vertex.Y,vertex.Z])

PointDistance=rs.Distance(ClosestPoint[0],[vertex.X,vertex.Y,vertex.Z])

if  PointDistance <  SmallestDistance : #store the good value
SmallestDistance = PointDistance
GoodIndex=index
CurentObjPoints=[vertex.X,vertex.Y,vertex.Z]
LastObjPoints=ClosestPoint[0]
``````

I will now explain why are we doing that. We are importing multiples .STL files and we want to optimize the printing volume. To do so, we check the distance between the two observed objects and we move them to the minimal distance we can due to the printer tolerance.

Doing it that way, with this code, take time and i wanted to know if there was another function or others way to do it faster and more efficiently. We did that with V5, but we have access to V6 now.

``  -Dave``

Yes, 3d printers place object on platform based on object bounding box dimensions. We use Solidscape printers but it is probably similar with others printers:

Minimum distance between vertex points of stl files should work in real life of 3d printing world. Here is solution that is based on determining min distance of vertex points - Grashoppher C# component in Rh6:

TestMeshDistance2.gh (3.7 MB)

I should have mentioned that every piece we print (SLS) is different so we can’t really use a template. Plus, when we have the smallest distance, using this code, we move the second object closer or further using vectors so we can be right on the tolerance of the printer (And maximize our printing volume)

I will check what i can do with Grasshopper!

Again, thank you

Hi Dave - here’s a clearance tester I use , in case it does any good -

-Pascal

Hi Pascal,

From what i understand and tested with your code, you have to select 1 point of a mesh to compare with 1 point of another mesh. However, i need to compare meshes of 200000 vertices and 72000 triangles (Approx.). So, i don’t think i can use it

But thanks for the help!

• Dave

Hi Dave - you only need to pick a point in the same general location as the visual closest point, not a particular vertex. Try it.
I don’t think there is any use in a second point pick at all - I’ll take that out, it was a copy/paste brain spasm.

-Pascal

Update!

I had some time to run that code and i like it. It seems really faster! My only concern is that i don’t get the same result with both code when i check the same 2 pieces in the same position. Mine give 1.25mm and yours 1.34mm. Do you have any ideas why it give that result?

-Dave

Hi!

I understand, but i should have mentioned too that i want to stack (for example) 100 objects automatically and it’s impossible for me to check all of them individually and place them manually every time.

-Dave

OK - I don’t say I’ve solved all your problems, I just thought you might be able to use some of that in writing your own thing.
Dunno if there might be something useful among the mesh clash RhinoCommon functions -

http://developer.rhino3d.com/api/RhinoCommon/search.html?SearchText=clash

-Pascal

Your code is checking/determining distance between first-mesh-vertex points and second mesh while my code is checking for min distance btw vertex points in both meshes => there is difference,
Both codes are approximation, but you should know what stl files you are working with and decide what approximation/precision is acceptable for you. For example if you set minimum distance btw objects to be 4mm then I think it is not really important if objects are placed so their distance is 3.9 or 4.1 mm as long as you get it fast.

It’s not what i was saying. There is definitely something i can do with it and i will check that link

Thank you

• Dave

I see! That explain it really clearly. I will have to do some tests because the minimum distance between 2 pieces have to be of 0.5 mm to maximize the number of pieces printed with a certain volume of powder.

Thank you

• Dave