Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Replace a style in the Part Document using API

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
Patrick1323
573 Views, 5 Replies

Replace a style in the Part Document using API

Hello,

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

Patrick1323_0-1603453447704.png

 

Labels (4)
5 REPLIES 5
Message 2 of 6
JelteDeJong
in reply to: Patrick1323

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

Message 3 of 6
Patrick1323
in reply to: JelteDeJong

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...

Message 4 of 6

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

Message 5 of 6

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

best regards
Patrick
Message 6 of 6
JelteDeJong
in reply to: Patrick1323

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report