Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Solidworks VBA with custom property 1

Status
Not open for further replies.

Jawed62

Industrial
Sep 3, 2024
2
Hello Folks,

I wanna try to use custom property with MACRO, to drive one standard part only by value, in order to CHANGE her configuration by executing that macro right ?

to simplify my question here, i design one part with 2 configurations :
-Extrusion_10
-Extrusion_50

Then i create a custom property with the same name for value right ?

Then i create a macro, in order to search about the custom property, and then its job is to switch the configuration compared with the custom property.
So this is the code :






' Macro pour changer la configuration d'une pièce en fonction de la valeur de la propriété personnalisée "Niveau d'extrusion".

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swConfMgr As SldWorks.ConfigurationManager
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim customPropName As String
Dim configValue As String
Dim resolvedVal As String
Dim wasResolved As Boolean
Dim swConfig As SldWorks.Configuration
Dim swActiveConfig As SldWorks.Configuration

Sub main()
' Nom de la propriété personnalisée à utiliser pour la sélection de la configuration
customPropName = "Niveau d'extrusion"

' Obtenir l'application SolidWorks
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

' Vérifier que le document actif est un modèle de pièce
If swModel Is Nothing Then
MsgBox "Aucun document actif."
Exit Sub
End If

If swModel.GetType <> swDocPART Then
MsgBox "Le document actif n'est pas une pièce."
Exit Sub
End If

' Obtenir le gestionnaire de propriétés personnalisées pour le document global
Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")

' Récupérer la valeur de la propriété personnalisée "Niveau d'extrusion"
wasResolved = swCustPropMgr.Get4(customPropName, False, configValue, resolvedVal)

If Not wasResolved Then
MsgBox "La propriété personnalisée '" & customPropName & "' n'existe pas ou n'a pas de valeur."
Exit Sub
End If

' Afficher la valeur récupérée pour diagnostic
MsgBox "Valeur récupérée: '" & configValue & "'"

' Obtenir le gestionnaire de configurations
Set swConfMgr = swModel.ConfigurationManager

' Obtenir la configuration spécifiée
On Error Resume Next
Set swConfig = swConfMgr.GetConfigurationByName(configValue)


On Error GoTo 0

If swConfig Is Nothing Then
MsgBox "La configuration '" & configValue & "' n'existe pas."
Exit Sub
End If

' Changer la configuration active
swModel.ConfigurationManager.ActivateConfiguration (configValue)

' Recalculer le modèle pour s'assurer que la configuration est bien chargée
swModel.EditRebuild3

MsgBox "Configuration changée en '" & configValue & "'."
End Sub





but it throws me an issue, which tells me it does not find like "Extrusion_10" configuration,
and stops.

what I'm doing wrong ?

Thanks in advance folks

 
Replies continue below

Recommended for you

I suspect that you have used ChatGPT or similar AI tool to create this macro. And hence is the reason for the errors.

Change
Set swConfig = swConfMgr.GetConfigurationByName(configValue)
to
Set swConfig = swModel.GetConfigurationByName(configValue)

Change
swModel.ConfigurationManager.ActivateConfiguration (configValue)
to
swModel.ShowConfiguration2 (configValue)


Deepak Gupta
SOLIDWORKS Champion and Expert
SW 2022 SP0.0, 2021 SP5.1 and 2020 SP5.0
Boxer's SOLIDWORKS Blog


 
Great, It works now

And yep, i tried to build a macro using advice from chatGPT.

changing this line resolve my problem !

Many thanks to your help mate, have a nice day/night
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor