Macro to create and modify properties in a SolidWorks part

Hello

I very often need to use and update parts created on SolidWorks for a long time.

Whenever I need to modify one of these parts or recreate a plane of it I have to assign a material to it (usually always the same one), create the custom Material property, create the Mass property and go to the document properties to modify the mass unit to change it to kg.

To avoid repeating these same actions over and over again, I would like to know if it will be possible to create a macro that would do all this.

I tried to record a macro while doing all of this, but when I want to use it, it just opens up the custom properties menu for me and nothing else happens.

As I don't know much about Macro, I hope someone will be able to help me.

 

Thank you in advance for your answers.

 

Guillaume

For the material look at this macro it will surely help you:

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

For properties you will find in the menu of this chapter, read a property and write a property:

https://www.codestack.net/solidworks-document-manager-api/document/data-storage/custom-properties/

See if you find what you're looking for and try to modify to your liking.

Otherwise without any knowledge of macro there are the Visiativ tools (MyCad-Integration) which does what you ask without any knowledge in macro and in batch (apply on a folder or an assembly and all the children:

https://help.visiativ.com/mycadtools/2018/fr/PrsentationdelutilitaireIntegrat.html

1 Like

Thank you for your answer.

I managed to manage the change of mass unit and the application of the material.

For the properties, I'll try to see with your link.

And my knowledge of macro stops at a few VBA courses on excel in bachelor's degree.

Ditto but thanks to this site I managed to get by with often a little help on this forum.

1 Like

Hello

You can also look at the task scheduler which allows you to apply custom properties to several parts or folders without the need for VBA.

1 Like

Hello

In my opinion there are simpler than the links given above:

Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim CustPropMgr As CustomPropertyManager
Dim bRet As Boolean

Sub main()
	Set swApp = Application.SldWorks
	Set Part = swApp.ActiveDoc
	If Not Part Is Nothing Then
		If Part.GetType() = swDocumentTypes_e.swDocPART Then
			bRet = Part.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swUnitSystem, swUnitSystem_e.swUnitSystem_Custom)
			bRet = Part.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swUnitsMassPropMass, swUnitsMassPropMass_e.swUnitsMassPropMass_Kilograms)
			Part.SetMaterialPropertyName2 "NomDeMaConfig", "C:\xxxxxxxx\MaBaseDeMateriaux.sldmat", "NomDeMonMateriau"
			Set CustPropMgr = Part.Extension.CustomPropertyManager("")
			bRet = CustPropMgr.Add3("Matiere", swCustomInfoType_e.swCustomInfoText, """SW-Material""", swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
			bRet = CustPropMgr.Add3("Masse", swCustomInfoType_e.swCustomInfoText, """SW-Masse""", swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
			Part.ForceRebuild3 True
		End If
	End If
End Sub

Of course, on the line with the "SetMaterialPropertyName2" function you replace the values in quotation marks as you wish.

Kind regards

4 Likes

Thank you @d.roger for your time.

The answer is great and is just what I need, the only problem I have left is for the choice of material.

Instead of "MyConfigName" is it possible to use something else to apply the modification whatever the config.

In some cases my parts have the "Default" configuration but in many cases it has other names.

 

Thanks in advance 

 

Guillaume

You can replace the line:

Part.SetMaterialPropertyName2 "MyConfigName", "C:\xxxxxxxx\MyMaterialBase.sldmat", "MyMaterialName"

By the lines:

Set swConfMgr = Part.ConfigurationManager
Set swconf = swConfMgr.ActiveConfiguration
Part.SetMaterialPropertyName2 swconf. Name, "C:\xxxxxxxx\MyMaterialBase.sldmat", "MyMaterialName"

This allows you to read the name of the active configuration and therefore to work on it.

Kind regards

1 Like

Thank you so much, it's perfect

 

Kind regards