Assigning Material Per Face causes InvalidInput Error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
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