Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Nx open chamfer feature rename 1

Status
Not open for further replies.

Halasox

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

I have a script which renames a chamfer feature by its values, but as a chamfer has three types I don't know what I would need to add to rename it based on the feature type.

this is what I have and it works fine for symmetrical type.

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Features

Module ChamferInfo

    Sub Main()
        Dim s As Session = Session.GetSession()
        Dim ufs As UFSession = UFSession.GetUFSession()
        Dim lw As ListingWindow = s.ListingWindow
        Dim dp As Part = s.Parts.Work
        Dim exps() As Expression
        Dim expression_name As String = Nothing
        Dim featcoll As FeatureCollection = dp.Features
        Dim index1 As Integer = 0
        lw.Open()
        
        
        For Each f As Feature In featcoll
              'lw.WriteLine("Feature Type: " & f.FeatureType.ToString)
            If f.FeatureType = "CHAMFER" Then
                exps = f.GetExpressions()
                For Each exp As Expression In exps
                    expression_name = exp.Description
                    index1 = expression_name.IndexOf("Length")
                    If dp.PartUnits = BasePart.Units.Inches Then
                         f.SetName("Chamfer - " & exp.RightHandSide.ToString & " in x 45 deg")
                    elseif dp.PartUnits = BasePart.Units.Millimeters Then
                        f.SetName("Chamfer - " & exp.RightHandSide.ToString & " mm x 45 deg")
                    End If
                Next
            
            End If
        Next
        
        'Update the work part
        dp.ModelingViews.WorkView.Regenerate()
        
    End Sub

    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

What version of NX are you using?
If you are using a version before NX1899, I'd suggest using the ChamferFeatureBuilder to get info about the chamfer feature. You can get the offset type and related expressions directly from the builder object.

If you are using NX1899 or later, it gets more complicated. It looks like the chamfer feature uses a new definition behind the scenes called "ApexRangeChamfer". There is a corresponding ApexRangeChamferBuilder; unfortunately, it does not seem to have the same options as the old ChamferFeatureBuilder and will not give you the offset type or expressions directly.

If you have files that use both chamfer definitions, your best bet may be to get the expressions directly from the feature (similar to what you are doing now) and look for keywords in the expression description.

www.nxjournaling.com
 
Hi cowski, thanks for your help! I am using the latest version of NX. Agree think using expression might be the easiest way to go. I have made it work for two chamfer option, but not sure how to do it for the distance/distance one (how to filter them out). Should I use maybe array? Appreciate any king of help.

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Features

Module ChamferInfo

    Sub Main()
        Dim s As Session = Session.GetSession()
        Dim ufs As UFSession = UFSession.GetUFSession()
        Dim lw As ListingWindow = s.ListingWindow
        Dim dp As Part = s.Parts.Work
        Dim exps() As Expression
        Dim expression_name As String = Nothing
        Dim featcoll As FeatureCollection = dp.Features
        Dim index1 As Integer = 0
        Dim inp1 as String = ""
        Dim inp2 as String = ""
        'Dim count As Integer = expressions.Length
        lw.Open()
        
        'Dim vArrNP(0 To 1) As Double
        
        For Each f As Feature In featcoll
              'lw.WriteLine("Feature Type: " & f.FeatureType.ToString)
            If f.FeatureType = "CHAMFER" Then
                exps = f.GetExpressions()
                
                For Each exp As Expression In exps
                    expression_name = exp.Description
                    'lw.WriteLine(expression_name)
                    index1 = expression_name.IndexOf("Angle")
                    lw.WriteLine(index1)
                    If exps.Length > 1 Then
                        'expression_name = exp.Description
                        'lw.WriteLine(expression_name)
                        'index1 = expression_name.IndexOf("Angle")
                        'lw.WriteLine(index1)
                        If index1 = -1 Then
                             inp1 = exp.RightHandSide.ToString
                             'lw.WriteLine(inp1)
                             'f.SetName("Chamfer - " & exp.RightHandSide.ToString & " in x 45 deg")
                        ElseIf not index1 = -1 Then
                            inp2 = exp.RightHandSide.ToString
                            'f.SetName("Chamfer - " & exp.RightHandSide.ToString & " in x 45 deg")
                            'lw.WriteLine(inp2)
                        End If
                            f.SetName("Chamfer - " & inp1 & " in x " & inp2 & " deg")
                        
                        
                   
                        
                    ElseIf exps.Length < 2 Then
                        expression_name = exp.Description
                        f.SetName("Chamfer - " & exp.RightHandSide.ToString & " in x 45 deg")
                    End if
                
                Next
            
            End If
        Next
        
        'Update the work part
        dp.ModelingViews.WorkView.Regenerate()
        
    End Sub

    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
 
I took a stab at this today. My first thought was to get the expressions from the chamfer feature and look at the number of expressions and the expression descriptions. For a symmetric chamfer, only a single expression is returned, so this case is easy. For the asymmetric or offset-angle types, 2 expressions are returned. The descriptions for the asymmetric expressions will include "Distance 1" and "Distance 2"; the offset-angle type will include "Angle" and "Distance". We could search for these terms in the expression descriptions, but that will only work for people using english as the NX language. Ideally, our code would be independent of the regional language setting used by NX. Instead, I started looking at the units of the expressions returned; really we are only interested in the dimensionality of the expression units: asymmetric = 2 length expressions, offset-angle = 1 angle and 1 length expression. Unfortunately, I could not find a direct method to report the dimensionality of an expression (specifically of the unit used by the expression). The code below uses the .GetMeasureTypes() function to return all the units of type "Angle" and checks the expression units against this list. This strategy still depends on the english word "Angle", but since it is only used as a parameter to an API function call, I'm 90% sure this will work regardless of the user's regional language setting.

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

Module Module2

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()

    Dim theUI As UI = UI.GetUI()
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

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

        lw.Open()

        ' Get the measures of this UnitCollection -- "Length", "Area”, "Mass", etc.
        'Dim measureTypes As String() = theSession.Parts.Work.UnitCollection.GetMeasures()
        'For Each temp As String In measureTypes
        '    lw.WriteLine(temp)
        'Next
        'lw.WriteLine("")

        Dim angularUnits As New List(Of Unit)

        'input parameter for .GetMeasureTypes() must be one of the measure types [string] returned from .GetMeasures()
        Dim angularUnitArray() As Unit = theSession.Parts.Work.UnitCollection.GetMeasureTypes("Angle")
        For Each temp As Unit In angularUnitArray
            angularUnits.Add(temp)
            'lw.WriteLine(temp.TypeName)
        Next
        'lw.WriteLine("")

        For Each someFeat As Feature In theSession.Parts.Work.Features
            'lw.WriteLine("feature .GetType: " & someFeat.GetType.ToString)
            'lw.WriteLine("feature .FeatureType: " & someFeat.FeatureType)
            If TypeOf (someFeat) Is ApexRangeChamfer Or TypeOf (someFeat) Is Chamfer Then
                lw.WriteLine(someFeat.GetFeatureName)
                Dim someFeatExps() As Expression = someFeat.GetExpressions
                lw.WriteLine("number of expressions: " & someFeatExps.Length.ToString)
                If someFeatExps.Length > 1 Then
                    'asymmetric or offset-angle type chamfer
                    Dim offsetAngleType As Boolean = False
                    For Each tempExp As Expression In someFeatExps
                        If angularUnits.Contains(tempExp.Units) Then
                            'offset-angle chamfer type
                            offsetAngleType = True
                        End If
                    Next
                    If offsetAngleType Then
                        lw.WriteLine("offset-angle chamfer")
                    Else
                        lw.WriteLine("asymmetric chamfer")
                    End If
                    lw.WriteLine("  controlled by expression: " & someFeatExps(0).Name)
                    lw.WriteLine("  value: " & someFeatExps(0).GetValueUsingUnits(Expression.UnitsOption.Expression).ToString & " " & someFeatExps(0).Units.Abbreviation)
                    lw.WriteLine("  controlled by expression: " & someFeatExps(1).Name)
                    lw.WriteLine("  value: " & someFeatExps(1).GetValueUsingUnits(Expression.UnitsOption.Expression).ToString & " " & someFeatExps(1).Units.Abbreviation)

                Else
                    'symmetric chamfer
                    lw.WriteLine("symmetric chamfer")
                    lw.WriteLine("  controlled by expression: " & someFeatExps(0).Name)
                    lw.WriteLine("  value: " & someFeatExps(0).GetValueUsingUnits(Expression.UnitsOption.Expression).ToString & " " & someFeatExps(0).Units.Abbreviation)
                End If

            End If
            lw.WriteLine("")
        Next


        lw.Close()

    End Sub


    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

www.nxjournaling.com
 
I had a chance to dig a bit deeper on this one. The new chamfer feature allows the user to select multiple edge chains and apply different options and values to each chain. Not a great idea in my opinion, but - whatever ¯\_(ツ)_/¯. The creation option is stored in an edge chain sub builder. The code below checks for old or new chamfer features and uses the corresponding feature builder to query the chamfer. It eliminates some of the shenanigans in the earlier journal.

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

Module Module3

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()

    Dim theUI As UI = UI.GetUI()
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

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

        lw.Open()


        For Each someFeat As Feature In theSession.Parts.Work.Features
            'lw.WriteLine("feature .GetType: " & someFeat.GetType.ToString)
            'lw.WriteLine("feature .FeatureType: " & someFeat.FeatureType)

            If TypeOf (someFeat) Is Chamfer Then
                lw.WriteLine("chamfer: " & someFeat.GetFeatureName)
                Dim theChamferBuilder As ChamferBuilder = theSession.Parts.Work.Features.CreateChamferBuilder(someFeat)

                lw.WriteLine("  offset method: " & theChamferBuilder.Method.ToString)
                lw.WriteLine("  cross section type: " & theChamferBuilder.Option.ToString)
                'all chamfer types use the FirstOffsetExp expression
                'asymmetric also uses SecondOffsetExp
                'offset-angle uses AngleExp
                lw.WriteLine("    controlled by expression: " & theChamferBuilder.FirstOffsetExp.Name)
                lw.WriteLine("      value: " & theChamferBuilder.FirstOffsetExp.GetValueUsingUnits(Expression.UnitsOption.Expression).ToString & " " & theChamferBuilder.FirstOffsetExp.Units.Abbreviation)

                Select Case theChamferBuilder.Option
                    Case Is = ChamferBuilder.ChamferOption.SymmetricOffsets
                        'controlled by single expression, already reported above
                    Case Is = ChamferBuilder.ChamferOption.TwoOffsets
                        lw.WriteLine("    controlled by expression: " & theChamferBuilder.SecondOffsetExp.Name)
                        lw.WriteLine("      value: " & theChamferBuilder.SecondOffsetExp.GetValueUsingUnits(Expression.UnitsOption.Expression).ToString & " " & theChamferBuilder.SecondOffsetExp.Units.Abbreviation)
                    Case Is = ChamferBuilder.ChamferOption.OffsetAndAngle
                        lw.WriteLine("    controlled by expression: " & theChamferBuilder.AngleExp.Name)
                        lw.WriteLine("      value: " & theChamferBuilder.AngleExp.GetValueUsingUnits(Expression.UnitsOption.Expression).ToString & " " & theChamferBuilder.AngleExp.Units.Abbreviation)
                    Case Else
                        lw.WriteLine("    unknown offset type")
                End Select
                theChamferBuilder.Destroy()

            End If


            If TypeOf (someFeat) Is ApexRangeChamfer Then
                lw.WriteLine("apex range chamfer: " & someFeat.GetFeatureName)

                Dim apexChamferBuilder As ApexRangeChamferBuilder = theSession.Parts.Work.Features.DetailFeatureCollection.CreateApexRangeChamferBuilder(someFeat)
                lw.WriteLine("  offset method: " & apexChamferBuilder.OffsetMethod.ToString)

                Dim numChains As Integer = apexChamferBuilder.EdgeManager.EdgeChainSetList.Length
                lw.WriteLine("  number of edge chains: " & numChains.ToString)
                For i As Integer = 0 To numChains - 1
                    Dim temp As GeometricUtilities.ChamferEdgeChainSetBuilder = apexChamferBuilder.EdgeManager.EdgeChainSetList.FindItem(i)
                    lw.WriteLine("    edge chain " & i.ToString & " cross section type: " & temp.CrossSectionOptions.ToString)

                    lw.WriteLine("      controlled by expression: " & temp.Distance1.Name)
                    lw.WriteLine("        value: " & temp.Distance1.GetValueUsingUnits(Expression.UnitsOption.Expression).ToString & " " & temp.Distance1.Units.Abbreviation)

                    Select Case temp.CrossSectionOptions
                        Case Is = GeometricUtilities.ChamferEdgeChainSetBuilder.CrossSectionType.Symmetric
                            'controlled by single distance expression, already reported above

                        Case Is = GeometricUtilities.ChamferEdgeChainSetBuilder.CrossSectionType.Asymmetric
                            lw.WriteLine("      controlled by expression: " & temp.Distance2.Name)
                            lw.WriteLine("        value: " & temp.Distance2.GetValueUsingUnits(Expression.UnitsOption.Expression).ToString & " " & temp.Distance2.Units.Abbreviation)

                        Case Is = GeometricUtilities.ChamferEdgeChainSetBuilder.CrossSectionType.OffsetandAngle
                            lw.WriteLine("      controlled by expression: " & temp.Angular.Name)
                            lw.WriteLine("        value: " & temp.Angular.GetValueUsingUnits(Expression.UnitsOption.Expression).ToString & " " & temp.Angular.Units.Abbreviation)

                        Case Else
                            lw.WriteLine("    unknown cross section type")
                    End Select

                Next

                apexChamferBuilder.Destroy()

            End If
            lw.WriteLine("")
        Next


        lw.Close()

    End Sub

    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

www.nxjournaling.com
 
Thanks for the code it works great! I was also stuck on the distance/distance expression, didn't know how to separate them, but your version is much more elegant[thumbsup2]. I used the expression example for the edge/face blend and draft and it works fine due to only one value, but the chamfer was tricky. Again thanks for your help and all best!
 
Status
Not open for further replies.
Back
Top