Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VBA-Selecting a sketch for a hole feature

3 REPLIES 3
Reply
Message 1 of 4
Anonymous
988 Views, 3 Replies

VBA-Selecting a sketch for a hole feature

I have created a pretty in-depth macro that mirrors the current hole feature in Inventor but also allows the use to enter tolerancing.  I was hoping someone has some tips on how to select the sketch to place the hole feature on.   The sketch will already have points on it for the hole centers and all other code is working great. 

 

-Using Inventor 2010 (and soon updgrading to 2011)

 

Currently I am doing it like this (which works but is not optimal) and the line in question is marked with a comment and three asterisks (***)::

 

Private Sub CreateHoleByThroughAllExtent()
    'Setup Necessary definitions and objects
    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oDoc.ComponentDefinition
    
    Dim oSketches As PlanarSketches
    Set oSketches = oCompDef.Sketches
    
    Dim oSketch As PlanarSketch
    Set oSketch = oSketches.Item(oSketches.Count)  '***This line is the one that should be changed, want to select most    

                                                                                    '***recent unused sketch, not just the most recent
    
    Dim oHoleCenters As ObjectCollection
    Set oHoleCenters = ThisApplication.TransientObjects.CreateObjectCollection
    
    'Find all points on the sketch to use as centerpoints
    Dim oSP As Integer
    For oSP = 1 To oSketch.SketchPoints.Count
        If oSketch.SketchPoints.Item(oSP).HoleCenter Then
            Call oHoleCenters.Add(oSketch.SketchPoints.Item(oSP))
        End If
    Next oSP
    
    'create placement object from centerpoints
    Dim oLinearPlacementDef As SketchHolePlacementDefinition
    Set oLinearPlacementDef = oCompDef.Features.HoleFeatures.CreateSketchPlacementDefinition(oHoleCenters)
    
    'Create hole feature
    Call oCompDef.Features.HoleFeatures.AddDrilledByThroughAllExtent(oLinearPlacementDef, dDiameter, kExtentDirection)
    
    'Get hole feature just created
    Dim oHole As HoleFeature
    Set oHole = oCompDef.Features.HoleFeatures.Item(oCompDef.Features.HoleFeatures.Count) ' need most recent index
        
    'Get the parameter controlling its diameter
    Dim oDiamParam As Parameter
    Set oDiamParam = oHole.HoleDiameter
    
    'Set the tolerance
    If (sToleranceType = "Deviation") Then
        Call oDiamParam.Tolerance.SetToDeviation(dUpperTol, dLowerTol)
    End If
    
    oDoc.Rebuild

End Sub

3 REPLIES 3
Message 2 of 4
sanjay.ramaswamy
in reply to: Anonymous

Replace those two lines with the following. But note that if the user reorders sketches in the browser, the most recent sketch may not be the same as the bottom most sketch in the model browser.

 

    Dim lSketchcount As Long
    lSketchcount = oSketches.count
    
    Dim oSketch As PlanarSketch
    Dim i As Long
    For i = lSketchcount To 1 Step -1
        Set oSketch = oSketches.Item(i)
        If Not oSketch.Consumed Then
            Exit For
        End If
    Next

 

Message 3 of 4
Anonymous
in reply to: sanjay.ramaswamy

Thank you very much, that is what I was looking for.  If there's even a better way to do it, let me know!

Message 4 of 4
Anonymous
in reply to: Anonymous

Actually, another situation, what if a sketch is already open for edit and the code is run.  Is there a way to select the open sketch, close it, and then perform the hole feature?

 

-Also I'd like to make it work on assemblies too if anyone knows what I need to change to give it dual functionality.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report