Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am trying to run a rule from an assembly to insert the parent name of each component to a custom property. I have this code that does exactly what I'm looking for but it errors out when there are read only parts, such as library parts, in the assembly. Is there a way to ignore read only files in the assembly? Sorry I'm not very good with code.
Sub Main Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition 'Iterate through all of the occurrences Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences Call ProcessAllChildren(oOccurrence) Next End Sub Public Sub ProcessAllChildren(ByRef oOccurrence As ComponentOccurrence) ' Get the custom property set. Dim invCustomPropertySet As PropertySet invCustomPropertySet = oOccurrence.Definition.Document.PropertySets.Item("Inventor User Defined Properties") Dim invParentCustProp As Inventor.Property Try 'If this fails Parent_Name Property does not exist invParentCustProp = invCustomPropertySet.Item("UsedInAssy") 'MessageBox.Show("invParentCustProp Exists" , oOccurrence.Name) Catch ' Create the property as it did not exist. 'MessageBox.Show("invParentCustProp Does not Exist" , oOccurrence.Name) Try ' oOccurrence.ParentOccurrence is nothing if it is a top level occurrence ' ParentOccurence will be the sub assembly the occurrence is in Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(oOccurrence.parentOccurrence.Definition.Document.Displayname) invCustomPropertySet.Add(filename, "UsedInAssy") Catch 'This works if the occurrence is in the top level assembly ' oOccurrence.Parent.Document.DisplayName Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(oOccurrence.Parent.Document.DisplayName) invCustomPropertySet.Add(filename, "UsedInAssy") End Try invParentCustProp = invCustomPropertySet.Item("UsedInAssy") End Try 'Update the value for the custom property for this occurrence if it does not already have it Try Dim strExistingVal As String = invParentCustProp.Value Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(oOccurrence.ParentOccurrence.Definition.Document.DisplayName) If Not strExistingVal.Contains(filename) Then ' Try to get the ParentOccurrence, this will fail if it is a top level occurrence invParentCustProp.Value = strExistingVal & "; " & filename 'MessageBox.Show("In Try after Occurrence.ParentOccurrence.Definition.Document.DisplayName" , oOccurrence.ParentOccurrence.Definition.Document.DisplayName) End If Catch Dim strExistingVal As String = invParentCustProp.Value Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(oOccurrence.Parent.Document.DisplayName) If Not strExistingVal.Contains(filename) Then 'This works the top level assembly invParentCustProp.Value = strExistingVal & "; " & filename End If End Try Dim oCompOcc As ComponentOccurrence Dim oComponentSubOccurrences As ComponentOccurrences ' For Each oCompOcc In oOccurrence.SubOccurrences If oCompOcc.DefinitionDocumentType = kAssemblyDocumentObject Then oComponentSubOccurrences = oCompOcc.SubOccurrences If Not oComponentSubOccurrences Is Nothing Then If oComponentSubOccurrences.Count > 0 Then ProcessAllChildren(oCompOcc) End If End If Else ProcessAllChildren(oCompOcc) End If Next End Sub
Solved! Go to Solution.