Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Model states

ZdenkoSantic
Contributor

Model states

ZdenkoSantic
Contributor
Contributor

Running rule for total qty in assembly is working just perfect but is not adding total qty for model states . Can this rule be modified? Thank you

Sub Main
	On Error Resume Next
	Dim openDoc As Document
	openDoc = ThisDoc.Document
	Dim docFile As Document

	Dim oCustomPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")
	If iProperties.Value("Custom", "TotalUnits") = 0 Then
		iProperties.Value("Custom", "TotalUnits") = 1
	End If
	Dim Total_Units = iProperties.Value("Custom", "TotalUnits")

	'**** FOR TESTING PURPOSES - place a ' in front of the next line to skip it
	'	MessageBox.Show("TotalUnits = " & Total_Units, "iLogic")

	If openDoc.DocumentType = 12291 Then
		For Each docFile In openDoc.AllReferencedDocuments

			Dim propertyName As String = "TTL"
			Dim propertyValue As Integer = 1
			customPropertySet = docFile.PropertySets.Item("Inventor User Defined Properties")
			
			Dim FNamePos As Long
			Dim docFName As String
			FNamePos = InStrRev(docFile.FullFileName, "\", -1)
			docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) -FNamePos)

			If docFile.IsModifiable = True Then
				Dim assemblyDoc As AssemblyDocument
				Dim assemblyDef As AssemblyComponentDefinition
				Dim partQty As ComponentOccurrencesEnumerator
				assemblyDoc = openDoc
				assemblyDef = assemblyDoc.ComponentDefinition
				partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)

				If IsNumeric(partQty.Count) = True Then
					iProperties.Value(docFName, "Custom", "TTL") = partQty.Count * Total_Units


				End If
			End If
		Next
	Else
		MessageBox.Show("You must have a valid Assembly document open before using this code!", "File Type Mismatch!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
	End If
End Sub

 

0 Likes
Reply
316 Views
4 Replies
Replies (4)

WCrihfield
Mentor
Mentor

Hi @ZdenkoSantic.  It's a little difficult to follow the intent of your code, but it looks like you are attempting to gather total number of components in every level of your main assembly's structure that references each of the documents being referenced by the main assembly, then write that data to a custom iProperty within the referenced document.  The problem with that approach is if you have, for example, 5 components that each reference the same model file, but each is set to a different ModelState, that will look like 5 different referenced documents, instead of just the one, and when writing the quantity data to that model file, the value will get overwritten by the other ModelState variations.  You need to be able to compare Document.FullFileName (not Document or Document.FullDocumentName), so that the ModelState variations to not get counted as different documents, and quantity data does not get overwritten multiple times.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

WCrihfield
Mentor
Mentor

Here is a similar code you can take a look at.  I slightly modified it from its original version, to include a Dictionary, and a way to check each referenced document's FullFileName, to see if we have already worked with that same base file before.  If we have worked with it before, it adds the new quantity to the existing quantity, instead of overwriting it.  This code is also using a newer tool to make sure the iLogic shortcut snippet for iProperties is working with the right document, without needing to supply a compiled document name.  Still though, if that file has multiple ModelStates, this data is only going to be written to that file's 'active' ModelState, so it might have a different value within the other ModelStates in that file.  There are ways to fix that, but we would have to get rid of the iLogic snippet, and include more code for writing the value to every ModelState in that file.

 

 

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
	If oRefDocs.Count = 0 Then Exit Sub
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	If oOccs.Count = 0 Then Exit Sub
	Dim oDict As New Dictionary(Of String, Integer) 'FullFileName, Quantity
	For Each oRefDoc As Document In oRefDocs
		Dim oFFN As String = oRefDoc.FullFileName
		Dim oCount As Integer = oOccs.AllReferencedOccurrences(oRefDoc).Count
		If oDict.Keys.Contains(oFFN) Then
			oDict.Item(oFFN) = oDict.Item(oFFN) + oCount
		Else
			oDict.Add(oFFN, oCount)
		End If
		SOP = StandardObjectFactory.Create(oRefDoc)
		Try : SOP.iProperties.Value("Custom", "TOTAL QTY") = oDict.Item(oFFN) : Catch : End Try
	Next
	MsgBox("Done!",,"")
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

ZdenkoSantic
Contributor
Contributor

Code is running no error but Custom TTL is still 0. Like in other  code it should ask how many total units and when i place (2) it will multiply all qty's with that number or any number i need 3, 4 ...

to run my  code above need custom ipropertis for TTL and TotalUnits

Thank you for you time to look into it

0 Likes

WCrihfield
Mentor
Mentor

Hi @ZdenkoSantic.  Sorry for the delay.  Got lots of stuff going on at the same time.  Below is a slightly modified version of the last code I posted.  The last code was just an example, and not personalized to your specific settings or property names.  This version includes checking & setting the value of the "TotalUnits" custom iProperty of the main assembly using a Try...Catch block, and creates it if needed, with the value of 1, since that is what we would be setting it to here, if found.  If it is found, and has a value other than zero, then its value is gathered without changing it.  Then I am now including that factor in the 'math' of the quantities, before recording them.  I also included the 'IsModifiable' check in the loop, since you were doing that also.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim iTotalUnits '<<< Is this Double or Integer?  Should declare its Type >>>>
	Try
		If oADoc.PropertySets.Item(4).Item("TotalUnits").Value = 0 Then
			oADoc.PropertySets.Item(4).Item("TotalUnits").Value = 1
		End If
	Catch 'that custom property was not found, so error, then this creates it
		oADoc.PropertySets.Item(4).Add(1, "TotalUnits")
	End Try
	iTotalUnits = oADoc.PropertySets.Item(4).Item("TotalUnits").Value
	Dim oRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
	If oRefDocs.Count = 0 Then Exit Sub
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	If oOccs.Count = 0 Then Exit Sub
	Dim oDict As New Dictionary(Of String, Integer) 'FullFileName, Quantity
	For Each oRefDoc As Document In oRefDocs
		If Not oRefDoc.IsModifiable Then Continue For 'skip to next oRefDoc
		Dim oFFN As String = oRefDoc.FullFileName
		Dim oCount As Integer = oOccs.AllReferencedOccurrences(oRefDoc).Count
		If oDict.Keys.Contains(oFFN) Then
			oDict.Item(oFFN) = oDict.Item(oFFN) + (oCount * iTotalUnits)
		Else
			oDict.Add(oFFN, (oCount * iTotalUnits))
		End If
		SOP = StandardObjectFactory.Create(oRefDoc)
		Try : SOP.iProperties.Value("Custom", "TTL") = oDict.Item(oFFN) : Catch : End Try
	Next
	MsgBox("Done!",,"")
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes