Haggard VBA code to count bends and add time to iproperty

Haggard VBA code to count bends and add time to iproperty

Anonymous
Not applicable
905 Views
7 Replies
Message 1 of 8

Haggard VBA code to count bends and add time to iproperty

Anonymous
Not applicable

 

Public Sub BendingTime()

'set a constant to identify sheet metal parts
Const CLSID_InventorSheetMetalPart_RegGUID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"

Dim m_inventorapplication As Inventor.Application
Set m_inventorapplication = GetObject(, "Inventor.Application")

'check that this is running on an assembly document
If (m_inventorapplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject) Then

    'get the active assembly
    Dim asmDoc As AssemblyDocument
    Set asmDoc = ThisApplication.ActiveDocument
    
Call TraverseAssembly(asmDoc.ComponentDefinition.Occurrences)

End If

msgbox ("this runs on assembly documents only")


End Sub





Private Sub TraverseAssembly(Occurrances As ComponentOccurrences)
        
    Dim Occ As ComponentOccurrence
    Dim oSheetMetalComp As Inventor.SheetMetalComponentDefinition
    Dim iBendCount As Integer
    Dim oCustomProps As Inventor.PropertySet
    Dim sBendPropName As String
    sBendPropName = "Bend Time"
    Dim oBendProp As Inventor.Property
    Dim oProp As Inventor.Property
    
  
    For Each Occ In Occurrances

          'If Occ.SubType = CLSID_InventorSheetMetalPart_RegGUID Then
                Set oSheetMetalComp = Occ.ComponentDefinition
                iBendTime = oSheetMetalComp.Bends.Count * 0.00556
                Set oCustomProps = Occ.PropertySets.Item("Inventor User Defined Properties")
                For Each oProp In oCustomProps
                    If oProp.Name = sBendPropName Then
                        'We already have an iproperty, we just need to write the new value
                    Set oBendProp = oProp
                        oBendProp.Value = iBendTime
                    End If
            Next oProp
          'We don't have an iproperty, so we create it and set its value
            If oBendProp Is Nothing Then Set oBendProp = oCustomProps.Add(iBendTime, sBendPropName)
          
    If Occ.Definition.DocumentType = kAssemblyDocumentObject Then
        Call TraverseAssembly(Occ.SubOccurrences)
        End If
                
    'Else
     'MsgBox ("not a sheetmetal doc.")
                
    
 Next
End Sub

 

 

I have searched quite a lot for people to just write my code for me without any result - actually surprising.   I even set up some stuff on fiverr!  No luck!

...so here is my cleaned up version where I combined a few snippets to try to add bend times to an assembly of parts, and to all subsequent subassemblies.

It won't detect if each part is sheetmetal - needs an if statement so it skips over standard parts without just erroring, silently.

I may be declaring things outside of the loop when I need to declare them inside the loop - but that doesn't make sense to me.


Currently hangs up at the red line, object doesn't support this property.  But it should!  Unless I need to declare the Occ as something else first.  I'm running this on an assembly comprised only of sheetmetal parts, so shouldn't be because it's a standard part it's finding.

 

Thanks in advance for any tips, and have a happy holidays!

James

 

0 Likes
Accepted solutions (1)
906 Views
7 Replies
Replies (7)
Message 2 of 8

bradeneuropeArthur
Mentor
Mentor
Set oSheetMetalComp = Occ.document.ComponentDefinition

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Anonymous,

 

Try the following VBA code

 

Set oSheetMetalComp = occ.Definition

Please feel free to contact if there is any queries,

 

If solves problem, click on "Accept as solution" / give a "Kudo"

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 4 of 8

dgreatice
Collaborator
Collaborator
Accepted solution

Hi,

 

try this :

 

Public Sub BendingTime()

Dim m_inventorapplication As Inventor.Application
Set m_inventorapplication = GetObject(, "Inventor.Application")

'check that this is running on an assembly document
If (m_inventorapplication.ActiveDocumentType <> DocumentTypeEnum.kPartDocumentObject) Then

    'get the active assembly
    Dim asmDoc As AssemblyDocument
    Set asmDoc = ThisApplication.ActiveDocument
   
Call TraverseAssembly(asmDoc.ComponentDefinition.Occurrences)
asmDoc.Update
End If

msgbox ("this runs on assembly documents only")


End Sub

 

Private Sub TraverseAssembly(Occurrances As ComponentOccurrences)
       
    Dim Occ As ComponentOccurrence
    Dim oSheetMetalComp As Inventor.SheetMetalComponentDefinition
    Dim iBendCount As Integer
    Dim oCustomProps As Inventor.PropertySet
    Dim sBendPropName As String
    sBendPropName = "Bend Time"
    Dim oBendProp As Inventor.Property
    Dim oProp As Inventor.Property
    Dim PartDoc As PartDocument
 
    For Each Occ In Occurrances

          If Occ.DefinitionDocumentType = kAssemblyDocumentObject Then
            Call TraverseAssembly(Occ.SubOccurrences)
        ElseIf Occ.DefinitionDocumentType = kPartDocumentObject Then
            Set PartDoc = Occ.ReferencedDocumentDescriptor.ReferencedDocument
            If PartDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
                Set oSheetMetalComp = PartDoc.ComponentDefinition
                iBendTime = oSheetMetalComp.Bends.Count * 0.00556
                Set oCustomProps = PartDoc.PropertySets.Item("Inventor User Defined Properties")
               
                On Error Resume Next
                Set oBendProp = oCustomProps.Item(sBendPropName)
               
                If Err.Number <> 0 Then
                    'We don't have an iproperty, so we create it and set its value
                    Set oBendProp = oCustomProps.Add(iBendTime, sBendPropName)
                Else
                    oBendProp.Value = iBendTime
                End If
            End If
        End If

     Next

End Sub

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
Message 5 of 8

Anonymous
Not applicable

😄 Thank you!

It sends me the "this runs on on assembly documents only" every time - so I just changed that line to "done."

Works great with sub assemblies - I'm in business.

 

 

0 Likes
Message 6 of 8

Anonymous
Not applicable

Can someone update the code, its not working. Thank you

 

 

0 Likes
Message 7 of 8

bradeneuropeArthur
Mentor
Mentor

Hi,

 

Why it is not working for you?

What is the error message?

What inventor version are you using?

 

Regards,

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 8 of 8

Anonymous
Not applicable

Today I manage to run the code in MACRO and it will return error massage:

this runs on assembly documents only

 I am using it in assembly with a lot of sheet metal parts, and other parts, but never mind the message error.

Can you tell me where are the results?

In iProperties in custom section there is no iBendTime. I am intersting in just BEND COUNTS because I need to have the number of times the bend machine will be used. If its needed I can just put 1 instead of 0.00556 to get bend counts, but I cant find the final result.

0 Likes