Assembly boolean

hello i am new to journaling and i am trying to make journal which in which i have to manually select the parts in an assembly
and activate it (make work part). after which i will have to use wave geometry linker to extract body of other
assembly parts. and use it for doing boolean operation.i have recored a journal in which by changing names of other parts it will work.
but i want the selection to be made manually.. can anybody please help me...





Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String)

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Make Work Part")

Dim component1 As Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT TARGET 1"), Assemblies.Component)

Dim partLoadStatus1 As PartLoadStatus
theSession.Parts.SetWorkComponent(component1, partLoadStatus1)

workPart = theSession.Parts.Work
partLoadStatus1.Dispose()

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

Dim nullFeatures_Feature As Features.Feature = Nothing

If Not workPart.Preferences.Modeling.GetHistoryMode Then
Throw(New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
End If

Dim waveLinkBuilder1 As Features.WaveLinkBuilder
waveLinkBuilder1 = workPart.BaseFeatures.CreateWaveLinkBuilder(nullFeatures_Feature)

Dim waveDatumBuilder1 As Features.WaveDatumBuilder
waveDatumBuilder1 = waveLinkBuilder1.WaveDatumBuilder

Dim compositeCurveBuilder1 As Features.CompositeCurveBuilder
compositeCurveBuilder1 = waveLinkBuilder1.CompositeCurveBuilder

Dim waveSketchBuilder1 As Features.WaveSketchBuilder
waveSketchBuilder1 = waveLinkBuilder1.WaveSketchBuilder

Dim waveRoutingBuilder1 As Features.WaveRoutingBuilder
waveRoutingBuilder1 = waveLinkBuilder1.WaveRoutingBuilder

Dim wavePointBuilder1 As Features.WavePointBuilder
wavePointBuilder1 = waveLinkBuilder1.WavePointBuilder

Dim extractFaceBuilder1 As Features.ExtractFaceBuilder
extractFaceBuilder1 = waveLinkBuilder1.ExtractFaceBuilder

Dim mirrorBodyBuilder1 As Features.MirrorBodyBuilder
mirrorBodyBuilder1 = waveLinkBuilder1.MirrorBodyBuilder

extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.FaceChain

waveLinkBuilder1.Type = Features.WaveLinkBuilder.Types.BodyLink

extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.FaceChain

extractFaceBuilder1.AngleTolerance = 45.0

waveDatumBuilder1.DisplayScale = 2.0

extractFaceBuilder1.ParentPart = Features.ExtractFaceBuilder.ParentPartType.OtherPart

mirrorBodyBuilder1.ParentPartType = Features.MirrorBodyBuilder.ParentPart.OtherPart

theSession.SetUndoMarkName(markId2, "WAVE Geometry Linker Dialog")

compositeCurveBuilder1.Section.DistanceTolerance = 0.01

compositeCurveBuilder1.Section.ChainingTolerance = 0.0095

extractFaceBuilder1.Associative = True

extractFaceBuilder1.MakePositionIndependent = False

extractFaceBuilder1.FixAtCurrentTimestamp = False

extractFaceBuilder1.HideOriginal = False

extractFaceBuilder1.InheritDisplayProperties = False

Dim scCollector1 As ScCollector
scCollector1 = extractFaceBuilder1.ExtractBodyCollector

extractFaceBuilder1.CopyThreads = True

Dim features1(0) As Features.Feature
Dim part1 As Part = CType(theSession.Parts.FindObject("TOOL"), Part)

Dim extrude1 As Features.Extrude = CType(part1.Features.FindObject("EXTRUDE(4)"), Features.Extrude)

features1(0) = extrude1
Dim component2 As Assemblies.Component = CType(displayPart.ComponentAssembly.RootComponent.FindObject("COMPONENT TOOL 1"), Assemblies.Component)

Dim bodyFeatureRule1 As BodyFeatureRule
bodyFeatureRule1 = workPart.ScRuleFactory.CreateRuleBodyFeature(features1, True, component2)

Dim rules1(0) As SelectionIntentRule
rules1(0) = bodyFeatureRule1
scCollector1.ReplaceRules(rules1, False)

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "WAVE Geometry Linker")

theSession.DeleteUndoMark(markId3, Nothing)

Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "WAVE Geometry Linker")

Dim nXObject1 As NXObject
nXObject1 = waveLinkBuilder1.Commit()

theSession.DeleteUndoMark(markId4, Nothing)

theSession.SetUndoMarkName(markId2, "WAVE Geometry Linker")

waveLinkBuilder1.Destroy()

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

Dim nullFeatures_BooleanFeature As Features.BooleanFeature = Nothing

Dim booleanBuilder1 As Features.BooleanBuilder
booleanBuilder1 = workPart.Features.CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature)

Dim scCollector2 As ScCollector
scCollector2 = booleanBuilder1.ToolBodyCollector

Dim booleanRegionSelect1 As GeometricUtilities.BooleanRegionSelect
booleanRegionSelect1 = booleanBuilder1.BooleanRegionSelect

booleanBuilder1.Tolerance = 0.01

booleanBuilder1.Operation = Features.Feature.BooleanType.Subtract

theSession.SetUndoMarkName(markId5, "Subtract Dialog")

Dim body1 As Body = CType(workPart.Bodies.FindObject("EXTRUDE(1)"), Body)

Dim added1 As Boolean
added1 = booleanBuilder1.Targets.Add(body1)

Dim targets1(0) As TaggedObject
targets1(0) = body1
booleanRegionSelect1.AssignTargets(targets1)

Dim scCollector3 As ScCollector
scCollector3 = workPart.ScCollectors.CreateCollector()

Dim bodies1(0) As Body
Dim body2 As Body = CType(workPart.Bodies.FindObject("LINKED_BODY(4)"), Body)

bodies1(0) = body2
Dim bodyDumbRule1 As BodyDumbRule
bodyDumbRule1 = workPart.ScRuleFactory.CreateRuleBodyDumb(bodies1, True)

Dim rules2(0) As SelectionIntentRule
rules2(0) = bodyDumbRule1
scCollector3.ReplaceRules(rules2, False)

booleanBuilder1.ToolBodyCollector = scCollector3

Dim targets2(0) As TaggedObject
targets2(0) = body1
booleanRegionSelect1.AssignTargets(targets2)

Dim markId6 As Session.UndoMarkId
markId6 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Subtract")

theSession.DeleteUndoMark(markId6, Nothing)

Dim markId7 As Session.UndoMarkId
markId7 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Subtract")

Dim nXObject2 As NXObject
nXObject2 = booleanBuilder1.Commit()

theSession.DeleteUndoMark(markId7, Nothing)

theSession.SetUndoMarkName(markId5, "Subtract")

booleanBuilder1.Destroy()

Dim markId8 As Session.UndoMarkId
markId8 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Hide Component")

Dim markId9 As Session.UndoMarkId
markId9 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Make Work Part")

Dim nullAssemblies_Component As Assemblies.Component = Nothing

Dim partLoadStatus2 As PartLoadStatus
theSession.Parts.SetWorkComponent(nullAssemblies_Component, partLoadStatus2)

workPart = theSession.Parts.Work
partLoadStatus2.Dispose()
theSession.SetUndoMarkName(markId9, "Make Work Part")

End Sub
End Module

What version of NX?

What do you want the user to select? the target or tool bodies, maybe both?

I assume this is related to your other thread:
http://nxjournaling.com/content/activating-assembly

thanks for your reply's. currently i am using NX9.0. yes it is related to my other thread. but that thread is just the part of this code. i want the selector to select both target and tool bodies. the following code gives an idea about the next part which is about using a wave link geometry. i have successfully completed the code but the wavelinked body is only giving the features which are recorded instead i want the user selection to be wavelinked. awaiting your response. mean while i will also try my best as i am still new to this subject of journaling





Imports System
Imports NXOpen
Imports NXOpen.UF
Imports System.Collections.Generic
Imports NXOpen.Assemblies
Imports NXOpenUI

Module NXJournal
Public Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim ufs As UFSession = UFSession.GetUFSession()

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

Dim nullFeatures_Feature As Features.Feature = Nothing

