VBA Insert holess from existing sketch points

VBA Insert holess from existing sketch points

pball
Mentor Mentor
858 Views
3 Replies
Message 1 of 4

VBA Insert holess from existing sketch points

pball
Mentor
Mentor

I'm looking to insert multiple holes with counterbores using sketch points on an existing sketch. I'd use an ifeature but I'm not aware of a way to use sketch points outside of a sheet metal tool with a punch style ifeature. I haven't done much VBA scripting for creating or editing parts so I'm pretty clueless with this. I started with the "Hole Feature - Through holes (RegularAndTapped)" sample from the api help, but that creates a part from scratch and I can't figure out how to get the hole command to use sketch points from an existing sketch. I'd appreciate the help figuring this out.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Accepted solutions (1)
859 Views
3 Replies
Replies (3)
Message 2 of 4

pball
Mentor
Mentor
Still looking for help with this.
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 3 of 4

ekinsb
Alumni
Alumni
Accepted solution

I've modified the sample that you referred to so that it now creates holes using an existing sketch.  It prompts the user to select an existing sketch, which you need to select in the browser.

 

Public Sub HoleSample()
    ' Check that a part is active and get it.
    Dim oPartDoc As PartDocument
    If ThisApplication.ActiveDocumentType = kPartDocumentObject Then
        Set oPartDoc = ThisApplication.ActiveDocument
    Else
        MsgBox "A part must be active."
        Exit Sub
    End If
                    
    ' Set a reference to the component definition.
    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oPartDoc.ComponentDefinition
    
    ' Have the user select a sketch.
    Dim oSketch As PlanarSketch
    Set oSketch = ThisApplication.CommandManager.Pick(kSketchObjectFilter, "Select a sketch with hole centers")
       
    ' Create an object collection for the hole center points.
    Dim oHoleCenters As ObjectCollection
    Set oHoleCenters = ThisApplication.TransientObjects.CreateObjectCollection
    
    ' Find the hole centers in the existing sketch and add them to the collection.
    Dim oPoint As SketchPoint
    For Each oPoint In oSketch.SketchPoints
        If oPoint.HoleCenter Then
            Call oHoleCenters.Add(oPoint)
        End If
    Next
    
    ' Create the hole feature.
    Call oCompDef.Features.HoleFeatures.AddDrilledByThroughAllExtent( _
                            oHoleCenters, "1 cm", kPositiveExtentDirection)
End Sub

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 4 of 4

pball
Mentor
Mentor
Thank you very much for the example code. It does exactly what I'm looking for.
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes