iLogic to create custom iProperty in sub-component

DRoam
Mentor
Mentor

iLogic to create custom iProperty in sub-component

DRoam
Mentor
Mentor

I found this code to create a custom iProperty in the current, open document:

 

 

Dim propertyName As String = "MyProperty"
Dim propertyValue as String = "Test Value"

customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")
Try
      prop = customPropertySet.Item(propertyName)
Catch
      ' Assume error means not found
      customPropertySet.Add("", propertyName)
End Try

iProperties.Value("Custom", propertyName) = propertyValue

 

I have a iLogic rule that goes through each component of an assembly, counts how many times it occurs in the assembly, and then assigns that value to an iProparty (i.e. it inserts the total quantity of the part in some iProperty). Currently, however, it assigns the value to a default iProperty value, "Authority". I'd like, instead, for it to assign it to a custom iProperty called "Total QTY". However, I don't know how to tell Inventor to do the above code for a component in the assembly rather than the assembly itself. Below is my entire code. I've labeled where I'm guessing the above code should go. Could someone tell me how I'd modify the above code to create the iProperty in the component being processed by the rule at that point? Thanks for any help!

 

Current code:

 

''True comment: Begin ''
'Commented-out code: Begin '

''PartDocumentObject = 12290
''AssemblyDocumentObject = 12291

Dim openDoc As Document
	openDoc = ThisDoc.Document
Dim docFile As Document

If openDoc.DocumentType = 12291 Then
	''If the open document is an assembly:
	''For each document referenced in this document:
	For Each docFile In openDoc.AllReferencedDocuments
		Dim FNamePos As Long
			FNamePos = InStrRev(docFile.FullFileName, "\", -1)
		Dim docFName As String
			docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos)

		If docFile.IsModifiable = True Then
			'If the document is not read only:
			Dim assemblyDoc As AssemblyDocument
				assemblyDoc = openDoc
			Dim assemblyDef As AssemblyComponentDefinition
				assemblyDef = assemblyDoc.ComponentDefinition
			Dim partDoc As PartDocument
				partDoc = ThisApplication.Documents.Open(docFile.FullFileName, False)
			Dim partQty As ComponentOccurrencesEnumerator
				partQty = assemblyDef.Occurrences.AllReferencedOccurrences(partDoc)
			
			'MessageBox.Show(partQty.Count, "")
			If IsNumeric(partQty.Count) = True Then
			
				'CREATE CUSTOM VARIABLE "Total QTY" HERE
			
				If CDblAny(partQty.Count) <>  CDblAny(iProperties.Value(docFName, "Custom", "Total QTY")) Then
					iProperties.Value(docFName, "Custom", "Total QTY") = partQty.Count
					partDoc.Close
				Else
					partDoc.Close
				End If
			End If
		End If
	Next
Else
	''If the open document is not an assembly:
	MessageBox.Show("You must have a valid Assembly document open before using this code.", "File Type Mismatch",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
End If

 

Reply
7,190 Views
3 Replies
Replies (3)

mrattray
Advisor
Advisor

Behavior of iLogic isn't always quite like VBA. In this case you don't need to mess around with any of that try catch stuff. All you need is the iProperties.Value() method. In iLogic this will automatically create the property if it doesn't already exist, and if it does exist it will set its value accordingly.

Mike (not Matt) Rattray

0 Likes

MegaJerk
Collaborator
Collaborator

The following code should do what you're asking for. 

The iProperty creation is tricky in iLogic because for some reason it doesn't like the idea of outright property creation... I had to make that empty Property just so I could create the darned thing 😕 

I hope that this helps. Tell me if it gives you any trouble. 

Dim openDoc As Document
openDoc = ThisDoc.Document

Dim docFile As Document

If openDoc.DocumentType = kAssemblyDocumentObject Then    
	For Each docFile In openDoc.AllReferencedDocuments
		If docFile.IsModifiable = True Then			
			Dim assemblyDoc As AssemblyDocument
			assemblyDoc = openDoc
				
			Dim assemblyDef As AssemblyComponentDefinition
			assemblyDef = assemblyDoc.ComponentDefinition
			
			Dim partQty As ComponentOccurrencesEnumerator
			partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)
		
			Dim customPropSet As PropertySet
			Dim customProp As Inventor.Property	
			Dim propertyName As String
			Dim propertyValue As Integer
			
			customPropSet = docFile.PropertySets.Item("Inventor User Defined Properties")
			propertyName = "TotalQty"

			Try
				propertyValue = customPropSet.Item(propertyName).Value
				If PartQty.Count <> propertyValue Then 
					customPropSet.Item(propertyName).Value = PartQty.Count
				End If 
				Catch ex As Exception					
				customProp = customPropSet.Add(PartQty.Count, propertyName)
			End Try 
		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 

 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub

Anonymous
Not applicable

Hello.

I am using this code from the previous post to calculate sub-component total quantities. The first time (per assembly) the rule works fine and all sub-components get a new iProperty. But after I make some modifications to the assembly, such as remove some components, the rule gives the following error:

 

Error message:

Error in rule: Calculate quantities, in document: Assembly1.iam

Parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

More info:

System.ArgumentException: Parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.PropertySet.Add(Object PropValue, Object Name, Object PropId)
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Does someone know how to fix this problem and what is causing it?

0 Likes