Changing the Part Color in assembly file.

Experimenter
Experimenter

I need help in Changing the Face Style of a part in the assembly document using dot net.

 

ScreenShot "face style" Option in Solid Edge is attached bellow.

 

 

1 REPLY

Re: Changing the Part Color in assembly file.

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

I prepared the following code sample, which sets the face style of all part occurrences in an assembly to "Steel" and then resets it again.

'----------------------------------------------------------------------
' ASM: Change the face style of all assembly occurrences to "Steel"
'----------------------------------------------------------------------
Private objApp As SolidEdgeFramework.Application = Nothing

Sub Main()
  objApp = Marshal.GetActiveObject("SolidEdge.Application")
  
  Dim strStyleName As String = "Steel"
  Dim objDoc As SolidEdgeAssembly.AssemblyDocument = objApp.Documents.Open("S:\Training\Coffee Pot.asm")
  ' Check if the style name exists
  Dim objStyles As FaceStyles = objDoc.FaceStyles
  Try
    Dim objSteel As FaceStyle = objStyles.Item(strStyleName)
  Catch ex As Exception
    MessageBox.Show("The face style [" & strStyleName & "] does not exist in the active assembly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
  End Try

  Dim bRecurse As Boolean = True
  SetStyleInAsm(objDoc, strStyleName, bRecurse)
  MsgBox("Click OK to reset all face styles", Microsoft.VisualBasic.MsgBoxStyle.OkOnly)
  SetStyleInAsm(objDoc, strStyleName, bRecurse, True)
End Sub

Sub SetStyleInAsm(ByVal objDoc As AssemblyDocument, ByVal styleName As String, ByVal bRecurse As Boolean, Optional ByVal bResetStyle As Boolean = False)
  For Each objOcc As Occurrence In objDoc.Occurrences
    If objOcc.Subassembly And bRecurse Then
      ' Set the face style for each individual subpart recursively
      SetStyleInSubOccs(objOcc.SubOccurrences, styleName, bResetStyle)
    Else
      ' To reset the face style, set it to an empty string
      objOcc.Style = IIf(bResetStyle, String.Empty, styleName)
    End If
  Next
End Sub

Sub SetStyleInSubOccs(ByVal objSubOccs As SubOccurrences, ByVal styleName As String, ByVal bResetStyle As Boolean)
  For Each objSubOcc As SubOccurrence In objSubOccs
    If objSubOcc.Subassembly Then
      SetStyleInSubOccs(objSubOcc, styleName, bResetStyle)
    Else
      ' To set the style in a suboccurrence, we need to access a reference to it
      ' To reset the face style, set it to an empty string
      objSubOcc.Reference.Style = IIf(bResetStyle, String.Empty, styleName)
    End If
  Next
End Sub