Create hole on a solid from a grouop of lines in layer

Hi All

I've managed to write the steps for a function that drills holes in solid based on the lines of a layer. It works one by one, but I have not been able to make it work in a cycle. Not sure why I manage to cycle through all the lines in the layer but I can only create one single feature.
You first have to select a solid, then enter a diameter, then a layer.
There are two functions that I used for testing below...

Could someone help me pinpoint where my mistake is so that I can create one hole per line?

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports System.Windows.Forms
Imports System.Collections.Generic

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim count as integer
Dim lw As ListingWindow = theSession.ListingWindow

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim nullFeatures_HolePackage As Features.HolePackage = Nothing
Dim holePackageBuilder1 As Features.HolePackageBuilder
holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(nullFeatures_HolePackage)
holePackageBuilder1.StartHoleData.BooleanOperation.Type = GeometricUtilities.BooleanOperation.BooleanType.Subtract
holePackageBuilder1.Tolerance = 0.0001
holePackageBuilder1.BooleanOperation.Type = GeometricUtilities.BooleanOperation.BooleanType.Subtract
holePackageBuilder1.HolePosition.SetAllowedEntityTypes(Section.AllowTypes.OnlyPoints)
holePackageBuilder1.HolePosition.AllowSelfIntersection(True)
holePackageBuilder1.HoleDepthLimitOption = Features.HolePackageBuilder.HoleDepthLimitOptions.ThroughBody
holePackageBuilder1.ProjectionDirection.ProjectDirectionMethod = GeometricUtilities.ProjectionOptions.DirectionType.Vector
'Layer of colling holes
Dim layernumber=NXInputBox.GetInputNumber("Enter the layer of the cooling holes: ")
'Diameter of the holes
Dim hole_dia=NXInputBox.GetInputNumber("Enter the diameter of the cooling holes: ")
holePackageBuilder1.GeneralHoleDiameter.RightHandSide = hole_dia
'Solid target
Dim targetBodies(0) As Body
Dim body1 As Body = SelectSolid()
'dim line_temp as line = Selectline()
targetBodies(0) = body1
holePackageBuilder1.BooleanOperation.SetTargetBodies(targetBodies)
lw.Open()
Dim cooling_holes() As DisplayableObject = theSession.Parts.Work.Lines.ToArray()
For Each temp_line As Line In cooling_holes
If temp_line.Layer = layernumber Then
'Line Selection
Dim line1 As Line = temp_line
Dim direction2 As Direction
direction2 = workPart.Directions.CreateDirection(temp_line, Sense.Reverse, SmartObject.UpdateOption.WithinModeling)
'hole dir
holePackageBuilder1.ProjectionDirection.ProjectVector = direction2
Dim success1 As Boolean
success1 = direction2.ReverseDirection()
holePackageBuilder1.ProjectionDirection.ProjectVector = direction2
'location of the hole
Dim point1 As Point
point1 = workPart.Points.CreatePoint(temp_line.startpoint)
' Hole creation....
Dim nXObject1 As NXObject
Dim xform1 As Xform
xform1 = workPart.Xforms.CreateExtractXform(temp_line, SmartObject.UpdateOption.WithinModeling, False, nXObject1)
Dim points1(0) As Point
points1(0) = point1
Dim curveDumbRule1 As CurveDumbRule
curveDumbRule1 = workPart.ScRuleFactory.CreateRuleCurveDumbFromPoints(points1)
Dim rules1(0) As SelectionIntentRule
rules1(0) = curveDumbRule1
Dim nullNXObject As NXObject = Nothing
Dim helpPoint1 As Point3d = New Point3d(0.0, 0.0, 0.0)
holePackageBuilder1.HolePosition.AddToSection(rules1, nullNXObject, nullNXObject, nullNXObject, helpPoint1, Section.Mode.Create, False)
'undo
Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Hole")
'hole committed
Dim nXObject2 As NXObject
nXObject2 = holePackageBuilder1.Commit()

theSession.SetUndoMarkName(markId1, "Hole")
Dim expression1 As Expression = holePackageBuilder1.GeneralTipAngle
Dim expression2 As Expression = holePackageBuilder1.GeneralHoleDiameter
Dim expression3 As Expression = holePackageBuilder1.GeneralHoleDepth
count =count +1
lw.WriteLine("#" & count.toString)
End If
Next
holePackageBuilder1.Destroy()
End Sub

Function SelectSolid() As Body

Dim ui As UI = ui.GetUI
Dim message As String = "Select solid body"
Dim title As String = "Selection"

Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim keepHighlighted As Boolean = False
Dim includeFeatures As Boolean = True

