Page 1 of 1

Macro to export GD&T and "separate" them

Posted: Mon Apr 18, 2022 9:16 am
by zwei
I am trying to figure out way(s) to separate the GD&T Exported into the following format

1: GD&T Symbol
2: Tolerance 1
3: Tolerance 2 (if any)
4: Primary Datum
5: Secondary Datum
6: Tertiary Datum
image.png
Take the following example :
image.png
image.png (18.46 KiB) Viewed 1429 times
I had tried a few approach but i cant figure out a proper way to separate them (especially for tolerance 1 and tolerance 2 value)

Method 1: GetTextAtIndex
Result:
GetTextAtIndex(0) = <GTOL-POSI>
GetTextAtIndex(1) = <MOD-DIAM>
GetTextAtIndex(2) = 0.4
GetTextAtIndex(3) = <MOD-MMC>
GetTextAtIndex(4) = A
GetTextAtIndex(5) = B
GetTextAtIndex(6) = <MOD-MMC>
GetTextAtIndex(7) = C
GetTextAtIndex(8) = <MOD-MMC>
Method 2: GetFrameValues+GetFrameSymbols3
Results:
GetFrameValues(1) = 0.4,,A,B<MOD-MMC>,C<MOD-MMC>
GetFrameSymbols3(1) = <GTOL-POSI>, <MOD-MMC>, , , <MOD-MMC>, <MOD-MMC>)
So far i am able to use the GetFrameValues+GetFrameSymbols3 method to get all field as string except Tolerance 1
and Tolerance 2

Any1 is able to help? :?

Re: Macro to export GD&T and "separate" them

Posted: Mon Apr 18, 2022 9:41 am
by AlexB
Can you be more specific as to what you're referring to as "Tolerance 1" and "Tolerance 2"? It's a bit unclear from the information you've given.

If it's referring to the standard +/- tolerance on the actual dimension, you can obtain that through IDimension.Tolerance. If it's referring to something else, please point us in the right direction.

Re: Macro to export GD&T and "separate" them

Posted: Mon Apr 18, 2022 9:58 am
by zwei
AlexB wrote: Mon Apr 18, 2022 9:41 am Can you be more specific as to what you're referring to as "Tolerance 1" and "Tolerance 2"? It's a bit unclear from the information you've given.

If it's referring to the standard +/- tolerance on the actual dimension, you can obtain that through IDimension.Tolerance. If it's referring to something else, please point us in the right direction.
See example below.
Tolerance 1 is diameter 0 @ MMC
Tolerance 2 is dia 0.05 max
image.png

Re: Macro to export GD&T and "separate" them

Posted: Mon Apr 18, 2022 11:47 am
by AlexB
The solidworks documentation for GetFrameValues shows that the return values are as follows:

Format of return array of strings is:

retval[0] = Tolerance 1
retval[1] = Tolerance 2
retval[2] = Datum 1
retval[3] = Datum 2
retval[4] = Datum 3

The returns for GetFrameSymbols3 is:

retval[0] = Geometric tolerance symbol
retval[1] = Material condition symbol for first tolerance value
retval[2] = Material condition symbol for second tolerance value
retval[3] = Material condition symbol for datum1
retval[4] = Material condition symbol for datum2
retval[5] = Material condition symbol for datum3

There are empty values in the return arrays that you posted that should be filled with the tolerance2 information if that field is filled out. I'm not able to code an example to verify this right now though.

Re: Macro to export GD&T and "separate" them

Posted: Mon Apr 18, 2022 12:39 pm
by Austin Schukar
It's interesting that they split these up like they do. For the tolerance values, it looks like you have to interrogate GetTextAtIndex manually. I whipped this up and ran some tests, see what you think

Code: Select all

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelExt As SldWorks.ModelDocExtension
Dim swSelMgr As SldWorks.SelectionMgr
Dim selGtol As SldWorks.Gtol
Dim idx As Long
Dim params As Variant
Dim arrSymbols As Variant
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swModelExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager
    Set selGtol = swSelMgr.GetSelectedObject6(1, 0)
    Debug.Print "GetTextCount = " & CStr(selGtol.GetTextCount)
