Formatted Text without style overrides

Formatted Text without style overrides

Curtis_Waguespack
Consultant Consultant
548 Views
5 Replies
Message 1 of 6

Formatted Text without style overrides

Curtis_Waguespack
Consultant
Consultant

I'm trying to get the text from a general note on a drawing, and insert it into a form within an add-in.

 

But, if the note has style overrides such as this:

 

Curtis_Waguespack_1-1669670882852.png

(see attached 2022 drawing example)

 

This is the result that is returned using the GeneralNote.FormattedText

 

MsgBox(oGeneralNote.FormattedText,,"Formatted Text")

Curtis_Waguespack_2-1669670948846.png

 

and this is the result using just GeneralNote.Text

MsgBox(oGeneralNote.Text,,"Text")

Curtis_Waguespack_3-1669670959216.png

 

Neither of those are the results I'm after.

 

Ultimately, I'm looking to get something like this in a form:

Curtis_Waguespack_4-1669671435019.png

 

Not this:

Curtis_Waguespack_0-1669670732428.png

 

and not this:

Curtis_Waguespack_5-1669671495658.png

 

Is there another way to extract the text that I'm overlooking?

EESignature

0 Likes
Accepted solutions (2)
549 Views
5 Replies
Replies (5)
Message 2 of 6

JelteDeJong
Mentor
Mentor
Accepted solution

You could try to replace the text parts that you don't want with empty strings. And the "<br />" with new lines. Something like this:

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument

' a reference to the active sheet.
Dim oActiveSheet As Sheet
oActiveSheet = oDrawDoc.ActiveSheet

' a reference to the GeneralNotes object
Dim oGeneralNotes As GeneralNotes
oGeneralNotes = oActiveSheet.DrawingNotes.GeneralNotes

Dim oGeneralNote = oGeneralNotes.Item(1)
Dim stext = oGeneralNote.FormattedText

stext = Text.RegularExpressions.Regex.Replace(stext, "<StyleOverride [\s\S]*?>", "")
stext = Text.RegularExpressions.Regex.Replace(stext, "</StyleOverride>", "")
stext = Text.RegularExpressions.Regex.Replace(stext, "<Br/>", System.Environment.NewLine)

MsgBox(stext,, "Formatted Text")

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 6

Curtis_Waguespack
Consultant
Consultant

Thanks @JelteDeJong !

 

I think that will work!

 

 

EESignature

0 Likes
Message 4 of 6

Curtis_Waguespack
Consultant
Consultant

Hmmm, I thought that was going to provide a solution... and it did for the question I initially asked.

But upon further testing, I'm not seeing a way to extract the value from an iProperty (such as Part Number) or derived property ( such as sheet number) .

Curtis_Waguespack_1-1669739498275.png

It is instead just returning the property names

( example 2022 files attached)

Curtis_Waguespack_0-1669739194132.png

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument

' a reference to the active sheet.
Dim oActiveSheet As Sheet
oActiveSheet = oDrawDoc.ActiveSheet

' a reference to the GeneralNotes object
Dim oGeneralNotes As GeneralNotes
oGeneralNotes = oActiveSheet.DrawingNotes.GeneralNotes

Dim oGeneralNote = oGeneralNotes.Item(1)
Dim stext = oGeneralNote.FormattedText
stext = Text.RegularExpressions.Regex.Replace(stext, "<StyleOverride [\s\S]*?>", "") stext = Text.RegularExpressions.Regex.Replace(stext, "</StyleOverride>", "") stext = Text.RegularExpressions.Regex.Replace(stext, "<DerivedProperty [\s\S]*?>", "") stext = Text.RegularExpressions.Regex.Replace(stext, "</DerivedProperty>", "") stext = Text.RegularExpressions.Regex.Replace(stext, "<Property [\s\S]*?>", "") stext = Text.RegularExpressions.Regex.Replace(stext, "</Property>", "") stext = Text.RegularExpressions.Regex.Replace(stext, "<Br/>", System.Environment.NewLine) MsgBox(stext,, "Stripped Formatted Text")

EESignature

0 Likes
Message 5 of 6

JelteDeJong
Mentor
Mentor
Accepted solution

