Hi @ward_hanotGZU45.
The first line is attempting to get a reference to a DrawingDocument, but since there is no guarantee that one of those will be open or available whenever you run the rule, it does so in a special way, to avoid problems. It essentially tries to 'Cast' (convert object Type) from whatever type of document that the 'ThisDoc.Document' phrase returns, to that needed DrawingDocument type, and if that fails, it will not throw an error, but will instead not set a value to that variable that was defined on the first line. So, line 2 checks to make sure that a value was set to that variable, and if not, it will exit the rule, because we do not have any document to work on.
Lines 4 & 5 could have been done in multiple different ways, but since I was not 100% clear about how you want this rule to work, I was going to show two different possible ways of doing this. For instance, I do not know if you only want to change a view from one specific ModelState to another specific ModelState, or if you simply do not care which ModelState the views are currently set to, but only want to set them all to one specific ModelState. We could have also gotten the name of the ModelState to change all the views to from the value of a Parameter in your drawing. Or, we could have created a list with all 3 ModelState names in it, then shown the user that list, to have them select one of those names, then use that selected name as the one to set all the view to. There are lots of possibilities there, but only showing one simple option, which in this case would require changing that name within the code itself. Just a simple example, primarily to show one possible way 'HOW' to do it, not necessarily that it must be done this exact way.
Line 6 starts iterating through all the sheets in the whole drawing, even if there may only be sheet, then Line 17 is the matching end of that iteration (or loop) of the sheets, which instructs it to go to the next sheet, if there is another one. Line 7 starts iterating through all the views on the current sheet that it is iterating through, with Line 16 being the matching end of that iteration (or loop) of all the views on that sheet. In each of those iteration starters, we are declaring a variable that will be representing the object we will be focused on within that iteration. On Line 8, we are checking which ModelState the view is currently set to. This property it is checking is ReadOnly, meaning we can read its value, but we can not change its value directly. If it is not already set to the ModelState we want it to be set to, then it will proceed to deeper code past that line, otherwise it will proceed to the next view.
Line 9 is starting a Try...Catch...End Try statement (similar to a If...ElseIf...Else...End If statement, but for a different purpose). This type of statement is used when you need to use a line of code that you believe will sometimes fail (or cause an error), and lets you avoid that error, which would normally stop your whole rule, and lets you continue past that error. You put the code you want to 'try' on the Try side of this statement, then put something (or nothing) on the Catch side that you want to happen if the code on the 'try' side fails (would have thrown an error).
Line 10 is a comment within the code. A comment has an apostrophe in front of it, which causes the code execution process to skip over it, so it is only useful for the person reading the code, for notes and reminders, and such.
Line 11 is using the method (a method is a Sub routine or a Function routine) which is needed for setting which ModelState we want this view to be set to. This method is the only way to change the value of that other property mentioned earlier.
Line 12 is capturing the error (Exception) that the 'try' side caused (only if it caused an error), so that the error can be reported about within the Catch side of this statement.
Line 13 is writing the error message to the iLogic Log window, if you have that tab visible. If that tab was not visible (usually beside your iLogic tab) then it will not be able to capture this message being written to it. To make sure it is available, go to the View tab (out in main Inventor), then go to User Interface tool, and within its drop-down, put a check in the checkbox next to "iLogic Log". That should make the tab visible. Then if you run your rule after that point, and it caught any errors, it will write those messages to that window for you to review after the rule has finished.
Whew. 😅
Wesley Crihfield

(Not an Autodesk Employee)