With VBA and the API I am inserting a note onto a drawing sheet and I want to be able to set the width of the note to a certain value so the text will wrap to that width. How do I do that?
I don't believe there's a direct API call to change the width of a note. But I know a way around it by adding newlines to the current text string as shown in the code below.
Dim swApp As SldWorks.SldWorksDim swModel As SldWorks.ModelDoc2Dim swSelMgr As SldWorks.SelectionMgrDim swNote As SldWorks.Note Dim vRetVal As VariantDim dWidth As DoubleDim dMaxWidth As DoubleDim sWords() As StringDim sText As StringDim bFirstWord As Boolean Sub main() Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc Set swSelMgr = swModel.SelectionManager Set swNote = swSelMgr.GetSelectedObject6(1, -1) ' Set your max note width down here ================================================== dMaxWidth = 70 ' in mm dWidth = GetWidth(swNote) Debug.Print "The current width is " & Round(dWidth * 1000, 2) & "mm" & vbNewLine sText = swNote.GetText sWords() = Split(sText, " ") sText = "" bFirstWord = True For i = LBound(sWords) To UBound(sWords) If bFirstWord Then sText = sText + sWords(i) bFirstWord = False Else swNote.SetText sText & " " & sWords(i) dWidth = GetWidth(swNote) If dWidth < dMaxWidth / 1000 Then sText = sText & " " & sWords(i) Else sText = sText & vbNewLine & sWords(i) End If End If Next i dWidth = GetWidth(swNote) Debug.Print "The width has changed to " & Round(dWidth * 1000, 2) & "mm" & vbNewLine End Sub Private Function GetWidth(swNote As SldWorks.Note) As Double vRetVal = swNote.GetExtent GetWidth = vRetVal(3) - vRetVal(0)End Function
Thanks Stefan. This is not exactly what I am looking for since I still want the lines to be continuous allowing the user to adjust the annotation width and the word wrapping feature to continues to function. This will allow me to set the default width of my new annotation, but allow the user to customize afterwards as necessary. You method will shrink, but not allow for easy enlargement of the annotation width and it circumvents the word wrapping feature, mostly.
Retrieving data ...