Extracting object data based on layer python

rhino
rhinopython
python

#1

Hey, I’m getting a little hung up. I am trying to extract the point data from various objects in a layer (all simple rectangles). so I am trying to choose the layer by name(there will be multiple with the same naming format…i.e Ballast_1), identify the objects in the layer, and extract all the point coordinates. it would be cool if it could just cycle through the layers based on the naming format. anything can help.

thanks, Chris


#2
import rhinoscriptsyntax as rs
import scriptcontext as sc
import math
 
##Start Evaluation
def main():
    response=rs.MessageBox( "Evaluate Classroom?",  4)
    if response !=7:
        #Select a layer    
        layer = rs.GetLayer("Please select a layer")

        #If a layer is chosen:
        if layer:
            #Gets objID
            obj = rs.ObjectsByLayer(layer)

            #Selects objects
            rs.SelectObjects(obj)
            ObjId=rs.SelectedObjects(include_lights=False, include_grips=False)
            for obj in ObjId:
                cent=rs.SurfaceAreaCentroid(obj)
                print cent[0]
                rs.AddPoint(cent[0])

    else:
        rs.MessageBox("Evaluation Aborted")

main()

so far i have this. the center point is more useful to me. my selection is still manual though which is holding me back a bit


#3

@Christopher_Gornall,

see if below is helpful, it adds the area centroids (points) to all surfaces on layers which name contains the word “Ballast”:

import Rhino
import scriptcontext 
import rhinoscriptsyntax as rs

def DoSomething():
    # define something as layer name to search for
    search_string = "Ballast"
    # iterate over all layers
    for layer in scriptcontext.doc.Layers:
        # check if search string appears in layer name
        if search_string.lower() in layer.Name.lower():
            # iterate all objects on that layer 
            for rh_obj in scriptcontext.doc.Objects.FindByLayer(layer):
                # only process surfaces
                if rs.IsSurface(rh_obj.Id):
                    # get the area centroid
                    rc = rs.SurfaceAreaCentroid(rh_obj.Id)
                    if rc:
                        # add a point on this layer
                        attr = Rhino.DocObjects.ObjectAttributes()
                        attr.LayerIndex = layer.LayerIndex
                        scriptcontext.doc.Objects.AddPoint(rc[0], attr)
    # refresh views
    scriptcontext.doc.Views.Redraw()
    
DoSomething()

c.


#4

thank you, this helped, id figured out the centroid aspect but the layer selection is super helpful. thanks