Rhino3dmIO (File3dm.Objects.ToList() returns the right count but with all null values)

Hi,
I am reading Rhino file using Rhino3dmIO module inside Revit. It reads the file but upon calling File3dm.Objects.ToList() returns all objects as nulls. I want to get InstanceReferenceGeometriy and InstanceDefinitionGeometry even after using filter it returns only nulls.

It seems there is a problem with my computer…same code worked on another computer :thinking:dont know what’s happening exactly.

find attached image.

Thanks!

Could it be a possible conflict between the 3dm reader that is shipped with Revit, and the one that is loaded with Rhino.Inside.Revit?

it seems that Rhino.RhinoDoc.ActiveDoc.Object.ToList() always returns null values but if you foreach or do something like Rhino.RhinoDoc.ActiveDoc.Object.Select(x=>x) it works fine.

Thanks!

1 Like

I am having the same problem but this is within a net6 solution with no Revit context. Was there a solution to this thread?

Hi @daniel4dev,

Are you using the C# version of Rhino3dm? As always, sample code that doesn’t work for you is helpful.

Thanks,

– Dale

Hi @dale,

Yes we are using the C# version of Rhino3dm, in all snippets I put below file is a File3dm type.

So at first we just tried to do var items = file.Objects.ToList() but got the error from above. Then I took the advice from above and tried var items = file.Objects.Select(x => x).ToList() but still got the error, but I think that’s because LINQ was simplifying expression so the select was essentially doing nothing. Then finally when I do var items = file.Objects.Where(x => true).ToList() I no longer get the error.

I think there might be something around the type of file.Objects inherits both from IList and IEnumerable and the IList implementation is failing so I need to force LINQ to query on the IEnumerable and casting file.Objects to IEnumerable doesn’t work either because LINQ is just too smart :laughing:

Thanks,
Daniel

When I use LINQ for File3dm.Objects I get a NullReferenceException.
Below is an example when the exception is thrown:

File3dm d = new File3dm();
d.Objects.AddLine(new Point3d(), new Point3d(1,1,1));

for (int i = 0; i < d.Objects.Count; i++)
{
File3dmObject docObj = d.Objects.ElementAt(i);
Console.WriteLine(docObj.Id);
}

I am using .Net runtime version 8 on windows 11. The Rhino 3dm version used is 8.9.0
Is there some reason why it is no longer possible to use LINQ for File3dm.Objects.

This is a known bug and is logged here:

RH-80461 File3dmObjectTable returns different values using linq vs iterator

As a workaround, use this getter function:

bool TryGetElementAt(File3dm doc, int index, out object element)
{
    element = default;
    int i = 0;
    foreach (object dobj in doc.Objects)
    {
        if (i == index)
        {
            element = dobj;
            return true;
        }

        i++;
    }

    return false;
}

using (File3dm doc = File3dm.Read(tempfile))
{
    if (TryGetElementAt(doc, 0, out object element))
    {
        Console.WriteLine(element.ToString());
    }
}

Thanks for your response. I will adjust my code with your workaround. What is special is that in an earlier version of the rhino3dm it did work. I only noticed when I upgraded that it no longer works.
In any case, thanks for your help.