Apply Material (Delegate)

Hi,

I've been reading alot about the avalaible delegates with SldWorks :

2017 SOLIDWORKS API Help - SolidWorks.Interop.sldworks Namespace

I can't seem to find a way for my addin to know when a material is applied to the part. For now, i am using the SaveFileNotify but it really isn't optimal.

Anyone has an idea?

Here's the my code to transfert Material Custom Properties to the Configuration Specific Custom Properties. Starting Function is TransfererProprietePersonalisee_Material :

    '''

    '''

    '''

    Private Class Classification

        Public Property Name As String = ""

        Public Property Materials As New List(Of Material)

        Public Sub New()

        End Sub

        Public Sub New(_Name As String)

            Name = _Name

        End Sub

    End Class

    '''

    '''

    '''

    Private Class Material

        Public Property Name As String = ""

        Public Property CustomProperties As New List(Of CustomProperty)

        Public Sub New()

        End Sub

        Public Sub New(_Name As String)

            Name = _Name

        End Sub

    End Class

    '''

    '''

    '''

    Private Class CustomProperty

        Public Property Name As String

        Public Property Value As String

        Public Sub New()

        End Sub

        Public Sub New(_NomPropriete As String)

            Name = _NomPropriete

        End Sub

        Public Sub New(_NomPropriete As String, _Valeur As String)

            Name = _NomPropriete

            Value = _Valeur

        End Sub

    End Class

    '''

    ''' Transfert les proprietes personalisees du materaux choisi vers file->properties->custom

    '''

    Public Sub TransfererProprietePersonalisee_Material(swApp As sldworks.SldWorks)

        'ugogo

        Dim ListClassifications As List(Of Classification) = ParseXMLCustomMaterial()

        Dim swDoc As sldworks.ModelDoc2 = swApp.ActiveDoc

        Dim swPartDoc As sldworks.PartDoc = swDoc

        'Dim OriginalConfig As String = swDoc.ConfigurationManager.ActiveConfiguration.Name

        For Each Configuration As String In swDoc.GetConfigurationNames

            Dim MaterialDbInfo As String = ""

            Dim Material As String = swPartDoc.GetMaterialPropertyName2(Configuration, MaterialDbInfo)

            DeleteMaterialCustomProperties(swDoc, Configuration, ListClassifications)

            For Each prop As CustomProperty In AddMaterialPropertiesToCustom(Material, ListClassifications)

                swDoc.AddCustomInfo3(Configuration, prop.Name.ToString, swCustomInfoType_e.swCustomInfoText, prop.Value.ToString)

            Next

        Next

        'swDoc.ShowConfiguration2(OriginalConfig)

    End Sub

    Private Function GetMaterial(swDoc As sldworks.ModelDoc2) As String

        Dim SwFeat As sldworks.Feature = swDoc.FirstFeature()

        'Fait le tour du feature tree (gauche) pour trouver le matériau sélectionner

        Do While Not SwFeat Is Nothing

            If SwFeat.GetTypeName2.ToUpper = "MATERIALFOLDER" Then

                Return SwFeat.Name

            End If

            SwFeat = SwFeat.GetNextFeature

        Loop

    End Function

    Private Function AddMaterialPropertiesToCustom(Material As String, ListClassifications As List(Of Classification)) As List(Of CustomProperty)

        Dim ReturnList As New List(Of CustomProperty)

        For Each Classif As Classification In ListClassifications

            For Each Mat As Material In Classif.Materials.Where(Function(x) x.Name = Material)

                ReturnList.AddRange(Mat.CustomProperties)

            Next

        Next

        Return ReturnList

    End Function

    '''

    '''

    '''

    Private Function DeleteMaterialCustomProperties(swDoc As sldworks.ModelDoc2, Configuration As String, ListClassifications As List(Of Classification)) As List(Of CustomProperty)

        Dim ReturnList As New List(Of CustomProperty)

        Dim PropertyNames As String() = swDoc.GetCustomInfoNames2(Configuration)

            For i As Integer = 0 To PropertyNames.Count - 1

            If IsMaterialCustomProp(PropertyNames(i), ListClassifications) Then

                swDoc.DeleteCustomInfo2(Configuration, PropertyNames(i))

            Else

                    'Faire une liste de ce qui est déjà traité?

                End If

            'Else

            '    'ReturnList.Add(New CustomProperty(PropertyNames(i), swDoc.GetCustomInfoValue(Configuration, PropertyNames(i))))

            '    ReturnList.Add(New CustomProperty(PropertyNames(i)))

            'End If

        Next

        Return ReturnList

    End Function

    Private Function IsMaterialCustomProp(PropertyName As String, ListClassifications As List(Of Classification)) As Boolean

        For Each Classif As Classification In ListClassifications

            For Each Mat As Material In Classif.Materials

                If Mat.CustomProperties.Where(Function(x) x.Name = PropertyName).Count Then Return True

            Next

        Next

        Return False

    End Function

    '''

    ''' '''Va chercher toute les propriete custom des materiaux custom dans la librairie du poste et sur le réseau

    '''

    '''

    Private Function ParseXMLCustomMaterial() As List(Of Classification)

        'check for ING library of custom mats

        Dim ReturnList As New List(Of Classification)

        Dim PathCustomMatING As String = MyConfig.ConfigApplications.PathCustomMatING

        Dim fichierArray() As String = Directory.GetFiles(PathCustomMatING)

        Dim PathXML As New List(Of String)

        If fichierArray.Count = 0 Then

            MsgBox("Impossible d'accèder au répertoire de matériaux : " + vbCr + PathCustomMatING)

        End If

        For Each fichier As String In fichierArray

            If Path.GetExtension(fichier) = ".sldmat" Then PathXML.Add(fichier)

        Next

        'check for local custommatfile xml (tries french then english)

        If File.Exists(MyConfig.ConfigApplications.PathCustomsldmatlocalfrench) Then

            PathXML.Add(MyConfig.ConfigApplications.PathCustomsldmatlocalfrench)

        Else

            If File.Exists(MyConfig.ConfigApplications.PathCustomsldmatlocalenglish) Then

                PathXML.Add(MyConfig.ConfigApplications.PathCustomsldmatlocalenglish)

            Else

                MsgBox("Le Fichier de matériaux personnalisés local est introuvable sur ce poste")

                'Exit Sub

            End If

        End If

        If PathXML.Count = 0 Then

            MsgBox("Aucune librairie de matériaux personnalisés n'a pu être trouvée")

            Exit Function

        End If

        For Each fichier As String In PathXML

            ReturnList.AddRange(ParseXMLTraitement(fichier))

        Next

        Return ReturnList

    End Function

    '''

    ''' Lit chacun des fichier XML trouvés pour extraire les proprietes personnalisées

    '''

    '''

    Private Function ParseXMLTraitement(PathXML As String) As List(Of Classification)

        Dim ReturnList As New List(Of Classification)

        Dim Configuration As New System.Xml.XmlDocument

        Configuration.Load(New System.IO.StreamReader(PathXML))

        Dim ClassificationNode As System.Xml.XmlNode = Configuration.FirstChild.NextSibling.FirstChild.NextSibling

        While Not ClassificationNode Is Nothing

            Dim newClassification As New Classification(ClassificationNode.Attributes("name").Value)

            Dim MaterialNode As System.Xml.XmlNode = ClassificationNode.FirstChild

            While Not MaterialNode Is Nothing

                Dim newMaterial As New Material(MaterialNode.Attributes("name").Value)

                Dim CustomNode As System.Xml.XmlNode = MaterialNode.SelectSingleNode("custom")

                While Not CustomNode Is Nothing

                    Dim PropNode As System.Xml.XmlNode = CustomNode.FirstChild

                    While Not PropNode Is Nothing

                        newMaterial.CustomProperties.Add(New CustomProperty(PropNode.Attributes("name").Value, PropNode.Attributes("value").Value))

                        PropNode = PropNode.NextSibling

                    End While

                    CustomNode = CustomNode.NextSibling

                End While

                newClassification.Materials.Add(newMaterial)

                MaterialNode = MaterialNode.NextSibling

            End While

            ReturnList.Add(newClassification)

            ClassificationNode = ClassificationNode.NextSibling

        End While

        Return ReturnList

    End Function

Thanks

Ce message a été modifié par : Michael Corriveau-Cote

Ce message a été modifié par : Michael Corriveau-Cote

SolidworksApi/macros