Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 4159

Iterating through groups of objects

$
0
0

I wrote a script that is working on a single selected group of curves. It’s for exporting nested sheets to dxf for CNC use. It copies the group, moves it to the origin, rotates, exports and then deletes the copy.

What I’m after is the ability to select multiple groups and have them each processed into individual files. I think I might be on the right track, however, all I’ve actually managed to achieve so far is that the groups all export into a single file with all the file names concatenated. Sooo… Maybe I’m not as close as I think!

It may be worth mentioning that the groups often contain other sub-groups but all of them have a rectangular outline on the “CNC” layer that I’m using to get the top level group name.

Here’s the original single use script for reference:

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs
import System.Guid, System.Drawing.Color

objectIDs = rs.GetObjects("Select objects for export as .DXF", 4 + 512, True, True, True)

objNames=[]

def RotateSelected(deg):
    geo = copy
    if copy:
        plane=rs.ViewCPlane()
        bb=rs.BoundingBox(geo,plane)
        if bb:
            rs.EnableRedraw(False)
            rs.RotateObjects(geo,(bb[0]),deg,copy=False)

for singleID in objectIDs:
    objName = rs.ObjectName(singleID)
    objNames.append(objName)

name = {x for x in objNames if x != None}
sheetnum = " ".join(name)
path = rs.DocumentPath()
exportcmd = '_-Export "' + path + "CNC\\" + sheetnum + '.dxf" _Enter'

if objectIDs:
    bbox = rs.BoundingBox(objectIDs)
    dir = rs.VectorCreate([0,0,0], bbox[0])
    copy = rs.CopyObjects(objectIDs, dir)
    rs.UnselectAllObjects()
    rs.SelectObjects(copy)
    RotateSelected(270)
    rs.Command(exportcmd)
    rs.DeleteObjects(copy)

Here’s the latest attempt at a multi-group version:

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs
import System.Guid, System.Drawing.Color

objs = rs.GetObjects("Select objects for export as .DXF", 4 + 512, True, True, True)

names = []
sheets = []
gnames = []
grps = []

#populate lists
for i in objs:
    if rs.ObjectLayer(i) == "CNC":
        sheets.append(i)
for i in sheets:
    gnames.append(rs.ObjectTopGroup(i))
for i in gnames:
    grps.append(rs.ObjectsByGroup(i))
for i in sheets:
    names.append(rs.ObjectName(i))

def exportdxf(objs):
#create save name    
    name = {x for x in names if x != None}
    sheetnum = " ".join(name)
    path = rs.DocumentPath()
    exportcmd = '_-Export "' + path + "CNC\\" + sheetnum + '.dxf" _Enter'
#operations
    if objs:
        bbox = rs.BoundingBox(objs)
        dir = rs.VectorCreate([0,0,0], bbox[0])
        copy = rs.CopyObjects(objs, dir)
        rs.UnselectAllObjects()
        rs.SelectObjects(copy)
    if copy:
        plane=rs.ViewCPlane()
        bb=rs.BoundingBox(copy,plane)
        if bb:
            rs.EnableRedraw(False)
            rs.RotateObjects(copy,(bb[0]),270,copy=False)
        rs.Command(exportcmd)
        rs.DeleteObjects(copy)
#try to get it to loop
for i in grps:
    exportdxf(i)

Any help is greatly appreciated!
Thanks!

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 4159