Find and replace note content

Hi,
I would like to find a specific Note with a name MAT and replace the content of that note in entire drawing, note was used more than 150 times in each drawing....
please help me with a code to replace that.....

Below is a journal that looks for note objects named "MAT" and replaces the text.

Option Strict Off
Imports System
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 = "update notes"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

Const noteName As String = "MAT"
Const noteText As String = "new note text"

For Each tempNote As Annotations.Note In workPart.Notes

If tempNote.Name = noteName Then

Dim draftingNoteBuilder1 As Annotations.DraftingNoteBuilder
draftingNoteBuilder1 = workPart.Annotations.CreateDraftingNoteBuilder(tempNote)

draftingNoteBuilder1.Origin.SetInferRelativeToGeometry(True)

Dim text1(0) As String
text1(0) = noteText
draftingNoteBuilder1.Text.TextBlock.SetText(text1)

Dim nXObject1 As NXObject
nXObject1 = draftingNoteBuilder1.Commit()

draftingNoteBuilder1.Destroy()

End If

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

Hi,
Its working fine but how can I add multiple notes to replace in this.

-[]-

Do you mean that you have multiple notes with different names and you want to change the note text based on the name of the note object?

If so, I would suggest changing the If block to a Select Case. This would allow you to test for multiple different note names and take different actions based on the name.

Here are some links about the Select Case construct:
https://msdn.microsoft.com/en-us/library/cy37t14y.aspx

http://www.dotnetperls.com/select-vbnet

Hello, this is working really good.

Is it possible to delete a named note?

Thank you and best regards

Once you find the named note, add it to the delete list and perform an update. If you record a journal while deleting an object, the resulting code will show you the required commands.

Hello and thank you for your help.

Sorry, I forgot the code the last time. I have already tried what you suggested. Add it to a list and update. That's why I was asking. There's no error but it is not working unfortunately.


Sub DeleteNote(ByVal NoteDesc As String)

Dim workPart As Part = theSession.Parts.Work
Dim NotesToDelete As New List(Of Annotations.Note)

For Each tempNote As Annotations.Note In workPart.Notes

If tempNote.Name = NoteDesc Then

NotesToDelete.Add(tempNote)

End If

Next

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Delete")

Dim notifyOnDelete1 As Boolean
notifyOnDelete1 = theSession.Preferences.Modeling.NotifyOnDelete

theSession.UpdateManager.ClearErrorList()

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(NotesToDelete.ToArray)

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId2)

End Sub

NX likes to convert the names of objects to all caps, try converting the name string to all upper case before the comparison. If you name the note object "test", NX will convert it to "TEST". When you pass in "test" to the subroutine, NX will not find the note because "test" <> "TEST".

Better yet, convert both the input string and the object name string to upper case before the comparison.

Sub DeleteNote(ByVal NoteDesc As String)

Dim workPart As Part = theSession.Parts.Work
Dim NotesToDelete As New List(Of Annotations.Note)

For Each tempNote As Annotations.Note In workPart.Notes

If tempNote.Name.ToUpper = NoteDesc.ToUpper Then

NotesToDelete.Add(tempNote)

End If

Next

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Delete")

Dim notifyOnDelete1 As Boolean
notifyOnDelete1 = theSession.Preferences.Modeling.NotifyOnDelete

theSession.UpdateManager.ClearErrorList()

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(NotesToDelete.ToArray)

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId2)

End Sub

This post was almost very useful to me 100%, except I don't know where is Note Name. For my purpose, I was looking for a way to find a note with specific text like and delete the text or the note. But the code above is not working since I don't know how to find the name of the note itself. Any idea please?

Regards,
MFJ

The comment here has a few links to code (on this site and eng-tips) that shows how to find a note by its text.

http://nxjournaling.com/comment/2768#comment-2768

Thanks for the link. But I also need to know how to assign a name to a text , while creating a next. It doesn't have to be automated. I also don't want to assign a name after the note is created. I want to assign a name while creating the note. Is it possible?

Regards,
MFJ

I guess, my question is - while creating a note, how to assign a name to it, with or without automation. So that I could use that name for automation.

Regards,
MFJ

In NXOpen, once you have a reference to a note object, you can use its .SetName method to give it a name.

In interactive NX, right click on a note object -> properties -> general -> enter a name and OK the dialog. Be aware that NX will convert the input to all capital letters; this will be important when searching for the name with journal code.

Thanks for the reply. I think this should work for me. Is there any example of nameing using NXOpen? If not, how can I find the 'reference to a note object' I just created?

Regards,
MFJ

The AnnotationManager's .CreateNote method will return a Note type object. You can use this object to further modify the note after it is created. Some VB pseudocode below:

Dim myNote as Annotations.Note
myNote = workpart.Annotations.CreateNote(...)
myNote.SetName("TEST")

Thanks a lot. I'll try this tomorrow.

Regards,
MFJ

Can I createnote with the draftingnotebuilder where I called an expression from the part to create a note?

Regards,
MFJ

Nevermind. It worked perfectly. I just used the drafting notebuilder to call the expression, then used .creatnotebuilder to call the draftingnotebuilder.

Regards,
MFJ