PDA

View Full Version : WISH ObjectType - for hatches2Layer script



flowww
05-12-2010, 01:38 PM
Hey,

you are doing a great job in implementing python i.e. scripting for the mac version. I was trying to write my 1st script...

selecting all hatches works fine

# use rhinoscriptsyntax to get all the functions in one shot
import rhinoscriptsyntax as rs

arrAllHatches = rs.ObjectsByType(65536, True)



extended it to send all hatches to a new layer...

# use rhinoscriptsyntax to get all the functions in one shot
import rhinoscriptsyntax as rs
from System.Drawing import Color

layerName = rs.GetString("layer name")

if rs.IsLayer(layerName):
print "The layer exists."
else:
print "created ", rs.AddLayer(layerName, Color.DarkSeaGreen)

arrAllHatches = rs.ObjectsByType(65536, False)

# move all hatches to Layer specified in layerName
for x in arrAllHatches:
rs.ObjectLayer(x, layerName)


Now I wanted to extend it to check all blocks for hatches and send them to that layer as well.
Though I'm a bit stuck as i am heavily missing ObjectType when iterating through all objects in a block to check if it is a hatch. Any better idea how to do this or any workaround?


arrBlocksAndInstances = rs.ObjectsByType(4096, False)
for y in arrBlocksAndInstances:
if rs.IsBlock(y) == True: # is this a block?
arrBlockObjects = rs.BlockObjects(y) # get objects inside the block
for z in arrBlockObjects:
if rs.ObjectType(z) == 4096: # is this a hatch?
rs.ObjectLayer(z, layerName)


the first hour playing with (the) python has been fun!

Steve Baer
05-12-2010, 02:45 PM
I altered your post. The ending tag should be [/HIGHLIGHT] and not [/HIGHLIGHT=Python]

Steve Baer
05-12-2010, 04:47 PM
I wrote up a sample on how to modify block reference geometry layers but ran into a bug in RhinoCommon which currently blocks the script from successfully completing. Fortunately the bug was easy to fix and will work in the next Windows/OSX builds.

NOTE: The following script will not work until the next available build of Rhino

import rhinoscriptsyntax as rs
from System.Drawing import Color
import scriptcontext
import Rhino

def RunMainFunction():
layerName = rs.GetString("layer name")

if rs.IsLayer(layerName):
print "The layer exists."
else:
print "created ", rs.AddLayer(layerName, Color.Bisque)

arrAllHatches = rs.ObjectsByType(65536, False)
if( arrAllHatches!=None ):
# move all hatches to Layer specified in layerName
for x in arrAllHatches:
rs.ObjectLayer(x, layerName)

# Currently there are no functions in rhinoscriptsyntax for directly manipulating the
# objects in the instance table. The following directly uses the RhinoCommon classes
# in order to get around this limitation in rhinoscriptsyntax
layer_index = scriptcontext.doc.Layers.Find(layerName, True)
instance_definitions = scriptcontext.doc.InstanceDefinitions.GetList(True )

if( layer_index==-1 or instance_definitions==None ):
return

for definition in instance_definitions:
rhino_objects = definition.GetObjects()
for obj in rhino_objects:
if( obj.ObjectType == Rhino.DocObjects.ObjectType.Hatch ):
if( obj.Attributes.LayerIndex != layer_index ):
attr = obj.Attributes
attr.LayerIndex = layer_index
scriptcontext.doc.Objects.ModifyAttributes(obj, attr, True)
scriptcontext.doc.Views.Redraw()

if( __name__ == '__main__' ):
RunMainFunction()