Getting mesh data of ply file

I have a mesh I’m trying to import as follows:

RhinoApp.RunScript("_selnone -_import " & FileName & " weld=yes weldangle=22.5 splitdisjointmeshes=yes modelunits=millimeters _enter ", True)
FootID = doc.Objects.MostRecentObject().Id
Foot = TryCast(doc.Objects.Find(FootID).Geometry, Mesh)

This works fine if the file is an stl file. But if it is a ply file then the Foot variable doesn’t get populated with the mesh of the ply file. What is the proper way to get the mesh data of a ply file?

Thanks,
Sam

Hi @samlochner,

Here is a sample that seems to work:

import System
import Rhino
import scriptcontext as sc

def test_import_ply():
    
    path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
    filename = System.IO.Path.Combine(path, "test.ply");
    filename = chr(34)+ filename + chr(34) # double-quote
    
    #  Get the runtime serial number of the next Rhino Object that will be created
    start_sn = Rhino.DocObjects.RhinoObject.NextRuntimeSerialNumber
    
    Rhino.RhinoApp.RunScript("_-import " + filename + " _weld=_yes _weldangle=22.5 _splitdisjointmeshes=_yes _modelunits=_millimeters _enter", True)
    
    #  Get the runtime serial number of the next Rhino Object that will be created
    end_sn = Rhino.DocObjects.RhinoObject.NextRuntimeSerialNumber
    
    rh_objects = []
    for sn in range(start_sn, end_sn):
        rh_obj = sc.doc.Objects.Find(sn)
        if rh_obj:
          rh_objects.append(rh_obj)
    
    for rh_obj in rh_objects:
        print(rh_obj.ObjectType)

if __name__ == "__main__":
    test_import_ply()

– Dale

Hi Dale,

What’s curious about my code is that the variable foot does get populated with faces and vertices. I just can’t add the mesh to the document. I even tried creating a new mesh and copying over the vertices and faces and computing normals and compacting but I still can’t add the mesh to the document.

I tried to get your method going. I’m working in VB.net and normally I can use a code converter, but for some reason converters aren’t doing a good job in this case. And I don’t quite understand what you are doing enough to write it myself. I think you have an array of meshes that somehow get appended to a new mesh… Any chance you could write this in VB.net for me?

Thanks,
Sam

Here you go.

TestSamCommand.vb.txt (1.3 KB)

– Dale

Thanks Dale, that is working, though I’m still not able to retrieve the resulting mesh and then add it back to the document as follows: ​

Dim TestMesh = TryCast(doc.Objects.Find(rh_objects(0).Id).Geometry, Mesh)
​doc.Objects.AddMesh(TestMesh)

It works if it’s an stl I’m dealing with, but not ply for some reason. Suggestions?

Thanks,
Sam

@samlochner - I’m confused. The code I provided hands you the MeshObject. What else do you need?

– Dale

Hi Dale,

I have the mesh object and I can do operations on it, like intersect a plane with it to get polylines. The odd thing though is that if I try to add it back to the doc, nothing happens.

Dim TestMesh = TryCast(doc.Objects.Find(rh_objects(0).Id).Geometry, Mesh)
doc.Objects.AddMesh(TestMesh)

That code does not add a mesh to the doc, despite the fact that I can take that TestMesh and get a list of vertices, do intersections, etc…

Thanks,
Sam