Counting bends?

Counting bends?

Anonymous
Not applicable
3,055 Views
10 Replies
Message 1 of 11

Counting bends?

Anonymous
Not applicable

Is there any VBA code that can count the number of bends used in a sheet metal part?

 

I would like to make a custom property of this, so it can be exported to Excel for input in our ERP system

 

Thanks!

0 Likes
Accepted solutions (1)
3,056 Views
10 Replies
Replies (10)
Message 2 of 11

alewer
Advocate
Advocate
Accepted solution

Try this:

 

Public Sub BendCountToIprop()
  'Get the part doc and sheet metal component defn.  No error handling:
  Dim oPartDocument As Inventor.PartDocument
  Set oPartDocument = ThisApplication.ActiveDocument
  Dim oSheetMetalComp As Inventor.SheetMetalComponentDefinition
  Set oSheetMetalComp = oPartDocument.ComponentDefinition
  
  'Get the bend count
  Dim iBendCount As Integer
  iBendCount = oSheetMetalComp.Bends.Count
  
  'Get the custom property set
  Dim oCustomProps As Inventor.PropertySet
  Set oCustomProps = oPartDocument.PropertySets.Item("Inventor User Defined Properties")
  
  'Define the name of the bend count iproperty name:
  Dim sBendPropName As String
  sBendPropName = "Bend Count"
  
  'See if we already have an iproperty for the bend count
  Dim oBendProp As Inventor.Property
  Dim oProp As Inventor.Property
  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 = iBendCount
      Exit Sub
    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(iBendCount, sBendPropName)
End Sub

 Let me know if it works.

0 Likes
Message 3 of 11

Anonymous
Not applicable
Public Sub BendCountToIprop()
  'Get the part doc and sheet metal component defn.  No error handling:
  Dim oApp As Inventor.Application
  Dim oDoc As Inventor.Document
  Set oApp = GetObject(, "Inventor.Application")
  If oApp.ActiveDocumentType = kDrawingDocumentObject Then
  Set oDoc = oApp.ActiveDocument
  Else: If TypeOf oApp.ActiveEditObject Is Sketch Then Exit Sub
  Set oDoc = oApp.ActiveEditObject
  End If
  Dim oSheetMetalComp As Inventor.SheetMetalComponentDefinition
  Set oSheetMetalComp = oDoc.ComponentDefinition
  
  'Get the bend count
  Dim iBendCount As Integer
  iBendCount = oSheetMetalComp.Bends.Count
  
  'Get the custom property set
  Dim oCustomProps As Inventor.PropertySet
  Set oCustomProps = oDoc.PropertySets.Item("Inventor User Defined Properties")
  
  'Define the name of the bend count iproperty name:
  Dim sBendPropName As String
  sBendPropName = "Bend Count"
  
  'See if we already have an iproperty for the bend count
  Dim oBendProp As Inventor.Property
  Dim oProp As Inventor.Property
  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 = iBendCount
      Exit Sub
    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(iBendCount, sBendPropName)
End Sub

 Works great! Thanks a lot!

 

I changed it a little, so i dont need to open each part as i usually do this from an assembly...

0 Likes
Message 4 of 11

Anonymous
Not applicable

There is one problem, if cut or hole cuts the bend line itll count it twice.

 

0 Likes
Message 5 of 11

alewer
Advocate
Advocate

@Anonymous wrote:

There is one problem, if cut or hole cuts the bend line itll count it twice.

 


I can't get this behavior.  With the attached part (SixBends.ipt / Inventor 2010) try to get the bend count two ways:  as is, with end of part above Extrusion1 and Hole1, then with the end of part rolled all the way down.  For both cases, I get six bends.

 

If you can upload a simple example file that shows this behavior I'd be curious, but unfortunately I'm still only on 2010...

0 Likes
Message 6 of 11

Anonymous
Not applicable

This only happens when the face is not connected after the bend, hard to explain...

 

Heres pic of what i mean, this part counts 7 bends.

0 Likes
Message 7 of 11

alewer
Advocate
Advocate

I see the issue:  on a long enough press brake, this would be formed with two bending operations.  But Inventor is counting a bend for each of the flange operations in the model.  You could incorporate some logic to remove from the count any colinear bends, but that will cause errors.  Open the attached file and walk with me...

 

To illustrate, let's look at three cases:

1-Real life bend count--how it is made in the shop

2-My code above (all Inventor bends)

3-Code that deducts colinear bend lines from Inventor's bend count

 

Look at the attached part.  For the three cases:

1-Three bends

2-Four bends

3-Two bends.  Two "real life" bending operations are required to accomplish what I created with Face4, but the bends are colinear!

 

I am sure it is possible to come up with some more elaborate code to better calculate the number of bends, but you're probably looking at a lot more code, and I doubt you will ever be able to program something that's right every time.

0 Likes
Message 8 of 11

Anonymous
Not applicable

I see what you mean...

 

we dont have a lot these "problem" parts so its not a big issue...

0 Likes
Message 9 of 11

k14348
Advocate
Advocate

I required the same type of macros for an assembly(If i run the Macros in an assembly i have to get the BendCount for all the parts). Is it possible? 

0 Likes
Message 10 of 11

Anonymous
Not applicable

Would anyone like to update this for 2014?

0 Likes
Message 11 of 11

Anonymous
Not applicable

Can anyone please update the code, cause its not working.

Thank you

0 Likes