If Not workPart.Preferences.Modeling.GetHistoryMode Then
Throw(New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
End If

Dim mySelectedObjectsa() As NXObject 'Assemblies.Component
Dim body1 As body
If selectbody("Select Target Body", mySelectedObjectsa) = Selection.Response.Cancel Then 'selectbody
Exit Sub
End if

Dim waveLinkBuilder1 As Features.WaveLinkBuilder
waveLinkBuilder1 = workPart.BaseFeatures.CreateWaveLinkBuilder(nullFeatures_Feature)

Dim waveDatumBuilder1 As Features.WaveDatumBuilder
waveDatumBuilder1 = waveLinkBuilder1.WaveDatumBuilder

Dim compositeCurveBuilder1 As Features.CompositeCurveBuilder
compositeCurveBuilder1 = waveLinkBuilder1.CompositeCurveBuilder

Dim waveSketchBuilder1 As Features.WaveSketchBuilder
waveSketchBuilder1 = waveLinkBuilder1.WaveSketchBuilder

Dim waveRoutingBuilder1 As Features.WaveRoutingBuilder
waveRoutingBuilder1 = waveLinkBuilder1.WaveRoutingBuilder

Dim wavePointBuilder1 As Features.WavePointBuilder
wavePointBuilder1 = waveLinkBuilder1.WavePointBuilder

Dim extractFaceBuilder1 As Features.ExtractFaceBuilder
extractFaceBuilder1 = waveLinkBuilder1.ExtractFaceBuilder

Dim mirrorBodyBuilder1 As Features.MirrorBodyBuilder
mirrorBodyBuilder1 = waveLinkBuilder1.MirrorBodyBuilder

extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.FaceChain

waveLinkBuilder1.Type = Features.WaveLinkBuilder.Types.BodyLink

extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.FaceChain

extractFaceBuilder1.AngleTolerance = 45.0

waveDatumBuilder1.DisplayScale = 2.0

extractFaceBuilder1.ParentPart = Features.ExtractFaceBuilder.ParentPartType.OtherPart

mirrorBodyBuilder1.ParentPartType = Features.MirrorBodyBuilder.ParentPart.OtherPart

theSession.SetUndoMarkName(markId1, "WAVE Geometry Linker Dialog")

compositeCurveBuilder1.Section.DistanceTolerance = 0.01

compositeCurveBuilder1.Section.ChainingTolerance = 0.0095

extractFaceBuilder1.Associative = True

extractFaceBuilder1.MakePositionIndependent = False

extractFaceBuilder1.FixAtCurrentTimestamp = False

extractFaceBuilder1.HideOriginal = False

extractFaceBuilder1.InheritDisplayProperties = False

Dim scCollector1 As ScCollector
scCollector1 = extractFaceBuilder1.ExtractBodyCollector

extractFaceBuilder1.CopyThreads = True

Dim features1(0) As Features.Feature
Dim part1 As Part =CType(theSession.Parts.FindObject("TOOL"), Part)

Dim extrude1 As Features.Extrude =CType(part1.Features.FindObject("EXTRUDE(4)"), Features.Extrude)

features1(0) = extrude1
Dim component1 As Assemblies.Component '= CType(displayPart.ComponentAssembly.RootComponent.FindObject("COMPONENT TOOL 1"), Assemblies.Component)
component1 = mySelectedObjectsa(0)

Dim bodyFeatureRule1 As BodyFeatureRule
bodyFeatureRule1 = workPart.ScRuleFactory.CreateRuleBodyFeature(features1, True, component1)

Dim rules1(0) As SelectionIntentRule
rules1(0) = bodyFeatureRule1
scCollector1.ReplaceRules(rules1, False)

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "WAVE Geometry Linker")

theSession.DeleteUndoMark(markId2, Nothing)

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "WAVE Geometry Linker")

Dim nXObject1 As NXObject
nXObject1 = waveLinkBuilder1.Commit()

theSession.DeleteUndoMark(markId3, Nothing)

theSession.SetUndoMarkName(markId1, "WAVE Geometry Linker")

waveLinkBuilder1.Destroy()

end sub

Function selectbody (ByVal prompt As String, byRef selObj() as NXObject ) As Selection.Response 'Assemblies.Component selectbody

Dim theUI As UI = UI.GetUI
Dim title As String = "Select Target Body"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = _
Selection.SelectionAction.ClearAndEnableSpecific

Dim scope As Selection.SelectionScope = Selection.SelectionScope.anyinassembly
Dim selectionMask_array(1) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_component_type
.Subtype = UFConstants.UF_all_subtype
.SolidBodySubtype = 0
End With
With selectionMask_array(1)
.Type = UFConstants.UF_component_type
.Subtype = UFConstants.UF_all_subtype
.SolidBodySubtype = 0

End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects( _
prompt, title, scope, selAction, _
includeFeatures, keepHighlighted,selectionMask_array, selObj)

If resp = Selection.Response.Ok
Return resp
Else
Return Selection.Response.Cancel
End If

End function
End Module

From what I see, you want to select 2 bodies in an assembly and subtract one from the other via wave linking. Below is a journal that will prompt you to select a target body, the body's component will be made the work part; it will then prompt to select a tool body, the tool body will be wave linked into the work part and subtracted from the originally selected body.


There may be an error if the 2nd body is in the same part as the first; you may want to add some checking to avoid that. Also, there is no error handling to speak of in this example code.



Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim currentPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const undoMarkName As String = "NXJ journal"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

Dim targetBody As Body

'select solid body, make owning part the work part
If SelectSolidBody("Select a solid body", targetBody) = Selection.Response.Cancel Then
Return
End If

'lw.WriteLine("is occurrence? " & targetBody.IsOccurrence.ToString)
'lw.WriteLine("target body owned by: " & targetBody.Prototype.OwningPart.FullPath)

Dim partLoadStatus1 As PartLoadStatus

Try
' TODO: Add your application code here
theSession.Parts.SetWorkComponent(targetBody.OwningComponent, PartCollection.RefsetOption.Current, PartCollection.WorkComponentOption.Visible, partLoadStatus1)

