After looking into this, it seems like I can't access the display string. My recommendation is to examine the prefix for the < and the > characters and then treat the whole block as 1 character. This would require you to write a customized Count() function though.
My output of my test file is below trying several methods to get what you are looking for:
GetDimension2.Value: (76.2) GetDimension2.SystemValue: (0.0762) GetText(swDimensionTextPrefix): () [Length: 0] GetDisplayData().GetTextAtIndex(0): ( ) [Length: 1] GetDimension2.Value: (0.949999999999993) GetDimension2.SystemValue: (0.000949999999999993) GetText(swDimensionTextPrefix): (M1.2X0.25 - 6H THRU) [Length: 19] GetDisplayData().GetTextAtIndex(0): (M1.2X0.25 - 6H THRU) [Length: 19] GetDimension2.Value: (14.7571921599941) GetDimension2.SystemValue: (0.0147571921599941) GetText(swDimensionTextPrefix): (<MOD-DIAM>) [Length: 10] GetDisplayData().GetTextAtIndex(0): (<MOD-DIAM>) [Length: 10]
Can you explain what you're looking to do with the string length? The reason I ask is that SolidWorks actually stores that whole string with the <MOD-DIAM> all the time. It just graphically replaces that special text with a symbol when you view the drawing. The symbol itself is not actually a font character either. It's a symbol whose definition is contained in the gtol.sym file.
If what you're really after is some measure of the actual length of the line to make sure it physically fits somewhere, I'm not sure what is available. If you were looking at a Note, there are methods for getting the dimensions of the text. I can't find any similar functions for a dimension though.
That's the conclusion I settled on after a little more testing. I figured there was a conversion that SW did to display the string so it makes sense that the GTOL.sym file is the answer to that. Thanks for helping clarify.
This makes me think that a custom function to count the length of the displayed string would be needed to parse out the gtol symbol tags and treat them as a single character.
Knowing what he wants to do with the string length would help as well.
I'm adding text to a hole wizard callout. I want to stay within the existing text string length, but the string that GetText returns is obviously not the converted data.
I thought about a lookup and conversion routine from the GTOL.sym file, but I believe that the data is stored graphically. There isn't that many "<>", so a conversion routine between <MOD-DIAM> to a character count of one might not be all that time consuming.
Dim iLeftBracketPosn As Integer
Dim iRightBracketPosn As Integer
Dim Conversion As String
Conversion = "<HOLE-DIAM>0.250 <HOLE-DEPTH>THRU"
Debug.Print "Before Text: " & Conversion & " Length = " & Len(Conversion)
iLeftBracketPosn = Strings.InStr(Conversion, "<")
iRightBracketPosn = -1
If iLeftBracketPosn > 0 Then
iRightBracketPosn = Strings.InStr(iLeftBracketPosn, Conversion, ">") '
If iLeftBracketPosn < 0 Or iRightBracketPosn < 0 Then
Dim RepTxt As String
RepTxt = Strings.Mid(Conversion, iLeftBracketPosn, iRightBracketPosn - (iLeftBracketPosn - 1))
Conversion = Strings.Replace(Conversion, RepTxt, " ")
Debug.Print "After Text: " & Conversion & " Length = " & Len(Conversion)
this will strip out the < > and all text between. hopefully this helps.
if there are multi line callout text items, you will need to split them into individual rows. i believe the separator is VBCRLF.
then run the above code against each text item.
Dim Txts() as string
txts = strings.split(CalloutTxt,VBCRLF)
get the longest one
OK, I wrote this function to "determine" the string length of a portion of a callout. It seems to work well. Hopefully you all might be able to use it.
swThisCallout is the DisplayDimension
swTextPart is the swDimensionTextParts_e Enumeration
Calculated string length as a double (long would be better...)
Function MeasureString(swThisCallout As SldWorks.DisplayDimension, swTextPart As Integer) As Double Dim strTempA As String Dim strTempB As String Dim lngPosition As Long Dim blnTrans As Boolean Dim i As Long lngPosition = 0 blnTrans = True strTempA = swThisCallout.GetText(swTextPart) If InStr(1, swThisCallout.GetText(swTextPart), "<") > 0 Then For i = 1 To Len(strTempA) If Mid(strTempA, i, 1) = "<" Then blnTrans = False End If If Mid(strTempA, i, 1) = ">" Then blnTrans = True End If If blnTrans Then strTempB = strTempB & Mid(strTempA, i, 1) Next i Else strTempB = strTempA End If MeasureString = Len(strTempB) End Function
You can easily parse those strings using regular expressions. Since SOLIDWORKS uses <> for its own purposes, you can parse those special bits out with a regex expression leaving with you what you need. There is a reference for that you add to your project from the VBE references box: Microsoft VBScript Regular Expressions 5.5
You can do a switch statement on IDimensionDisplay::Type2 (swDimensionType_e Enumeration) to replace the SW special string accordingly if ever it exists: