Read geometry by GUID from 3dm Files

Hello,

I want to read certain items from a 3dm-File, but I don’t get how to get the Find(GUID)-Function to work.
http://developer.rhino3d.com/5/api/RhinoCommonWin/html/M_Rhino_DocObjects_Tables_ObjectTable_Find.htm

It thought it worked somehow like this (in Python):

filename = “C:\path\to\file.3dm”
objs = Rhino.FileIO.File3dm.Read(filename).Objects
ReferencedGeo = objs.Find(GUID)

Any Ideas?

Hi @stevebaer,

today a was trying to do what is described in the OP. I have an object id and a file path to a Rhino file. I want to import only the object using the id and import it along with it’s attributes (Layer, Color etc) in my currently opened Rhino 5 file. After loading the file like this:

f = Rhino.FileIO.File3dm.Read(file_name)

it seems i cannot search the object table of my file for the object id using f.Objects.Find(obj_id). So i iterate over the object table of the file, one by one, check if it matches my obj_id and import the geometry along with its attributes using:

for i in xrange(f.Objects.Count):
        obj = f.Objects.Item[i]
        if obj.Attributes.ObjectId == obj_id:
             geo = f.Objects.Item[i].Geometry
             attr = f.Objects.Item[i].Attributes
             scriptcontext.doc.Objects.Add(geo, attr)

However, this does not create the layer the object was placed on. The object is imported on the layer which is having the same index as the one defined in the object attributes.

If i iterate over f.Layers in the file, which is an File3dmLayerTable (does not appear in the RhinoCommon docs yet), i can use the layer index of the object attributes to find the layer name the object is on. I thought i then can just get the layer path from the layer in the file and create the layer myself in the opened Rhino file, then get the index and add the object i want to this layer.

But i have no idea how i easily can get the full layer path for this layer. If my object layer is nested below multiple other layers in the file, i need to get all its parent layer(s). To get the first parent of the layer i got from the LayerIndex of the object attributes i can use this:

layer = f.Layers[attr.LayerIndex]
parent_layer_id = layer.ParentLayerId

i cannot search for other parents of the parent_layer_id. It seems that the Find method which is available for an opened documents layer table is not available for the File3dmLayerTable. I tried it like so:

f.Layers.Find(parent_layer_id, True)

which tells me: ‘File3dmLayerTable’ object has no attribute ‘Find’

To get the full layer path of a layer in an opened doc, i normaly would use this:

path = layer.FullPath

But this returns just the layer name, not the full path. My layer is a sub layer. I think this is a bug. I see there has been a similar report for a related issue on Mac. Is this supposed to work on Rhino 5 windows ?

EDIT: In V6, the full path is returned…but there are other problems iterating the obj table…
_
c.

Sorry for never getting back to you on this post. I’m not sure if this is a bug in V5, but if V6 returns a different result and is closer to what you expect I can only assume that this was a V5 bug.

the following Code worked fine in GH in Rhino5
but in Rhino6 there is an Error Message:

import Rhino

f = Rhino.FileIO.File3dm.Read(FileName)

for i in xrange(f.Objects.Count):
obj = f.Objects.Item[i]
if obj.Attributes.ObjectId == ObjGuid:
geo = obj.Geometry
attr = obj.Attributes
layer = f.Layers[attr.LayerIndex]
name = attr.Name

a = geo
b = layer.FullPath
c = name

Runtime error (MissingMemberException): ‘File3dmObjectTable’ object has no attribute ‘Item’
Traceback:
line 6, in script

is there any chance to get this to work in R6?
thanks in advance…

importGUID.gh (9.0 KB)
test-guid.3dm (22.6 KB)

after fiddeling through the rhinocommon API I partly succeed with the following:

import Rhino

f = Rhino.FileIO.File3dm.Read(FileName)

for obj in f.Objects:
    if obj.Attributes.ObjectId == ObjGuid:
        geo = obj.Geometry
        attr = obj.Attributes
        name = attr.Name
    
a = geo
b = name

However what I don’t get to work is to retrieve the layer and the color of the referenced geometry…
Is there someone around with an idea how to make it?

importGUID-R6.gh (7.3 KB)
test-guid.3dm (29.9 KB)

Hi @3achs1,

to get the layer you can use:

layer = f.Layers.FindIndex(attr.LayerIndex)

to get the layer color use:

color = layer.Color

alternatively, to get the object color:

color = attr.ObjectColor

_
c.

It works!!!

Thank You very much!