Dim gTOLcoll As New Collection
    For idx = 0 To selGtol.GetTextCount - 1
        gTOLcoll.Add selGtol.GetTextAtIndex(idx)
        Debug.Print "GetTextAtIndex(" & CStr(idx) & ") = " & selGtol.GetTextAtIndex(idx)
    Next idx
    Debug.Print "GetFrameCount = " & (CStr(selGtol.GetFrameCount) - 1)
    For idx = 1 To selGtol.GetFrameCount - 1
        params = selGtol.GetFrameValues(idx)
        Debug.Print "GetFrameValues(" & CStr(idx) & ") = " & params(0) & "," & params(1) & "," & params(2) & "," & params(3) & "," & params(4)
        arrSymbols = selGtol.GetFrameSymbols3(idx)
        Debug.Print "  GetFrameSymbols3(" & CStr(idx) & ") = " & arrSymbols(0) & ", " & arrSymbols(1) & ", " & arrSymbols(2) & ", " & arrSymbols(3) & ", " & arrSymbols(4) & ", " & arrSymbols(5) & ")"
   Next idx
   
    Dim count As Integer
    
    Dim tol1 As String
    Dim tol2 As String
    Dim item As Variant
    Dim nomTolPassed As Boolean
    nomTolPassed = False
    
    params = selGtol.GetFrameValues(1)
    arrSymbols = selGtol.GetFrameSymbols3(1)
    gTOLcoll.Remove 1                                               'Remove gTOL symbol from collection
    If arrSymbols(5) <> "" Then gTOLcoll.Remove gTOLcoll.count      'Remove Datum 3 modifier from collection
    If params(4) <> "" Then gTOLcoll.Remove gTOLcoll.count          'Remove Datum 3 from collection
    If arrSymbols(4) <> "" Then gTOLcoll.Remove gTOLcoll.count      'Remove Datum 2 modifier from collection
    If params(3) <> "" Then gTOLcoll.Remove gTOLcoll.count          'Remove Datum 2 from collection
    If arrSymbols(3) <> "" Then gTOLcoll.Remove gTOLcoll.count      'Remove Datum 1 modifier from collection
    If params(2) <> "" Then gTOLcoll.Remove gTOLcoll.count          'Remove Datum 1 from collection
    
    'Now we should be left with Tolerance 1 and/or Tolerance 2 values. I'm assuming Tolerance 1 exists.
    'Get Tolerance 1
    If params(0) <> "" Then
        For Each item In gTOLcoll
            If count = 0 And (item = "<MOD-DIAM>" Or item = "<MOD-SPHDIA>") Then
                tol1 = tol1 & CStr(item)
                gTOLcoll.Remove 1
                count = count + 1
            ElseIf IsNumeric(item) Then
                tol1 = tol1 & CStr(item)
                gTOLcoll.Remove 1
                nomTolPassed = True
            ElseIf count > 0 And nomTolPassed Then
                If item = "<MOD-DIAM>" Or item = "<MOD-SPHDIA>" Or IsNumeric(item) Then
                    Exit For
                Else
                    tol1 = tol1 & CStr(item)
                    gTOLcoll.Remove 1
                End If
            End If
        Next item
        count = 0
    End If
    
    'Get Tolerance 2
    If params(1) <> "" Then
        For Each item In gTOLcoll
            If count = 0 And (item = "<MOD-DIAM>" Or item = "<MOD-SPHDIA>") Then
                tol2 = tol2 & CStr(item)
                gTOLcoll.Remove 1
                count = count + 1
            ElseIf IsNumeric(item) Then
                tol2 = tol2 & CStr(item)
                gTOLcoll.Remove 1
                nomTolPassed = True
            ElseIf count > 0 And nomTolPassed Then
                If item = "<MOD-DIAM>" Or item = "<MOD-SPHDIA>" Or IsNumeric(item) Then
                    Exit For
                Else
                    tol2 = tol2 & CStr(item)
                    gTOLcoll.Remove 1
                End If
            End If
        Next item
    End If
    Debug.Print tol1
    Debug.Print tol2        
End Sub

Function ToCollection(a As Variant) As Collection
    Dim c As New Collection
    For Each item In a
      c.Add item
    Next item
    Set ToCollection = c
End Function

Sorry for the poor format. I just ripped and butchered the Help example with no mercy.

Re: Macro to export GD&T and "separate" them

Posted: Mon Apr 18, 2022 1:22 pm
by JSculley
Austin Schukar wrote: Mon Apr 18, 2022 12:39 pm It's interesting that they split these up like they do. For the tolerance values, it looks like you have to interrogate GetTextAtIndex manually.
The tolerance values are in the array returned from GetFrameValues. You have to use this along with GetFrameDiameterSymbols and GetFrameSymbols3 to get the full picture.

A symbol like this:
image.png
outputs a set of data like this:

Code: Select all

Frame 1 start
	================== Has Diameter Symbol =================
	True
	True
	================== Frame Symbols =================
	<GTOL-PERP>
	<MOD-MMC>
	<MOD-LMC>
	<MOD-LMC>
	<MOD-MMC>
	<MOD-LMC>
	================== Frame Values =================
	0
	.05 MAX
	A<MOD-LMC>
	B<MOD-MMC>
	C<MOD-LMC>
Frame 1 end
The LMC/MMC information associated with the datums appears in both the results from GetFrameSymbols3 and GetFrameValues. The whole IGtol API seems like it was programmed by comittee.

Re: Macro to export GD&T and "separate" them

Posted: Mon Apr 18, 2022 2:21 pm
by Austin Schukar
JSculley wrote: Mon Apr 18, 2022 1:22 pm The tolerance values are in the array returned from GetFrameValues. You have to use this along with GetFrameDiameterSymbols and GetFrameSymbols3 to get the full picture.

A symbol like this:

image.png

outputs a set of data like this:

Code: Select all

Frame 1 start
	================== Has Diameter Symbol =================
	True
	True
	================== Frame Symbols =================
	<GTOL-PERP>
	<MOD-MMC>
	<MOD-LMC>
	<MOD-LMC>
	<MOD-MMC>
	<MOD-LMC>
	================== Frame Values =================
	0
	.05 MAX
	A<MOD-LMC>
	B<MOD-MMC>
	C<MOD-LMC>
Frame 1 end
The LMC/MMC information associated with the datums appears in both the results from GetFrameSymbols3 and GetFrameValues. The whole IGtol API seems like it was programmed by comittee.
I hadn't noticed GetFrameDiameterSymbols...nice!

Where I think the trouble lies is determining where the modifiers are in the frame (specifically, tolerance 1 and tolerance 2). I know I was overthinking it, though.

Re: Macro to export GD&T and "separate" them

Posted: Mon Apr 18, 2022 7:58 pm
by zwei
JSculley wrote: Mon Apr 18, 2022 1:22 pm The tolerance values are in the array returned from GetFrameValues. You have to use this along with GetFrameDiameterSymbols and GetFrameSymbols3 to get the full picture.

A symbol like this:

image.png

outputs a set of data like this:

Code: Select all

Frame 1 start
	================== Has Diameter Symbol =================
	True
	True
	================== Frame Symbols =================
	<GTOL-PERP>
	<MOD-MMC>
	<MOD-LMC>
	<MOD-LMC>
	<MOD-MMC>
	<MOD-LMC>
	================== Frame Values =================
	0
	.05 MAX
	A<MOD-LMC>
	B<MOD-MMC>
	C<MOD-LMC>
Frame 1 end
The LMC/MMC information associated with the datums appears in both the results from GetFrameSymbols3 and GetFrameValues. The whole IGtol API seems like it was programmed by comittee.
Nice!

Just one last question... using GetFrameDiameterSymbols seem to return TRUE for both cylindrical and spherical tolerance zone, is there anyway to determine whether it is a cylindrical or spherical modifier (<MOD-DIAM>0 vs <MOD-SPHDIA>)?