Dim selectionAction As Selection.SelectionAction = _
Selection.SelectionAction.ClearAndEnableSpecific

Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With

Dim selectedObject As NXObject = Nothing
Dim cursor As Point3d

ui.SelectionManager.SelectObject(message, title, scope, _
selectionAction, includeFeatures, _
keepHighlighted, selectionMask_array, _
selectedObject, cursor)

Dim solid As Body = CType(selectedObject, Body)

If solid Is Nothing Then
Return Nothing
End If

Return solid

End Function
Function SelectLine() As Line

Dim ui As UI = ui.GetUI
Dim message As String = "Select line"
Dim title As String = "Selection"

Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim keepHighlighted As Boolean = False
Dim includeFeatures As Boolean = True

Dim selectionAction As Selection.SelectionAction = _
Selection.SelectionAction.ClearAndEnableSpecific

Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_line_type
.Subtype = 0
.SolidBodySubtype = 0
End With

Dim selectedObject As NXObject = Nothing
Dim cursor As Point3d

ui.SelectionManager.SelectObject(message, title, scope, _
selectionAction, includeFeatures, _
keepHighlighted, selectionMask_array, _
selectedObject, cursor)

Dim line_temp As line = CType(selectedObject, line)

If line_temp Is Nothing Then
Return Nothing
End If

Return line_temp

End Function

End Module

The .Commit method of a builder object should only be called once when using the builder. One issue with your code is that a single hole package builder is created and the .Commit method is called multiple times in the loop. If you want to create multiple, individual hole features, create the builder object in the loop.

Some pseudocode:

For Each line in lines
create builder object
set properties
builder.Commit
Next

Thank you!
A simple chance and it works. ...

Hi I am working with something similar to this. Could you post your final code that worked please.

Paul Fillion

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports System.Windows.Forms
Imports System.Collections.Generic

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim count as integer
Dim lw As ListingWindow = theSession.ListingWindow

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim nullFeatures_HolePackage As Features.HolePackage = Nothing
Dim holePackageBuilder1 As Features.HolePackageBuilder
holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(nullFeatures_HolePackage)
holePackageBuilder1.StartHoleData.BooleanOperation.Type = GeometricUtilities.BooleanOperation.BooleanType.Subtract
holePackageBuilder1.Tolerance = 0.0001
holePackageBuilder1.BooleanOperation.Type = GeometricUtilities.BooleanOperation.BooleanType.Subtract
holePackageBuilder1.HolePosition.SetAllowedEntityTypes(Section.AllowTypes.OnlyPoints)
holePackageBuilder1.HolePosition.AllowSelfIntersection(True)
holePackageBuilder1.HoleDepthLimitOption = Features.HolePackageBuilder.HoleDepthLimitOptions.ThroughBody
holePackageBuilder1.ProjectionDirection.ProjectDirectionMethod = GeometricUtilities.ProjectionOptions.DirectionType.Vector
'Layer of colling holes
Dim layernumber = NXInputBox.GetInputNumber("Enter the layer of the cooling holes: ")
'Diameter of the holes
Dim hole_dia = NXInputBox.GetInputNumber("Enter the diameter of the cooling holes: ")
holePackageBuilder1.GeneralSimpleHoleDiameter.RightHandSide = hole_dia
'Solid target
Dim targetBodies(0) As Body
Dim body1 As Body = SelectSolid()
'dim line_temp as line = Selectline()
targetBodies(0) = body1
holePackageBuilder1.BooleanOperation.SetTargetBodies(targetBodies)
lw.Open()
Dim cooling_holes() As DisplayableObject = theSession.Parts.Work.Lines.ToArray()
For Each temp_line As Line In cooling_holes
If temp_line.Layer = layernumber Then
'Line Selection
Dim line1 As Line = temp_line
Dim direction2 As Direction
direction2 = workPart.Directions.CreateDirection(temp_line, Sense.Reverse, SmartObject.UpdateOption.WithinModeling)
'hole dir
holePackageBuilder1.ProjectionDirection.ProjectVector = direction2
Dim success1 As Boolean
success1 = direction2.ReverseDirection()
holePackageBuilder1.ProjectionDirection.ProjectVector = direction2
'location of the hole
Dim point1 As Point
point1 = workPart.Points.CreatePoint(temp_line.startpoint)
' Hole creation....
Dim nXObject1 As NXObject
Dim xform1 As Xform
xform1 = workPart.Xforms.CreateExtractXform(temp_line, SmartObject.UpdateOption.WithinModeling, False, nXObject1)
Dim points1(0) As Point
points1(0) = point1
Dim curveDumbRule1 As CurveDumbRule
curveDumbRule1 = workPart.ScRuleFactory.CreateRuleCurveDumbFromPoints(points1)
Dim rules1(0) As SelectionIntentRule
rules1(0) = curveDumbRule1
Dim nullNXObject As NXObject = Nothing
Dim helpPoint1 As Point3d = New Point3d(0.0, 0.0, 0.0)
holePackageBuilder1.HolePosition.AddToSection(rules1, nullNXObject, nullNXObject, nullNXObject, helpPoint1, Section.Mode.Create, False)
'undo
Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Hole")
'hole committed
Dim nXObject2 As NXObject
nXObject2 = holePackageBuilder1.Commit()

