Suppress constraints with similar name

<?xml encoding=”UTF-8″>By Adam Nagy

If components (e.g. constraints) with names that are the same until the last number part are logically connected, and so you want to find them, then the easiest might be to use Regular Expressions.

In this VBA sample we check the pattern or main part of the constraint’s name so that we can match it against other constraints. If we find such constraints we suppress those too:

Sub SuppressConstraintsWithNamePattern()
Dim doc As AssemblyDocument
Set doc = ThisApplication.ActiveDocument
' Get the currently selected constraint
Dim c As AssemblyConstraint
Set c = doc.SelectSet(1)
' Easiest is to use Regular Expressions
' to get the ending number.
' E.g. if we have "Con1_23_12" it
' will return "Con1_23_" and "12",
' for "Con:12:32" it returns
' "Con:12:" and "32"
' Useful web pages:
' https://www.udemy.com/blog/vba-regex/
' http://www.rubular.com/r/qMjioXRiOG
Set re = CreateObject("vbscript.regexp")
re.pattern = "^(.*?)([0-9]*)$"
' If we run into an error we just continue
On Error Resume Next
Dim matches As Object
Set matches = re.Execute(c.Name)
Dim pattern As String
pattern = matches(0).SubMatches(0)
' Nothing to do if no pattern found
' in name of constraint
If pattern = "" Then Exit Sub
' Now iterate through the other
' constraints to see if any exists
' with a similar name
For Each c In doc.ComponentDefinition.Constraints
Set matches = re.Execute(c.Name)
Dim s As String
s = matches(0).SubMatches(0)
' If its name has the same pattern then
' suppress it
If s = pattern Then c.Suppressed = True
Next
End Sub

When running the code we get this:

Regex

 


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading