Wednesday, October 23, 2013

...An iProp For Everyone!

In my last post, I showed one solution to a common task of using assembly level parameters to set iProperties of part files in your assembly using an Array and a For Next Loop.  Using that solution, we specified each component that we wanted to push the iProperties to.

In this post, I'd like to discuss another solution that will push iProperties down to all components in your assembly, even those inside of sub-assemblies.


Sub Main
        Dim oAsmDoc As AssemblyDocument 
        oAsmDoc = ThisApplication.ActiveDocument 

    '    Call the function that does the recursion. 
        Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)
End Sub 

Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer) 
    '    Iterate through all of the occurrences in this assembly.  Level 1 components. 
    
        Dim oOcc As ComponentOccurrence 
        For Each oOcc In Occurrences 
        
        '    Get the selected item document occurrence name
            Dim oBrowserNode As String
            oBrowserNode = oOcc.Name
    
                Try                                                
                    '    Push the required iProperties down to the occurrences
                        iProperties.Value(oBrowserNode, "Custom", "SO_Number") = Sales_Order_Number
                        iProperties.Value(oBrowserNode, "Custom", "Company") = Customer_Name
                        iProperties.Value(oBrowserNode, "Project", "Project") = Project_Description
        
                Catch
                    '    Do not raise or display an error.
                
                End Try
        
            '    Check to see if this occurrence represents a subassembly 
            '    and recursively call this function to traverse through it. 
                If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
                    
                    Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) 
                    
                End If 
        Next 
End Sub

The above code will go iterate through each occurrence in your assembly and set the required iProperties.  If the occurrence is a sub-assembly, it will also iterate through each component inside of that sub-assembly.

Hopefully you see some benefit in the above solution.  Leave me a comment and let me know.

Randy

"The only way to do great work is to love what you do." - Steve Jobs

11 comments:

  1. Keep up the good work Randy. I'll add this blog to my feedly feed. Thanks for sharing

    ReplyDelete
  2. Good code but it has some problems. If i have an assembly with lets say 100 same ipt occurences (bolts, washers ) it will write my custom properties in the occurence each time it meets the part (100 times!!!) making the machine very slow. If only you could make a filter to write the property the first time it meets an occurence and when it meets again the same occurence it will just simply skip it...

    ReplyDelete
  3. Great point Geo....resolution forthwith I'm sure.

    ReplyDelete
  4. Geomanowar, I just posted an alternative solution using AllReferencedDocuments. Please take a look at that and let me know how that works for you. I'd be very interested in the difference in times that each rule takes to run. Please share if you can.

    ReplyDelete
    Replies
    1. I can't find the alternative code you've mentioned here? Did you just replace the code above with it because I cant find the AllReferencedDocuments command you mentioned.

      Delete
  5. Thanks! Works great, able to write custom properties in an assembly with 100+ parts in less than 3 seconds!

    ReplyDelete
  6. Good to hear. Out of curiosity, how long did it take before?

    ReplyDelete
  7. Previously it took 1min+ to write iproperties in 25 ipt's. When i tried 100+ the program got stuck and i had to terminate it from task manager! Now i am trying to find a way to autopurge empty iproperties from ipt files ( i have an ilogic rule to import various custom iproperties in case i need them) and also autopurge unused title blocks from an idw. Can you help?

    ReplyDelete
  8. Here is a link to another iLogic blog that may help you with that. Take a look at Curtis's post entitled "iLogic - Delete Custom iProperties". If all the components you are wanting to remove custom iProperties from are within an assembly, you can use the code from my "An iProp for the unique items" post in conjuction with the code from Curtis's post.

    ReplyDelete
  9. http://inventortrenches.blogspot.com/

    ReplyDelete
  10. Hello Randy,
    I used your code in some issues that we are having with BIM, that we finally resolved, thanks to your effort. Im not a programer, but I understand a few things. In my opinion is a very good piece of code, Thanks for that!
    Now perhaps you can help me with this one, Im trying to get the unique identifier every single object in an assembly, then pass this value to a custom property. The result will be in a data base field called maybe "OBJECT_ID", this will allow us to link an iam inventor file read it from naviswork, linked with an excel data sheet and modify this values with excell outside Naviswork. I don't know if this make sense (probably in my head) but like I said, maybe you can help us.
    KInd Regards
    Pedro

    ReplyDelete