INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Read out NX partname and replace it

Read out NX partname and replace it

(OP)
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

RE: Read out NX partname and replace it

HI,

So is what you're say that...

_St_nn

Becomes...

_nn.100

...and that nn can only be from 00 to 99
...and that there will never be any value AFTER _St_nn in the original string?

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Read out NX partname and replace it

(OP)

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

RE: Read out NX partname and replace it

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

RE: Read out NX partname and replace it

(OP)
Thank you for your fast answer, work fine.

RE: Read out NX partname and replace it

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close