Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Model States and Changing iProperties with iLogic

4 REPLIES 4
Reply
Message 1 of 5
Matthew_Policelli
378 Views, 4 Replies

Model States and Changing iProperties with iLogic

Background 

I have an iLogic that creates a custom form for the user to update iProperties on a design copied file without having to go into the iProperties to do so. Some properties are prompted from the user and others are calculated using the file name. This iLogic is run from the .dwg file and changes iProperties for both the model document and the .dwg.

 

Problem

If the design copied file has model states, only the master model state of the model document has the iProperties changed to match the user input. The others are left unchanged, regardless if the edit scope is individual or factory. I attempted to address this by having the code loop through model states, activating the model state and changing the properties, and at the end activating the original model state again. However, this throws an error (probably due to the code running from the .dwg instead of the .iam or .ipt)

 

Question

Is there a way to change model state iProperties from the .dwg? Does each model state have its own Document object? I tried looping through the model states and using ModelState.Document in place of ModelDocument in the code below, but it threw an error. Surely there has to be a way to manipulate model state iProperties; if not, then model states are entirely unusable (which is already somewhat the case in Inventor 2022 due to the whole 0 quantity BOM issue)

 

Here is a code snippet of how I'm changing the iProperties

With ModelDocument
   .PropertySets("Design Tracking Properties")("Description").Value = Results.Item("Description")
   .PropertySets("Design Tracking Properties")("Designer").Value = oName	' set the designer iProperty
   .PropertySets("Inventor Summary Information")("Author").Value = oName	' set the Author iProperty
   .PropertySets("Design Tracking Properties")("Creation Time").Value = DateTime.Now	' set creation time to the current time
   .PropertySets("Inventor Summary Information")("Revision Number").Value = "-"	' set to initial revision

End With

 

Labels (3)
4 REPLIES 4
Message 2 of 5

Hi @Matthew_Policelli.  Yes, it is possible, just a bit more complicated than it should be.  It sounds like you already have a fairly good understanding about coding and ModelStates, so I will try to go from there.  Yes, each ModelState represents a different Document within the same File on disk, but you can not simply edit each ModelState Document.  When a File has zero, or only the one original ModelState, then there is no 'factory' yet, just a single, regular document.  If the file has 2 or more ModelStates, then there will be a 'factory', and that factory will be determined by which ModelState is currently active.  The Document of the currently 'active' ModelState, will be the factory document, and it should be fully Read/Write, but while that ModelState is active, the Document objects you get from the other ModelStates are called 'member' documents, and are usually just ReadOnly.

 

There a are a few ways around these restrictions though.  One way is by changing the MemberEditScope setting, but it sounds like that is not working for you right now, for some reason.  Another way is by editing the ModelStateTable (or the ModelStates.ExcelWorkSheet), but editing the ModelStateTable only seems to allow editing existing values, and not 'adding' new items.  Going the Excel route is less restrictive, but more complicated.  We can also use the 'ModelStatesGlobalScope' tool to ensure that the 'input' document we will be working with is set to its 'global/factory/all members' scope when we edit it.  That object is directly recognized in an iLogic rule, because it comes from the Autodeak.iLogic.Runtime Namespace, which iLogic rules automatically include/reference.  Another tool at our disposal is the 'ModelStateUtils' Class, which is also directly recognized in iLogic rules, from the same source.  It includes multiple resources for working with ModelStates, including two Methods for writing values to a specific Cell in the ModelStateTable directly.  There are other new tools available also.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 5

I'm thinking the ModelStateTable looks like the most promising option, since the iProperties (including the custom ones) already exist, I am just changing their values.

 

How would my code need to be modified to modify those iProperties through the ModelStateTable?

Message 4 of 5

Hi @Matthew_Policelli.  Maybe try it like this first.  Less code changes needed this way.  If this does not work, they maybe we will look into an alternate route.  This will also get you familiar with some of those new tools I mentioned.  This route is pretty good if you intend on editing all ModelStates the same way, but not for when you need to edit them differently.  When using this route, you do not need to 'active' each ModelState or activate the correct MemberEditScope, hopefully eliminating the error.

Dim ModelDocument As Inventor.Document = ThisDoc.Document
'check if this document has at least 2 or more ModelStates
Dim oMSs As ModelStates = ModelStateUtils.GetModelStates(ModelDocument)
If oMSs IsNot Nothing AndAlso oMSs.Count >= 2 Then
	'if so, then we need to make sure we access it through the 'factory' document
	ModelDocument = ModelStateUtils.GetFactoryDocument(ModelDocument)
	'and make sure the 'model' is set to 'global/factory/all members' editing mode
	Using MSGS As New ModelStatesGlobalScope(ModelDocument)
		With ModelDocument
		   .PropertySets("Design Tracking Properties")("Description").Value = Results.Item("Description")
		   .PropertySets("Design Tracking Properties")("Designer").Value = oName	' set the designer iProperty
		   .PropertySets("Inventor Summary Information")("Author").Value = oName	' set the Author iProperty
		   .PropertySets("Design Tracking Properties")("Creation Time").Value = DateTime.Now	' set creation time to the current time
		   .PropertySets("Inventor Summary Information")("Revision Number").Value = "-"	' set to initial revision
		End With
	End Using
End If

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)

Message 5 of 5

Hi @Matthew_Policelli.  Here is another example that does use the ModelStateTable route, and does it without the help of those 'iLogic' helper utilities/tools (Except for the 'ThisApplication' term).  But I found a problem when going this route.  The Creation Time iProperty does not seem to ever get included in the ModelStateTable anywhere, so that one is not available to edit this way.  Basically when you set it in one ModelState, that also sets it for all ModelStates exactly the same, and therefore, since it is always the same for every ModelState, there is no difference to list in the table.  Generally the only stuff included in the ModelStateTable are things that have different values between different ModelStates.  If an iProperty has always had the same value for all ModelStates present, then it is not included in the ModelStateTable yet.

 

Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim ModelDocument As Inventor.Document = Nothing
If oDDoc.ReferencedDocuments.Count > 0 Then
	ModelDocument = oDDoc.ReferencedDocuments.Item(1)
End If
If ModelDocument Is Nothing Then Return
'if this is a ModelState 'member' document, then make sure we are working with the 'factory'
If ModelDocument.ComponentDefinition.IsModelStateMember Then
	ModelDocument = ModelDocument.ComponentDefinition.FactoryDocument
End If
Dim oMSs As ModelStates = ModelDocument.ComponentDefinition.ModelStates
If oMSs.Count >= 2 Then
	Dim oMSTable As ModelStateTable = oMSs.ModelStateTable
	Dim oRows As ModelStateTableRows = oMSTable.TableRows
	For Each oRow As ModelStateTableRow In oRows
		oRow.Item("Description [Project]").Value = "My Description 1" 'Results.Item("Description")
		oRow.Item("Designer [Project]").Value = "My Name 1" 'oName	' set the designer iProperty
		oRow.Item("Author [Summary]").Value = "My Name 1" 'oName	' set the Author iProperty
		'oRow.Item("Creation Time [Project]").Value = DateTime.Now	' set creation time to the current time
		oRow.Item("Revision Number [Project]").Value = "-"	' set to initial revision
	Next 'oRow
ElseIf oMSs.Count < 2 Then
	With ModelDocument
	   .PropertySets("Design Tracking Properties")("Description").Value = "My Description 1" 'Results.Item("Description")
	   .PropertySets("Design Tracking Properties")("Designer").Value = "My Name 1" 'oName	' set the designer iProperty
	   .PropertySets("Inventor Summary Information")("Author").Value = "My Name 1" 'oName	' set the Author iProperty
	   .PropertySets("Design Tracking Properties")("Creation Time").Value = DateTime.Now	' set creation time to the current time
	   .PropertySets("Inventor Summary Information")("Revision Number").Value = "-"	' set to initial revision
	End With
End If

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report