Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read out NX partname and replace it

Status
Not open for further replies.

Sim77Son

Mechanical
Aug 4, 2012
18
Hello,

first to explain what I try to do:
First I read out the filename from a NX part (NX version 10) which looks in our company like this (1521w1_0.115, 1521w1_St_01, 1521w1_z_BG and so on).
Then I look with InStr for the "_" and add 1 to the result so I get a number for the Mid function. The result till now is our position number (1521w1_0.115==>0.115, 1521w1_St_01==>St_01, 1521w1_z_BG==>z_BG).

But now my problem, I need to replace all results which are like this St_01, St_02, St_03 with a special number. That means St_01 will be replaced with 01.100, St_02 will be replaced with 02.100 and so on.

At the end I will write an attribute called POS which contains either the result from POS or if the partname was St_01 the result of POS2.


Here what I have done till now:



Imports NXOpen
Imports System.Text.RegularExpressions
Module NXJournal
Sub Main()

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

' das ist der filename ohne endung
Dim filename As String = workPart.Leaf
System.Windows.Forms.MessageBox.Show(filename)

' anzahl der zeichen
Ergebnis = InStr(1, filename, "_")
System.Windows.Forms.MessageBox.Show(Ergebnis)

zeichenposition = (Ergebnis + 1).ToString()
System.Windows.Forms.MessageBox.Show(zeichenposition)

' gibt aus "Positionsnummer"
Dim POS As String = Mid(filename, zeichenposition)
System.Windows.Forms.MessageBox.Show(POS)

' Stationen abändern
Dim POS2 As String
POS2 = POS.replace("St_01", "01.100")
POS2 = POS.replace("St_02", "02.100")

System.Windows.Forms.MessageBox.Show(POS2)

' Attribute
workPart.SetAttribute("POS", POS)

End Sub
End Module
 
Replies continue below

Recommended for you

HI,

So is what you're say that...
[tt]
_St_nn
[/tt]
Becomes...
[tt]
_nn.100
[/tt]
...and that nn can only be from 00 to 99
...and that there will never be any value AFTER [tt]_St_nn[/tt] in the original string?

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 

Yes, so it is.

I changed my work to this, this will work now but I think its not really the best way to solve this.


Imports System
Imports NXOpen
Imports System.Text.RegularExpressions
Module NXJournal
Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim Ergebnis As String
Dim zeichenposition As String
Dim filename As String
Dim POS As String

' das ist der filename ohne endung
filename = workPart.Leaf
'System.Windows.Forms.MessageBox.Show(filename)

' anzahl der zeichen
Ergebnis = InStr(1, filename, "_")
'System.Windows.Forms.MessageBox.Show(Ergebnis)

zeichenposition = (Ergebnis + 1).ToString()
'System.Windows.Forms.MessageBox.Show(zeichenposition)

' gibt "Positionsnummer" aus
POS = Mid(filename, zeichenposition)
'System.Windows.Forms.MessageBox.Show(POS)

If POS = "St_01" Then Pos = "01.100" Else
If POS = "St_02" Then Pos = "02.100" Else
If POS = "St_03" Then Pos = "03.100" Else
If POS = "St_04" Then Pos = "04.100" Else
If POS = "St_05" Then Pos = "05.100" Else
If POS = "St_06" Then Pos = "06.100" Else
If POS = "St_07" Then Pos = "07.100" Else
If POS = "St_08" Then Pos = "08.100" Else
If POS = "St_09" Then Pos = "09.100" Else
If POS = "St_10" Then Pos = "10.100" Else
If POS = "St_11" Then Pos = "11.100" Else
If POS = "St_12" Then Pos = "12.100" Else
If POS = "St_13" Then Pos = "13.100" Else
If POS = "St_14" Then Pos = "14.100" Else
If POS = "St_15" Then Pos = "15.100" Else
If POS = "St_16" Then Pos = "16.100" Else
If POS = "St_17" Then Pos = "17.100" Else
If POS = "St_18" Then Pos = "18.100" Else
If POS = "St_19" Then Pos = "19.100" Else
If POS = "St_20" Then Pos = "20.100" Else
If POS = "St_21" Then Pos = "21.100" Else
If POS = "St_22" Then Pos = "22.100" Else
If POS = "St_23" Then Pos = "23.100" Else
If POS = "St_24" Then Pos = "24.100" Else
If POS = "St_25" Then Pos = "25.100" Else
If POS = "St_26" Then Pos = "26.100" Else
If POS = "St_27" Then Pos = "27.100" Else
If POS = "St_28" Then Pos = "28.100" Else
If POS = "St_29" Then Pos = "29.100" Else
If POS = "St_30" Then Pos = "30.100" Else
If POS = "St_31" Then Pos = "31.100" Else
If POS = "St_32" Then Pos = "32.100" Else
If POS = "St_33" Then Pos = "33.100" Else
If POS = "St_34" Then Pos = "34.100" Else
If POS = "St_35" Then Pos = "35.100" Else
If POS = "St_36" Then Pos = "36.100" Else
If POS = "St_37" Then Pos = "37.100" Else
If POS = "St_38" Then Pos = "38.100" Else
If POS = "St_39" Then Pos = "39.100" Else
If POS = "St_40" Then Pos = "40.100" Else
If POS = "St_41" Then Pos = "41.100" Else
If POS = "St_42" Then Pos = "42.100" Else
If POS = "St_43" Then Pos = "43.100" Else
If POS = "St_44" Then Pos = "44.100" Else
If POS = "St_45" Then Pos = "45.100" Else
If POS = "St_46" Then Pos = "46.100" Else
If POS = "St_47" Then Pos = "47.100" Else
If POS = "St_48" Then Pos = "48.100" Else
If POS = "St_49" Then Pos = "49.100" Else
If POS = "St_50" Then Pos = "50.100" Else
If POS = "St_51" Then Pos = "51.100" Else
If POS = "St_52" Then Pos = "52.100" Else
If POS = "St_53" Then Pos = "53.100" Else
If POS = "St_54" Then Pos = "54.100" Else
If POS = "St_55" Then Pos = "55.100" Else
If POS = "St_56" Then Pos = "56.100" Else
If POS = "St_57" Then Pos = "57.100" Else
If POS = "St_58" Then Pos = "58.100" Else
If POS = "St_59" Then Pos = "59.100" Else
If POS = "St_60" Then Pos = "60.100" Else
If POS = "St_61" Then Pos = "61.100" Else
If POS = "St_62" Then Pos = "62.100" Else
If POS = "St_63" Then Pos = "63.100" Else
If POS = "St_64" Then Pos = "64.100" Else
If POS = "St_65" Then Pos = "65.100" Else
If POS = "St_66" Then Pos = "66.100" Else
If POS = "St_67" Then Pos = "67.100" Else
If POS = "St_68" Then Pos = "68.100" Else
If POS = "St_69" Then Pos = "69.100" Else
If POS = "St_70" Then Pos = "70.100" Else
If POS = "St_71" Then Pos = "71.100" Else
If POS = "St_72" Then Pos = "72.100" Else
If POS = "St_73" Then Pos = "73.100" Else
If POS = "St_74" Then Pos = "74.100" Else
If POS = "St_75" Then Pos = "75.100" Else
If POS = "St_76" Then Pos = "76.100" Else
If POS = "St_77" Then Pos = "77.100" Else
If POS = "St_78" Then Pos = "78.100" Else
If POS = "St_79" Then Pos = "79.100" Else
If POS = "St_80" Then Pos = "80.100" Else
If POS = "St_81" Then Pos = "81.100" Else
If POS = "St_82" Then Pos = "82.100" Else
If POS = "St_83" Then Pos = "83.100" Else
If POS = "St_84" Then Pos = "84.100" Else
If POS = "St_85" Then Pos = "85.100" Else
If POS = "St_86" Then Pos = "86.100" Else
If POS = "St_87" Then Pos = "87.100" Else
If POS = "St_88" Then Pos = "88.100" Else
If POS = "St_89" Then Pos = "89.100" Else
If POS = "St_90" Then Pos = "90.100" Else
If POS = "St_91" Then Pos = "91.100" Else
If POS = "St_92" Then Pos = "92.100" Else
If POS = "St_93" Then Pos = "93.100" Else
If POS = "St_94" Then Pos = "94.100" Else
If POS = "St_95" Then Pos = "95.100" Else
If POS = "St_96" Then Pos = "96.100" Else
If POS = "St_97" Then Pos = "97.100" Else
If POS = "St_98" Then Pos = "98.100" Else
If POS = "St_99" Then Pos = "99.100" Else
'System.Windows.Forms.MessageBox.Show(POS)

' Attribut setzen
workPart.SetAttribute("POS", POS)

End Sub
End Module
 
You can iterate like so

Dim temp as String

For index As Integer = 1 to 99
If index < 10 Then
temp = "St_0" & CStr(index)
Else
temp = "St_" & CStr(index)
EndIf
If POS = temp Then
If index < 10 Then
POS = "0" & CStr(index) & ".100"
Else
POS = CStr(index) & ".100"
EndIf
EndIf
Next
 
Thank you for your fast answer, work fine.
 
Here's a regular expression solution, just for fun. The St_Replace function does the work, the rest of the code in Sub Main is to test various string input.

Code:
Option Strict Off
Imports System
Imports System.Text.RegularExpressions
Imports NXOpen

Module Module1

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

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

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

        Dim myString As String = "1521w1_St_01"
        Dim outString As String = St_Replace(myString)

        lw.WriteLine("input string: " & myString)
        lw.WriteLine("output string: " & outString)
        lw.WriteLine("")

        myString = "1522z1_ST_34"
        outString = St_Replace(myString)

        lw.WriteLine("input string: " & myString)
        lw.WriteLine("output string: " & outString)
        lw.WriteLine("")

        myString = "1521w1_0.115"
        outString = St_Replace(myString)

        lw.WriteLine("input string: " & myString)
        lw.WriteLine("output string: " & outString)
        lw.WriteLine("")

        myString = workPart.Leaf
        outString = St_Replace(myString)

        lw.WriteLine("input string: " & myString)
        lw.WriteLine("output string: " & outString)
        lw.WriteLine("")

        lw.Close()

    End Sub

    Function St_Replace(ByVal inputString As String) As String

        'change search string as needed
        Dim strRegex As String = "St_(?<num>\d\d)"
        Dim myRegex As New Regex(strRegex, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
        'change replacement string as necessary
        Dim strReplace As String = "${num}.100"

        Return myRegex.Replace(inputString, strReplace)

    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

www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor