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
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
Thank you very much, that is what I was looking for. If there's even a better way to do it, let me know!
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.