iProperties in referenced document (Modelstate) + Vault

iProperties in referenced document (Modelstate) + Vault

richterBKSAC
Advocate Advocate
3,092 Views
27 Replies
Message 1 of 28

iProperties in referenced document (Modelstate) + Vault

richterBKSAC
Advocate
Advocate

Hi everyone,

 

I've create a userform which does show the iProperties of the active drawing and it's referenced document (IPT/IAM). Also it lets me "transfer" the iProperty values of the drawing, which you can edit via the form, to the referenced document (so the title block of the drawing updates).

I've also added some code so you can choose the referenced document to write to, when there is more than one referenced document.

Now to my actual problem:

I've came across an exception where the drawing has an assembly with 2 custom modelstates and the default modelstate. Only the 2 custom modelstates are displayed on the drawing. By looking at the "api tree" I've found out that Inventor also creates 2 referenced Document Items, which makes sense. The problem is that if I write the iproperty values of the drawing to one of these Items and check them into Vault the "title" iproperty, for example, remains empty. Also the title block "title" remains empty. I've also found out that you have to write the iproperties into the default modelstate of the referenced document for it to show in the title block as well as in Vault.

 

My question is how can I access the propertysets of the default modelstate if it isn't present in the drawing?

Of course I could open the part or assembly and fill out the information manually, but that's kind of against why I created the form in the first place, to not have to edit both files.

 

0 Likes
3,093 Views
27 Replies
Replies (27)
Message 21 of 28

WCrihfield
Mentor
Mentor

Hi @richterBKSAC.  I copied your code from 'Message 19', and altered it.  The document type check part may be unnecessary, but usually good practice to include.  I also included a lot of comments in there.  In general, the 'factory' document is the only version that is Read/Write, while the 'member' versions are ReadOnly, so before we start trying to change or activate anything under a document reference, we need to stop at the document reference, and make sure which one we are working with.  What the next step is depends on what your plans are.  For instance, if you want to make exactly the same changes to all ModelStates in the file, then the specific document reference you have stored in your variable is not that important.  However sometimes you may want to make unique changes to that specific document, that you do not want to make to other specific documents in that file.  If the first case is true, you can simply set your document variable to pointing to the factory document, even if that is a different document than you started with.  But if the second situation is true, you will want to maintain your specific document reference after this next step.  If your original document is not already the factory document, then in order to maintain your reference to the original document, you may need to record which ModelState that original document represents, then get a reference to the factory document, then activate the ModelState that the original document represented, in order to make it become the factory document, then switch back to that original document reference again.

Here is the altered code:

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim refdocs As DocumentsEnumerator = oDoc.ReferencedDocuments
'stop at getting the referenced document, because we need to make sure it is the Factory document
'if it is not the factory document, we may need to make it the factory document
'because we can only make changes to the factory document, if one exists
'there will only be a factory document when the file has more than one ModelState present
Dim refdoc As Inventor.Document = refdocs.Item(1)
If refdoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject And _
	refdoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
	Exit Sub 'it is neither an assembly or part
End If
'only parts and assemblies have this property that we can check
If refdoc.ComponentDefinition.IsModelStateMember Then
	'if it is not currently the factory, we have two options
	'1) switch the value of our document reference 'refdoc' to pointing to the factory document
	'2) cause 'refdoc' to be the factory document, by activating the ModelState it represents
	'here we get the name of the ModelState that 'refdoc' represents (we already know it is not active)
	Dim sMSName As String = refdoc.ModelStateName
	'now since we can only make changes to the factory document, we need to get that first
	refdoc = refdoc.ComponentDefinition.FactoryDocument
	'now we activate that other ModelState, which will shift which document is the factory document
	refdoc.ComponentDefinition.ModelStates.Item(sMSName).Activate
	'now set 'refdoc' back to that document which represents the active ModelState, and is now the factory document
	refdoc = refdoc.ComponentDefinition.FactoryDocument
End If
Dim oModelStates As ModelStates = refdoc.ComponentDefinition.ModelStates
oModelStates.MemberEditScope = MemberEditScopeEnum.kEditAllMembers

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 22 of 28

CattabianiI
Collaborator
Collaborator

Maybe it is an idea to place a view of the "default" modelstate on the drawing outside of the actual drawing. This way I could access the factory.

Nope that it would be the member of the default model state, not the factory.

To get the factory you need to do something like that (from the code @WCrihfield posted above):

	'now since we can only make changes to the factory document, we need to get that first
	factoryRefDoc = refdoc.ComponentDefinition.FactoryDocument

 

0 Likes
Message 23 of 28

Frederick_Law
Mentor
Mentor

