Solved

Reading mesh object from json

  • 2 December 2021
  • 7 replies
  • 45 views

Badge +1

Hello,

 

I have a 3d file created with Speckle, in json format (attached). Can you help me to read the geometry?

 

I have been able to extract the mesh data using the JSONFragmenter, but am struggling with making geometry from it. I have tried the GeometryReplacer but cant seem to find the right encoding option.

 

The data is the sample revit model, and should look like this

imageMany thanks,

 

Owen

icon

Best answer by daveatsafe 3 December 2021, 02:06

View original

7 replies

Userlevel 2
Badge +17

Hi @owen​,

Please try the attached workspace. It uses JSONFragmenters to break up and flatten the JSON into geometry and property lists, then a bit of Python to build the mesh geometry from the lists.

 

Screen Shot 2021-12-02 at 5.00.58 PM

Badge +1

Hi @owen​,

Please try the attached workspace. It uses JSONFragmenters to break up and flatten the JSON into geometry and property lists, then a bit of Python to build the mesh geometry from the lists.

 

Screen Shot 2021-12-02 at 5.00.58 PM

@daveatsafe​ This is absolutely fantastic, thank you so much. You've really helped us out here!🙌

Userlevel 2
Badge +17

Hi @owen​,

Sorry, there was a bit of an error in the first workspace - the first value in the faces quartet is a flag, not an index, so the output geometry is a little off. Please try this update.

There also seem to be some sub-elements within some of the features. I will look into ways to extract these as well, if needed.

Badge +1

Hi @owen​,

Sorry, there was a bit of an error in the first workspace - the first value in the faces quartet is a flag, not an index, so the output geometry is a little off. Please try this update.

There also seem to be some sub-elements within some of the features. I will look into ways to extract these as well, if needed.

Thanks again Dave. Yes I did notice that and wondered if it was the order or the vertices but , looks really good now. If you were interested, this is how the Rhino connector builds geometry from Speckle data https://github.com/specklesystems/speckle-sharp/blob/main/Objects/Converters/ConverterRhinoGh/ConverterRhinoGhShared/ConverterRhinoGh.Geometry.cs#L681

Userlevel 2
Badge +17

Thanks again Dave. Yes I did notice that and wondered if it was the order or the vertices but , looks really good now. If you were interested, this is how the Rhino connector builds geometry from Speckle data https://github.com/specklesystems/speckle-sharp/blob/main/Objects/Converters/ConverterRhinoGh/ConverterRhinoGhShared/ConverterRhinoGh.Geometry.cs#L681

Thanks for that link. I did find another codebase that suggests that the flag is to designate 3 or 4 sided faces in the mesh. If you find some data with 4 sided faces, you may want to try the Speckle output to confirm.

There are a couple of ways I could handle the sub-elements:

  • create separate feature with a parent id pointing to the main feature
  • create the hierarchical geometry structure, with the attributes as geometry traits at the appropriate level

The second method is how we read IFC data, and is a pain to work with for most users. The first method would be the simplest, and I think the most useful. What are your thoughts?

Badge +1

Thanks again Dave. Yes I did notice that and wondered if it was the order or the vertices but , looks really good now. If you were interested, this is how the Rhino connector builds geometry from Speckle data https://github.com/specklesystems/speckle-sharp/blob/main/Objects/Converters/ConverterRhinoGh/ConverterRhinoGhShared/ConverterRhinoGh.Geometry.cs#L681

I think it depends which format you would want to write back to, as to how you would deal with the sub elements. The first option would probably be fine for most use cases, but maybe the second might be required to wrote back to a Speckle stream. It seems like there are instances or referenced objects in there too.

 

This is great for now anyway. I think it might require a Specke reader or SpeckleConnector transformer, plus a Speckle Manager FME Connector Speckle Connectors

Badge +1

Hi @daveatsafe​, @owen​,

I'm very glad to have found this post, it helped us greatly in processing the JSON output from Speckle. Considering three- and four-sided faces, we changed the python code a bit. For further quests here is the code we use: 

import fme
import fmeobjects
 
 
def makeGeom(feature):
    # read faces and vertices from attributes
    faces = feature.getAttribute('faces{}')
    vertices = feature.getAttribute('vertices{}')
    
    # create new Mesh geometry
    mesh = fmeobjects.FMEMesh()
    
    # create vertex XYZ from three vertex list entries
    for v in range(int(len(vertices)/3)):
        mesh.appendVertex(vertices[v*3],vertices[v*3+1],vertices[v*3+2])
    
    # create face from four or five face list vertex entries
    i = 0
    while i < len(faces):
        if faces[i] == 0:
            mesh.addMeshPart(0,[faces[i+1],faces[i+2],faces[i+3]],None,None)
            i = i + 4
        else:
            mesh.addMeshPart(0,[faces[i+1],faces[i+2],faces[i+3],faces[i+4]],None,None)
            i = i + 5
        
    # set geometry to new Mesh
    feature.setGeometry(mesh)

 

Reply