Replace a style in the Part Document using API

Replace a style in the Part Document using API

Patrick1323
Enthusiast Enthusiast
908 Views
5 Replies
Message 1 of 6

Replace a style in the Part Document using API

Patrick1323
Enthusiast
Enthusiast

Hello,

is there a way to perform this action (replace Style) in the part document, but by code?

Patrick1323_0-1603453447704.png

 

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

JelteDeJong
Mentor
Mentor

i could not find a command for Replacing a text style. But i found a way to delete the style and copy the other. it works like this:

Dim originalStyleName As String = "ReplaceMe"
Dim replaceBy As String = "replaceWithMe"

Dim doc As PartDocument = ThisDoc.Document
Dim def As PartComponentDefinition = doc.ComponentDefinition

Dim textStyleOrg As TextStyle = Nothing
Dim textStyleNew As TextStyle = Nothing
For Each style As TextStyle In doc.TextStyles
    If (Style.Name = originalStyleName) Then
        textStyleOrg = Style
    End If
    If (Style.Name = replaceBy) Then
        textStyleNew = Style
    End If
Next

textStyleOrg.Delete()
textStyleNew.Copy(originalStyleName)

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

0 Likes
Message 3 of 6

Patrick1323
Enthusiast
Enthusiast

Hello JelteDeJong,

thanks for your response, unfortunately I can't delete styles which are in use.

This will throw an exception:

 

textStyleOrg.Delete()

 

 

As far as I know there's no way to create a "Standard" like for the drawing styles, and set that new standard as the active one. Then I would be able to delete the old style...

0 Likes
Message 4 of 6

wolfgang_nickl
Advocate
Advocate

Hello Patrick1323,

I don't know what you want exatly, but could it be a solution to redefine the Style in Style library as you want and then Update the Stile from Style library?

extStyleOrg.UpdateFromGlobal()

Best Regards

Wolfgang

0 Likes
Message 5 of 6

Patrick1323
Enthusiast
Enthusiast
Hello Wolfgang,
thats not want to do, I want to delete a style in use.

best regards
Patrick
0 Likes
Message 6 of 6

JelteDeJong
Mentor
Mentor
Accepted solution

does this work for you:

Dim originalStyleName As String = "Note Text (ANSI) - 3DA"
Dim replaceBy As String = "Sketch Text (ANSI) - 3DA"

Dim doc As PartDocument = ThisDoc.Document
Dim def As PartComponentDefinition = doc.ComponentDefinition

Dim textStyleOrg As TextStyle = Nothing
Dim textStyleNew As TextStyle = Nothing
For Each style As TextStyle In doc.TextStyles
    If (Style.Name = originalStyleName) Then
        textStyleOrg = Style
    End If
    If (Style.Name = replaceBy) Then
        textStyleNew = Style
    End If
Next

' in this part all properties from textStyleNew are copyed to textStyleOrg
Dim type As Type = GetType(Inventor.TextStyle)
For Each prop As Reflection.PropertyInfo In type.GetProperties()
    If (prop.CanWrite) Then
        If (prop.Name = "Name" Or prop.Name = "_Name") Then
            Continue For
        End If

        Dim obj = prop.GetValue(textStyleNew)
        prop.SetValue(textStyleOrg, obj)

    End If
Next
' both styles are now the same.

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