workPart = theSession.Parts.Work

Catch ex As NXException
theSession.UndoToMark(markId1, undoMarkName)
MsgBox(ex.Message)

Finally
partLoadStatus1.Dispose()

End Try

'select tool body for wavelink
Dim waveTool As Body

'select solid body, wave link into target body file
If SelectSolidBody("Select a solid body", waveTool) = Selection.Response.Cancel Then
Return
End If

Dim tool As Features.ExtractFace = WaveLink(workPart, waveTool)
Dim toolBody As Body = tool.GetBodies(0)

Subtract(workPart, targetBody, toolBody)

'reset original part
theUfSession.Assem.SetWorkPart(currentPart.Tag)

lw.Close()

End Sub

Function SelectSolidBody(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select a solid body"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Function WaveLink(targetPart As Part, targetBody As Body) As Features.ExtractFace

Dim nullFeatures_Feature As Features.Feature = Nothing

Dim waveLinkBuilder1 As Features.WaveLinkBuilder
waveLinkBuilder1 = targetPart.BaseFeatures.CreateWaveLinkBuilder(nullFeatures_Feature)

Dim extractFaceBuilder1 As Features.ExtractFaceBuilder

waveLinkBuilder1.Type = Features.WaveLinkBuilder.Types.BodyLink
waveLinkBuilder1.FixAtCurrentTimestamp = True
waveLinkBuilder1.CopyThreads = False

extractFaceBuilder1 = waveLinkBuilder1.ExtractFaceBuilder
extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.FaceChain
'extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.SingleFace
extractFaceBuilder1.AngleTolerance = 45.0
extractFaceBuilder1.ParentPart = Features.ExtractFaceBuilder.ParentPartType.OtherPart
extractFaceBuilder1.Associative = True
extractFaceBuilder1.MakePositionIndependent = False
extractFaceBuilder1.FixAtCurrentTimestamp = True
extractFaceBuilder1.HideOriginal = False
extractFaceBuilder1.InheritDisplayProperties = False
extractFaceBuilder1.CopyThreads = False

Dim selectObjectList1 As SelectObjectList
selectObjectList1 = extractFaceBuilder1.BodyToExtract

Dim added1 As Boolean
added1 = selectObjectList1.Add(targetBody)

Dim nXObject1 As Features.ExtractFace
nXObject1 = waveLinkBuilder1.Commit()

waveLinkBuilder1.Destroy()

Return nXObject1

End Function

Sub Subtract(thePart As Part, theTarget As Body, theTool As Body)

Dim nullFeatures_BooleanFeature As Features.BooleanFeature = Nothing

Dim booleanBuilder1 As Features.BooleanBuilder
booleanBuilder1 = thePart.Features.CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature)

'Dim scCollector1 As ScCollector
'scCollector1 = booleanBuilder1.ToolBodyCollector

Dim booleanRegionSelect1 As GeometricUtilities.BooleanRegionSelect
booleanRegionSelect1 = booleanBuilder1.BooleanRegionSelect

booleanBuilder1.Tolerance = 0.001

booleanBuilder1.Operation = Features.Feature.BooleanType.Subtract

'Dim body1 As Body = CType(thePart.Bodies.FindObject("BLOCK(1)"), Body)

Dim added1 As Boolean
added1 = booleanBuilder1.Targets.Add(theTarget)

Dim targets1(0) As TaggedObject
targets1(0) = theTarget
booleanRegionSelect1.AssignTargets(targets1)

Dim scCollector2 As ScCollector
scCollector2 = thePart.ScCollectors.CreateCollector()

Dim bodies1(0) As Body
'Dim body2 As Body = CType(thePart.Bodies.FindObject("LINKED_BODY(6)"), Body)

bodies1(0) = theTool
Dim bodyDumbRule1 As BodyDumbRule
bodyDumbRule1 = thePart.ScRuleFactory.CreateRuleBodyDumb(bodies1)

Dim rules1(0) As SelectionIntentRule
rules1(0) = bodyDumbRule1
scCollector2.ReplaceRules(rules1, False)

booleanBuilder1.ToolBodyCollector = scCollector2

Dim targets2(0) As TaggedObject
targets2(0) = theTarget
booleanRegionSelect1.AssignTargets(targets2)

Dim nXObject1 As NXObject
nXObject1 = booleanBuilder1.Commit()

booleanBuilder1.Destroy()

End Sub

End Module

Hello,

I found this quite useful and have been trying to tweek it for a multi selection for the tool with no luck so far.

The code is modified from above with some from the "Add interactivity to your journal" article.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
public mySelectedObjects as NXObject()

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim currentPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const undoMarkName As String = "NXJ journal"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

Dim targetBody As Body

