iLogic - Determine size and position of the nth element in a hole pattern

iLogic - Determine size and position of the nth element in a hole pattern

Anonymous
Not applicable
1,621 Views
7 Replies
Message 1 of 8

iLogic - Determine size and position of the nth element in a hole pattern

Anonymous
Not applicable

This is a repost from the Inventor forum to the Inventor Customization forum.

 

Problem: Given a hole pattern (rectangular, circular, or mirror) containing different sizes of holes, determine the corresponding hole feature for every element in the pattern.

 

Background:

I am using Inventor Professional 2019. I am trying to catalog all of the holes in a part by their size and center point coordinates, and of course I have to handle patterns. There is already a solution to count holes in patterns and determining the size of holes in a homogeneous pattern (only one hole feature in the pattern) is trivial. However, I cannot figure out how to determine the size of a hole in a pattern when it is made up of multiple different hole features (non-homogeneous).

 

Discussion:

The problem with the sample code below for non-homogeneous patterns is in the following snippet:

Dim hole as HoleFeature = mirror.ParentFeatures.Item(1)

The index on the Item method (1 in this case) is what selects the hole feature that was patterned. For a hole pattern with two different hole features this index can be 1 or 2.  The sample code below works fine for a homogeneous pattern because the hole variable is always the same. However, for a non-homogeneous pattern, that variable must change or else the code thinks the pattern is homogeneous. What is the relationship between the Item method's index value and the current element? From the API there doesn't appear to be any code that relates the two.

 

Sample Code for Homogeneous Pattern:

For Each mirror As MirrorFeature In part.Definition.Features.MirrorFeatures
	If Not mirror.Suppressed Then
		If (TypeOf mirror.ParentFeatures.Item(1) Is HoleFeature) Then
                        Dim hole as HoleFeature = mirror.ParentFeatures.Item(1)
			For j = 1 to mirror.PatternElements.count
				dim element as FeaturePatternElement
				element = mirror.PatternElements.item(j)
				If Not element.Suppressed Then
					' Get hole position
                                        ' Get hole size
				End If
			next j
		End If
	End If
Next mirror

Example Part:

Here is an example of the type of pattern I am trying to analyze. In the end I need the following information about this pattern

"There are 6 hole features in this pattern. Three of them are .25 in. diameter and their center points are x1,y1,z1...x3,y3,z3. The other three are .75 in. diameter and their center points are x1,y1,z1...x3,y3,z3."Capture.PNG

 

 

 

 

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

philip1009
Advisor
Advisor

Out of curiosity, do you just want to get a table telling you the coordinates of the holes and their size?  Do you need this info for a drawing or something else?

0 Likes
Message 3 of 8

philip1009
Advisor
Advisor

Okay, my original idea was to make a Hole Table in each drawing, but this is for error checking a whole assembly at once.  It looks like your final loop is going through each FeaturePatternElement which doesn't result in the features you want to check.  In the FeaturePatternElement Object is a property called ResultFeatures which I believe returns the collection of features created in the FeaturePatternElement.  I think for each Pattern Element you have to peruse the ResultFeatures, check if the object type is a hole feature, then you can gather the rest of your info from there.  I'm not 100% percent on this path though, I'll keep looking in the meantime.

0 Likes
Message 4 of 8

Anonymous
Not applicable

That was my thought too! But the code:

FeaturePatternElement.ResultFeatures

results in a null object everytime for me.

0 Likes
Message 5 of 8

Anonymous
Not applicable

I can't edit my previous post for some reason, but the null object code should be this:

FeaturePatternElement.ResultFeatures.Count

yields:

Object reference not set to an instance of an object.
0 Likes
Message 6 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@Anonymous

 


Here is an example of the type of pattern I am trying to analyze. In the end I need the following information about this pattern

"There are 6 hole features in this pattern. Three of them are .25 in. diameter and their center points are x1,y1,z1...x3,y3,z3. The other three are .75 in. diameter and their center points are x1,y1,z1...x3,y3,z3."Capture.PNG

 

 

 

 


To get size and position for above example part, try below iLogic code.

Sub Main()
    Dim oDoc As PartDocument
    oDoc = ThisApplication.ActiveDocument
    
    Dim oDef As PartComponentDefinition
    oDef = oDoc.ComponentDefinition
    
    Dim oPatternFeature As RectangularPatternFeature
    oPatternFeature = oDef.Features.RectangularPatternFeatures.Item(1)
    
    Dim i As Integer
    
    
    Dim oParantFeature As HoleFeature
    For Each oParantFeature In oPatternFeature.ParentFeatures
        i = i + 1
        Dim oSize As Double
        Dim x1 As Double
        Dim y1 As Double
        Dim z1 As Double
        Dim j As Integer
        Dim cnt As Integer
        Dim dist As Double
        Dim oFeatdim As FeatureDimension
        For Each oFeatdim In oPatternFeature.FeatureDimensions
            If oFeatdim.DimensionType = kRectangularCountFeatureDimension Then
                cnt = oFeatdim.Parameter.ModelValue
            ElseIf oFeatdim.DimensionType = kLinearFeatureDimension Then
                dist = oFeatdim.Parameter.ModelValue
            End If
        Next
        
        MessageBox.Show ("Size of Hole" & i & " : " & oParantFeature.HoleDiameter.Expression)
        MessageBox.Show ("Position of Hole " & i & " are :- X1 : " & oParantFeature.HoleCenterPoints.Item(1).Geometry3d.X & " Y1 : " & oParantFeature.HoleCenterPoints.Item(1).Geometry3d.Y & " Z1 : " & oParantFeature.HoleCenterPoints.Item(1).Geometry3d.Z)
        x1 = oParantFeature.HoleCenterPoints.Item(1).Geometry3d.X
        y1 = oParantFeature.HoleCenterPoints.Item(1).Geometry3d.Y
        z1 = oParantFeature.HoleCenterPoints.Item(1).Geometry3d.Z
         
        For j = 2 To cnt
            
            If oPatternFeature.XDirectionEntity.Geometry.Direction.X = -1 Then
                x1 = x1 - dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)
            ElseIf oPatternFeature.XDirectionEntity.Geometry.Direction.X = 1 Then
                x1 = x1 + dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)
            ElseIf oPatternFeature.XDirectionEntity.Geometry.Direction.Y = 1 Then
                y1 = y1 + dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)
            ElseIf oPatternFeature.XDirectionEntity.Geometry.Direction.Y = -1 Then
                y1 = y1 - dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)
            ElseIf oPatternFeature.XDirectionEntity.Geometry.Direction.Y = 1 Then
                z1 = z1 + dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)
            ElseIf oPatternFeature.XDirectionEntity.Geometry.Direction.Y = -1 Then
                z1 = z1 - dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)
            End If
        Next
        
    Next
    
End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 7 of 8

Anonymous
Not applicable

Hi Chandra,

 

Thanks for the reply! This is the code I needed! Although, I had to change the axes on the inner loop (j-loop). And then I got the output I wanted. I don't know if this code assumes that the pattern is on the XY plane? The part in the screenshot was drawn on the XZ plane. Is there a way to know which axes the pattern is aligned to?

 

Had to change code to work with XZ plane. Did this for every direction.

oPatternFeature.XDirectionEntity.Geometry.Direction.X = -1

changed to:

oPatternFeature.XDirectionEntity.Geometry.Direction.Z = -1

 

0 Likes
Message 8 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

Yes, j - loop has typo mistake while copying the code. Try below changes inside j-loop to handle XZ plane pattern.

If oPatternFeature.XDirectionEntity.Geometry.Direction.X = -1 Then
                x1 = x1 - dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)
            ElseIf oPatternFeature.XDirectionEntity.Geometry.Direction.X = 1 Then
                x1 = x1 + dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)
            ElseIf oPatternFeature.XDirectionEntity.Geometry.Direction.Y = 1 Then
                y1 = y1 + dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)
            ElseIf oPatternFeature.XDirectionEntity.Geometry.Direction.Y = -1 Then
                y1 = y1 - dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)
            ElseIf oPatternFeature.XDirectionEntity.Geometry.Direction.Z = 1 Then
                z1 = z1 + dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)
            ElseIf oPatternFeature.XDirectionEntity.Geometry.Direction.Z = -1 Then
                z1 = z1 - dist
                MessageBox.Show ("Position of Hole " & i & " are :- X" & j & " : " & x1 & " Y" & j & " : " & y1 & " Z" & j & " : " & z1)

 

Note: Line or Edge which is selected for pattern direction should be straight. If it is inclined, code may not give proper result.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes