Hello friends.
I’m working on a Python script and I’ve reached a point where I need to export / add objects from my active .3dm (where I’m running the script) to another existing .3dm without overwriting the existing objects, layers, etc.
Can you help me?
I have tried the “Export” command, but it overwrites the file deleting what was there before.
The Export command always overrides any existing archive.
If you need to append new geometry to an existing archive, you’ll either need to open the file, add your geometry, and save. Or you can try reading and writing the 3dm file yourself.
For example:
import Rhino
import rhinoscriptsyntax as rs
def create_circle():
plane = Rhino.Geometry.Plane.WorldXY
circle = Rhino.Geometry.Circle(plane, 5.0)
curve = Rhino.Geometry.ArcCurve(circle)
return curve
def get_open_filename():
fd = Rhino.UI.OpenFileDialog()
fd.Filter = 'Rhino 3D Models (*.3dm)|*.3dm||'
if fd.ShowOpenDialog():
return fd.FileName
return None
def test_append_3dm():
fname = get_open_filename()
if fname is None:
return
f = Rhino.FileIO.File3dm.Read(fname)
if f:
curve = create_circle()
f.Objects.AddCurve(curve)
f.Write(fname, 0)
test_append_3dm()
This solution is not exactly what I want to do.
I need to append an existing object.
For example:
import Rhino
import rhinoscriptsyntax as rs
def get_open_filename():
fd = Rhino.UI.OpenFileDialog()
fd.Filter = 'Rhino 3D Models (*.3dm)|*.3dm||'
if fd.ShowOpenDialog():
return fd.FileName
return None
def test_append_3dm():
fname = get_open_filename()
if fname is None:
return
f = Rhino.FileIO.File3dm.Read(fname)
if f:
object = rs.GetObject(message="Select polysurface object:", filter=16, preselect=False, select=False, custom_filter=None, subobjects=False)
f.Objects.Add(object)
f.Write(fname, 0)
test_append_3dm()
Message: expected File3dmObject, got Guid
Traceback:
line 20, in test_append_3dm, "C:\Users\mcr16\AppData\Local\Temp\TempScript.py"
line 23, in <module>, "C:\Users\mcr16\AppData\Local\Temp\TempScript.py"
How I can solve it? f.Objects.Add(object) expects a File3dmObject but I have the ID. How I can get the File3dmObject of this object?.