It became a bit more complex and also a bit longer. Also, it's not complete in all cases. (have a look at the comments in the code.) But It will work for your example.

Public Sub Main()

    Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument

    ' a reference to the active sheet.
    Dim oActiveSheet As Sheet
    oActiveSheet = oDrawDoc.ActiveSheet

    ' a reference to the GeneralNotes object
    Dim oGeneralNotes As GeneralNotes
    oGeneralNotes = oActiveSheet.DrawingNotes.GeneralNotes

    Dim oGeneralNote = oGeneralNotes.Item(1)
    Dim stext = oGeneralNote.FormattedText

    stext = RemoveStyleOverride(stext)
    stext = AddNewLines(stext)
    stext = ReplaceDerivedProperty(oActiveSheet, stext)
    stext = ReplaceProperty(oActiveSheet, stext)

    MsgBox(stext,, "Formatted Text")

End Sub

Private Function AddNewLines(text As String)
    Return System.Text.RegularExpressions.Regex.Replace(text, "<Br/>", System.Environment.NewLine)
End Function

Private Function RemoveStyleOverride(text As String)
    Dim regexProperty = "<StyleOverride [\s\S]*?>([\s\S]*?)</StyleOverride>"
    Dim regex As New Text.RegularExpressions.Regex(regexProperty)


    Dim someThingChanged = False
    Do
        someThingChanged = False

        Dim match As Text.RegularExpressions.Match = regex.Match(text)
        If (match.Success) Then

            Dim orgText = match.Groups.Item(1).Value
            text = text.Replace(match.Groups.Item(0).Value, orgText)

            someThingChanged = True
        End If
    Loop While someThingChanged

    Return text
End Function


Private Function ReplaceDerivedProperty(sheet As Sheet, text As String)

    Dim regexProperty = "<DerivedProperty DerivedID='([\s\S]*?)'>([\s\S]*?)<\/DerivedProperty>"
    Dim regex As New Text.RegularExpressions.Regex(regexProperty)

    Dim someThingChanged = False
    Do
        someThingChanged = False
        Dim match As Text.RegularExpressions.Match = regex.Match(text)

        If (match.Success) Then
            Dim derivedID = match.Groups.Item(1).Value

            Dim newValue = String.Empty
            ' This are not all ID's you might want to add more.
            Select Case derivedID
                Case "29703"
                    newValue = ThisDoc.Document.Sheets.Count
                Case "29707"
                    newValue = sheet.DrawingViews.Item(1).ScaleString
            End Select

            text = text.Replace(match.Groups.Item(0).Value, newValue)
            someThingChanged = True
        End If
    Loop While someThingChanged

    Return text
End Function

Private Function ReplaceProperty(sheet As Sheet, text As String)

    Dim regexProperty = "<Property Document='([\S]*?)' PropertySet='([\s\S]*?)' Property='([\s\S]*?)' FormatID='([\s\S]*?)' PropertyID='([\s\S]*?)'>([\s\S]*?)<\/Property>"
    Dim regex As New Text.RegularExpressions.Regex(regexProperty)
    Dim match As Text.RegularExpressions.Match = regex.Match(text)

    If (match.Success) Then
        Dim docDirection = match.Groups.Item(1).Value
        Dim propSetName = match.Groups.Item(2).Value
        Dim propName = match.Groups.Item(3).Value


        Dim doc As Document = ThisDoc.Document
        ' this if statement is just 1 example. At som point you might want to look for other documents
        If (docDirection.Equals("model")) Then
            doc = sheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument
        End If

        Dim propSet = doc.PropertySets.Item(propSetName)
        Dim propValue = propSet.Item(propName).Value

        Return text.Replace(match.Groups.Item(0).Value, propValue)
    End If

    Return text
End Function

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 6 of 6

Curtis_Waguespack
Consultant
Consultant

HI @JelteDeJong

Thanks for the taking the time on this!

I was hoping I was just overlooking some other, more simple method.

I'm not sure how far to take this... 🤔 🤔 🤔

do I really want to have a select case for every possible derived property?

can I extract the referenced view more dynamically?

I don't know?

It doesn't seem like it should be this involved, to get the text out 😉

But I appreciate the effort and time you put into looking into this!

EESignature

0 Likes