<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Assigning Material Per Face causes InvalidInput Error in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/assigning-material-per-face-causes-invalidinput-error/m-p/14044195#M86622</link>
    <description>&lt;P&gt;I am trying to assign different materials to different faces of a Solid3d but when I try and assign the material to the face, the Brep.Faces I am using to traverse the faces is getting corrupted for some reason. Here is the snippet of code that I am using to set Material Textures to a specific face of a solid. In my actual code, the materialName is a valid Material that exists within the MdiActiveDocument and materials is populated with the existing materialName's and the ObjectId's are valid ObjectId's of materials within the MdiActiveDocument. The plugin I am working on works on a different CAD platform with the same API as AutoCAD and works as expected. So I am not sure if there is a bug in ACAD where this code should work or if there is a bug in the other CAD platform that allows this to work when in reality it shouldn't work.&lt;/P&gt;&lt;LI-CODE lang="visual-basic"&gt;Public Sub AssignDifferentMaterialsToDifferentFaces()
    Dim materials As New Dictionary(Of String, ObjectId)
    Dim materialName As String = "materialName"
    Dim oDocLock As _AcAp.DocumentLock = _AcAp.Application.DocumentManager.MdiActiveDocument.LockDocument()
    Dim oTransaction As Transaction = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()
    Dim solid As Solid3d = CType(oTransaction.GetObject(_ObjectId, OpenMode.ForWrite), Solid3d)
    Dim ids() As ObjectId = {solid.ObjectId}
    Dim path As FullSubentityPath = New FullSubentityPath(ids, New SubentityId(SubentityType.Null, IntPtr.Zero))
    Using brep As New Brep(path)
        For Each curFace As Face In brep.Faces
            SetFaceMaterial(solid, curFace, materialName, materials)
        Next
    End Using
End Sub

Private Sub SetFaceMaterial(Solid As Solid3d, CurFace As Face, MaterialName As String, Materials As Dictionary(Of String, ObjectId))
    Dim materialObjectId As ObjectId
    If Materials.TryGetValue(MaterialName, materialObjectId) Then
        Solid.SetSubentityMaterial(CurFace.SubentityPath.SubentId, materialObjectId)
    End If
End Sub&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 05 Mar 2026 16:46:02 GMT</pubDate>
    <dc:creator>jason_aPCJML</dc:creator>
    <dc:date>2026-03-05T16:46:02Z</dc:date>
    <item>
      <title>Assigning Material Per Face causes InvalidInput Error</title>
      <link>https://forums.autodesk.com/t5/net-forum/assigning-material-per-face-causes-invalidinput-error/m-p/14044195#M86622</link>
      <description>&lt;P&gt;I am trying to assign different materials to different faces of a Solid3d but when I try and assign the material to the face, the Brep.Faces I am using to traverse the faces is getting corrupted for some reason. Here is the snippet of code that I am using to set Material Textures to a specific face of a solid. In my actual code, the materialName is a valid Material that exists within the MdiActiveDocument and materials is populated with the existing materialName's and the ObjectId's are valid ObjectId's of materials within the MdiActiveDocument. The plugin I am working on works on a different CAD platform with the same API as AutoCAD and works as expected. So I am not sure if there is a bug in ACAD where this code should work or if there is a bug in the other CAD platform that allows this to work when in reality it shouldn't work.&lt;/P&gt;&lt;LI-CODE lang="visual-basic"&gt;Public Sub AssignDifferentMaterialsToDifferentFaces()
    Dim materials As New Dictionary(Of String, ObjectId)
    Dim materialName As String = "materialName"
    Dim oDocLock As _AcAp.DocumentLock = _AcAp.Application.DocumentManager.MdiActiveDocument.LockDocument()
    Dim oTransaction As Transaction = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()
    Dim solid As Solid3d = CType(oTransaction.GetObject(_ObjectId, OpenMode.ForWrite), Solid3d)
    Dim ids() As ObjectId = {solid.ObjectId}
    Dim path As FullSubentityPath = New FullSubentityPath(ids, New SubentityId(SubentityType.Null, IntPtr.Zero))
    Using brep As New Brep(path)
        For Each curFace As Face In brep.Faces
            SetFaceMaterial(solid, curFace, materialName, materials)
        Next
    End Using
End Sub

Private Sub SetFaceMaterial(Solid As Solid3d, CurFace As Face, MaterialName As String, Materials As Dictionary(Of String, ObjectId))
    Dim materialObjectId As ObjectId
    If Materials.TryGetValue(MaterialName, materialObjectId) Then
        Solid.SetSubentityMaterial(CurFace.SubentityPath.SubentId, materialObjectId)
    End If
