Edit: I just found this: Autodesk Support Article, but I wonder if there is a resolution for this yet or anything code-wise I could do to prevent the error
I am trying to create an iLogic to delete old model states that were migrated over from Levels of Detail that were present on a template my company used years ago. My plan is to put this in the Before Save event trigger, however while testing it I get this error:
![Matthew_Policelli_0-1687195151968.png Matthew_Policelli_0-1687195151968.png](https://forums.autodesk.com/t5/image/serverpage/image-id/1229780i2C94BF02B2DC0DA5/image-size/medium?v=v2&px=400)
I should note that my testing involves creating a new document, adding the Model State with the name from the old template, and trying to save to see if it conflicts with other Before Save iLogics we use. So I am not 100% sure this would happen outside of a testing scenario. Regardless, it does cause a crash so I'd like to figure out how to deal with it.
A couple other notes:
- The goal of this iLogic rule is to help clean up unnecessary/unused model states to reduce file size during the course of regular document editing, without requiring users to check out anything they are not already working on.
- I originally wanted to put the code in the Open Document trigger. However, doing so would add the steps of checking that the document is the active document (and not subassemblies that the user is not editing) can be checked out/edited by the user, which for some large assemblies seems to cause a large slowdown when opening. Therefore I decided that Before Save would be better as it would only effect the assemblies the user is actively editing.
Here is my code:
Dim ListOfOldModelStates As New List(Of String) From {"Low", "Medium", "Client Request", "Cabinet shop detail" } 'list of old template levels of detail that would have become model states after migration
Dim FoundList As List(Of ModelState) = ThisAssembly.Document.ComponentDefinition.ModelStates.Cast(Of ModelState).Where(Function(m) ListOfOldModelStates.Any(Function(x) x = m.Name)).ToList
If FoundList.Count >= 1 Then
For Each oMS As ModelState In FoundList
Try
oMS.Delete
Catch
MessageBox.Show("Could Not Delete " & oMS.Name)
End Try
Next
End If