Recognize holes

Recognize holes

GeorgK
Advisor Advisor
1,814 Views
19 Replies
Message 1 of 20

Recognize holes

GeorgK
Advisor
Advisor

Hello together,

 

sometimes I use the add-in feature recognition.

 

https://apps.autodesk.com/PDSU/en/Detail/Index?id=9172877436288348979&appLang=en&os=Win32_64

 

The feature recognition recognize each hole as a separate hole. Is it possible to find all holes from the same type and replace them with a pattern?

How could I get the positions of the holes? Or is it possible to move all holes of the same type to one sketch?

 

Thanks Georg

 

0 Likes
1,815 Views
19 Replies
Replies (19)
Message 2 of 20

GeorgK
Advisor
Advisor
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oHoleFeat As HoleFeature

For Each oHoleFeat In oDoc.ComponentDefinition.Features.HoleFeatures
    Debug.Print ("X:" & oHoleFeat.HoleCenterPoints.Item(1).X)
    Debug.Print ("Y:" & oHoleFeat.HoleCenterPoints.Item(1).Y)
    Debug.Print ("Z:" & oHoleFeat.HoleCenterPoints.Item(1).Z)
    Debug.Print ("Durchmesser:" & oHoleFeat.HoleDiameter.Expression)
    Debug.Print ("Bohrungstiefe:" & oHoleFeat.Extent.Distance.Expression)
Next

'#################
Sub addHole() 
    Dim oDoc As PartDocument 
    Set oDoc = ThisDocument 
    Dim oCD As PartComponentDefinition 
    Set oCD = oDoc.ComponentDefinition 
    
    Dim oHF As HoleFeature 
    Set oHF = oCD.Features.HoleFeatures.Item(1) 
    
    Dim oSK As PlanarSketch 
    Set oSK = oHF.Sketch 
    Call oSK.Edit 
    Dim oPt As Inventor.Point2d 
    Set oPt = ThisApplication.TransientGeometry.CreatePoint2d(3, 1) 
    Call oSK.SketchPoints.Add(oPt, True) 
    Call oSK.ExitEdit 
    
    Dim oNewObjColl As ObjectCollection 
    Set oNewObjColl = ThisApplication.TransientObjects.CreateObjectCollection 
  
    Call oNewObjColl.Add(oCD.Sketches(2).SketchPoints(1)) ' Existing hole centre 
    Call oNewObjColl.Add(oCD.Sketches(2).SketchPoints(2)) ' New hole centre 
    oHF.HoleCenterPoints = oNewObjColl 
End Sub 
0 Likes
Message 3 of 20

sajith_subramanian
Autodesk Support
Autodesk Support

Hi Georg,

 

I understand the hole is sketch based. Hence, you would need to access the X, Y, Z coordinates using the Geometry API  in the for loop

 

So instead of

             Debug.Print ("X:" & oHoleFeat.HoleCenterPoints.Item(1).X)

 

you could use

            Debug.Print ("X:" & oHoleFeat.HoleCenterPoints.Item(2).Geometry.X)

 

 

Also, in the addHole() subroutine you could change

           Call oNewObjColl.Add(oCD.Sketches(2).SketchPoints(1)) ' Existing hole centre 

 

to use the reference sketch of the hole.

            Call oNewObjColl.Add(oSK.SketchPoints(1))

 

 

Please let me know if this helps.

 

Regards,

Sajith

 


Sajith Subramanian
Autodesk Developer Network
0 Likes
Message 4 of 20

GeorgK
Advisor
Advisor

Hello Sajith,

 

the first problem is to get the position of each hole. The second problem is to compare the different holes and move them to the right sketch.

 

Could you help me with the comparison of the holes?

 

Thanks

 

Georg

0 Likes
Message 5 of 20

sajith_subramanian
Autodesk Support
Autodesk Support

Hi Georg,

 

 

To compare the type of holes you could use the Holetype api.

 

A simple way would be to filter out the type in the for loop itself.

 

For Each oHoleFeat In oDoc.ComponentDefinition.Features.HoleFeatures

    

