CustomBrepObject bad LayerIndex after make2D

I have some customBrepObjects (some beams) which work perfectly well, except when I execute make2D on this object. The layerIndex of the customBrepObject is modified, but it’s only visible through Rhinocommon, in Rhino UI the object is still on the good layer.

here is my code :

Public Class CustomBrepObjectTest
    Inherits Custom.CustomBrepObject

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(b As Brep)
        MyBase.New(b)
    End Sub

    Public Overrides Function ShortDescription(plural As Boolean) As String
        If plural Then
            Return "CustomBrepObjects"
        Else
            Return "CustomBrepObject"
        End If
    End Function

End Class

I call this function from a command :

Private Function TestCustomBrepObject(doc As RhinoDoc) As Result
        Dim filter As ObjectType = Rhino.DocObjects.ObjectType.Brep
        Dim objref As Rhino.DocObjects.ObjRef = Nothing
        Dim rc As Result = Rhino.Input.RhinoGet.GetOneObject("Select brep to test", False, filter, objref)
       
        If rc <> Rhino.Commands.Result.Success Then  Return rc       

        Dim test = New CustomBrepObjectTest(objref.Brep)
        doc.Objects.Replace(objref, test)

        Return Rhino.Commands.Result.Success
End Function

Hi @Yohann_Lesle,

Does the code above repeat the issue, or do I need to do something else?

– Dale

Hi Dale,

Yes I can repeat the issue by running the TestCustomBrepObject, and then manualy running Make2D on the CustomBrepObject with the option Properties=ByOutputLayer.

Here is function that repeat the issue, and does not involve manual operations:

  Protected Overrides Function RunCommand(ByVal doc As RhinoDoc, ByVal mode As RunMode) As Result

        Const filter As ObjectType = Rhino.DocObjects.ObjectType.Brep
        Dim objref As Rhino.DocObjects.ObjRef = Nothing
        Dim rc As Result = Rhino.Input.RhinoGet.GetOneObject("Select breps to test", False, filter, objref)
        If rc <> Rhino.Commands.Result.Success Then
            Return rc
        End If

        Dim test = New CustomBrepObjectTest(objref.Brep)
        doc.Objects.Replace(objref, test)

        Dim LastLayerIndex = objref.Object.Attributes.LayerIndex

        doc.Objects.Select(objref)
        RhinoApp.RunScript("_-Make2D P B Enter", True)

        Dim newLayerIndex = objref.Object.Attributes.LayerIndex

        If LastLayerIndex <> newLayerIndex Then
            RhinoApp.WriteLine("Test Failed")
            Return Rhino.Commands.Result.Failure
        Else
            RhinoApp.WriteLine("Test Passed")
            Return Rhino.Commands.Result.Success
        End If

End function

Hi @Yohann_Lesle,

When you replace an object, then any reference, or ObjRef, to it becomes obsolete, and you’ll need to reacquire the object to proceed.

For example:

Protected Overrides Function RunCommand(ByVal doc As RhinoDoc, ByVal mode As RunMode) As Result

  Const filter As ObjectType = ObjectType.Brep
  Dim objref As ObjRef = Nothing
  Dim rc As Result = RhinoGet.GetOneObject("Select breps to test", False, filter, objref)
  If rc <> Result.Success Then
    Return rc
  End If

  Dim rhObject As RhinoObject = objref.Object()
  If rhObject Is Nothing Then
    Return Result.Failure
  End If

  Dim objectId As Guid = rhObject.Id
  Dim layerIndex = rhObject.Attributes.LayerIndex

  ' TODO: replace object here

  ' Reacquire...
  rhObject = doc.Objects.FindId(objectId)
  If rhObject IsNot Nothing Then
    Dim newLayerIndex = rhObject.Attributes.LayerIndex
    If layerIndex <> newLayerIndex Then
      RhinoApp.WriteLine("Test Failed")
    Else
      RhinoApp.WriteLine("Test Passed")
    End If
  End If

  Return Result.Success

End Function

– Dale

Hi Dale,

Even if I reacquire the object to proceed the test failed :

Protected Overrides Function RunCommand(ByVal doc As RhinoDoc, ByVal mode As RunMode) As Result
        Const filter As ObjectType = Rhino.DocObjects.ObjectType.Brep
        Dim objref As Rhino.DocObjects.ObjRef = Nothing
        Dim rc As Result = Rhino.Input.RhinoGet.GetOneObject("Select breps to test", False, filter, objref)
        If rc <> Rhino.Commands.Result.Success Then Return rc
        Dim rhObject As RhinoObject = objref.Object()
        If rhObject Is Nothing Then Return Result.Failure

        Dim objectId As Guid = rhObject.Id
        Dim layerIndex = rhObject.Attributes.LayerIndex

        Dim test = New CustomBrepObjectTest(objref.Brep)
        doc.Objects.Replace(objref, test)
        doc.Objects.Select(objectId)
        RhinoApp.RunScript("_-Make2D P B _Enter", True)

        rhObject = doc.Objects.FindId(objectId)
        If rhObject Is Nothing Then Return Result.Failure
        Dim newLayerIndex = rhObject.Attributes.LayerIndex

        If layerIndex <> newLayerIndex Then
            RhinoApp.WriteLine("Test Failed")
        Else
            RhinoApp.WriteLine("Test Passed")
        End If

        Return Result.Success

End Function

Hi Dale,

have you been able to reproduce the bug?