How many holes in a part?

How many holes in a part?

Anonymous
Not applicable
1,185 Views
10 Replies
Message 1 of 11

How many holes in a part?

Anonymous
Not applicable

I'm looking to find how many holes there are in a part ( and then what their diameters are).

I tried using

 

Dim oPartDoc As Document
Set oPartDoc = ThisApplication.ActiveDocument

Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition

MsgBox "there are " & oCompDef.Features.HoleFeatures.Count & " holes"

 

but this falls down if a hole feature created more than one hole at the same time.

 

I'm open to suggestions for some better code.

 

Thanks

0 Likes
1,186 Views
10 Replies
Replies (10)
Message 2 of 11

sanjay.ramaswamy
Alumni
Alumni

The HoleFeature.HoleCenterPoints property can be used to determine exactly how many holes a hole feature defines. However, this approach won't cover for cases where other features (such as an extruded cut) resulted in holes. If you want your approach to be a generic one, you'd be better off analyzing the part for "holes" rather than relying on feature counts. One method is to look for all interior cylindrical faces (i.e. hollow cylindrical faces). There's a sample in programming help (titled "Check if cylindrical face is interior or exterior") that demonstrates this. There are other topology-based feature recognition techniques as well that you can use to find holes in a part.

 

0 Likes
Message 3 of 11

Anonymous
Not applicable

Thanks for the guidance Sanjay

0 Likes
Message 4 of 11

Anonymous
Not applicable

Sorry Sanjay, but I need you to elaborate a bit more on how to use HoleCenterPoints.

I'm a bit stumped at the moment !

0 Likes
Message 5 of 11

sanjay.ramaswamy
Alumni
Alumni

The count of items in the collection returned by the HoleCenterPoints property indicates the number of holes. The collection contains (2d) SketchPoint objects for hole centers defined by sketch points, and (3d) Point objects for all other hole definitions.

 

0 Likes
Message 6 of 11

Anonymous
Not applicable

Sanjay,

I give up, could you post me a snippit of code showing how to get the HoleCenterPoints collection as while I understand what needs to be done, even reading the programming help files several times I can't achieve it  Man Sad

 

Maybe they should have sent me on the advanced training which covered macro's.  It was included in the package when we bought Inventor but subsequently forgotten about!

0 Likes
Message 7 of 11

sanjay.ramaswamy
Alumni
Alumni

If all you care about is the count of holes defined by hole features, try this...

 

Sub HoleCount()
   
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
   
    Dim oDef As PartComponentDefinition
    Set oDef = oDoc.ComponentDefinition
       
    Dim lHoleFeatureCount As Long
    lHoleFeatureCount = oDef.Features.HoleFeatures.Count
   
    Dim lHoleCount As Long
    lHoleCount = 0
   
    Dim oHoleFeature As HoleFeature
    For Each oHoleFeature In oDef.Features.HoleFeatures
        lHoleCount = lHoleCount + oHoleFeature.HoleCenterPoints.Count
    Next
   
    MsgBox "There are " & lHoleFeatureCount & " hole features and " & _
       lHoleCount & " holes in the part."
   
End Sub

 

 

0 Likes
Message 8 of 11

Anonymous
Not applicable

Thank you for the code Sanjay.

 

I have a little problem trying to learn for the future.  I have printed out the IV API Object Model for reference when looking for how I might code something.  Using this I can go Part Document - PartComponentDefinition - PartFeatures - HoleFeatures -  HoleFeature -      but at this point I get lost as I don't see HoleCenterPoints on the printout  ??

 

How should I proceed in future?

0 Likes
Message 9 of 11

sanjay.ramaswamy
Alumni
Alumni

I prefer to use the VBA object browser...

 

 

0 Likes
Message 10 of 11

Anonymous
Not applicable

Hy, although this is an old thread, i have a question.

 

I tested the code and it works well, but. In my part i have created holes, by copying a hole feature, or mirroring etc.

 

these features and holes are not counted. Can you tell me how to count (and evaluate) those holes?

 

Thanks Bodo

0 Likes
Message 11 of 11

perrysc
Enthusiast
Enthusiast

I know I'm necroposting but here's the solution.

 

Function _GetHoleCountAndDiameters(oPartDoc As PartDocument) As Double(,)
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	Dim oTO As TransientObjects = ThisApplication.TransientObjects
	Dim oPartDef As PartComponentDefinition = oPartDoc.ComponentDefinition
	Dim oSizes As New List(Of Double)()
	Dim oLines As ObjectCollection = oTO.CreateObjectCollection
	Dim oTestLine As Line
	Dim oIsExisting As Boolean
	For Each oFace As Face In oPartDef.SurfaceBodies.Item(1).Faces
		If oFace.SurfaceType = 5891 Then
			oTestLine = oTG.CreateLine(oFace.Geometry.BasePoint,oFace.Geometry.AxisVector.AsVector)
			oIsExisting = False
			For Each oLine As Line In oLines
				If oLine.IsColinearTo(oTestLine) Then oIsExisting = True			
			Next
			If Not oIsExisting Then
				oLines.Add(oTestLine)
				oSizes.Add(oFace.Geometry.Radius*2)
			End If
		End If
	Next
	Dim oUniqueSizeList As New List(Of Double)()
	For Each oSize As Double In oSizes
		If Not oUniqueSizeList.Contains(oSize) Then oUniqueSizeList.Add(oSize)	
	Next
	oUniqueSizeList.Sort
	Dim oHoles(oUniqueSizeList.Count-1,1) As Double
	For i As Integer = 0 To oUniqueSizeList.Count-1
		oHoles(i,0) = oUniqueSizeList.Item(i)
		oHoles(i,1) = 0
	Next
	For Each oSize As Double In oSizes
		oHoles(oUniqueSizeList.IndexOf(oSize),1) = oHoles(oUniqueSizeList.IndexOf(oSize),1) + 1
	Next
	Return oHoles
End Function