'select solid body, make owning part the work part
If SelectSolidBody("Select a solid body", targetBody) = Selection.Response.Cancel Then
Return
End If

'lw.WriteLine("is occurrence? " & targetBody.IsOccurrence.ToString)
'lw.WriteLine("target body owned by: " & targetBody.Prototype.OwningPart.FullPath)

Dim partLoadStatus1 As PartLoadStatus

Try
' TODO: Add your application code here
theSession.Parts.SetWorkComponent(targetBody.OwningComponent, PartCollection.RefsetOption.Current, PartCollection.WorkComponentOption.Visible, partLoadStatus1)

workPart = theSession.Parts.Work

Catch ex As NXException
theSession.UndoToMark(markId1, undoMarkName)
MsgBox(ex.Message)

Finally
partLoadStatus1.Dispose()

End Try

'select tool body for wavelink
Dim waveTool As Body

'select solid body, wave link into target body file

If SelectObjects("Hey, select multiple somethings", mySelectedObjects) = Selection.Response.Cancel Then
Return
End If

Dim tool As Features.ExtractFace = WaveLink(workPart, mySelectedObjects)
Dim toolBody As Body = tool.GetBodies(0)

Subtract(workPart, targetBody, toolBody)

'reset original part
theUfSession.Assem.SetWorkPart(currentPart.Tag)

lw.Close()

End Sub

Function SelectSolidBody(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select a solid body"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Function SelectObjects(prompt As String, ByRef selObj as NXObject()) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim typeArray() As Selection.SelectionType = _
{Selection.SelectionType.all}

Dim resp As Selection.Response = theUI.SelectionManager.SelectObjects( _
prompt, "Selection", _
Selection.SelectionScope.AnyInAssembly, _
False, typeArray, selobj)

If resp = Selection.Response.ObjectSelected Or _
resp = Selection.Response.ObjectSelectedByName Or _
resp = Selection.Response.OK Then
Return Selection.Response.Ok
Else
return Selection.Response.Cancel
End If

End Function

Function WaveLink(targetPart As Part, targetBody As Body) As Features.ExtractFace

Dim nullFeatures_Feature As Features.Feature = Nothing

Dim waveLinkBuilder1 As Features.WaveLinkBuilder
waveLinkBuilder1 = targetPart.BaseFeatures.CreateWaveLinkBuilder(nullFeatures_Feature)

Dim extractFaceBuilder1 As Features.ExtractFaceBuilder

waveLinkBuilder1.Type = Features.WaveLinkBuilder.Types.BodyLink
waveLinkBuilder1.FixAtCurrentTimestamp = True
waveLinkBuilder1.CopyThreads = False

extractFaceBuilder1 = waveLinkBuilder1.ExtractFaceBuilder
extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.FaceChain
'extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.SingleFace
extractFaceBuilder1.AngleTolerance = 45.0
extractFaceBuilder1.ParentPart = Features.ExtractFaceBuilder.ParentPartType.OtherPart
extractFaceBuilder1.Associative = True
extractFaceBuilder1.MakePositionIndependent = False
extractFaceBuilder1.FixAtCurrentTimestamp = True
extractFaceBuilder1.HideOriginal = False
extractFaceBuilder1.InheritDisplayProperties = False
extractFaceBuilder1.CopyThreads = False

Dim selectObjectList1 As SelectObjectList
selectObjectList1 = extractFaceBuilder1.BodyToExtract

Dim added1 As Boolean
added1 = selectObjectList1.Add(targetBody)

Dim nXObject1 As Features.ExtractFace
nXObject1 = waveLinkBuilder1.Commit()

waveLinkBuilder1.Destroy()

Return nXObject1

End Function

Sub Subtract(thePart As Part, theTarget As Body, theTool As Body)

Dim nullFeatures_BooleanFeature As Features.BooleanFeature = Nothing

Dim booleanBuilder1 As Features.BooleanBuilder
booleanBuilder1 = thePart.Features.CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature)

'Dim scCollector1 As ScCollector
'scCollector1 = booleanBuilder1.ToolBodyCollector

Dim booleanRegionSelect1 As GeometricUtilities.BooleanRegionSelect
booleanRegionSelect1 = booleanBuilder1.BooleanRegionSelect

booleanBuilder1.Tolerance = 0.001

booleanBuilder1.Operation = Features.Feature.BooleanType.Subtract

'Dim body1 As Body = CType(thePart.Bodies.FindObject("BLOCK(1)"), Body)

Dim added1 As Boolean
added1 = booleanBuilder1.Targets.Add(theTarget)

Dim targets1(0) As TaggedObject
targets1(0) = theTarget
booleanRegionSelect1.AssignTargets(targets1)

Dim scCollector2 As ScCollector
scCollector2 = thePart.ScCollectors.CreateCollector()

Dim bodies1(0) As Body
'Dim body2 As Body = CType(thePart.Bodies.FindObject("LINKED_BODY(6)"), Body)

bodies1(0) = theTool
Dim bodyDumbRule1 As BodyDumbRule
bodyDumbRule1 = thePart.ScRuleFactory.CreateRuleBodyDumb(bodies1)

Dim rules1(0) As SelectionIntentRule
rules1(0) = bodyDumbRule1
scCollector2.ReplaceRules(rules1, False)

booleanBuilder1.ToolBodyCollector = scCollector2

Dim targets2(0) As TaggedObject
targets2(0) = theTarget
booleanRegionSelect1.AssignTargets(targets2)

Dim nXObject1 As NXObject
nXObject1 = booleanBuilder1.Commit()

booleanBuilder1.Destroy()

End Sub

End Module

nx 9

Hi can you please edit Below code with below points

1. Activate part by selecting component in assembly
2. Allow user to select multiple solids to link
3. and rotate bodies at required angle(user input) with respected AXIS(vector) in entire assembly
4. No Boolean

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim currentPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const undoMarkName As String = "NXJ journal"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

Dim targetBody As Body

'select solid body, make owning part the work part
If SelectSolidBody("Select a solid body", targetBody) = Selection.Response.Cancel Then
Return
End If

'lw.WriteLine("is occurrence? " & targetBody.IsOccurrence.ToString)
'lw.WriteLine("target body owned by: " & targetBody.Prototype.OwningPart.FullPath)

Dim partLoadStatus1 As PartLoadStatus

Try
' TODO: Add your application code here
theSession.Parts.SetWorkComponent(targetBody.OwningComponent, PartCollection.RefsetOption.Current, PartCollection.WorkComponentOption.Visible, partLoadStatus1)

workPart = theSession.Parts.Work

Catch ex As NXException
theSession.UndoToMark(markId1, undoMarkName)
MsgBox(ex.Message)

Finally
partLoadStatus1.Dispose()

End Try

'select tool body for wavelink
Dim waveTool As Body

'select solid body, wave link into target body file
If SelectSolidBody("Select a solid body", waveTool) = Selection.Response.Cancel Then
Return
End If

Dim tool As Features.ExtractFace = WaveLink(workPart, waveTool)
Dim toolBody As Body = tool.GetBodies(0)

Subtract(workPart, targetBody, toolBody)

'reset original part
theUfSession.Assem.SetWorkPart(currentPart.Tag)

lw.Close()

End Sub

Function SelectSolidBody(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select a solid body"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Function WaveLink(targetPart As Part, targetBody As Body) As Features.ExtractFace

Dim nullFeatures_Feature As Features.Feature = Nothing

Dim waveLinkBuilder1 As Features.WaveLinkBuilder
waveLinkBuilder1 = targetPart.BaseFeatures.CreateWaveLinkBuilder(nullFeatures_Feature)

Dim extractFaceBuilder1 As Features.ExtractFaceBuilder

waveLinkBuilder1.Type = Features.WaveLinkBuilder.Types.BodyLink
waveLinkBuilder1.FixAtCurrentTimestamp = True
waveLinkBuilder1.CopyThreads = False

extractFaceBuilder1 = waveLinkBuilder1.ExtractFaceBuilder
extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.FaceChain
'extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.SingleFace
extractFaceBuilder1.AngleTolerance = 45.0
extractFaceBuilder1.ParentPart = Features.ExtractFaceBuilder.ParentPartType.OtherPart
extractFaceBuilder1.Associative = True
extractFaceBuilder1.MakePositionIndependent = False
extractFaceBuilder1.FixAtCurrentTimestamp = True
extractFaceBuilder1.HideOriginal = False
extractFaceBuilder1.InheritDisplayProperties = False
extractFaceBuilder1.CopyThreads = False

Dim selectObjectList1 As SelectObjectList
selectObjectList1 = extractFaceBuilder1.BodyToExtract

Dim added1 As Boolean
added1 = selectObjectList1.Add(targetBody)

Dim nXObject1 As Features.ExtractFace
nXObject1 = waveLinkBuilder1.Commit()

waveLinkBuilder1.Destroy()

Return nXObject1

End Function

Sub Subtract(thePart As Part, theTarget As Body, theTool As Body)

Dim nullFeatures_BooleanFeature As Features.BooleanFeature = Nothing

Dim booleanBuilder1 As Features.BooleanBuilder
booleanBuilder1 = thePart.Features.CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature)

'Dim scCollector1 As ScCollector
'scCollector1 = booleanBuilder1.ToolBodyCollector

Dim booleanRegionSelect1 As GeometricUtilities.BooleanRegionSelect
booleanRegionSelect1 = booleanBuilder1.BooleanRegionSelect