End Sub&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2026 16:46:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/assigning-material-per-face-causes-invalidinput-error/m-p/14044195#M86622</guid>
      <dc:creator>jason_aPCJML</dc:creator>
      <dc:date>2026-03-05T16:46:02Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning Material Per Face causes InvalidInput Error</title>
      <link>https://forums.autodesk.com/t5/net-forum/assigning-material-per-face-causes-invalidinput-error/m-p/14052013#M86660</link>
      <description>&lt;P&gt;Are you trying the code on solids that have had or have not had material already applied to the individual faces?&amp;nbsp; I have a very sloppily wrote program (that works great) that will swap the material on a solid's faces but it has to check if a face material has been applied first.&amp;nbsp; I can share the portion of it that may be of some use, but unfortunately I can't share the entire program as there is proprietary code...&amp;nbsp; Also note that this code is for ACAD 2024 and earlier running .netFramework 4.8 and it has not been converted to .NET for newer versions (yet).&amp;nbsp; I hope this helps you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dim DeletedFaceCnt_Solid As Integer = 0&lt;BR /&gt;Dim ObjId_Solid As ObjectId()&lt;BR /&gt;ReDim ObjId_Solid(0)&lt;BR /&gt;ObjId_Solid(0) = Cur.Ent.ObjectId&lt;BR /&gt;Dim SubEntPath_Solid As New FullSubentityPath(ObjId_Solid, New SubentityId(SubentityType.Null, IntPtr.Zero))&lt;BR /&gt;'SubEntPath_Solid = New FullSubentityPath(ObjId_Solid, New SubentityId(SubentityType.Null, IntPtr.Zero))&lt;/P&gt;&lt;P&gt;If Not SubEntPath_Solid.IsNull Then&lt;/P&gt;&lt;P&gt;Dim objBrep_Solid As Brep = New Brep(SubEntPath_Solid)&lt;BR /&gt;Using objBrep_Solid&lt;/P&gt;&lt;P&gt;Dim FaceCnt As Integer = -1&lt;/P&gt;&lt;P&gt;For Each Face As BoundaryRepresentation.Face In objBrep_Solid.Faces&lt;/P&gt;&lt;P&gt;'' go through the individual faces and save the face information&lt;BR /&gt;Try&lt;/P&gt;&lt;P&gt;FaceCnt += 1&lt;BR /&gt;'ReDim Preserve CurSubent_Solid(FaceCnt)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;ReDim Preserve Cur.Face.SubentPath(FaceCnt)&lt;BR /&gt;ReDim Preserve Cur.Face.SubentID(FaceCnt)&lt;BR /&gt;ReDim Preserve Cur.Face.SubentMatID(FaceCnt)&lt;/P&gt;&lt;P&gt;Cur.Face.SubentPath(FaceCnt) = Face.SubentityPath&lt;BR /&gt;Cur.Face.SubentID(FaceCnt) = Face.SubentityPath.SubentId&lt;BR /&gt;Cur.Face.SubentMatID(FaceCnt) = Cur.Ent.GetSubentityMaterial(Cur.Face.SubentID(FaceCnt))&lt;/P&gt;&lt;P&gt;'acEnt(x).Ent.Highlight(acEnt(x).Face(FaceCnt).SubentPath, True)&lt;/P&gt;&lt;P&gt;Catch ex As System.Exception&lt;BR /&gt;''==========================================================================================================&lt;BR /&gt;'' It is normal for this try-catch to hit. It'll hit whenever a face doesn't have a seperate material applied.&lt;BR /&gt;'' Uncomment the lines below this comment for debugging if desired.&lt;BR /&gt;'' However, it can be completly ignored as there is no real "error" that'll be a problem.&lt;BR /&gt;'' DO NOT REMOVE THIS TRY-CATCH, IT NEEDS TO BE HERE! If you remove it, you'll have problems.&lt;BR /&gt;''==========================================================================================================&lt;BR /&gt;Dim exCur As String = ex.Message&lt;BR /&gt;DeletedFaceCnt_Solid += 1&lt;BR /&gt;exCur = exCur&lt;BR /&gt;FaceCnt -= 1&lt;BR /&gt;'Debug.WriteLine(exCur)&lt;BR /&gt;End Try&lt;BR /&gt;Next Face&lt;/P&gt;&lt;P&gt;'Call CoreChange(acEnt, acTrans, FaceCnt, row, CurSubent)&lt;/P&gt;&lt;P&gt;Try&lt;BR /&gt;'Change the core&lt;BR /&gt;' acEnt(x).Ent.UpgradeOpen()&lt;/P&gt;&lt;P&gt;For cnt = 0 To Mats.ToName.Count - 1&lt;/P&gt;&lt;P&gt;If Cur.Ent.Material = Mats.FromName(cnt) Then&lt;BR /&gt;Cur.Ent.Material = Mats.ToName(cnt)&lt;BR /&gt;'Utils.RegenEntity(Cur.Ent.ObjectId)&lt;BR /&gt;'AcDoc.TransactionManager.FlushGraphics()&lt;BR /&gt;'Cur.Ent.Unhighlight()&lt;/P&gt;&lt;P&gt;End If&lt;/P&gt;&lt;P&gt;Next&lt;/P&gt;&lt;P&gt;Catch ex As Autodesk.AutoCAD.Runtime.Exception&lt;BR /&gt;ex = ex&lt;BR /&gt;'MsgBox("There has been an error while changing the Core materials." &amp;amp; vbCrLf &amp;amp; ex.Message &amp;amp; vbCrLf &amp;amp; ex.StackTrace)&lt;BR /&gt;Catch es As System.Exception&lt;BR /&gt;es = es&lt;BR /&gt;'MsgBox("There has been an error while changing the Core materials." &amp;amp; vbCrLf &amp;amp; es.Message &amp;amp; vbCrLf &amp;amp; es.StackTrace)&lt;BR /&gt;End Try&lt;/P&gt;&lt;P&gt;'Call FaceCount(FaceCnt, acEnt, acTrans, row, CurSubent)&lt;/P&gt;&lt;P&gt;'Dim ChangeToMatID As ObjectId = Nothing&lt;/P&gt;&lt;P&gt;For cnt = 0 To FaceCnt&lt;BR /&gt;Try&lt;/P&gt;&lt;P&gt;For i = 0 To Mats.ToID.Count - 1&lt;/P&gt;&lt;P&gt;'Cur.Ent.Highlight(Cur.Face.SubentPath(cnt), True)&lt;BR /&gt;If Cur.Face.SubentMatID(cnt) = Mats.FromID(i) Then&lt;BR /&gt;Cur.Ent.SetSubentityMaterial(Cur.Face.SubentID(cnt), Mats.ToID(i))&lt;BR /&gt;'Utils.RegenEntity(Cur.Ent.ObjectId)&lt;BR /&gt;'AcDoc.TransactionManager.FlushGraphics()&lt;BR /&gt;End If&lt;/P&gt;&lt;P&gt;Next&lt;/P&gt;&lt;P&gt;'acEnt(cur).Ent.SetSubentityMaterial(acEnt(cur).Face(FaceCnt).SubentID, )&lt;BR /&gt;'acEnt.SetSubentityMaterial(CurSubent_Solid(cur).CurSubentID, CurSubent_Solid(cur).CurSubentMatID)&lt;BR /&gt;Catch ex As Autodesk.AutoCAD.Runtime.Exception&lt;BR /&gt;ex = ex&lt;BR /&gt;'MsgBox("There has been an error while changing the Core's visual materials back to what they were." &amp;amp; vbCrLf &amp;amp; ex.Message &amp;amp; vbCrLf &amp;amp; ex.StackTrace)&lt;BR /&gt;Catch es As System.Exception&lt;BR /&gt;es = es&lt;BR /&gt;'MsgBox("There has been an error while changing the Core's visual materials back to what they were." &amp;amp; vbCrLf &amp;amp; es.Message &amp;amp; vbCrLf &amp;amp; es.StackTrace)&lt;BR /&gt;End Try&lt;BR /&gt;Next cnt&lt;/P&gt;&lt;P&gt;'Utils.RegenEntity(Cur.Ent.ObjectId)&lt;BR /&gt;'AcDoc.TransactionManager.FlushGraphics()&lt;/P&gt;&lt;P&gt;End Using&lt;/P&gt;&lt;P&gt;End If&lt;/P&gt;</description>
      <pubDate>Thu, 12 Mar 2026 10:04:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/assigning-material-per-face-causes-invalidinput-error/m-p/14052013#M86660</guid>
      <dc:creator>david_sisson0278</dc:creator>
      <dc:date>2026-03-12T10:04:05Z</dc:date>
    </item>
    <item>
      <title>Re: Assigning Material Per Face causes InvalidInput Error</title>
      <link>https://forums.autodesk.com/t5/net-forum/assigning-material-per-face-causes-invalidinput-error/m-p/14053351#M86663</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like you do not dispose the locked document and the transaction and do not commit the transaction.&lt;/P&gt;
