I am asked today if it is possible with a macro to identify the constraints in error due to missing components and to remove them.
With the help of the AI, I have this code which unfortunately is not operational (the condition If swFeature.GetTypeName = " Mate " is never checked):
Dim swApp As Object
Dim swModel As ModelDoc2
Dim swAssembly As AssemblyDoc
Dim swMate As Mate2
Dim swFeature As Feature
Dim swEntity As Object
Dim swComponent As Component2
Dim swError As Long
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssembly = swModel
' Parcourir toutes les contraintes de l'assemblage
Set swFeature = swAssembly.FirstFeature
Do While Not swFeature Is Nothing
If swFeature.GetTypeName = "Mate" Then
Set swMate = swFeature.GetSpecificFeature2
swError = swMate.Status
' Vérifier si la contrainte est en erreur
If swError <> swMateStatusOK Then
' Vérifier les composants associés
Set swEntity = swMate.GetMateEntities(0)
If Not swEntity Is Nothing Then
Set swComponent = swEntity.GetComponent
If swComponent Is Nothing Then
' Supprimer la contrainte en erreur
swAssembly.DeleteFeature swFeature.Name
End If
End If
End If
End If
Set swFeature = swFeature.GetNextFeature
Loop
End Sub
Could anyone tell me what's wrong? Thank you in advance.
Hello This example of the API seems to me to meet the need for constraint scanning. Get Mates and Mate Entities Example (VBA) - 2024 - SOLIDWORKS API Help On the other hand, for the broken constrained part there is nothing direct if I don't make a mistake, so you have to manage a processing on the line of the example:
Debug.Print " Component = " & swComp.Name2
In my tests, normally there are the two components indicated with a " / " between each name. If any are missing (even if only a reference), he puts the name of the ASM as the name of the component.
Option Explicit
' Références nécessaires :
' - SolidWorks 2020 Type Library
' - SolidWorks 2020 Constant Type Library
' Constantes d'erreur (à adapter/compléter selon votre SW 2020)
Const swMateErrorNone As Long = 0
Const swMateErrorEntityNotFound As Long = 1
Const swMateErrorComponentNotFound As Long = 2
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swFeat As SldWorks.Feature
Dim nextFeat As SldWorks.Feature
Dim swMate As SldWorks.Mate2
Dim deleteCount As Long
Dim ok As Boolean
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
' Vérification : document ouvert et de type assemblage
If swModel Is Nothing Or swModel.GetType <> swDocASSEMBLY Then
MsgBox "Ouvrez un assemblage avant d'exécuter la macro.", vbExclamation
Exit Sub
End If
Set swAssy = swModel
deleteCount = 0
' Parcours de toutes les features
Set swFeat = swModel.FirstFeature
Do While Not swFeat Is Nothing
Set nextFeat = swFeat.GetNextFeature
' Tenter de récupérer un Mate2
On Error Resume Next
Set swMate = swFeat.GetSpecificFeature2
On Error GoTo 0
If Not swMate Is Nothing Then
' **Filtre** : on ne supprime QUE si c'est bien un
' composant manquant (code 2)
If swMate.GetMateError2 = swMateErrorComponentNotFound Then
swModel.ClearSelection2 True
ok = swFeat.Select2(False, -1)
If ok Then
swAssy.EditDelete
deleteCount = deleteCount + 1
End If
End If
End If
Set swFeat = nextFeat
Loop
MsgBox deleteCount & " contrainte(s) en erreur supprimée(s).", vbInformation
End Sub
On the other hand I can't really test because when I delete components, my associated constraints switch to a kind of " deleted (missing) " state but not to " error "?? :
EDIT: new code to target missing components only
EDIT2:
The following are the values of the error codes returned by GetMateError2, as defined in the swFeatureError_e enumeration (mate-related entries):
Constant
Value
Meaning
swFeatureErrorNone
0
No mistakes
swFeatureErrorMateInvalidEdge
38
One of the edges of the mate is deleted, invalid or no longer exists
swFeatureErrorMateInvalidFace
39
One of the faces of the mate is deleted, invalid or no longer exists
swFeatureErrorMateFailedCreatingSurface
40
Mat surface type not supported
swFeatureErrorMateInvalidEntity
41
One of the features in the mate is deleted, invalid, or no longer exists
swFeatureErrorMateUnknownTangent
42
The tangent mate is not satisfied
swFeatureErrorMateDanglingGeometry
43
The mate points to a "dangling" geometry (missing face/edge)
swFeatureErrorMateEntityNotLinear
44
Nonlinear edges cannot be used for this mate
swFeatureErrorMateEntityFailed
45
Mating not supported or impossible on any of the components
swFeatureErrorMateOverdefined
46
This matte over-defines the assembly
swFeatureErrorMateIlldefined
47
This mate cannot be solved (poorly defined)
swFeatureErrorMateBroken
48
One or more mate entities are deleted/deleted (mate "broken")
These constants allow you to precisely filter the reasons for errors in your mates, for example to remove only those whose entities (faces, edges, etc.) are missing (codes 38, 39 or 43). (help.solidworks.com)
Maybe the constant to put here could be 41, 45, or 48:
Thank you @Cyril_f , I couldn't find this option anymore.
So @Silver_Surfer , a possible alternative to the macro, is to uncheck the box shown by @Cyril_f which groups the constraints with missing components apart from the other errors:
All you have to do is select all the constraints in the " (missing) " folder, right-click, then " delete " (the red cross).
To be confirmed anyway if nothing else can be found in this case. My habits of constraints are too good; I have trouble generating errors
In fact it's not such a good idea because they would be paid to repair their c_nneries. The real best way is to withhold a salary up to the amount of time lost caused by their c_nneries! That's the only way it will fit.
Certainly for the option ok, but for habits there is bad will; I have never checked the option and I have never left constraints related to components that no longer exist.
Maybe it's just me who is too hard, eh, but a lack of rigor that impacts the rest of the team drives me crazy.