Rhino.FileIO.File3dm.Read is not keeping layers

Hello,

I am using RhinoCommon in Python to load a 3dm file. Right now, this is happening “headless”, but I would like the code to not break if I run it in the Rhino GUI. The code snippet below seems to load the correct number of objects, but the layers are lost. There is only one layer named “default” instead of the ones that are really in the file.

I suspect that the way the objects are added into the document is not correct.

Thanks in advance for your advice.

        # Check if the file exists
        if os.path.exists(filePath) is False:
            message = f'The file does not exist: "{filePath}"'
            self.log(message)
            return 0


        # If the file does not exist it throws an exception
        # If the file exists but is not a Rhino file, then it returns None
        file3dm = self.rhino.FileIO.File3dm.Read(filePath)
        self.log(f'Result of reading the Rhino 3dm file is {file3dm}')
        if file3dm is None:
            return 0


        counter = 0
        for obj in file3dm.Objects:
            geo = obj.Geometry
            attr = obj.Attributes

            # Add the objects to active document
            self.document.Objects.Add(geo, attr)
            counter += 1

        self.log(f'Added {counter} objects to the document')
        self.document.Views.Redraw()


        # For testing, let's print all of the layer names
        layerCount = self.document.Layers.Count
        self.log(f'Here are the layer names for {layerCount} layers')
        for n in range(layerCount):
            layerObject = self.document.Layers[n]
            self.log(f'    Name={layerObject.Name}')


        return file3dm.Objects.Count

Hi @Henry_Wede,

You need to add the layers from the File3dm object - File3dm.AllLayers - to the document yourself.

Or am I missing something in your code?

– Dale

No, you’re not missing anything. I thought adding the objects would also put them on the layers that they are on in the file.

What is the recommended way to do this?

On a side note, this seems like a lot of work to open/import an existing file. Am I doing this all wrong?

Thanks for your help.

Hi @Henry_Wede,

If you are trying import a .3dm file into an existing document, you might try RhinoDoc.Import.

– Dale

Dale -

The RhinoDoc.Import method works as expected. So, I am going to run with that and ignore my File3dm failure.

Henry