Quantcast
Channel: NX Journaling - Journaling / NXOpen API
Viewing all articles
Browse latest Browse all 787

Replacing Text in a Table with Formatted text

$
0
0

Hello All,

So I am working on a bit of script to replace cells in my tables with new information. It looks for the thing I want to replace and replaces it with new text. I have managed to do this part.

The problem I am having is that when I am writing the code, I would like to have the option to add a newline into the cell. I want to do this when I put in the new string, but when I use \n or \r it just exports "\r".

Is there a way to do this other than having some code which looks for that character in the string I say I want to replace, separates the code, and then adds the newline and the 2 different string to the write to cell?

' ------------------------------------------------------------------------------------------------------------' NX 8.5.2.3' Replace Cell Text' Created 13 Jan 2017' ------------------------------------------------------------------------------------------------------------ 
Option Strict Off
Imports System
Imports System.Collections.GenericImports NXOpen
Imports NXOpenUI
Imports NXOpen.UF 
 
Module Label_Information_Extraction
 
	'--------------------------------------' Sets up the NX session'-------------------------------------- 
	Dim theSession As Session = Session.GetSession()Dim theUISession As UI = UI.GetUIDim theUfSession As UFSession = UFSession.GetUFSession()Dim workPart As Part = theSession.Parts.Work 
	Sub Main() 
		'--------------------------------------' Opens the Listing Window'-------------------------------------- 
		Dim lw As ListingWindow = theSession.ListingWindow
		lw.Open()Dim OldText AsString=""Dim NewText AsString=""Dim NewText2 AsString=""Dim NumberOfReplacements AsInteger=0 
		OldText = NXInputBox.GetInputString("Input the String you would like to replace:", "Table Text Replacement")
		NewText = NXInputBox.GetInputString("Input the String you would like to the old text with:", "Table Text Replacement")
		NewText2 = NXInputBox.GetInputString("Input the String you would like to the old text with:", "Table Text Replacement") 
		lw.WriteLine("You would like to replace: "& OldText &" with: "& NewText &".") 
		If NewText !=""Then
			FindTabularNotes(lw, OldText, NewText, NewText2, NumberOfReplacements)Else
			lw.WriteLine("You did not insert Text, if you want to replace please run again")EndIf 
		lw.WriteLine("We found and replaced: "& NumberOfReplacements.ToString()&" occurances of "& OldText &" with: "& NewText &".") 
 
	EndSub 
	Sub FindTabularNotes(lw As ListingWindow, OldText AsString, NewText AsString, NewText2 AsString, ByRef numreplaced AsInteger) 
		Dim tmpTabNote As NXOpen.Tag= NXOpen.Tag.NullDim NxType AsIntegerDim NxSubtype AsIntegerDim tmpTabNoteObj as displayableObject
		Dim numTables AsInteger=0 
		Do
			theUfSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_tabular_note_type, tmpTabNote) 
			If tmpTabNote <> NXOpen.Tag.NullThen 
				theUfSession.Obj.AskTypeAndSubtype(tmpTabNote, NxType, NxSubtype) 
				If NxSubtype = UFConstants.UF_tabular_note_subtypeThen 
					'Now we need to search for text
					FindReplaceText(tmpTabNote, lw, OldText, NewText, NewText2 , numreplaced)
					numTables +=1 
				EndIf 
			EndIf 
		Loop Until tmpTabNote = NXOpen.Tag.Null 
	EndSub 
 
	Sub FindReplaceText(tmpTabNote As Tag, lw As ListingWindow, OldText AsString, NewText AsString, NewText2 AsString, ByRef numreplaced AsInteger) 
		Dim rowTag As Tag =NothingDim colTag As Tag =NothingDim numRows AsInteger=0Dim numCols AsInteger=0Dim cellText AsString=""Dim NewCellText AsString=""Dim cellTag As Tag =Nothing 
		theUfSession.Tabnot.AskNmColumns(tmpTabNote , numCols)
		theUfSession.Tabnot.AskNmRows(tmpTabNote , numRows) 
		For y AsInteger=0To numRows-1 
			For x AsInteger=0To numCols-1 
 
				theUfSession.Tabnot.AskNthRow(tmpTabNote , y, rowTag)
				theUfSession.Tabnot.AskNthColumn(tmpTabNote , x, colTag)
				theUfSession.Tabnot.AskCellAtRowCol(rowTag, colTag, cellTag)
				theUfSession.Tabnot.AskCellText(cellTag, cellText) 
				If celltext.Contains(OldText)Then
					numreplaced +=1
					NewCellText = celltext.Replace(OldText, NewText)If NewText2 !=""Then
						theUfSession.Tabnot.SetCellText(cellTag, NewCellText & vbLf & NewText2)Else
						theUfSession.Tabnot.SetCellText(cellTag, NewCellText)EndIfEndIf 
			Next 
		Next 
	EndSub 
EndModule

Viewing all articles
Browse latest Browse all 787

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>