Sample file to use rhino3dm library to create a 3DM file

Are there any sample python programs to create a 3DM file using the rhino3dm library, just want something to get me started. i.e. something like just create a file with a box.

A box you can create as:

pln = rhino3dm.Plane.WorldXY
box = rhino3dm.Box( pln, rhino3dm.Interval(0,1000), rhino3dm.Interval(0,800), rhino3dm.Interval(0,500) )
brp = box.ToBrep()
1 Like

I am struck before that.

I see that I can have

import rhino3dm

file = File3dm()
file.Write("Path") 

Which the doc says “Writes contents of this model to an openNURBS archive. If the model is not valid, then Write will refuse to write it.”

But it is not clear to me how I create “this model”

For reading I have the following but not clear how to create and write

class File3dm:
    def __init__(self, path):
        self.f3dm = r3.File3dm.Read(path)

    def parse_objects(self, doc=None):
        for i in range(len(self.f3dm.Objects)):
            obj_fullname = "{}".format(self.f3dm.Objects[i].Geometry)
            first_split = obj_fullname.split(".")
            second_split = first_split[-1].split(" ")
     

Hi @Keith_Sloan ,

import rhino3dm

# init model
model = rhino3dm.File3dm()

# create and add layer
layer = rhino3dm.Layer()
layer.Name = 'test lay'
model.Layers.Add(layer)

# create box brep
pln = rhino3dm.Plane.WorldXY
box = rhino3dm.Box( pln, rhino3dm.Interval(0,1000), rhino3dm.Interval(0,800), rhino3dm.Interval(0,500) )
brp = box.ToBrep()

# add brep to model
model.Objects.AddBrep(brp)

# save to file
filefull = R'C:\coding\test.3dm'
rhVersion = 6
succ = model.Write(filefull, rhVersion)
2 Likes

Thanks

I tried the code and had a couple of problems

  1. Did not like the rhino3dm.Box, had to create via
    box = r3.Box(r3.BoundingBox
    (
    0,
    length(obj.Length),
    0,
    length(obj.Width),
    0,
    length(obj.Height)
    )
    )

An complained about no ToBrep

14:55:05 Traceback (most recent call last):
File “”, line 4, in
File “/Users/keithsloan/Library/Application Support/FreeCAD/Mod/ImportExport_3DM/./freecad/importExport3DM/export3DM.py”, line 158, in export
export3DM(first, filepath, fileExt)
File “/Users/keithsloan/Library/Application Support/FreeCAD/Mod/ImportExport_3DM/./freecad/importExport3DM/export3DM.py”, line 66, in export3DM
addObjToModel(first, model)
File “/Users/keithsloan/Library/Application Support/FreeCAD/Mod/ImportExport_3DM/./freecad/importExport3DM/export3DM.py”, line 108, in addObjToModel
brp = box.ToBrep()
<class ‘AttributeError’>: ‘rhino3dm._rhino3dm.Box’ object has no attribute ‘ToBrep’

rhino3DM version is 7.15.0

I am using .NET version of rhino3dm library. It seems that python version does not have Box.ToBrep() method. Replace the line “brp = box.ToBrep()” with “brp = rhino3dm.Brep.CreateFromBox(box)”