Naming object fail

Hi, using visual studio and C# to create some automation I need to assign a name to an object as follow:

obj.Object().Attributes.Name = obj.Object().Name.Replace("PS", "").Replace("SB", "") + (obj.Brep().GetBoundingBox(true).Center.Y > 0 ? "PS" : "SB");

The problem is that after the code run the object has the same name as before, even using:

doc.Objects.Select(obj.Object().Id);
Rhino.RhinoApp.RunScript("_SetObjectName " + obj.Object().Name.Replace("PS", "").Replace("SB", "") + (obj.Brep().GetBoundingBox(true).Center.Y > 0 ? "PS" : "SB") + " ", true);
doc.Objects.UnselectAll();

What I’m missing? Is a silly behavior because in other codes both way worked
I called commitchanges after all

Ciao Edo,

public static bool RenameObjectBasedOnPosition(RhinoDoc doc, Guid objId)
    {
        RhinoObject obj = doc.Objects.Find(objId);
        if (obj == null)
        {
            RhinoApp.WriteLine("Object not found.");
            return false;
        }

        string originalName = obj.Name;
        string newName = originalName.Replace("PS", "").Replace("SB", "");
        BoundingBox bbox = obj.Geometry.GetBoundingBox(true);
        if (bbox.IsValid)
        {
            newName += bbox.Center.Y > 0 ? "PS" : "SB";
        }
        else
        {
            RhinoApp.WriteLine("Invalid bounding box.");
            return false;
        }

        if (newName == originalName)
        {
            RhinoApp.WriteLine("The object's name remains unchanged.");
            return false;
        }

        var attributes = obj.Attributes.Duplicate();
        attributes.Name = newName;

        if (doc.Objects.Replace(objId, obj.Geometry, attributes))
        {
            RhinoApp.WriteLine($"Object renamed to {newName}.");
            doc.Views.Redraw();
            return true;
        }

        RhinoApp.WriteLine("Failed to rename the object.");
        return false;
    }
}

Spero possa tornarti utile :blush:
Un saluto !!!

Ciao Farouk, purtroppo no, avevo provato anche passando dagli attributi e niente, il problema è che alle volte funziona e altre no, ma non riesco a capire quale sia il dettaglio che mi sfugge. Identiche librerie e metodi, in un comando funziona nell’altro no

Se carichi il modello 3d di esempio contenente 2 oggetti, uno della quale riesci a riassegnare l’idn e uno no, riusciamo a risolvere. Altrimenti puoi vedere con @Dale, però se vuoi fare un po’ prima carica il modello 3d che ti aiuto volentieri.

Non posso caricare il codice, però il problema è indipendente dall’oggetto

Hi @sartoriedo,

The code posted by @farouk.serragedine look good. If something isn’t working for you, you’ll need to provide us a way of repeating.

Thanks,

– Dale

Hi Dale, thank’s for your time, as I was saying to Farouk, his code doesn’t work and is a way I already tried… I wanted to know if this happen to someone else. The same method for renaming an object is working in a command and not in another one.
I can’t share the code due to company policy, so the only way for me to know in this case is ask to who has more knowledge if there are some known error or bad practice I’ve to avoid.

Hi @sartoriedo,

This test command (also) seems to work.

TestSetObjectName.cs (2.0 KB)

– Dale

I’ll try it tomorrow when I’ll back in the office, thank in advance