Sub main()
Dim oPrt As PartDocument
oPrt = ThisApplication.ActiveDocument
Dim oHole As HoleFeature
Dim oPdef As PartComponentDefinition
oPdef=oPrt.ComponentDefinition
Dim oEdge1 As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Linear Edge 1")
Dim oEdge2 As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Linear Edge 2")
Dim oPlane As Face =Nothing
If oEdge1 Is Nothing Or oEdge2 Is Nothing Then
MessageBox.Show("UnLess 2 edges Selected!Exit!", "Title")
Exit Sub
Else
Dim oLine As LineSegment = oEdge1.Geometry
For Each ofc As Face In oEdge1.Faces
If ofc.Edges.cast(Of Edge).Where(Function(oEd) oEd Is oEdge2).Count=1 Then
oPlane = ofc
Exit For
End If
Next
If oPlane Is Nothing Or oLine.IntersectWithCurve(oEdge2.Geometry).Count = 0 Then
MessageBox.Show("The edges are not on same face or coplanar,exit!", "Title")
Else
' MessageBox.Show("Successful selected,Be Create HoleFeature", "Title")
End If
End If
Dim oBiasPoint As Point
oBiasPoint=oPdef.MassProperties.CenterOfMass
Dim oLhdef As LinearHolePlacementDefinition
oLhdef = oPdef.Features.HoleFeatures.CreateLinearPlacementDefinition(oPlane, oEdge1, "1.5 cm", oEdge2, "1.5 cm", oBiasPoint)
oHole=oPdef.Features.HoleFeatures.AddDrilledByThroughAllExtent(oLhdef,"1 cm",PartFeatureExtentDirectionEnum.kPositiveExtentDirection)
End Sub
Try, and also refer to the sample "Hole feature linear placement".
Public Sub HoleFeatureLinearPlacement()
' Create a new part document, using the default part template.
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))
' Set a reference to the component definition.
Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition
' Create a new sketch on the X-Y work plane.
Dim oSketch As PlanarSketch
Set oSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes.Item(3))
' Set a reference to the transient geometry object.
Dim oTransGeom As TransientGeometry
Set oTransGeom = ThisApplication.TransientGeometry
' Create a square on the sketch.
Call oSketch.SketchLines.AddAsTwoPointRectangle( _
oTransGeom.CreatePoint2d(0, 0), _
oTransGeom.CreatePoint2d(6, 6))
' Create the profile.
Dim oProfile As Profile
Set oProfile = oSketch.Profiles.AddForSolid
' Create an extrusion.
Dim oExtrudeDef As ExtrudeDefinition
Set oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kJoinOperation)
Call oExtrudeDef.SetDistanceExtent("2 cm", kNegativeExtentDirection)
Dim oExtrude As ExtrudeFeature
Set oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
' Get the start face of the extrude.
Dim oFace As Face
Set oFace = oExtrude.StartFaces(1)
' Get two adjacent edges on the start face.
Dim oEdge1, oEdge2 As Edge
Set oEdge1 = oFace.Edges(1)
Set oEdge2 = oFace.Edges(2)
' Create a bias point for hole placement to place it at
' the expected location. This is the model point
' corresponding to the center of the square in the sketch.
Dim oBiasPoint As Point
Set oBiasPoint = oSketch.SketchToModelSpace(oTransGeom.CreatePoint2d(1.5, 1.5))
' Create the hole feature placement definition.
Dim oLinearPlacementDef As LinearHolePlacementDefinition
Set oLinearPlacementDef = oCompDef.Features.HoleFeatures.CreateLinearPlacementDefinition _
(oFace, oEdge1, "2 cm", oEdge2, "2 cm", oBiasPoint)
' Create the hole feature.
Call oCompDef.Features.HoleFeatures.AddDrilledByThroughAllExtent( _
oLinearPlacementDef, "1 cm", kPositiveExtentDirection)
End Sub