Read Custom properties from parts within Assembly

Read Custom properties from parts within Assembly

damian_dymczyk
Contributor Contributor
1,289 Views
6 Replies
Message 1 of 7

Read Custom properties from parts within Assembly

damian_dymczyk
Contributor
Contributor

I would like to be able to read all part's custom properties that are included in Assembly. I found code written by mod the machine authors but I have problems modifying it so it would iterate through all the parts and give me but the info. 

Dim entity = ThisApplication.CommandManager.Pick(
  SelectionFilterEnum.kAssemblyOccurrenceFilter, 
  "Select Component:")

If (Not entity Is Nothing) And _
(TypeOf entity Is ComponentOccurrence) Then
  Dim doc = entity.Definition.Document
  Dim propSet = doc.propertySets(
    "Inventor User Defined Properties")
  Dim msg As String
  For Each prop In propSet
    msg = msg + prop.Name + " = " + _
      prop.Value.ToString() + vbCrLf
  Next
  MessageBox.Show(msg, "iProperties")
End If

 Can somebody help me with the issue? 

0 Likes
Accepted solutions (2)
1,290 Views
6 Replies
Replies (6)
Message 2 of 7

JhoelForshav
Mentor
Mentor
Accepted solution

@damian_dymczyk 

Try this 🙂

Dim oAsm As AssemblyDocument = ThisDoc.Document
For Each oRefDoc As Document In oAsm.AllReferencedDocuments
	If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oRefDoc).Count > 0
		Dim oOccName As String = oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oRefDoc)(1).Name
		If oOccName.Contains(":") Then oOccName = oOccName.Split(":")(0)
		Dim oMsg As String = "Component name in assembly: " & oOccName & vbCrLf & vbCrLf
		Dim propSet = oRefDoc.PropertySets(
		"Inventor User Defined Properties")
		For Each prop In propSet
			oMsg = oMsg + prop.Name + " = " + _
			prop.Value.ToString() + vbCrLf
		Next
		MessageBox.Show(oMsg, oRefDoc.DisplayName)
	End If
Next
Message 3 of 7

damian_dymczyk
Contributor
Contributor

Thanks a lot. it works great ! But can stay at first level of assembly. What I mean read props from parts and subassemblies and don't go to subassemblies' parts ?

0 Likes
Message 4 of 7

JhoelForshav
Mentor
Mentor

Like this then 🙂

Dim oAsm As AssemblyDocument = ThisDoc.Document
For Each oRefDoc As Document In oAsm.ReferencedDocuments
	If oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oRefDoc).Count > 0
		Dim oOccName As String = oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oRefDoc)(1).Name
		If oOccName.Contains(":") Then oOccName = oOccName.Split(":")(0)
		Dim oMsg As String = "Component name in assembly: " & oOccName & vbCrLf & vbCrLf
		Dim propSet = oRefDoc.PropertySets(
		"Inventor User Defined Properties")
		For Each prop In propSet
			oMsg = oMsg + prop.Name + " = " + _
			prop.Value.ToString() + vbCrLf
		Next
		MessageBox.Show(oMsg, oRefDoc.DisplayName)
	End If
Next
Message 5 of 7

damian_dymczyk
Contributor
Contributor

Thanks for instant respond ! I just notice that if I have several identical parts places in one assembly it will just return  info from one so I assume is basing on ipt files not on occurrence in assembly. Of course it works with the purpose I presented in the post. I'm asking because my next step would be to export those info to excel end then I would need to have info of every item in the assembly. 

0 Likes
Message 6 of 7

JhoelForshav
Mentor
Mentor
Accepted solution

Ok... I just assumed you wanted to use the document because two occurrences of the same document will always have the same properties. But if you want to go through every occurrence try this 🙂

Dim oAsm As AssemblyDocument = ThisDoc.Document
For Each oOcc As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences
		Dim oRefDoc As Document = oOcc.Definition.Document
		Dim oOccName As String = oOcc.Name
		Dim oMsg As String = "Component name in assembly: " & oOccName & vbCrLf & vbCrLf
		Dim propSet = oRefDoc.PropertySets(
		"Inventor User Defined Properties")
		For Each prop In propSet
			oMsg = oMsg + prop.Name + " = " + _
			prop.Value.ToString() + vbCrLf
		Next
		MessageBox.Show(oMsg, oRefDoc.DisplayName)
Next
0 Likes
Message 7 of 7

damian_dymczyk
Contributor
Contributor

Thank you veeeery much works like a charm