It did not work Geometries

Sorry, it did not work. please help me. thank you

Seems to work here… I named 100 objects here Test001 to Test100. Don’t forget the last line AutoNameObjects() needs to be out-dented all the way to the left. In the above, it’s indented one tab, so the function never gets called.

Excuse me but I don’t understand this line…


https://docs.python.org/2/library/stdtypes.html#string-methods

What that means in the context of his script:

He set the variable ‘width’ as the number of characters in the string representation of the number that represents the quantity of his objects.

width = len(str(len(objects)+1))

for example if there are 100 objects:
len(objects)+1 = 101
str(len(objects)+1) makes a string of 3 characters long
len(str(len(objects)+1)) thus equals 3

therefore his “fill width” is 3

So, .rjust(width, '0') will fill the string with leading zeroes until the string is 3 characters long. So his numbering will look like 001…009; 010…099; and 100…999.

2 Likes

Hello,

Can you share a file which does not work with this script? Are you running it via the python editor in Rhino 6?

In this script you cannot choose who 's the first one so…
You select all objects. Validate… And after it autoname…
If you select one by one?

Hard to know what is not working on your end when it works here. Are you getting any error messages?

Have you tried:

  • restarting Rhino
  • different files, different objects
  • running the script from the script editor
  • saving the script above as a .py file somewhere and running it via RunPythonScript
  • if other scripts you might have run normally?

And, just to make sure we are understanding here, the script does nothing more than give the objects a name - which should be visible in the Properties box when the object is selected individually - but it does not create a number as text or dot or anything else that would be visible on-screen.

thank you. I did it

And it works now? What changed?

I still use the code above. not edit anything. I had a mistake earlier

The code above is named. And I want to number the objects

Sorry, it did not work

Hello,

If your object is a closed planar curve you can use rs.CurveAreaCentroid(curve_id) to get the centre point

Graham

1 Like

Sorry, it did not work

Can you share a small file containing the objects and desired result?

Maybe make a surface from the curves, find the surface area centroid and the delete the surface?

1 Like

Sorry, it did not work

This is not as clear and unambiguous as you think…

Sorry, it did not work

Are the curves always planar? Are they polycurves, oriented in space, always rectangular, … , …

One issue that I see in your code above it that towards the end, when you produce the text dots, you use the curve mid point, which is a point at normalised curve length 0.5, not the polygon centroid!

You could try to be more specific when getting the center points.

Example:

import rhinoscriptsyntax as rs

objects = rs.GetObjects("Select objects to label")

if len(objects) > 1:
    # Get the objects center points
    center_pts = []
    for obj in objects:
        # Get the area centroid for closed, planar curves    
        if rs.IsCurve(obj) and rs.IsCurveClosed(obj) and rs.IsCurvePlanar(obj):
            center_pt = rs.CurveAreaCentroid(obj)[0]
        # Get the bounding box center point for all other objects
        else:
            bbox = rs.BoundingBox(obj)
            if bbox:
                diagonal = bbox[6] - bbox[0]
                center_pt = bbox[0] + diagonal * 0.5
        center_pts.append(center_pt)
    
    # Sort the center points by their y- and x-values
    sorted_cpts = [p for p, o in sorted(zip(center_pts, objects), key=lambda k: [k[0].Y, k[0].X])]
    sorted_objs = [o for p, o in sorted(zip(center_pts, objects), key=lambda k: [k[0].Y, k[0].X])]
    
    # Parse the prefix
    prefix = rs.GetString("Prefix for labels, press Enter for none")
    if prefix is None: 
        prefix = ""
    
    # Label the objects
    for i in xrange(len(sorted_cpts)):
        # Parse the suffix as zero padded number
        suffix = str(i+1).zfill(len(str(len(sorted_cpts))))
        dot = rs.AddTextDot(prefix + suffix, sorted_cpts[i])
        rs.ObjectName(sorted_objs[i], prefix + suffix)

For closed, planar curves, the center point is the polygon centroid. For other curves and objects (i.e. surfaces, meshes, etc.) the center point is the center of the object’s bounding box.

Addendum:

You might want to project your center points to current working plane, if your curves or objects aren’t coplanar. Having the labels all in one plane seems simply more convenient.

2 Likes

Ahh - even if you find the correct area centroid in the if rs.IsCurve() block, the sortcompare function then replaces that point with a point on the curve .