booleanBuilder1.Tolerance = 0.001

booleanBuilder1.Operation = Features.Feature.BooleanType.Subtract

'Dim body1 As Body = CType(thePart.Bodies.FindObject("BLOCK(1)"), Body)

Dim added1 As Boolean
added1 = booleanBuilder1.Targets.Add(theTarget)

Dim targets1(0) As TaggedObject
targets1(0) = theTarget
booleanRegionSelect1.AssignTargets(targets1)

Dim scCollector2 As ScCollector
scCollector2 = thePart.ScCollectors.CreateCollector()

Dim bodies1(0) As Body
'Dim body2 As Body = CType(thePart.Bodies.FindObject("LINKED_BODY(6)"), Body)

bodies1(0) = theTool
Dim bodyDumbRule1 As BodyDumbRule
bodyDumbRule1 = thePart.ScRuleFactory.CreateRuleBodyDumb(bodies1)

Dim rules1(0) As SelectionIntentRule
rules1(0) = bodyDumbRule1
scCollector2.ReplaceRules(rules1, False)

booleanBuilder1.ToolBodyCollector = scCollector2

Dim targets2(0) As TaggedObject
targets2(0) = theTarget
booleanRegionSelect1.AssignTargets(targets2)

Dim nXObject1 As NXObject
'nXObject1 = booleanBuilder1.Commit()

booleanBuilder1.Destroy()

End Sub

End Module

-[]-

I copied this code, and made 2 minor changes:

Commented out the call to the WaveLink function
Set toolBody equal to waveTool

'Dim tool As Features.ExtractFace = WaveLink(workPart, waveTool)
Dim toolBody As Body = waveTool

I selected 2 bodies that intersect within the same component and now I get this error, can anyone help me understand what is happening here:

Journal execution results for C:\Users\dhuskic\Desktop\Subtract.vb...
Runtime error:
NXOpen.NXException: No tool solid selected.
at NXOpen.Builder.Commit()
at Module1.Subtract(Part thePart, Body theTarget, Body theTool) in C:\Users\dhuskic\AppData\Local\Temp\NXJournals5884\journal.vb:line 189
at Module1.Main() in C:\Users\dhuskic\AppData\Local\Temp\NXJournals5884\journal.vb:line 64

DHuskic
Nx 9 VB

What version of the above code did you start with? Also, what version of NX?

I modified the code that I posted (timestamp: Fri, 07/18/2014 - 13:37) to reflect the changes that you made and it worked without error in NX 9.

I started with the one you posted:

Re: Assembly Boolean
NXJournaling - Fri, 07/18/2014 - 14:37

I was using a subtract method in another program, but for some reason, it wouldn't work. I tried your code and received the same results. I'm actually using Nx9 currently.

DHuskic
Nx 9 VB

Below is the code that I modified per your description. I tested it by creating a new part file, adding a block and cylinder objects that interfered, run the journal selecting the block at the first prompt and the cylinder at the 2nd prompt. The cylinder is correctly subtracted with no errors.

The error that you are getting "no tool solid selected" makes me wonder if there are geometry errors in your part (use examine geometry command to check) or perhaps the selected body is a sheet body instead of a solid...

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If

Dim workPart As Part = theSession.Parts.Work
Dim currentPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()

Const undoMarkName As String = "NXJ journal"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

Dim targetBody As Body

'select solid body, make owning part the work part
If SelectSolidBody("Select a solid body", targetBody) = Selection.Response.Cancel Then
Return
End If

'lw.WriteLine("is occurrence? " & targetBody.IsOccurrence.ToString)
'lw.WriteLine("target body owned by: " & targetBody.Prototype.OwningPart.FullPath)

Dim partLoadStatus1 As PartLoadStatus

Try
' TODO: Add your application code here
theSession.Parts.SetWorkComponent(targetBody.OwningComponent, PartCollection.RefsetOption.Current, PartCollection.WorkComponentOption.Visible, partLoadStatus1)

workPart = theSession.Parts.Work

Catch ex As NXException
theSession.UndoToMark(markId1, undoMarkName)
MsgBox(ex.Message)

Finally
partLoadStatus1.Dispose()

End Try

'select tool body for wavelink
Dim waveTool As Body

'select solid body, wave link into target body file
If SelectSolidBody("Select a solid body", waveTool) = Selection.Response.Cancel Then
Return
End If

'Dim tool As Features.ExtractFace = WaveLink(workPart, waveTool)
'Dim toolBody As Body = tool.GetBodies(0)
Dim toolBody as Body = waveTool

Subtract(workPart, targetBody, toolBody)

'reset original part
theUfSession.Assem.SetWorkPart(currentPart.Tag)

lw.Close()