theSession.SetUndoMarkName(markId1, "Hole")
Dim expression1 As Expression = holePackageBuilder1.GeneralTipAngle
Dim expression2 As Expression = holePackageBuilder1.GeneralSimpleHoleDiameter
Dim expression3 As Expression = holePackageBuilder1.GeneralSimpleHoleDepth
count =count +1
lw.WriteLine("#" & count.toString)
End If
Next
holePackageBuilder1.Destroy()
End Sub

Function SelectSolid() As Body

Dim ui As UI = ui.GetUI
Dim message As String = "Select solid body"
Dim title As String = "Selection"

Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim keepHighlighted As Boolean = False
Dim includeFeatures As Boolean = True

Dim selectionAction As Selection.SelectionAction = _
Selection.SelectionAction.ClearAndEnableSpecific

Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With

Dim selectedObject As NXObject = Nothing
Dim cursor As Point3d

ui.SelectionManager.SelectObject(message, title, scope, _
selectionAction, includeFeatures, _
keepHighlighted, selectionMask_array, _
selectedObject, cursor)

Dim solid As Body = CType(selectedObject, Body)

If solid Is Nothing Then
Return Nothing
End If

Return solid

End Function
Function SelectLine() As Line

Dim ui As UI = ui.GetUI
Dim message As String = "Select line"
Dim title As String = "Selection"

Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim keepHighlighted As Boolean = False
Dim includeFeatures As Boolean = True

Dim selectionAction As Selection.SelectionAction = _
Selection.SelectionAction.ClearAndEnableSpecific

Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_line_type
.Subtype = 0
.SolidBodySubtype = 0
End With

Dim selectedObject As NXObject = Nothing
Dim cursor As Point3d

ui.SelectionManager.SelectObject(message, title, scope, _
selectionAction, includeFeatures, _
keepHighlighted, selectionMask_array, _
selectedObject, cursor)

Dim line_temp As line = CType(selectedObject, line)

If line_temp Is Nothing Then
Return Nothing
End If

Return line_temp

End Function

End Module

Paul Fillion

I was able to update the commands to get this to make one hole. But I could not get the solution to work.

For Each line in lines
create builder object
set properties
builder.Commit
Next

Paul Fillion

