Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

NXOPEN Add edge blend to each part in an assembly 3

Status
Not open for further replies.

Halasox

Civil/Environmental
Jun 5, 2016
16
0
0
US
Hi all,

I am looking to apply to each part in an assembly an edge blend either mm or inch using "body edges" filter. The code works from part file and applies the edge blend. But when I tried to convert and do this from assembly level it's not creating them and the code runs without errors. I am not sure where the problem is and appreciate any help!

Code:
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.Assemblies
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.Features

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow
    Dim workPart As NXOpen.Part = theSession.Parts.Work
    Dim displayPart As NXOpen.Part = theSession.Parts.Display
    
    Dim uniqueParts As New List(Of Part)
    Dim uniqueSubAssemblies As New List(Of Part)

    Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature = Nothing
    Dim blendSize As Double = 0
    Dim edgeBodyRule1 As NXOpen.EdgeBodyRule = Nothing

    Sub Main()

        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        Dim workPart As Part = theSession.Parts.Work
        Dim dispPart As Part = theSession.Parts.Display
        lw.Open()

        Try
            Dim c As ComponentAssembly = dispPart.ComponentAssembly
            If Not IsNothing(c.RootComponent) Then
                reportComponentChildren(c.RootComponent)
            Else
                lw.WriteLine("Part has no components")
            End If
        Catch e As Exception
            theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
        End Try

        'lw.WriteLine(dispPart.FullPath)
        'lw.WriteLine("count of unique sub-assemblies: " & uniqueSubAssemblies.Count.ToString)
        For Each temp As Part In uniqueSubAssemblies
            Try
                'may throw an error if the file is not loaded
                'lw.WriteLine("  " & temp.FullPath)
            Catch ex As Exception
            End Try
        Next
        'lw.WriteLine("")

        lw.WriteLine("count of unique parts: " & uniqueParts.Count.ToString)
        For Each temp As Part In uniqueParts
            Try
                'may throw an error if the file is not loaded
                'lw.WriteLine("  " & temp.FullPath)
                
            blendSize = askBlendSize()
            
            Dim edgeBlendBuilder1 As NXOpen.Features.EdgeBlendBuilder = Nothing
            edgeBlendBuilder1 = workPart.Features.CreateEdgeBlendBuilder(nullNXOpen_Features_Feature)
    
            For Each body As NXOpen.Body In workPart.Bodies
                
        
                Dim scCollector1 As NXOpen.ScCollector = Nothing
                scCollector1 = workPart.ScCollectors.CreateCollector()
                
                Dim selectionIntentRuleOptions1 As NXOpen.SelectionIntentRuleOptions = Nothing
                selectionIntentRuleOptions1 = workPart.ScRuleFactory.CreateRuleOptions()
                
                selectionIntentRuleOptions1.SetSelectedFromInactive(False)
                
                ' Create a new edge blend builder for each body
                edgeBlendBuilder1 = workPart.Features.CreateEdgeBlendBuilder(nullNXOpen_Features_Feature)
            
                ' Create a new selection intent rule for each body
                edgeBodyRule1 = workPart.ScRuleFactory.CreateRuleEdgeBody(body, selectionIntentRuleOptions1)
            
                Dim rules1(0) As NXOpen.SelectionIntentRule
                rules1(0) = edgeBodyRule1
                scCollector1.ReplaceRules(rules1, False)
            
                scCollector1.AddEvaluationFilter(NXOpen.ScEvaluationFiltertype.LaminarEdge)
            
                edgeBlendBuilder1.Tolerance = 0.01
                ' Set other edge blend builder options as needed
            
                Dim csIndex1 As Integer = Nothing
                csIndex1 = edgeBlendBuilder1.AddChainset(scCollector1, blendSize.ToString())
            
                Dim feature1 As NXOpen.Features.Feature = Nothing
                feature1 = edgeBlendBuilder1.CommitFeature()
            
                edgeBlendBuilder1.Destroy()
            Next

            Catch ex As Exception
                ' Handle any exceptions that occur while processing unique parts
                lw.WriteLine("Error processing part: " & ex.Message)
            End Try
        Next

        lw.Close()

    End Sub

    Sub reportComponentChildren(ByVal comp As Component)

        For Each child As Component In comp.GetChildren()
            Dim thePart As Part = child.Prototype.OwningPart
            If child.GetChildren.Length <> 0 Then
                '*** this is a subassembly, add code specific to subassemblies
                If Not uniqueSubAssemblies.Contains(thePart) Then
                    uniqueSubAssemblies.Add(thePart)
                End If
                '*** end of code to process subassembly
            Else
                'this component has no children (it is a leaf node)
                If Not uniqueParts.Contains(thePart) Then
                    uniqueParts.Add(thePart)
                End If
            End If
            reportComponentChildren(child)
        Next
    End Sub

    Function askBlendSize() As Double
        Dim blSz As Double
        If displayPart.PartUnits = BasePart.Units.Inches Then
             blSz = 0.008
        elseif displayPart.PartUnits = BasePart.Units.Millimeters Then
            blSz = 0.2
        End If
    Return blSz
    End Function
    
    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function

End Module
 
Replies continue below

Recommended for you

Code:
For Each body As NXOpen.Body In workPart.Bodies

The "workPart" is defined early on in your code, and I don't see that it ever changes. This means that the only bodies that will be modified are the ones in the current work part when the journal is run. I'd guess that the top level assembly is the work part when the journal is run. There are not usually any bodies defined in the top level assembly, so your code completes without processing anything.

To process the bodies in each component part file, first make sure the file is fully loaded. Then you can either reference the component part file directly or make it the work part before creating features.

www.nxjournaling.com
 
cowski

Thanks for your help, this makes sense! Yes the top lvl asm was the work part and should have been skipped. Have updated the code in case someone else needs this.

Code:
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpen.Assemblies
Imports NXOpen.Features

Module EdgeBlend_Each_PRT_in_ASM

Dim theSession As Session = Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = theSession.ListingWindow
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display

Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature = Nothing
Dim blendSize As Double = 0
Dim edgeBodyRule1 As NXOpen.EdgeBodyRule = Nothing

Sub Main()
    If IsNothing(theSession.Parts.BaseWork) Then
        ' Active part required
        Return
    End If

    lw.Open()

    Try
        Dim c As ComponentAssembly = displayPart.ComponentAssembly
        If Not IsNothing(c.RootComponent) Then
            Dim processedParts As New Dictionary(Of Part, Boolean) ' Track processed parts
            reportComponentChildren(c.RootComponent, processedParts)
        Else
            lw.WriteLine("Part has no components")
        End If
    Catch e As Exception
        theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
    End Try

    ' ...

    lw.Close()
End Sub

Sub reportComponentChildren(ByVal comp As Component, ByRef processedParts As Dictionary(Of Part, Boolean))
    Dim originalWorkPart As Part = workPart

    For Each child As Component In comp.GetChildren()
        Dim thePart As Part = child.Prototype.OwningPart

        If Not processedParts.ContainsKey(thePart) Then
            ' Temporarily set the component as the work part and process bodies
            theSession.Parts.SetWorkComponent(child, NXOpen.PartCollection.RefsetOption.Entire, NXOpen.PartCollection.WorkComponentOption.Visible, Nothing)
            For Each body As Body In thePart.Bodies
                ProcessBody(thePart, body) ' Pass thePart as a parameter
            Next

            ' Restore the original work part
            theSession.Parts.SetWork(originalWorkPart)

            ' Mark the part as processed
            processedParts.Add(thePart, True)
        End If

        reportComponentChildren(child, processedParts)
    Next
End Sub

Sub ProcessBody(ByVal part As Part, ByVal body As Body) ' Include part as a parameter
    Try
        blendSize = askBlendSize()

        Dim edgeBlendBuilder1 As NXOpen.Features.EdgeBlendBuilder = Nothing
        edgeBlendBuilder1 = workPart.Features.CreateEdgeBlendBuilder(nullNXOpen_Features_Feature)

        Dim scCollector1 As NXOpen.ScCollector = Nothing
        scCollector1 = workPart.ScCollectors.CreateCollector()

        Dim selectionIntentRuleOptions1 As NXOpen.SelectionIntentRuleOptions = Nothing
        selectionIntentRuleOptions1 = workPart.ScRuleFactory.CreateRuleOptions()

        selectionIntentRuleOptions1.SetSelectedFromInactive(False)

        ' Create a new selection intent rule for the current body
        edgeBodyRule1 = workPart.ScRuleFactory.CreateRuleEdgeBody(body, selectionIntentRuleOptions1)

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

        scCollector1.AddEvaluationFilter(NXOpen.ScEvaluationFiltertype.LaminarEdge)

        edgeBlendBuilder1.Tolerance = 0.01
        ' Set other edge blend builder options as needed

        Dim csIndex1 As Integer = Nothing
        csIndex1 = edgeBlendBuilder1.AddChainset(scCollector1, blendSize.ToString())

        Dim feature1 As NXOpen.Features.Feature = Nothing
        feature1 = edgeBlendBuilder1.CommitFeature()

        edgeBlendBuilder1.Destroy()

    Catch ex As Exception
        Dim partName As String = ""
        If Not IsNothing(part) Then
            partName = part.FullPath
            partName = IO.Path.GetFileNameWithoutExtension(partName)
        End If
        lw.WriteLine(ex.Message & "Try manually setting the size!")
        lw.WriteLine("  " & partName)
        lw.WriteLine("  ")
    End Try
End Sub

Function askBlendSize() As Double
    Dim blSz As Double
    If displayPart.PartUnits = BasePart.Units.Inches Then
        blSz = 0.008
    ElseIf displayPart.PartUnits = BasePart.Units.Millimeters Then
        blSz = 0.2
    End If
    Return blSz
End Function

Public Function GetUnloadOption(ByVal arg As String) As Integer
    Return Session.LibraryUnloadOption.Immediately
End Function

End Module
 
Status
Not open for further replies.
Back
Top