Does the iProp you set will be the same for all ModelState?

If so, set scope to Factory before changing iPorp.

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/model-state-toggle-edit-scope-via-il...

 

If not, activate each ModelState and edit iProp.

 

PS cannot activate ModelState in drawing.

Non ModelState file will only return [Primary].

 

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim refdocs As DocumentsEnumerator = oDoc.ReferencedDocuments
Dim oModelState As ModelState
For Each refdoc In refdocs
	Logger.Info(refdoc.DisplayName)
	Dim oModelStates As ModelStates = refdoc.ComponentDefinition.ModelStates
	For Each oModelState In oModelStates
	Logger.Info(oModelState.Name)
	Next
Next

 

This can get iProperty from the refdoc but not in each ModelState:

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim refdocs As DocumentsEnumerator = oDoc.ReferencedDocuments
Dim oModelState As ModelState
Dim refdoc As Inventor.Document
For Each refdoc In refdocs
	Logger.Info("Refdoc "&refdoc.DisplayName)
	Dim invDesignInfo As PropertySet = refdoc.PropertySets.Item("Design Tracking Properties")
	Logger.Info("PN "&invDesignInfo.Item("Part Number").Expression)
	Dim oModelStates As ModelStates = refdoc.ComponentDefinition.ModelStates
	For Each oModelState In oModelStates
		Logger.Info(oModelState.Name)
		Logger.Info("MS PN "&invDesignInfo.Item("Part Number").Expression)
	Next
Next

 

0 Likes
Message 24 of 28

richterBKSAC
Advocate
Advocate

Thank you all for your suggestions!

@Frederick_Law yes I want to set the IProp for all Members. Although it would be enough to set the IProperty for the factoryDocument, since its iProp will show up in my drawing titleblock.

 

@WCrihfield I've tried your code and there is no 

 

refdoc.ComponentDefinition.FactoryDocument

 

But I've started searching in the object tree and I found a solution thanks to your code 🙂 Hopefully it's not a hidden property again 😁

 

Dim ISI As String = "Inventor Summary Information"
Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim refdocs As DocumentsEnumerator = oDoc.ReferencedDocuments
Dim oFactoryModelStates As ModelStates = refdocs.Item(1).ComponentDefinition.FactoryDocument.ComponentDefinition.ModelStates
oFactoryModelStates.MemberEditScope = MemberEditScopeEnum.kEditAllMembers
refdocs.Item(1).ComponentDefinition.FactoryDocument.PropertySets.Item(ISI).Item("Title").Value = iProperties.Value(ISI, "Title")
				

 

 Now it works as intended.

0 Likes
Message 25 of 28

Frederick_Law
Mentor
Mentor

I don't think you need to get down to FactoryDocument.

Try:

refdocs.Item(1).PropertySets

 

0 Likes
Message 26 of 28

WCrihfield
Mentor
Mentor

The AssemblyComponentDefinition.FactoryDocument & PartComponentDefinition.FactoryDocument properties will return 'Nothing' when ComponentDefinition.ModelStates.Count is less than 2.  Because there is no ModelState 'factory' until there are at least 2 ModelStates.  Since every new model document already has 1 ModelState, there is still only 1 document reference in that file, and that 1 document is not yet a 'factory'.  In those cases, you do not need to worry about trying to access the 'factory' version, because it does not exist.  To avoid that situation, we always check some things before moving forward with a document variable.  You can try checking the ModelStates.Count, and if less than 2, don't worry about ModelStates at all, and if 2 or more, proceed to make extra checks.  You can try checking the AssemblyComponentDefinition.IsModelStateMember and/or AssemblyComponentDefinition.IsModelStateFactory to help determine which version you have.  Just keep the simple fact in mind that the factory is simply the document which is currently under the influence of the file's 'active' ModelState, not necessarily the 'Master/Primary' ModelState.  If you want to access the Master/Primary ModelState, it is always the first Item in the ModelStates collection, no matter how many there may be.  But if there are more than one ModelState, and that Master/Primary ModelState is not currently the 'active' one, you will need to activate that ModelState before you will be able to make changes to it.  The exception to that is when editing within the ModelStatesTable or the ModelStates.ExcelWorksheet.  You can read data (without making changes) from any ModelState, at any time, no matter which one is active, as long as that file is open (or initialized / loaded into Inventor's session memory).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 27 of 28

richterBKSAC
Advocate
Advocate
Unfortunatley that's not working.
0 Likes
Message 28 of 28

richterBKSAC
Advocate
Advocate
The code I've posted is just a snippet, so you couldn't know that I've build in an if-condition to check if the referenced Document is a ModelStateMember. But thanks for pointing that out.
0 Likes