How to change all views in all sheet to diffrent model state

How to change all views in all sheet to diffrent model state

ward_hanotGZU45
Enthusiast Enthusiast
744 Views
7 Replies
Message 1 of 8

How to change all views in all sheet to diffrent model state

ward_hanotGZU45
Enthusiast
Enthusiast

Hello,

 

Situation:

I have a .iam and linked .idw witch I copy and edit. 

This .iam has 3 types of the model (model states: 325, 375, 425). I use a form tot change the model state from 325 (starting/saved file) to one of the other two. I do this with a multi value list user parameter.

Then I open the .idw link to this .iam. In the .idw there are 18 sheets, with 1 tot 4 views.

I then have to change every signle view on every sheet to the 375 or 425 model state to show the change in type.

Question:

Is there an "easy" ilogic snippet for this? I searched the forums for examples but my programming knowledge is limited. I found that this: 

DrawingView.SetActiveModelState

Shoud be used but I have no idea how. 

 

I there is such a snippet could ther be a littel explenation witch each line tot help me understand the diffrent lines?

 

Thanks in advance

 

0 Likes
Accepted solutions (2)
745 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @ward_hanotGZU45.  Here is likely the simplest code route for that task, in the form of an iLogic rule.  But pay attention to the setting in Line 11, because you may want to change those.  Here is a link to the online help documentation for that specific method (DrawingView.SetActiveModelState), which will explain those options for you.

 

Sub Main
	Dim oDDoc As DrawingDocument = TryCast(ThisDoc.Document, Inventor.DrawingDocument)
	If oDDoc Is Nothing Then Return
	'Dim sFrom As String = "325"
	Dim sTo As String = "425"
	For Each oSheet As Sheet In oDDoc.Sheets
		For Each oView As DrawingView In oSheet.DrawingViews
			If oView.ActiveModelState <> sTo Then
				Try
					'specify whether to update PartsList &/or overrides
					oView.SetActiveModelState(sTo, True, False)
				Catch e As System.Exception
					Logger.Error(e.Message)
				End Try
			End If
		Next oView
	Next oSheet
End Sub

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

ward_hanotGZU45
Enthusiast
Enthusiast

Can you provide info on what each line is suppost to do? 

 

I tried running it. I created 3 rules with 

Dim sTo As String = "425"

chaned to 325 and 375.  It changed each time but i get this error:

 

Error on line 8 in rule: Rule8, in document: LUX-Cabine-Start.idw

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

Now I need to figure out how to automate it to rule one off the 3 rules and how to arange the dimensions already in place

0 Likes
Message 4 of 8

WCrihfield
Mentor
Mentor
Accepted solution

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

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 8

ward_hanotGZU45
Enthusiast
Enthusiast

Thank you for your reply and solution

 

0 Likes
Message 6 of 8

ward_hanotGZU45
Enthusiast
Enthusiast

 

"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."

 

I think this is where there is a problem.  Besides the obvious error:

"Error on line 8 in rule: 425, in document: LUX-Cabine-Start.idw

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))"

I noticed that some views don't change to the desired modelstate. It usualy is on pages where ther ar more than 1 view but some sheets do chage with multiple views.

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Hi @ward_hanotGZU45.  The online help documentation for that property (DrawingView.ActiveModelState) says that it's value Type is String, but that it may return a 'null' String (no value at all, not just an empty String), if it is a view of a PresentationDocument (instead of a view of a PartDocument or AssemblyDocument), or in cases where the model is unresolved (can not find the model file it was referencing).  So, if one of those cases were true, and a 'null' String was returned, then it would not like trying to compare 'null' to the 'sTo' variable which represents a String value.  There are likely a few different ways to possibly work around that potential error prone scenario, all of which would require more code to be included.  So, I will post an updated code example for you to try out.  This one includes a whole lot of comments in it, for reference, and also includes the 'choose a ModelState name from list' idea, to eliminate the need for multiple rules.  It also handles that one error prone area of the code a little differently.

Sub Main
	'try to get a reference to a DrawingDocument, using the ThisDoc.Document term
	'this will try to Cast (convert) whatever Type it gets to the Type we need
	'if it fails to do that, it will not throw an error, it will simply not get a value
	Dim oDDoc As DrawingDocument = TryCast(ThisDoc.Document, Inventor.DrawingDocument)
	'check if a value was assigned to the variable above...if not, then exit this rule
	If oDDoc Is Nothing Then Return
	'create a list of ModelState names, for the user to choose from
	Dim oModelStateNames As New List(Of String)
	oModelStateNames.Add("325")
	oModelStateNames.Add("375")
	oModelStateNames.Add("425")
	'create a String type variable to hold the chosen ModelState name
	Dim sChosenModelStateName As String
	'prompt the user to select one of the names in that list above
	sChosenModelStateName = InputListBox("Choose ModelState Name.", oModelStateNames, "", "ModelState Names")
	'check if a value was chosen...if not, then exit the rule
	If sChosenModelStateName Is Nothing OrElse sChosenModelStateName = "" Then Return
	'start iterating through each Sheet in this drawing
	For Each oSheet As Sheet In oDDoc.Sheets
		'start iterating through each view on this Sheet
		For Each oView As DrawingView In oSheet.DrawingViews
			'first creating a variable to hold the value we are trying to get
			Dim sViewModelStateName As String
			'try to get name of ModelState that this view is currently set to
			'this may fail, so using a Try statement
			Try 'what code you want to 'try', but might fail
				sViewModelStateName = oView.ActiveModelState
			Catch 'what to do when what you 'tried' fails
				MsgBox("Error accessing DrawingView.ActiveModelState property for" _
				& vbCrLf & "view named " & oView.Name & " on Sheet named " & oSheet.Name, _
				vbInformation, "iLogic Rule Error - Avoided")
			End Try 'end of Try statement
			'now check the variable to see if it has a usable value
			'if not, then skip to next view, if any
			If sViewModelStateName Is Nothing OrElse sViewModelStateName = "" Then
				Continue For 'skips to next item in iteration (a DrawingView in this case)
			ElseIf sViewModelStateName <> sChosenModelStateName Then
				'if the variable had a value, but it was not the one we wanted to be
				'try to change the view to the ModelState we want it set to
				Try
					'first True means update PartsList based on that view
					'second False means do not keep overrides
					oView.SetActiveModelState(sChosenModelStateName, True, False)
				Catch e As System.Exception 'capturing error object
					'writing error message to the iLogic Log window
					Logger.Error(e.Message)
				End Try 'end of Try statement
			End If 'end of statement checking value of sViewModelStateName variable
		Next oView 'go to next DrawingView, if any
	Next oSheet 'go to next Sheet, if any
End Sub 'end of this 'Main' code routine's block of code

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

ward_hanotGZU45
Enthusiast
Enthusiast

Thank you verry much, it works like a charm and the explanition is very clear and will help me understand more of iLogic.

0 Likes