End Sub

Function SelectSolidBody(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response

Dim theUI As UI = UI.GetUI
Dim title As String = "Select a solid body"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
Dim selectionMask_array(0) As Selection.MaskTriple

With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_BODY
End With

Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Function WaveLink(targetPart As Part, targetBody As Body) As Features.ExtractFace

Dim nullFeatures_Feature As Features.Feature = Nothing

Dim waveLinkBuilder1 As Features.WaveLinkBuilder
waveLinkBuilder1 = targetPart.BaseFeatures.CreateWaveLinkBuilder(nullFeatures_Feature)

Dim extractFaceBuilder1 As Features.ExtractFaceBuilder

waveLinkBuilder1.Type = Features.WaveLinkBuilder.Types.BodyLink
waveLinkBuilder1.FixAtCurrentTimestamp = True
waveLinkBuilder1.CopyThreads = False

extractFaceBuilder1 = waveLinkBuilder1.ExtractFaceBuilder
extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.FaceChain
'extractFaceBuilder1.FaceOption = Features.ExtractFaceBuilder.FaceOptionType.SingleFace
extractFaceBuilder1.AngleTolerance = 45.0
extractFaceBuilder1.ParentPart = Features.ExtractFaceBuilder.ParentPartType.OtherPart
extractFaceBuilder1.Associative = True
extractFaceBuilder1.MakePositionIndependent = False
extractFaceBuilder1.FixAtCurrentTimestamp = True
extractFaceBuilder1.HideOriginal = False
extractFaceBuilder1.InheritDisplayProperties = False
extractFaceBuilder1.CopyThreads = False

Dim selectObjectList1 As SelectObjectList
selectObjectList1 = extractFaceBuilder1.BodyToExtract

Dim added1 As Boolean
added1 = selectObjectList1.Add(targetBody)

Dim nXObject1 As Features.ExtractFace
nXObject1 = waveLinkBuilder1.Commit()

waveLinkBuilder1.Destroy()

Return nXObject1

End Function

Sub Subtract(thePart As Part, theTarget As Body, theTool As Body)

Dim nullFeatures_BooleanFeature As Features.BooleanFeature = Nothing

Dim booleanBuilder1 As Features.BooleanBuilder
booleanBuilder1 = thePart.Features.CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature)

'Dim scCollector1 As ScCollector
'scCollector1 = booleanBuilder1.ToolBodyCollector

Dim booleanRegionSelect1 As GeometricUtilities.BooleanRegionSelect
booleanRegionSelect1 = booleanBuilder1.BooleanRegionSelect

booleanBuilder1.Tolerance = 0.001

booleanBuilder1.Operation = Features.Feature.BooleanType.Subtract

'Dim body1 As Body = CType(thePart.Bodies.FindObject("BLOCK(1)"), Body)

Dim added1 As Boolean
added1 = booleanBuilder1.Targets.Add(theTarget)

Dim targets1(0) As TaggedObject
targets1(0) = theTarget
booleanRegionSelect1.AssignTargets(targets1)

Dim scCollector2 As ScCollector
scCollector2 = thePart.ScCollectors.CreateCollector()

Dim bodies1(0) As Body
'Dim body2 As Body = CType(thePart.Bodies.FindObject("LINKED_BODY(6)"), Body)

bodies1(0) = theTool
Dim bodyDumbRule1 As BodyDumbRule
bodyDumbRule1 = thePart.ScRuleFactory.CreateRuleBodyDumb(bodies1)

Dim rules1(0) As SelectionIntentRule
rules1(0) = bodyDumbRule1
scCollector2.ReplaceRules(rules1, False)

booleanBuilder1.ToolBodyCollector = scCollector2

Dim targets2(0) As TaggedObject
targets2(0) = theTarget
booleanRegionSelect1.AssignTargets(targets2)

Dim nXObject1 As NXObject
nXObject1 = booleanBuilder1.Commit()

booleanBuilder1.Destroy()

End Sub

End Module

Above Code is working fine in nx9 but I am using 2019 (I think) and it seems to not be able to execute the booleanBuilder1. Everything else seems to work up to that point

Which version of the journal are you using? Also, what does the assembly hierarchy look like, are you selecting bodies from "sibling" components or something else?

I am using the one you recently posted above my first reply. I tried the assembly hierarchy in multiple different orientations. Tool as a 'child', 'sibling' and in a parent assembly. kept getting the same error.

That version of the code was modified to answer a specific question from DHuskic.

Please try the version of the code in the post with the timestamp:
Fri, 07/18/2014 - 13:37

Alright, thank you. I'll give it a try. Apologies for the misunderstanding

Not a problem!
Threads get a bit confusing when multiple questions are being asked and answered in the same thread. I try to minimize that, but it gets out of hand sometimes.