Macro solidworks - Face selection and impose color

Hello, I can't select a face and then impose a color on it in my macro. I figured out how to do it for a function, but I can't seem to modify the code to make it work for one side. Are you trying how to do it?

Here's the code for who works for a function: 
 

            boolstatus = Part.Extension.SelectByID2("Song Before", "BODYFEATURE", 0, 0, 0, True, 0, Nothing, 0)

        Set swSelMgr = swModel.SelectionManager
        Set swFeat = swSelMgr.GetSelectedObject6(1, -1)
        vMatPrps = GetFeatureColor(swFeat)

            vMatPrps(0) = 1
            vMatPrps(1) = 1
            vMatPrps(2) = 1
            swFeat.SetMaterialPropertyValues2 vMatPrps, swInConfigurationOpts_e.swThisConfiguration, ""
            swModel.ClearSelection2 True

And here is the face I want to select:

boolstatus = Part.Extension.SelectByID2("", "FACE", -0.128447455152696, 1.825000000000516E-02, -7.95118360022116E-02, False, 0, Nothing, 0)

Thank you in advance.

the declaration of variables if necessary:

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelExt As ModelDocExtension
Dim Part As SldWorks.PartDoc
Dim swConfig As SldWorks.Configuration
Dim boolstatus As Boolean
Sun swSelMgr As SldWorks.SelectionMgr
Dim swFeat As SldWorks.Feature

Dim vMatPrps As Variant

Hello. Try this:

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swFace As SldWorks.Face2
    Dim vProp As Variant
    Dim bRet As Boolean
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    If swModel Is Nothing Then MsgBox "Ouvrir une pièce": Exit Sub
    If swModel.GetType <> swDocumentTypes_e.swDocPART Then MsgBox "Ouvrir une pièce": Exit Sub
    If swSelMgr.GetSelectedObjectCount2(-1) = 0 Then MsgBox "Sélectionner une face": Exit Sub
    Set swFace = swSelMgr.GetSelectedObject6(1, -1)
    vProp = swFace.MaterialPropertyValues
    If IsEmpty(vProp) Then vProp = swModel.MaterialPropertyValues
    Debug.Print "Couleur courante: (" & vProp(0) * 255# & ", " & vProp(1) * 255# & ", " & vProp(2) * 255# & ")"
    bRet = swModel.SelectedFaceProperties(RGB(0, 255, 0), vProp(3), vProp(4), vProp(5), vProp(6), vProp(7), vProp(8), False, "")
    swModel.ClearSelection2 True
End Sub

 

1 Like

Thank you JeromeP it's exactly what I wanted 

Is it possible to select several faces and then launch the macro to put the color, or do you have to face by face like the current code?

Is it possible to select several faces and then launch the macro, or do you have to do it face by face like the current code?

@JeromeP 

Is it possible to select several faces and run the macro, or do you have to face by face like the current code?

Anything is possible!

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swFace As SldWorks.Face2
    Dim swEnt As SldWorks.Entity
    Dim vProp As Variant
    Dim bRet As Boolean
    Dim i As Integer
    Dim vFaces As New Collection
    Dim vFace As Variant
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    If swModel Is Nothing Then MsgBox "Ouvrir une pièce": Exit Sub
    If swModel.GetType <> swDocumentTypes_e.swDocPART Then MsgBox "Ouvrir une pièce": Exit Sub
    If swSelMgr.GetSelectedObjectCount2(-1) = 0 Then MsgBox "Sélectionner une face": Exit Sub
    For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
        vFaces.Add swSelMgr.GetSelectedObject6(i, -1)
    Next
    For Each vFace In vFaces
        Set swFace = vFace
        Set swEnt = swFace
        vProp = swFace.MaterialPropertyValues
        If IsEmpty(vProp) Then vProp = swModel.MaterialPropertyValues
        Debug.Print "Couleur courante: (" & vProp(0) * 255# & ", " & vProp(1) * 255# & ", " & vProp(2) * 255# & ")"
        swEnt.Select4 False, Nothing
        bRet = swModel.SelectedFaceProperties(RGB(0, 255, 0), vProp(3), vProp(4), vProp(5), vProp(6), vProp(7), vProp(8), False, "")
    Next vFace
    swModel.ClearSelection2 True
End Sub

 

3 Likes

I never think about curls, thank you anyway:)

How do I use this code without having to select the surfaces but using a selection set?