Macro to automatically reload a certain number of materials on the components of an assembly

Hello

Following the transition to the 2020 version, Solidworks is working hatching with a lot of strokes and complicated hatching patterns.

Our material used for the welds was unfortunately in houndstooth factor 32 (suffice to say that it takes minutes to load the slightest detail plan).

So we changed our materials to come back to lighter hatching types and scale factors.

Our current problem is that we copy a lot of old plans (so with the old materials saved in the rooms).

Would there be someone who would be able to make a macro to scan all the parts of an assembly and automatically reload the materials that have the following names: "Stainless steel welding", "Stellite Grade 21 (RC)", "Stellite Grade 12 (RB)", "Stellite Grade 6 (RA)"

Thank you in advance

 

 

 

 

 

Here is an example of a rule that you can apply with Integration (Mycadtools utility) in order to change materials according to a rule (here if the material is a or B or C)

To do this, you get the assembly in progress, you break it down, then you filter only on the parts, you launch and then you let it run.

This will save you from creating a macro and it remains easily editable without programming knowledge.

 

The link to the Integration help:

https://help.visiativ.com/mycadtools/2020/fr/Integration.html


maj_inox_316.mcact
1 Like

Thanks @sbadenis but unfortunately I don't have the mycad utilities.

In addition, and to make things more complicated: our parts to be edited are virtual parts that are saved inside the assembly. Given what you describe, I don't think your method works as a result.

However, in your profile I see thanks to the medal, that you are subscribed to Mycadservices so normally you have access to utilities at no extra cost.

Look in the menu on the left if you have access in the Produce menu to the Mycadtools menu (at worst there is a trial period of 1 month it seems to me but in view of the medal for me you must have access to it)

For the virtual parts, I just did a test, it's not a problem.

All you have to do is open the assembly in solidworks, in integration press add active document, having checked the dependencies box, then in filter to filter the parts, launch and finally save the assembly with the material changes made in each part.

If necessary I could guide you.

 

 

1 Like

I was a Mycad subscriber for a while but I'm not anymore (and we don't want to pay for 8 mycad licenses for life either (even if it's more reasonable than the cost of SW maintenance)....)

There is indeed more than the macro left, but I don't manage enough on the subject to do it in the little free time I find at the moment.

In this case, here's a start for the macro:

https://www.codestack.net/solidworks-api/document/materials/show-edit-material-dialog/

From your assembly, you will have to loop the set of parts, then open them one by one and change the material if it is one of the materials concerned.

.

Hello

Why not select all the parts from the assembly and right-click, select material and change the material of all the parts in 1 time

Joel

Hi all

If you have changed your materials, when we update, normally the new hatching applies. If you want to do this in bulk without opening the files, you can update from the Task Scheduler.

@ joel.condevaux : Because I only have the problem with the welds (which must end up black which explains my houndstooth + strong initial scale): either one or two pieces out of the ten or 50 that the assembly contains. Otherwise, in practice, that's what we do, but it's a bit repetitive to do this on all the planes we copy.

@ i will survive: we're on EPDM so it complicates things a bit: you have to have extracted the assembly file before you can modify it so that the MEP file can be used. And in addition, the parts whose material needs to be modified are virtual and have different names each time: difficult to make in batch.

I still looked at the 'task scheduler' tool and I don't see with which command you can modify the material of a part (and the case of modifying the material of a virtual part inside an assembly seems totally ignored to me). The tool can only work on read/write tools, i.e. extracted files (which seems quite logical).

For me it's a matter of making the changes from the assembly:

  1.  In assembly: filter the parts according to the material and assign a new one

@ joel.condevaux : we can indeed make a filter on the name of the material to identify the parts where the material needs to be changed. But as I have 4 different possible materials, you have to do 4 different searches and then reload the material.

So it's still long, tedious and repetitive.

Hence the interest in being able to make a macro so that it is the PC that does this work and not the projector.

Hello

Here is a macro that should be able to serve as a starting point, don't forget to modify the line "databaseName = ... " to put the name of your sldmat file .

All files must be in resolved mode in your assembly.

Kind regards

Option Explicit

Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2
Dim myMatVisProps As SldWorks.MaterialVisualPropertiesData
Dim orgBlend As Boolean
Dim orgApply As Boolean
Dim orgAngle As Double
Dim orgScale As Double
Dim longstatus As Long
Dim bRet As Boolean
Dim i As Long
Dim Assembly As ModelDoc2
Dim myAssy As AssemblyDoc
Dim myCmps As Variant
Dim myCmp As Component2
Dim nInfo As Long

Sub main()
    Set swApp = Application.SldWorks
    Set Assembly = swApp.ActiveDoc
    Set myAssy = Assembly

    myCmps = myAssy.GetComponents(False)
    For i = 0 To UBound(myCmps)
        Set myCmp = myCmps(i)
        If (myCmp.GetSuppression = 3) Or (myCmp.GetSuppression = 2) Then
            bRet = myCmp.Select2(False, 0)
            bRet = myAssy.EditPart2(True, True, nInfo)
            Set swModel = myAssy.GetEditTarget

            If swModel.GetType = 1 Then
                Dim result As String
                Dim Mat As String
                Dim configName As String
                configName = "Défaut"
                Dim databaseName As String
                databaseName = "C:/Program Files/SOLIDWORKS Corp/SOLIDWORKS/lang/french/sldmaterials/solidworks materials.sldmat"

                Dim searchPropName1 As String
                Dim searchPropName2 As String
                Dim searchPropName3 As String
                Dim searchPropName4 As String
                searchPropName1 = "Soudure inox"
                searchPropName2 = "Stellite Grade 21 (RC)"
                searchPropName3 = "Stellite Grade 12 (RB)"
                searchPropName4 = "Stellite Grade 6 (RA)"

                Dim myPart As SldWorks.PartDoc
                Set myPart = swModel
                result = myPart.GetMaterialPropertyName2(configName, Mat)

                If result = searchPropName1 Then
                    myPart.SetMaterialPropertyName2 configName, databaseName, searchPropName1
                    Set myMatVisProps = myPart.GetMaterialVisualProperties()
                    Call apply_material_visual_properties(myMatVisProps, myPart)
                ElseIf result = searchPropName2 Then
                    myPart.SetMaterialPropertyName2 configName, databaseName, searchPropName2
                    Set myMatVisProps = myPart.GetMaterialVisualProperties()
                    Call apply_material_visual_properties(myMatVisProps, myPart)
                ElseIf result = searchPropName3 Then
                    myPart.SetMaterialPropertyName2 configName, databaseName, searchPropName3
                    Set myMatVisProps = myPart.GetMaterialVisualProperties()
                    Call apply_material_visual_properties(myMatVisProps, myPart)
                ElseIf result = searchPropName4 Then
                    myPart.SetMaterialPropertyName2 configName, databaseName, searchPropName4
                    Set myMatVisProps = myPart.GetMaterialVisualProperties()
                    Call apply_material_visual_properties(myMatVisProps, myPart)
                End If
            End If

            myAssy.EditAssembly
        End If
    Next i

    Assembly.ForceRebuild3 False

End Sub


Private Sub apply_material_visual_properties(myMatVisProps As SldWorks.MaterialVisualPropertiesData, myPart As SldWorks.PartDoc)
    If Not myMatVisProps Is Nothing Then
        orgAngle = myMatVisProps.Angle
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)

        orgScale = myMatVisProps.Scale2
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)

        If myMatVisProps.BlendColor = 0 Then
            orgBlend = False
        Else
            orgBlend = True
        End If
        myMatVisProps.BlendColor = Not orgBlend
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        myMatVisProps.BlendColor = orgBlend
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)

        If myMatVisProps.ApplyMaterialColorToPart = 0 Then
            orgApply = False
        Else
            orgApply = True
        End If
        myMatVisProps.ApplyMaterialColorToPart = Not orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        myMatVisProps.ApplyMaterialColorToPart = orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)

        If myMatVisProps.ApplyMaterialHatchToSection = 0 Then
            orgApply = False
        Else
            orgApply = True
        End If
        myMatVisProps.ApplyMaterialHatchToSection = Not orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        myMatVisProps.ApplyMaterialHatchToSection = orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)

        If myMatVisProps.ApplyAppearance = 0 Then
            orgApply = False
        Else
            orgApply = True
        End If
        myMatVisProps.ApplyAppearance = Not orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        myMatVisProps.ApplyAppearance = orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
    End If
End Sub

 

1 Like

@ D.Roger :

It's just perfect.

A big thank you you will save us precious time (and also to all those who made the mistake of putting high scale factors on their SW materials).

We still have one problem though (as long as SW hasn't fixed the bug: SPR 476546 which has been open in High Impact since 2014...): On our MEPs, parts with a plain hatch pattern (so the ones I asked you to modify...) appear transparent instead of uniformly black. To get around the problem, you have to select one of the white hatched areas (instead of black), right-click, and deselect the "Material hatching" checkbox. Strangely, this allows you to apply the plain black pattern on the weld. And this must be done on all the views and all the hatched parts in plain of the drawing.

Do you think it would be able to make a macro to do this deselection automatically on all the views of the MEP (either by going to look for the materials from the list that you have already used, or by going to find all the materials that have 'united' as a type of hatching)??

This macro will also be useful for all those who have the stupid idea of using plain hatching for their welds (SW has done very well because they managed to create 2 bugs that make it impossible to make welds on a MEP without taking a headache: if it's united, you don't see the hatching, if it's a hatching with a strong scale factor, your drawing grinds and you're on the verge of suicide...).

 


capture_hachures.jpg

Hello

At first glance it's a bit complicated because the "GetFaceHatchCount" method returns a null value for plain hatching from materials that appear transparent, I'll look into the problem occasionally but when I have a little more time.

Kind regards

@ d.roger : Thank you again.

I'm closing this topic.

I'll eventually reopen one if you can find a solution to this issue of transparent plain hatching on drawings in order to share this new macro with the community.

It is indeed possible that it is not so simple (otherwise SW might have solved this SPR with high customer impact in 6 years...).