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: 

Clearing/unsetting textual custom iProperty value via `ModelStateTableCell` object

19 REPLIES 19
Reply
Message 1 of 20
meGVGMF
934 Views, 19 Replies

Clearing/unsetting textual custom iProperty value via `ModelStateTableCell` object

Hi

 

I was wondering how to clear the value from a textual custom iProperty via the `ModelStateTable` object.

 

I've tried setting it to a string of `"<Empty>"` (after seeing this at some point in the GUI dialog and getting it to work there), but this sets the `ModelStateTableCell.IsValid` property to false, and setting it simply to an actually empty `""` immediately throws an error.

 

None of the methods on the `ModelStateTableCell` class seem like they would help.

 

Is there nothing I can do to accomplish this?

 

Thanks

19 REPLIES 19
Message 2 of 20
CattabianiI
in reply to: meGVGMF

Setting the cell value to String.Empty/"" is the proper way.
So you had to investigate the error you get.
How are you retrieving the modelstatetable?
From which document instance? If you do write operations you had to get it via factory document.

Message 3 of 20
WCrihfield
in reply to: meGVGMF

Hi @meGVGMF.  This sounds to me like you may have obtained the ModelStateTable from a ModelState 'member' document, instead of from the ModelState 'factory' document.  If that is the case, everything you get within that document will be ReadOnly.  You need to make sure you obtain it from the ModelState 'factory' version of the document.  The 'factory' is basically just the version of the document that is currently under the influence of the ModelState that is 'active' in that document.  And only a document that has more than the one, original ModelState will have a 'factory' version.  (Link)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 20
meGVGMF
in reply to: CattabianiI

The following would be accessing the table from the factory document, right?
`ThisDoc.Document.ComponentDefinition.ModelStates.ModelStateTable`

 

(The file starts as a master-model-state-only document, but a new model state is created just before this. But also I just tested that adding a model state manually and closing and reopening the file before running the script gives the same result.)


Trying to set to either `String.Empty` or `""` gives the error in the image below. Setting it to a dummy, non-empty string works.

 

Screenshot 2023-01-17 141459.png

Message 5 of 20
meGVGMF
in reply to: WCrihfield

Sadly I don't think that's it.
I'm getting the table from `ThisDoc.Document.ComponentDefinition.ModelStates.ModelStateTable`
Message 6 of 20
CattabianiI
in reply to: meGVGMF

just to be sure, before getting the model state table log this property to check if you have a member or factory document: 

Logger.Info("Is Factory? {0}", ThisDoc.Document.ComponentDefinition.IsModelStateFactory) 

If you're working directly in Part1.ipt which is the one who contains the rule then ThisDoc is a factory document; on the other hands if you're executing this rule on trigger event or from a RunRule in parent assembly i'm not sure, I don't use very often ThisDoc instance. 

Which is your Inventor version including updates?

Message 7 of 20
WCrihfield
in reply to: meGVGMF

Hi @meGVGMF.  Using ThisDoc.Document will usually return the same document object as ThisDoc.FactoryDocument, when you have that model document open directly, and 'active' on your screen, but just as @CattabianiI pointed out, that may not be the case in other odd situations, where you may not be directly editing the model as the 'active' document.  He also pointed out one of the main tools for checking if the document you have is a 'member' or 'factory', which I also pointed out within the 'Link' at the end of my earlier responce.

Here is an example you can look at:

Dim oDoc As Document = ThisDoc.Document 'or ThisDoc.FactoryDocument
Dim oMSTable As ModelStateTable = Nothing
If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	Dim oADoc As AssemblyDocument = oDoc
	If oADoc.ComponentDefinition.IsModelStateMember Then oADoc = oADoc.ComponentDefinition.FactoryDocument
	oMSTable = oADoc.ComponentDefinition.ModelStates.ModelStateTable
ElseIf oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
	Dim oPDoc As PartDocument = oDoc
	If oPDoc.ComponentDefinition.IsModelStateMember Then oPDoc = oPDoc.ComponentDefinition.FactoryDocument
	oMSTable = oPDoc.ComponentDefinition.ModelStates.ModelStateTable
Else
	Exit Sub 'wrong document type
End If
'now you should have obtained the ModelStateTable from the 'Factory' document
Dim oMyCol As ModelStateTableColumn = Nothing
For Each oCol As ModelStateTableColumn In oMSTable.TableColumns
	If oCol.ReferencedDataType = iComponentColumnTypeEnum.kFilePropertyColumn And _
		oCol.DisplayHeading = "MyCustomPropertyName" Then
		oMyCol = oCol : Exit For
	End If
Next
oMSTable.TableRows.Item("ModelStateName").Item(oMyCol).Value = ""
'oMSTable.TableRows.Item("ModelStateName").Item("ColumnHeading").Value = ""

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 20
meGVGMF
in reply to: meGVGMF

Thanks very much @CattabianiI and @WCrihfield 
I won't be able to work on this today, but it looks promising and I'll report back as soon as I can.

Message 9 of 20
meGVGMF
in reply to: CattabianiI

Seems like I've been using the factory document the whole time, but still can't seem to set it to an empty string.

I'm using build 153, 2022, no updates, I'm guessing.
Message 10 of 20
meGVGMF
in reply to: WCrihfield

It turns out I've been using the factory document the entire time.
Message 11 of 20
CattabianiI
in reply to: meGVGMF

I'm using build 153, 2022, no updates, I'm guessing.

Then, first of all, update to 2022.4. In these almost two years, many improvements and bug fixes have been released about model states.

Message 12 of 20
meGVGMF
in reply to: CattabianiI

Good idea, but I've updated and now have build 492, 2022.4, but still I get the same error when using an empty string.

Message 13 of 20
CattabianiI
in reply to: meGVGMF

Is the issue just on that part or in any part in which you run the rule?

Could you please share a little bit more to try to reproduce your situation? Because just setting a model state table cell value to an empty string is ok on my side

Message 14 of 20
CattabianiI
in reply to: meGVGMF

Hi @meGVGMF,
you're right! 
I got an exception too setting the cell value to an empty string. Let me do some more tests 

Message 15 of 20
WCrihfield
in reply to: CattabianiI

Just a thought, but maybe the expected data type of that column is playing a part in this issue.  Each ModelStateTableColumn has a ReadOnly property called ReferencedDataType, which has a value from the iComponentColumnTypeEnum, and is supposed to indicate/dictate which type of data that column is for.  So, if an empty String does not meet the requirements of the expected data, or the way that data is supposed to be formatted, it is certainly logical that it would throw an error.  For instance, if it is expecting a Boolean value, or the value of a Parameter, or a Quantity of something, or an 'Enum' variation, or a thread spec, then a value other than an empty String must be present, or it does not know how to process that.  However, if the column is just for a 'file property' and it is a 'custom' property, which the other members currently all have a String type value for, then it would seem logical for an empty String to be OK.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 16 of 20
meGVGMF
in reply to: WCrihfield

Good thought, but yeah, I'm using only custom properties in this case.
Message 17 of 20
meGVGMF
in reply to: CattabianiI

Okay, great! Please let me know what you find out
Message 18 of 20
CattabianiI
in reply to: meGVGMF

Hi @meGVGMF I opened this new thread to report the issue: https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/bug-report-error-setting-model-state...
an unanswered post has more view that a thread with almost 20 messages.
You can subscribe to that thread.

I'll do the same report via another private channel.
I'll update this thread anyway.

Message 19 of 20
WCrihfield
in reply to: CattabianiI

I am also using Inventor Pro 2022.4, and getting the same results...error while trying to set the value of the cell to an empty String.  Interestingly, I have been trying some other things, and found that if I try to set it to 'vbNull' it sets the value to "1".  And it will let me set " " (one empty whitespace) as the value successfully.

Tried 'Nothing', 'vbNullString' & 'vbNullChar' with same error.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 20 of 20
CattabianiI
in reply to: WCrihfield

The weird thing about the whitespace is that from Inventor point of view seems to be treated like an empty string except for what we see in the spreadsheet. But this is just a thing related to the setting via model state table, setting an iProperty in the active document/model state to whitespace is possible and is not equal to empty string.

  1. Open new part
  2. create a model state
  3. create a custom iProperty named testprop in Model State1
  4. activate primary model state
  5. edit the testprop to another value (now the column of that property is in the excel table)
  6. Run the following rule:
Dim propName As String = "testprop"
Dim propTblHeader As String = "Custom"
Dim msName As String = "Model State1"
Dim propValue As String = ""

Dim facDoc As Document = ThisApplication.ActiveDocument
Dim msTbl As ModelStateTable = facDoc.ComponentDefinition.ModelStates.ModelStateTable
Dim tblClms As ModelStateTableColumns = msTbl.TableColumns
Dim thisClm As ModelStateTableColumn = tblClms.Item(propName & " [" & propTblHeader & "]")

If String.IsNullOrEmpty(propValue) Then
	propValue = " "
End If

Dim thisCell As ModelStateTableCell = thisClm.Item(msName)
thisCell.Value = propValue

If String.IsNullOrEmpty(thisCell.Value) Then
	Logger.Info("Empty cell now")
End If

facDoc.ComponentDefinition.ModelStates.Item(msName).Activate()

If String.IsNullOrEmpty(iProperties.Value("Custom", propName)) Then
	Logger.Info("Empty iProp too")
End If

 

 

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

Post to forums  

Autodesk Design & Make Report