&lt;P&gt;To be able to edait each face, it is needed to enumerate them all (i.e., create an array) before editing the first one.&lt;/P&gt;
&lt;P&gt;Here's a C# example:&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;public static void SetMaterialToSolidFaces(ObjectId solidId, string materialName)
{
    // Check if the OjectId is a Solid3d
    if (solidId.ObjectClass != RXObject.GetClass(typeof(Solid3d)))
        return;

    var db = solidId.Database;
    using (var tr = db.TransactionManager.StartTransaction())
    {
        // Check if the materials dictionanary contains the material
        var materials = (DBDictionary)tr.GetObject(db.MaterialDictionaryId, OpenMode.ForRead);
        if (!materials.Contains(materialName))
            return;

        var materialId = materials.GetAt(materialName);
        var solid = (Solid3d)tr.GetObject(solidId, OpenMode.ForWrite);
        var fullSubentityPath = new FullSubentityPath(
            [solidId],
            new SubentityId(SubentityType.Null, IntPtr.Zero));
        using (var brep = new Brep(fullSubentityPath))
        {
            // Enumerate the faces SubentityIds before editing them
            var faces = brep.Faces.ToArray();

            // Edit the faces
            foreach (var face in faces)
            {
                solid.SetSubentityMaterial(face.SubentityPath.SubentId, materialId);
            }
        }
        tr.Commit();
    }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2026 13:12:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/assigning-material-per-face-causes-invalidinput-error/m-p/14053351#M86663</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2026-03-13T13:12:30Z</dc:date>
    </item>
  </channel>
</rss>