If oHoleFeat.HoleType = HoleTypeEnum.kDrilledHole Then     'Filter out the drilled holes

 

    Debug.Print ("X:" & oHoleFeat.HoleCenterPoints.Item(1).X)

    Debug.Print ("Y:" & oHoleFeat.HoleCenterPoints.Item(1).Y)

    Debug.Print ("Z:" & oHoleFeat.HoleCenterPoints.Item(1).Z)

    Debug.Print ("Durchmesser:" & oHoleFeat.HoleDiameter.Expression)

    Debug.Print ("Bohrungstiefe:" & oHoleFeat.Extent.Distance.Expression)

 

End if

Next

 

Similarly you can filter out the various types by referring below.

 

kCounterBoreHole

:

Hole is counterbored.

kCounterSinkHole   :

 

Hole is countersunk.

kDrilledHole            :

 

Hole is drilled (no countersink or counterboring).

kSpotFaceHole         :

 

Hole is spotfaced.

 

 

Regards,

Sajith


Sajith Subramanian
Autodesk Developer Network
0 Likes
Message 6 of 20

GeorgK
Advisor
Advisor

Hello Sajith,

 

thanks for the sample code. But how could I recognize if the holes could be patternd?

 

Georg

 

 

0 Likes
Message 7 of 20

sajith_subramanian
Autodesk Support
Autodesk Support

Hi Georg,

 

In Inventor 2017, there is an api to create a pattern from a sketch.

 

Once you have your points moved to the sketch, you could use the sketch and the hole feature that needs to be patterned, to create a sketch based pattern.

 

In this case your code would be something similar to the following.

 

 

 Sub addHole()


    Dim oDoc As PartDocument
    Set oDoc = ThisDocument
    Dim oCD As PartComponentDefinition
    Set oCD = oDoc.ComponentDefinition
   
    Dim oHF As HoleFeature
    Set oHF = oCD.Features.HoleFeatures.Item(1)
   
    Dim oSK As PlanarSketch
    Set oSK = oHF.Sketch
    Call oSK.Edit
    Dim oPt As Inventor.Point2d
    Set oPt = ThisApplication.TransientGeometry.CreatePoint2d(3 ,1 ) 
    Call oSK.SketchPoints.Add(oPt, True)
    Call oSK.ExitEdit
   
    Dim oNewObjColl As ObjectCollection
    Set oNewObjColl = ThisApplication.TransientObjects.CreateObjectCollection
 
    Call oNewObjColl.Add(oSK.SketchPoints(1)) ' Existing hole centre
    Call oNewObjColl.Add(oSK.SketchPoints(2)) ' New hole centre
    oHF.HoleCenterPoints = oNewObjColl
   

    '## Add  the hole feature to an object collection
    Dim oNewHolesColl As ObjectCollection
    Set oNewHolesColl = ThisApplication.TransientObjects.CreateObjectCollection
    Call oNewHolesColl.Add(oHF)
   

  '## Pass the sketch and hole feature as input to create a  pattern. 
    Dim defn As SketchDrivenPatternDefinition
    Call oCD.Features.SketchDrivenPatternFeatures.Add(oCD.Features.SketchDrivenPatternFeatures.CreateDefinition(oNewHolesColl, oSK))

 

 

Regards,

Sajith


Sajith Subramanian
Autodesk Developer Network
0 Likes
Message 8 of 20

GeorgK
Advisor
Advisor

Hello Sajith,

 

I am searching for an easy way to get all points from the same holes to one sketch. Do you know a way to do this?

 

Thanks

 

Georg

0 Likes
Message 9 of 20

sajith_subramanian
Autodesk Support
Autodesk Support

Hello Georg,

 

To get all the hole points from same type of holes to one sketch, you could try the following code which gets the hole centre points and adds them to a new sketch.

 

  Dim xcoord As Double
  Dim ycoord As Double
  Dim oPt As Inventor.Point2d
 

 ' Create a new sketch on the XY plane.
   Dim oNewSketch As PlanarSketch
   Set oNewSketch = oCD.Sketches.Add(oCD.WorkPlanes.Item(3))

 

