Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swDwg As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension
Dim sCurSuffix As String
Dim nOpenParPos As Long
Dim nCloseParPos As Long
Dim vDimVal As Variant
Dim dInchVal As Double
Dim sInchString As String
Dim sNewSuffix As String
Dim KillFlag As Integer
Dim sMsg As String
Dim sRefPfx As String
Dim nRefNum As Long
Const DEFSYM As String = "C#-"
Sub AddDimRefNums()
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
If swDoc.GetType <> swDocDRAWING Then
MsgBox "This macro only works for drawing files."
Exit Sub
End If
sMsg = "This macro will add a symbol containing a dimension reference number " & _
vbCrLf & "to all dimensions in this drawing." & vbCrLf & vbCrLf & _
"To add or update dimension reference symbols, choose ""Yes""" & vbCrLf & _
"To remove reference symbols, choose ""No""" & _
vbCrLf & "To quit, choose ""Cancel"""
KillFlag = MsgBox(sMsg, vbYesNoCancel, "Dimension Reference Symbols")
If KillFlag = vbCancel Then
Exit Sub
End If
If KillFlag = vbYes Then
sRefPfx = InputBox("Please enter the desired symbol code." & vbCrLf & _
"For example, ""C#-"" for circles, ""S#-"" for squares." & vbCrLf & _
"Note that symbol codes are case sensitive.", "Symbol Type", DEFSYM)
Else
sRefPfx = ""
End If
Set swDwg = swDoc
nRefNum = 1
Set swView = swDwg.GetFirstView
While Not (swView Is Nothing)
Set swDispDim = swView.GetFirstDisplayDimension5
While Not swDispDim Is Nothing
Set swDim = swDispDim.GetDimension
sInchString = sRefPfx & nRefNum
nRefNum = nRefNum + 1
sCurSuffix = swDispDim.GetText(swDimensionTextSuffix)
nOpenParPos = InStr(1, sCurSuffix, "<", vbTextCompare)
nCloseParPos = InStr(1, sCurSuffix, ">", vbTextCompare)
If (KillFlag = vbNo) And (nOpenParPos > 0) And (nCloseParPos > 0) Then
sNewSuffix = Left(sCurSuffix, nOpenParPos - 1)
sNewSuffix = sNewSuffix & Right(sCurSuffix, Len(sCurSuffix) - nCloseParPos)
ElseIf (nOpenParPos > 0) And (nCloseParPos > 0) Then
sNewSuffix = Left(sCurSuffix, nOpenParPos)
sNewSuffix = sNewSuffix & sInchString
sNewSuffix = sNewSuffix & Right(sCurSuffix, Len(sCurSuffix) - (nCloseParPos - 1))
Else
If KillFlag <> vbNo Then
sNewSuffix = Trim(sCurSuffix) & " <" & sInchString & ">"
Else
sNewSuffix = sCurSuffix
End If
End If
swDispDim.SetText swDimensionTextSuffix, sNewSuffix
Set swDispDim = swDispDim.GetNext5
Wend
Set swView = swView.GetNextView
Wend
End Sub