When using a builder object, you should call the .Commit method once and .Destroy the builder shortly after. Calling .Commit multiple times in a loop will lead to errors and/or undesired behavior. In your case, I think there are 2 options:
1) create, commit, and destroy the builder each time in the loop. This will result in multiple hole features and may or may not be desirable.
2) create the builder before the loop, gather information inside the loop, then - when the loop ends - use the information in the builder, then commit and destroy the builder. This should result in a single hole feature.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI
Imports System.Windows.Forms
Imports System.Collections.Generic

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim count as integer
Dim lw As ListingWindow = theSession.ListingWindow

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim nullFeatures_HolePackage As Features.HolePackage = Nothing
Dim holePackageBuilder1 As Features.HolePackageBuilder
holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(nullFeatures_HolePackage)
holePackageBuilder1.StartHoleData.BooleanOperation.Type = GeometricUtilities.BooleanOperation.BooleanType.Subtract
holePackageBuilder1.Tolerance = 0.0001
holePackageBuilder1.BooleanOperation.Type = GeometricUtilities.BooleanOperation.BooleanType.Subtract
holePackageBuilder1.HolePosition.SetAllowedEntityTypes(Section.AllowTypes.OnlyPoints)
holePackageBuilder1.HolePosition.AllowSelfIntersection(True)
holePackageBuilder1.HoleDepthLimitOption = Features.HolePackageBuilder.HoleDepthLimitOptions.ThroughBody
holePackageBuilder1.ProjectionDirection.ProjectDirectionMethod = GeometricUtilities.ProjectionOptions.DirectionType.Vector
'Layer of colling holes
Dim layernumber = NXInputBox.GetInputNumber("Enter the layer of the cooling holes: ")
'Diameter of the holes
Dim hole_dia = NXInputBox.GetInputNumber("Enter the diameter of the cooling holes: ")
holePackageBuilder1.GeneralSimpleHoleDiameter.RightHandSide = hole_dia
'Solid target
Dim targetBodies(0) As Body
Dim body1 As Body = SelectSolid()
'dim line_temp as line = Selectline()
targetBodies(0) = body1
holePackageBuilder1.BooleanOperation.SetTargetBodies(targetBodies)
lw.Open()
Dim cooling_holes() As DisplayableObject = theSession.Parts.Work.Lines.ToArray()
For Each temp_line As Line In cooling_holes
If temp_line.Layer = layernumber Then
'Line Selection
Dim line1 As Line = temp_line
Dim direction2 As Direction
direction2 = workPart.Directions.CreateDirection(temp_line, Sense.Reverse, SmartObject.UpdateOption.WithinModeling)
'hole dir
holePackageBuilder1.ProjectionDirection.ProjectVector = direction2
Dim success1 As Boolean
success1 = direction2.ReverseDirection()
holePackageBuilder1.ProjectionDirection.ProjectVector = direction2
'location of the hole
Dim point1 As Point
point1 = workPart.Points.CreatePoint(temp_line.startpoint)
' Hole creation....
Dim nXObject1 As NXObject
Dim xform1 As Xform
xform1 = workPart.Xforms.CreateExtractXform(temp_line, SmartObject.UpdateOption.WithinModeling, False, nXObject1)
Dim points1(0) As Point
points1(0) = point1
Dim curveDumbRule1 As CurveDumbRule
curveDumbRule1 = workPart.ScRuleFactory.CreateRuleCurveDumbFromPoints(points1)
Dim rules1(0) As SelectionIntentRule
rules1(0) = curveDumbRule1
Dim nullNXObject As NXObject = Nothing
Dim helpPoint1 As Point3d = New Point3d(0.0, 0.0, 0.0)
holePackageBuilder1.HolePosition.AddToSection(rules1, nullNXObject, nullNXObject, nullNXObject, helpPoint1, Section.Mode.Create, False)
'undo
Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Hole")
theSession.SetUndoMarkName(markId1, "Hole")
Dim expression1 As Expression = holePackageBuilder1.GeneralTipAngle
Dim expression2 As Expression = holePackageBuilder1.GeneralSimpleHoleDiameter
Dim expression3 As Expression = holePackageBuilder1.GeneralSimpleHoleDepth
count =count +1
lw.WriteLine("#" & count.toString)
'hole committed
End If
Next
Dim nXObject2 As NXObject
nXObject2 = holePackageBuilder1.Commit()
holePackageBuilder1.Destroy()

End Sub

Function SelectSolid() As Body

Dim ui As UI = ui.GetUI
Dim message As String = "Select solid body"
Dim title As String = "Selection"

Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim keepHighlighted As Boolean = False
Dim includeFeatures As Boolean = True

Dim selectionAction As Selection.SelectionAction = _
Selection.SelectionAction.ClearAndEnableSpecific

Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With

Dim selectedObject As NXObject = Nothing
Dim cursor As Point3d

ui.SelectionManager.SelectObject(message, title, scope, _
selectionAction, includeFeatures, _
keepHighlighted, selectionMask_array, _
selectedObject, cursor)

Dim solid As Body = CType(selectedObject, Body)

If solid Is Nothing Then
Return Nothing
End If

Return solid

End Function
Function SelectLine() As Line

Dim ui As UI = ui.GetUI
Dim message As String = "Select line"
Dim title As String = "Selection"

Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim keepHighlighted As Boolean = False
Dim includeFeatures As Boolean = True

Dim selectionAction As Selection.SelectionAction = _
Selection.SelectionAction.ClearAndEnableSpecific

Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_line_type
.Subtype = 0
.SolidBodySubtype = 0
End With

Dim selectedObject As NXObject = Nothing
Dim cursor As Point3d

ui.SelectionManager.SelectObject(message, title, scope, _
selectionAction, includeFeatures, _
keepHighlighted, selectionMask_array, _
selectedObject, cursor)

Dim line_temp As line = CType(selectedObject, line)

If line_temp Is Nothing Then
Return Nothing
End If

Return line_temp

End Function

End Module

Paul Fillion

It made the 15 holes and the points where correct but it is not following the vector for each line. Also I like how it only creates one feature.

Paul Fillion