' Put the sketch in edit mode.
    oNewSketch.Edit


For Each oHoleFeat In oDoc.ComponentDefinition.Features.HoleFeatures
 If oHoleFeat.HoleType = HoleTypeEnum.kDrilledHole Then
     
    xcoord = oHoleFeat.HoleCenterPoints.Item(1).Geometry.X
    ycoord = oHoleFeat.HoleCenterPoints.Item(1).Geometry.Y
    Set oPt = ThisApplication.TransientGeometry.CreatePoint2d(xcoord, ycoord)
    Call oNewSketch.SketchPoints.Add(oPt, True)


 End If   


Next


oNewSketch.ExitEdit

 


Sajith Subramanian
Autodesk Developer Network
0 Likes
Message 10 of 20

GeorgK
Advisor
Advisor

Hello Sajith,

 

thank you very much.

 

Georg

0 Likes
Message 11 of 20

GeorgK
Advisor
Advisor

Hello Sajith,

 

with this code the orientation of the holes changes if the holes are not on the XY-plane:

 

 ' Create a new sketch on the XY plane.
   Dim oNewSketch As PlanarSketch
   Set oNewSketch = oCD.Sketches.Add(oCD.WorkPlanes.Item(3))

 

How could I keep it?

 

Thanks

 

Georg

0 Likes
Message 12 of 20

GeorgK
Advisor
Advisor

Hello Sajith,

 

how could I replace a hole with another hole-feature (save the position, delete the hole and replace it with another one)?

 

Thanks

 

Georg

0 Likes
Message 13 of 20

GeorgK
Advisor
Advisor
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button65.Click
Dim m_inventorApp As Inventor.Application = Nothing

' Try to get an active instance of Inventor
Try
m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
Catch ex As Exception
MsgBox(ex.Message)
End Try


' Create a transaction.
Dim oTransMgr As TransactionManager
oTransMgr = m_inventorApp.TransactionManager
Dim oTrans As Transaction
oTrans = oTransMgr.StartTransaction(m_inventorApp.ActiveDocument, "Replace hole")

Dim oSketch As PlanarSketch = Nothing
Dim oDoc As PartDocument
oDoc = m_inventorApp.ActiveDocument

' Set a reference to the component definition.
Dim oCompDef As PartComponentDefinition
oCompDef = oDoc.ComponentDefinition

Dim oHoles As HoleFeatures
oHoles = oDoc.ComponentDefinition.Features.HoleFeatures
Dim oHole As HoleFeature

For Each oHole In oHoles

oDoc.SelectSet.Clear()
oDoc.SelectSet.Select(oHole)

Dim oHoleCenter As Point
Dim oSketchPoint As SketchPoint

Dim oCollection As ObjectCollection
oCollection = m_inventorApp.TransientObjects.CreateObjectCollection

If TypeOf oHole.HoleCenterPoints.Item(1) Is SketchPoint Then
' Get the sketch point.
oSketchPoint = oHole.HoleCenterPoints.Item(1)

' Get the position of the sketch point in model space.
oSketch = oSketchPoint.Parent
oHoleCenter = oSketch.SketchToModelSpace(oSketchPoint.Geometry)


'Dim oSketchpoint As SketchPoint
For Each oSketchPoint In oHole.Sketch.SketchPoints
If oSketchPoint.HoleCenter Then
Call oCollection.Add(oSketchPoint)
End If
Next

'Löschen
oHole.Delete(True)
Call oCompDef.Features.HoleFeatures.AddDrilledByThroughAllExtent(oCollection, "10 mm", PartFeatureExtentDirectionEnum.kPositiveExtentDirection)

oDoc.Update()
Else

oHoleCenter = oHole.HoleCenterPoints.Item(1)
MsgBox(oHoleCenter.X & "; " & oHoleCenter.Y & "; " & oHoleCenter.Z)

' How to replace the hole without sketch?

End If

Next

oTrans.End()
End Sub

How could I replace the holes without sketch?

0 Likes
Message 14 of 20

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi George,

 

Please find the sample code to change the hole without sketch

 

Public Sub ModifY_Hole()
        Dim m_inventorApp As Inventor.Application = Nothing

        ' Try to get an active instance of Inventor
        Try
            m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


        ' Create a transaction.
        Dim oTransMgr As TransactionManager
        oTransMgr = m_inventorApp.TransactionManager
        Dim oTrans As Transaction
        oTrans = oTransMgr.StartTransaction(m_inventorApp.ActiveDocument, "Replace hole")

        Dim oSketch As PlanarSketch = Nothing
        Dim oDoc As PartDocument
        oDoc = m_inventorApp.ActiveDocument

        ' Set a reference to the component definition.
        Dim oCompDef As PartComponentDefinition
        oCompDef = oDoc.ComponentDefinition

        Dim oCollection As ObjectCollection
        oCollection = m_inventorApp.TransientObjects.CreateObjectCollection

        Dim ohole As HoleFeature
        ohole = m_inventorApp.CommandManager.Pick(SelectionFilterEnum.kPartFeatureFilter, "Select hole feature")

        ohole.SetCBore("10 mm", "2.5 mm")

        oTrans.End()
    End Sub

 

Please feel free to contact if there is any doubt.


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 15 of 20

GeorgK
Advisor
Advisor

Hello Chandra,

 

thank you very much for the code. But the holes could not be changed in all cases. There is another post with the same problem:

 

https://forums.autodesk.com/t5/inventor-customization/copy-hole-settings/m-p/6935986/highlight/false...

 

I think there is a bug in the API because I can reproduce the errors.

 

Georg

0 Likes
Message 16 of 20

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi George,

 

My example works for PartDocument. Meanwhile, it is very switch to AssemblyDocument. I gone through the link provided in the post. There is no such example is explained.

 

Can you please provide example with situation? I will look into it.


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 17 of 20

GeorgK
Advisor
Advisor

Hello Chandra,

 

the first idea was to copy the hole settings from one hole to another one. But this does not work very stable at all. Therefore I tried to get the position of the origin hole, delete it, and put a new one at the origin position. But this does not work either very stable.

 

https://forums.autodesk.com/autodesk/attachments/autodesk/120/70598/1/Platte_Bohrungen00_2017.ipt

 

Maybe you could find a stable solution that works in part and assembly environment.

 

Your code works for some holes, but not for all. For one hole it works and for the next one it does not work. The type of holes are equal.

 

Thank you

 

Georg

0 Likes
Message 18 of 20

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi George,

 

There are few holes which is having "LinearHolePlacementDefinition" fails to convert other hole types. Which throws an exception "The parameter is incorrect". It may be due to references of "LinearHolePlacemntDefinition" as shown in the following image. Other than, many hole types are able to convert hole types.

 

LinearDefinition.JPG


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 19 of 20

GeorgK
Advisor
Advisor

Hello Chandra,

 

Wayne Brill did  some modifications to solve the problem. But the code does not work for all hole types and directions.

 

https://forums.autodesk.com/t5/inventor-customization/copy-hole-settings/m-p/6918816/highlight/false...

 

Thank you Georg

0 Likes
Message 20 of 20

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi George,

 

Please see go through the following sample code. It adds a drilled hole feature using "LinearHolePlacementDefinition"

 

    Dim oLinearPlacementDef As LinearHolePlacementDefinition
    Set oLinearPlacementDef = oCompDef.Features.HoleFeatures.CreateLinearPlacementDefinition _
    (oFace, oEdge1, "2 cm", oEdge2, "2 cm", oBiasPoint)
    
    ' Create the hole feature.
    Call oCompDef.Features.HoleFeatures.AddDrilledByThroughAllExtent( _
                            oLinearPlacementDef, "1 cm", kPositiveExtentDirection)

For definition of "LinearHolePlacementDefinition" requires more parameters like face, edges and base point. So, unable to convert these kind of holefeature.

 

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes