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 - Compile Error - Generating Holes

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
James_OBrien2
199 Views, 3 Replies

VBA - Compile Error - Generating Holes

Hello, 

 

I am wondering if anyone can help with the code below as I keep getting a compile error near the bottom of the code when it gets to Dim oHoleDef As HoleFeatureDefinition.   The error states Compile Error User Defined Type Not Defined.  The code itself is very basic creating a plate with 4 holes to be added and this is where my stumbling block is and I cant quite work out what is the fix.  Any help would be greatly appreciated.  I am doing this via code rather than just modelling it for demonstration purposes.

 

Sub CreatePart()

' Declare variables
Dim oApp As Application
Dim oPartDoc As PartDocument
Dim oPartCompDef As PartComponentDefinition
Dim oTransGeom As TransientGeometry
Dim oSketch As PlanarSketch
Dim oProfile As Profile
Dim oExtrudeDef As ExtrudeDefinition
Dim oExtrude As ExtrudeFeature
Dim oHolePlacementDef As SketchHolePlacementDefinition
Dim oHoleFeature As HoleFeature

' Set the Inventor application
Set oApp = ThisApplication

' Create a new part document
Set oPartDoc = oApp.Documents.Add(kPartDocumentObject, oApp.FileManager.GetTemplateFile(kPartDocumentObject))

' Set the component definition
Set oPartCompDef = oPartDoc.ComponentDefinition

' Set the transient geometry object
Set oTransGeom = oApp.TransientGeometry

' Create the first sketch on the XY plane
Set oSketch = oPartCompDef.Sketches.Add(oPartCompDef.WorkPlanes.Item(3))

' Draw the rectangle (450x300 mm)
With oSketch.SketchLines
.AddAsTwoPointRectangle oTransGeom.CreatePoint2d(0, 0), oTransGeom.CreatePoint2d(450, 300)
End With

' Create the profile for the extrusion
Set oProfile = oSketch.Profiles.AddForSolid

' Create the extrusion (20 mm thick)
Set oExtrudeDef = oPartCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kSymmetricExtentDirection)
oExtrudeDef.SetDistanceExtent 20, kMillimeterLengthUnits
Set oExtrude = oPartCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)

' Create a new sketch on the top face of the extruded rectangle
Set oSketch = oPartCompDef.Sketches.Add(oExtrude.Faces.Item(1))

' Add hole centers
Dim centerPoints As Variant
centerPoints = Array( _
oTransGeom.CreatePoint2d(100, 100), _
oTransGeom.CreatePoint2d(350, 100), _
oTransGeom.CreatePoint2d(100, 200), _
oTransGeom.CreatePoint2d(350, 200))

Dim i As Integer
For i = LBound(centerPoints) To UBound(centerPoints)
oSketch.SketchPoints.Add(centerPoints(i))
Next i

' Create holes
For i = 1 To oSketch.SketchPoints.Count
' Create hole placement definition
Set oHolePlacementDef = oPartCompDef.Features.HoleFeatures.CreateSketchPlacementDefinition(oSketch.SketchPoints.Item(i))

' Create hole feature definition
Dim oHoleDef As HoleFeatureDefinition
Set oHoleDef = oPartCompDef.Features.HoleFeatures.CreateHoleFeatureDefinition(oHolePlacementDef)

' Set hole properties
oHoleDef.SetThroughAllExtent kNegativeExtentDirection
oHoleDef.Diameter.Value = 16 ' Set diameter to 16 mm

' Add the hole feature
Set oHoleFeature = oPartCompDef.Features.HoleFeatures.Add(oHoleDef)
Next i

' Save the part
oPartDoc.SaveAs "C:\Temp\Part1.ipt", False
End Sub

 

3 REPLIES 3
Message 2 of 4
WCrihfield
in reply to: James_OBrien2

Hi @James_OBrien2.  What version/year of Inventor are you using?  In Inventor 2024 there is no such thing as a 'HoleFeatureDefinition', and no such method as 'CreateHoleFeatureDefinition'.  They may have existed in an older version of Inventor though.  Take a look at the following online help documentation:

HoleFeatures 

HoleFeature 

HolePlacementDefinition 

HoleTapInfo 

HoleClearanceInfo 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4
WCrihfield
in reply to: James_OBrien2

Hi @James_OBrien2.  Give this slightly edited version of your code a try, and see if it works better for you.

 

Sub CreatePart()

' Declare variables
Dim oApp As Application
Dim oPartDoc As PartDocument
Dim oPartCompDef As PartComponentDefinition
Dim oTransGeom As TransientGeometry
Dim oSketch As PlanarSketch
Dim oProfile As Profile
Dim oExtrudeDef As ExtrudeDefinition
Dim oExtrude As ExtrudeFeature
Dim oHolePlacementDef As SketchHolePlacementDefinition
Dim oHoleFeature As HoleFeature

' Set the Inventor application
Set oApp = ThisApplication

' Create a new part document
Set oPartDoc = oApp.Documents.Add(kPartDocumentObject, oApp.FileManager.GetTemplateFile(kPartDocumentObject))

' Set the component definition
Set oPartCompDef = oPartDoc.ComponentDefinition

' Set the transient geometry object
Set oTransGeom = oApp.TransientGeometry

' Create the first sketch on the XY plane
Set oSketch = oPartCompDef.Sketches.Add(oPartCompDef.WorkPlanes.Item(3))

' Draw the rectangle (450x300 mm)
With oSketch.SketchLines
.AddAsTwoPointRectangle oTransGeom.CreatePoint2d(0, 0), oTransGeom.CreatePoint2d(450, 300)
End With

' Create the profile for the extrusion
Set oProfile = oSketch.Profiles.AddForSolid

' Create the extrusion (20 mm thick)
Set oExtrudeDef = oPartCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kSymmetricExtentDirection)
oExtrudeDef.SetDistanceExtent 20, kMillimeterLengthUnits
Set oExtrude = oPartCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)

' Create a new sketch on the top face of the extruded rectangle
Set oSketch = oPartCompDef.Sketches.Add(oExtrude.Faces.Item(1))

' Add hole centers
Dim centerPoints As Variant
centerPoints = Array( _
oTransGeom.CreatePoint2d(100, 100), _
oTransGeom.CreatePoint2d(350, 100), _
oTransGeom.CreatePoint2d(100, 200), _
oTransGeom.CreatePoint2d(350, 200))

Dim oSketchPointsCollection As ObjectCollection
Set oSketchPointsCollection = ThisApplication.TransientObjects.CreateObjectCollection

Dim oSketchPoints As SketchPoints
Set oSketchPoints = oSketch.SketchPoints
Dim oSketchPoint As SketchPoint

Dim i As Integer
For i = LBound(centerPoints) To UBound(centerPoints)
    Set oSketchPoint = oSketchPoints.Add(centerPoints(i))
    Call oSketchPointsCollection.Add(oSketchPoint)
Next i

Dim oHoleFeats As HoleFeatures
Set oHoleFeats = oPartCompDef.Features.HoleFeatures

' Create hole placement definition
Set oHolePlacementDef = oHoleFeats.CreateSketchPlacementDefinition(oSketchPointsCollection)

' Add the hole feature
Set oHoleFeature = oHoleFeats.AddDrilledByThroughAllExtent(oHolePlacementDef, "16 mm", kNegativeExtentDirection)

' Save the part
oPartDoc.SaveAs "C:\Temp\Part1.ipt", False
End Sub

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 4

hi @James_OBrien2 , as you can see in @WCrihfield solution you must use ObjectCollection and pass it to CreateSketchPlacementDefinition method. That is very often approach.

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report