8 Replies Latest reply on Sep 21, 2017 9:33 AM by Dave Paul

    Find the displayed dimension text string length?

    Dave Paul

      The command "swDispDim.GetText(swDimensionTextPrefix)" returns "20X <MOD-DIAM> .201 THRU ALL", which has a string length of 28.

      Is there a way to return the displayed dimension string of "20X Ø .201 THRU ALL", for instance, which has a string length of 19?

       

      I figure there is a way, but I cannot find it.

      Thanks! - Dave

        • Re: Find the displayed dimension text string length?
          Alex Burnett

          Can you try the GetDisplayData().GetTextAtIndex(0) method and see if that gets you what you're looking for?

           

          Edit: I don't have a way to test this so I don't know if it will work.

            • Re: Find the displayed dimension text string length?
              Alex Burnett

              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]
              
            • Re: Find the displayed dimension text string length?
              Josh Brady

              Dave,

              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.

                • Re: Find the displayed dimension text string length?
                  Alex Burnett

                  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.

                  • Re: Find the displayed dimension text string length?
                    Dave Paul

                    Josh,

                    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.

                    Dave

                      • Re: Find the displayed dimension text string length?
                        Jacob Corder

                            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)

                         

                            Do

                                iLeftBracketPosn = Strings.InStr(Conversion, "<")

                                iRightBracketPosn = -1

                                If iLeftBracketPosn > 0 Then

                                    iRightBracketPosn = Strings.InStr(iLeftBracketPosn, Conversion, ">") '

                                End If

                         

                                If iLeftBracketPosn < 0 Or iRightBracketPosn < 0 Then

                                Exit Do

                                End If

                                Dim RepTxt As String

                                RepTxt = Strings.Mid(Conversion, iLeftBracketPosn, iRightBracketPosn - (iLeftBracketPosn - 1))

                                Conversion = Strings.Replace(Conversion, RepTxt, " ")

                            Loop

                         

                         

                            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)

                        loop txts

                        get the longest one

                          • Re: Find the displayed dimension text string length?
                            Dave Paul

                            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.

                            Parameters:

                            swThisCallout is the DisplayDimension

                            swTextPart is the swDimensionTextParts_e Enumeration

                            Return:

                            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
                            
                      • Re: Find the displayed dimension text string length?
                        Amen Allah Jlili

                        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


                        Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript

                         

                        Preview:

                        regex.png

                         

                        You can do a switch statement on  IDimensionDisplay::Type2 (swDimensionType_e Enumeration) to replace the SW special string accordingly if ever it exists:

                         

                        Cheers,
                        Amen
                        CADHERO consulting SUARL – CAD Design Automation & Training – SOLIDWORKS API