Purge styles?

Purge styles?

CadUser46
Collaborator Collaborator
1,128 Views
7 Replies
Message 1 of 8

Purge styles?

CadUser46
Collaborator
Collaborator

Hello

 

Im trying to simulate the purge styles button in amongst other things but am always left with outstanding styles in the list when i go back to the manage>purge button.

 

1.  Is the stylesmanager.styles collection a collection of all of the other styles?  Or do i need to loop through each of the styles manager collections deleting them individually?

 

In the first instance i had this code:

 

Sub PurgeStyles()

Dim oDoc As Inventor.DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oStyles As DrawingStylesManager
Set oStyles = oDoc.StylesManager

Dim ostyle As Style

For Each ostyle In oStyles.Styles
    If (ostyle.StyleLocation = kLocalStyleLocation) And (ostyle.InUse = False) Then
    ostyle.Delete
    End If
    Next
      
End Sub

 

In the second i went to this extenet:

 

Sub PurgeStyles2()

Dim oDoc As Inventor.DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oStyles As DrawingStylesManager
Set oStyles = oDoc.StylesManager
Dim oActive As DrawingStandardStyle
Dim oBalloon As BalloonStyle
Dim oCMark As CentermarkStyle
Dim oDimStyle As DimensionStyle
Dim oFCFS As FeatureControlFrameStyle
Dim oHTS As HoleTableStyle
Dim oLayer As Layer
Dim oLeader As LeaderStyle
Dim oODS As ObjectDefaultsStyle
Dim oSS As DrawingStandardStyle
Dim ostyle As Style
Dim oSTS As SurfaceTextureStyle
Dim oText As TextStyle

For Each oBalloon In oStyles.BalloonStyles
    If (oBalloon.StyleLocation = kLocalStyleLocation) And (oBalloon.InUse = False) Then
    oBalloon.Delete
    End If
    Next
For Each oCMark In oStyles.CentermarkStyles
    If (oCMark.StyleLocation = kLocalStyleLocation) And (oCMark.InUse = False) Then
    oCMark.Delete
    End If
    Next
For Each oDimStyle In oStyles.DimensionStyles
    If (oDimStyle.StyleLocation = kLocalStyleLocation) And (oDimStyle.InUse = False) Then
    oDimStyle.Delete
    End If
    Next
For Each oFCFS In oStyles.FeatureControlFrameStyles
    If (oFCFS.StyleLocation = kLocalStyleLocation) And (oFCFS.InUse = False) Then
    oFCFS.Delete
    End If
    Next
For Each oHTS In oStyles.HoleTableStyles
    If (oHTS.StyleLocation = kLocalStyleLocation) And (oHTS.InUse = False) Then
    oHTS.Delete
    End If
    Next
For Each oLayer In oStyles.Layers
    If (oLayer.StyleLocation = kLocalStyleLocation) And (oLayer.InUse = False) Then
    oLayer.Delete
    End If
    Next
For Each oLeader In oStyles.LeaderStyles
    If (oLeader.StyleLocation = kLocalStyleLocation) And (oLeader.InUse = False) Then
    End If
    Next
For Each oODS In oStyles.ObjectDefaultsStyles
    If (oODS.StyleLocation = kLocalStyleLocation) And (oODS.InUse = False) Then
    oODS.Delete
    End If
    Next
For Each oSS In oStyles.StandardStyles
    If (oSS.StyleLocation = kLocalStyleLocation) And (oSS.InUse = False) Then
    oSS.Delete
    End If
    Next
For Each ostyle In oStyles.Styles
    If (ostyle.StyleLocation = kLocalStyleLocation) And (ostyle.InUse = False) Then
    ostyle.Delete
    End If
    Next
For Each oSTS In oStyles.SurfaceTextureStyles
    If (oSTS.StyleLocation = kLocalStyleLocation) And (oSTS.InUse = False) Then
    oSTS.Delete
    End If
    Next
For Each oText In oStyles.TextStyles
    If (oText.StyleLocation = kLocalStyleLocation) And (oText.InUse = False) Then
    oText.Delete
    End If
    Next
      
End Sub

 

Am i way off the mark or just missing some small point?

 

Thanks.


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
1,129 Views
7 Replies
Replies (7)
Message 2 of 8

YuhanZhang
Autodesk
Autodesk

The StylesManager.Styles is the general collection for all the styles, so if your method #1 does not delete all the styles you want to delete, I guess that the reason maybe that you have some styles(call sub-style) that are used by other styles, so in your loop, the sub-style maybe ignored. So you need to check if the style is a sub-style, and you can only delete it when all the styles that referencing it are deleted first.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 3 of 8

CadUser46
Collaborator
Collaborator

Cheers Rocky, i will have a further look into it when i get a chance.

 

Just to add something to your suggestion.  When i am pressing the button Manage > Purge the tick box for include sub styles is not ticked so i would have to assume the ones remaining in the list are not sub styles?

 

If it wasnt that is there a smarter way i should be writing the IF statement to be more/less specific?

 


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
Message 4 of 8

CadUser46
Collaborator
Collaborator

I need to run the first macro 3 times to get it to clear all the styles.  I cant quite figure out how to get it to keep looping.  Any pointers?


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
Message 5 of 8

Anonymous
Not applicable

Maybe add a flag to see if you got any that pass?

 

public void purgeStyles(Inventor.DrawingDocument doc)
{
    Boolean noneLeft;
    do
    {
        noneLeft = true;
        foreach (Inventor.Style s in doc.StylesManager.Styles)
        {
            if (!s.InUse && s.StyleLocation == Inventor.StyleLocationEnum.kLocalStyleLocation)
            {
                s.Delete();
                noneLeft = false;
            }
        }
    }
    while (noneLeft);
}

 

0 Likes
Message 6 of 8

CadUser46
Collaborator
Collaborator

Interesting, i shall try that out.  Is that C#?


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
Message 7 of 8

Anonymous
Not applicable

Yeah. I mostly work in java so C#'s syntax feels alot more natural to me. Let me know if it works.

0 Likes
Message 8 of 8

CadUser46
Collaborator
Collaborator

Thanks bd727, your concept worked after i got the syntax right.

 

Private Sub PurgeStyles()

Dim odoc As Inventor.DrawingDocument
Set odoc = ThisApplication.ActiveDocument

Dim oStyles As DrawingStylesManager
Set oStyles = odoc.StylesManager

Dim noneleft As Boolean
noneleft = True

Dim ostyle As Style
   
Do While (noneleft)
noneleft = False
    For Each ostyle In oStyles.Styles
        If (ostyle.StyleLocation = kLocalStyleLocation) And (ostyle.InUse = False) Then
        ostyle.Delete
        noneleft = True
        End If
    Next
Loop

End Sub


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro