This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
This macro increments the numeric value of the notes using SOLIDWORKS API. This can be useful if it required to increment the revision for note or notes without the need of manually selecting and changing the note. This macro can be also used in the batch processing software.
Numeric value is matched by specified regular expression. It is possible to modify the regular expression to match specific numeric values. Note can contain free text (in this case only numeric part will be updated as per specified regular expression)
It is required to add the text tag to the note in order to increment its value. Follow Add Tag To Selected Note example of instructions for adding the tag to the note.
By default the numeric value is incremented by 1, but this can be changed by modifying the value of increment parameter of the IncrementNoteValue function.
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
IncrementNoteValue "_CodeStackNote_", "\d+", 1
EndSubSub IncrementNoteValue(noteTag AsString, pattern AsString, increment AsDouble)
Dim swNote As SldWorks.Note
Set swNote = FindNodeByTag(swModel, noteTag)
IfNot swNote IsNothingThenDim newText AsString
newText = IncrementNumericMatches(swNote.GetText(), pattern, increment)
swNote.SetText newText
EndIfEndSubFunction IncrementNumericMatches(text AsString, pattern AsString, increment AsDouble) AsStringDim resultText AsString
resultText = text
Dim regEx AsObjectSet regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
regEx.pattern = pattern
Dim regExMatches AsObjectSet regExMatches = regEx.Execute(text)
If regExMatches.Count > 0 ThenDim i AsIntegerDim offset AsIntegerFor i = 0 To regExMatches.Count - 1
Dim regExMatch AsObjectSet regExMatch = regExMatches(i)
Dim newValue AsDouble
newValue = CDbl(regExMatch.Value) + increment
resultText = Left(resultText, regExMatch.FirstIndex + offset) & newValue & Right(resultText, Len(resultText) - regExMatch.FirstIndex - regExMatch.Length - offset)
offset = offset + Len(CStr(newValue)) - regExMatch.Length
NextEndIf
IncrementNumericMatches = resultText
EndFunctionFunction FindNodeByTag(model As SldWorks.ModelDoc2, tag AsString) As SldWorks.Note
If tag <> ""ThenDim vAnnots AsVariant
vAnnots = model.Extension.GetAnnotations
Dim swNote As SldWorks.Note
Dim i AsIntegerIfNot IsEmpty(vAnnots) ThenFor i = 0 To UBound(vAnnots)
Dim swAnn As SldWorks.Annotation
Set swAnn = vAnnots(i)
If swAnn.GetType() = swAnnotationType_e.swNote ThenSet swNote = swAnn.GetSpecificAnnotation
If swNote.TagName = tag ThenSet FindNodeByTag = swNote
ExitFunctionEndIfEndIfNextEndIfIf model.GetType() = swDocumentTypes_e.swDocDRAWING ThenDim swDraw As SldWorks.DrawingDoc
Set swDraw = model
Dim vSheets AsVariant
vSheets = swDraw.GetViews()
For i = 0 To UBound(vSheets)
Dim vViews AsVariant
vViews = vSheets(i)
Dim j AsIntegerFor j = 0 To UBound(vViews)
Dim swView As SldWorks.View
Set swView = vViews(j)
Dim vNotes AsVariant
vNotes = swView.GetNotes()
Dim k AsIntegerFor k = 0 To UBound(vNotes)
Set swNote = vNotes(k)
If swNote.TagName = tag ThenSet FindNodeByTag = swNote
ExitFunctionEndIfNextNextNextEndIfEndIfEndFunction