- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
Solved! Go to Solution.