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: 

Modifying stock number of newly created model state

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
meGVGMF
660 Views, 12 Replies

Modifying stock number of newly created model state

Hello!

 

I'm trying to create model states and modify their iProperties programmatically.

 

I'm getting errors when I try to access the model states' 'Design Tracking Properties' PropertySet.

 

How can I initialize it or otherwise get to the point where I can modify the values here?

 

Thanks

12 REPLIES 12
Message 2 of 13
Dev_rim
in reply to: meGVGMF

Hi there,

I am sharing piece of code from a helpful article.

 

Sub ModelStateProps()

Dim oPartDoc  As PartDocument
Set oPartDoc = ThisDocument ''You can use every document you want to use

Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition

Dim oModelStates As ModelStates
Set oModelStates = oCompDef.ModelStates

‘Fetch the required Model State from the collection
Dim oModelState As ModelState
Set oModelState = oCompDef.ModelStates("YOUR_MODEL_STATE_NAME")

'Activate the model state
oModelState.Activate

' Get the design tracking property set for our model state.
Dim propSet As Inventor.PropertySet
Set propSet = oPartDoc.PropertySets.Item("Design Tracking Properties")

' Get the Stock Number property.
Dim prop As Property
Set prop = propSet.Item("Stock Number")

' Set the Member Edit Scope
oModelStates.MemberEditScope = kEditActiveMember  'kEditAllMembers for all members

'Set the value
prop.Value = "VALUE"

End Sub

 

 

 

So you can use this one. If you want to read more about topic, author also wrote really nice article:

https://adndevblog.typepad.com/manufacturing/2022/01/working-with-propertysets-and-model-states.html

 

Thanks

Devrim

If my answer is solved your problem, please mark it as Solution

Freundliche Grüße / Kind Regards
Message 3 of 13

Can you please share the code?

Regards,

Arthur Knoors

Autodesk Affiliations:

Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!


! For administrative reasons, please mark a "Solution as solved" when the issue is solved !

Message 4 of 13

use it this way:

Sub Main()

Dim oPartDoc As PartDocument 
oPartDoc = ThisDoc.Document ''You can use every document you want to use

Dim oCompDef As PartComponentDefinition
oCompDef = oPartDoc.ComponentDefinition

Dim oModelStates As ModelStates
oModelStates = oCompDef.ModelStates

'Fetch the required Model State From the collection
Dim oModelState As ModelState
oModelState = oCompDef.ModelStates("Model State1")

'Activate the model state
oModelState.Activate

' Get the design tracking property set for our model state.
Dim propSet As Inventor.PropertySet
propSet = oPartDoc.PropertySets.Item("Design Tracking Properties")

' Get the Stock Number property.
Dim prop As Inventor.Property
prop = propSet.Item("Stock Number")

' Set the Member Edit Scope
oModelStates.MemberEditScope = kEditActiveMember  'kEditAllMembers for all members

'Set the value
prop.Value = "001"

End Sub

Regards,

Arthur Knoors

Autodesk Affiliations:

Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!


! For administrative reasons, please mark a "Solution as solved" when the issue is solved !

Message 5 of 13

Yeah I was just working on that! Take a look:

 

 

 

Sub Main()
	Const sDTPropSetName As String = "Design Tracking Properties"
	Const sDTPropSetInternalName As String = "{32853F0F-344-11D1-9E93-0060B03C1CA6}"

	Dim oDoc As PartDocument = ThisDoc.Document
	Dim oModelStates As ModelStates = oDoc.ComponentDefinition.ModelStates
	oModelStates(1).Activate
	oModelStates.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
	For i As Integer = oModelStates.Count To 2 Step -1
		oModelStates(i).Delete
	Next

	oModelStates.Add
	Dim oMS As ModelState = oModelStates(oModelStates.Count)

	Dim oDTPropSet As PropertySet
	Dim oMSDoc As PartDocument = oMS.Document
	Dim bNeedsInit As Boolean = False
	If oMSDoc.PropertySets.PropertySetExists(sDTPropSetInternalName, oDTPropSet) Then
		oDTPropSet = oMSDoc.PropertySets.Add(sDTPropSetName, sDTPropSetInternalName)
		bNeedsInit = True
	End If

	Const sPropName As String = "Stock Number"
	Const iPropId As Integer = 55
	Const sPropVal As String = "Test"
	Try
		oDTPropSet(sPropName).Value = sPropVal
	Catch
		oDTPropSet.Add(sPropVal, sPropName, iPropId)
	End Try

	If Not bNeedsInit Then
		Exit Sub
	End If
	'per https://knowledge.autodesk.com/support/inventor/learn-explore/caas/simplecontent/content/add-ilogic-rule-to-event-trigger-using-external-ilogic-rule.html
    Dim oTempProp As Inventor.Property = oDTPropSet.Add("", "Delete")
    oTempProp.Delete
    oMSDoc.Update
End Sub

 

 

 

It fails when checking whether the PropertySet exists because, I believe, the document within the model state is initialized as `Nothing`

Message 6 of 13

The problem is that rebuilding the model when activating a model state is too costly an operation.

A possibility is to do this whenever I find the model state document uninitialized, but I'm still curious to know if it's possible to do this purely without activation.

Message 7 of 13
meGVGMF
in reply to: Dev_rim

Thanks @Dev_rim, but I want to do this without activating the model state. You can take a look at some sample code I've posted in a different reply:
https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/modifying-stock-number-of-newly-crea...
Message 8 of 13
WCrihfield
in reply to: meGVGMF

Hi @meGVGMF.  In order to avoid the complicated document references, model state activation, & setting edit member scope, you could just edit the ModelStateTable instead.  It is certainly a different process, but can be really nice in some scenarios, plus you can easily make changes to multiple model states at one time, because they are all in that table together.  You just need to know the layout of that table and how to navigate it.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 13
meGVGMF
in reply to: WCrihfield

Yeah that's been something I've been meaning to look into.

You don't happen to have a reference on basic API functions handy, do you?

Message 10 of 13
WCrihfield
in reply to: meGVGMF

Unfortunately, many of the URL's in the Inventor online help area for the ModelState related API Objects (and many other newer additions to the online help) are still screwed up, but I think they are 'slowly' working on fixing them.

 

Once you get to the ModelStates object (Document.ComponentDefinition.ModelStates), that object has a property called ModelStateTable, which returns a ModelStateTable  object.  This object has two properties (TableColumns & TableRows).  You can use those as a starting point for looping or navigating through the tables columns & rows, as you would expect.  If specifying a specific row or column using Item(), you can either specify its Index (Integer), or the String type value that represents the column's heading, or row's member name (the name of the ModelState).  Most of the object properties in the table are ReadOnly, but the ModelStateTableCell.Value is Read/Write.  And the ModelStateTableColumn object has a useful Property named ReferenceDataType, which returns a variatant of the iComponentColumnTypeEnum, which helps inform you of what type of data that column is for.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 11 of 13
meGVGMF
in reply to: WCrihfield

Thanks I'm looking into this now!
Message 12 of 13
WCrihfield
in reply to: meGVGMF

Also, I just noticed that within the code you posted in message 5, I see that you were attempting to 'Add' an iProperty into the "Design Tracking Properties" set.  That is not possible.  The first 3 PropertySet objects in every Inventor document are fixed size, so you can not add or remove any of the properties in those sets, or rename any of them.  Many of them even have ReadOnly values, but some have Read/Write values.  The fourth PropertySet is pretty much always the 'custom' one, named "Inventor User Defined Properties", and that set does not have any properties, by default, until you add them.  You can add or remove properties in that 'custom' set all you want.  The link referenced in a commented line within your code is talking about a special PropertySet that is 'hidden' by default, by Autodesk, and it does not even exist in a document until you have used the Event Triggers dialog to add rules to events within that document (or created it by code, which is still a bit buggy).  That PropertySet is very unique, and is used by Autodesk internally.  It was only found by Inventor users by trial/error/accident back around 2012-2013.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 13 of 13
meGVGMF
in reply to: WCrihfield

Fair enough. I had other use cases involving the custom PropertySet. But that code never made it that far, anyway.

I was wondering, though, is it possible to add a column to the ModelStateTable object, somehow?

Actually I've just figured something out. I'm changing the value of something that's about to change, via `iProperties.Value`, to have the table generate the column for me.

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report