Move group & export to dxf in Python

Hi all,

I’m trying to make a script for moving and exporting a group of objects but I get stuck. I did a few tutorials on rhino python but am not completely common with it.

What I want to do for every group is move (copy) it with the lower left corner to the zero point, export it to a folder I can choose and delete afterwards so that I still have the original group in it’s original place but the dxf is exported at the zero point. The layers with it’s colors should be maintained in the dxf.

When I try moving I already get stuck because it moves every object separately and not as a group.

This is what I have so far:

import rhinoscriptsyntax as rs
import dxfwrite

objcts = rs.GetObjects(“Select objects”)

zeropoint = [0,0,0]

for x in objcts:
bb = rs.BoundingBox(x)
bb1 = bb[0]
mvvec = rs.VectorCreate(zeropoint, bb1)
rs.MoveObject(x, mvvec)

Any help would be highly appreciated, thanks!

160302 group to dxf export.3dm (25.6 KB)

Okay I currently got it to move each group of objects to zero position and be deleted. I’m sure this is a very cumbersome way to do it but it works. Now I need to do the exporting step.

import rhinoscriptsyntax as rs
import dxfwrite

objcts = rs.GetObjects(“Select objects”)

zeropoint = [0,0,0]

arealst = 0
biggestobject = []
dellist = []
grouplist = []
groupedlist = []
a = []
b = []

for x in objcts:
group = rs.ObjectGroups(x)
if group not in grouplist:
grouplist.append(group)
count = len(grouplist)

groupedlist = [[] for x in xrange(count)]
grouplist = []

for x in objcts:
group = rs.ObjectGroups(x)
if group in grouplist:
a = grouplist.index(group)
groupedlist[a].append(x)
else:
grouplist.append(group)
b = grouplist.index(group)
print b
groupedlist[b].append(x)

for lst in groupedlist:
for x in lst:
rea = rs.Area(x)
if rea >= arealst:
arealst = rea
biggestobject = x
bb = rs.BoundingBox(biggestobject)
bb1 = bb[0]
for x in lst:
y = rs.CopyObject(x)
mvvec = rs.VectorCreate(zeropoint, bb1)
rs.MoveObject(y, mvvec)
dellist.append(y)
rs.DeleteObjects(dellist)

Here is a simple example that might be helpful:

import rhinoscriptsyntax as rs

objects = rs.GetObjects("Select objects")
if objects:
    bbox = rs.BoundingBox(objects);
    dir = rs.VectorCreate([0,0,0], bbox[0])
    copies = rs.CopyObjects(objects, dir)
    rs.UnselectAllObjects()
    rs.SelectObjects(copies)
    rs.Command("_-Export c:\users\dale\desktop\test.dxf _Enter")
    rs.DeleteObjects(copies)

Nice, thanks! I’m close to the final script.
Only thing I need to solve now is how to export each dxf to a new name in the directory I choose.

I got it!
How do you copy your script so it keeps indenting? Then I can post the solution.

See this post - pinned to the top of the category:

Thanks Wim. Here is the script.
It doesn’t work for groups inside groups though.

#a python script that takes groups of sheets, 
# moves them to zero point and exports them to a folder of choice
#DOESN'T work with groups in groups, it will export every single group


#import the stuff we need
import rhinoscriptsyntax as rs

#asks for the objects
objcts = rs.GetObjects("Select objects")
"""if objcts = None:
return"""
direct = rs.BrowseForFolder(None, "Choose a folder", None)

#turn of redrawing for speed
rs.EnableRedraw(False)

#point to move to
zeropoint = [0,0,0]

#define some lists 
arealst = 0
biggestobject = []
grouplist = []
groupedlist = []
a = 0
b = 0

#counting the amount of groups that are selected
for x in objcts:
    group = rs.ObjectGroups(x)
    if group not in grouplist:
        grouplist.append(group)
count = len(grouplist)

#make list with right amount of (empty) lists
groupedlist = [[] for x in xrange(count)]
grouplist = []

#check for in which group each object is and put object in the right list
for x in objcts:
    group = rs.ObjectGroups(x)
    if group in grouplist:
        a = grouplist.index(group)
        groupedlist[a].append(x)
    else:
        grouplist.append(group)
        b = grouplist.index(group)
        groupedlist[b].append(x)

#starting number for exporting
count = 1

#Copy each list of objects (group)
for lst in groupedlist:
    copylist = rs.CopyObjects(lst)
#move to zero point
    bb = rs.BoundingBox(copylist)
    bb1 = bb[0]
    mvvec = rs.VectorCreate(zeropoint, bb1)
    rs.MoveObject(copylist, mvvec)
#define filepath & export
    countstr = str(count)
    path = direct+"/"+"Sheet"+countstr+".dxf"
    rs.SelectObjects(copylist)
    commandline = '_-export "{0}" _Enter'.format(path)
    rs.Command(commandline)
    count += 1
#delete objects
    rs.DeleteObjects(copylist)

#redraw
rs.Redraw()

Hi Dale, how to change the export directory? For example, I would like to name each 3dm file from a list of variables: itemname. I tried to conjugate the export directory like this, and it’s not working:

rs.Command("_-Export Z:\11_CARGO\253_test{itemname} _Enter")

I always get: Directory “” does not exist.

Thanks!!

Hi @Yang_Cao,

On Windows, your string should look like this:

rs.Command("_-Export Z:\\11_CARGO\\253_test{itemname} _Enter")

– Dale

1 Like