4 Replies Latest reply on Sep 20, 2018 7:15 AM by Korbi Anis

    select geometrical tolerance in assembly and faces associated to these tolerances

    Korbi Anis

      Hi my friends , mm I want there are a method to select geometric tolerance in assembly and get the face related to this tolerance  using vba macro ? .Thank you very much

        • Re: select geometrical tolerance in assembly and faces associated to these tolerances
          Fifi Riri

          Hello,

          geometric tolerances are a part feature.

          Run this macro below on a part to select the faces associated with all geometric tolerances.

          If you want it for the assembly you will have to loop through the parts.

           

          Option Explicit

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swModelDocExt As SldWorks.ModelDocExtension

          Dim boolstatus As Boolean

          Dim AnnArr As Variant

          Dim Ann As Variant

          Dim swAnn As SldWorks.Annotation

          Dim EntArr As Variant

          Dim Ent As Variant

          Dim swEnt As SldWorks.Entity

          Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              swModel.ClearSelection2 True

              Set swModelDocExt = swModel.Extension

              AnnArr = swModelDocExt.GetAnnotations

              If swModelDocExt.GetAnnotationCount > 0 Then

                  For Each Ann In AnnArr

                      Set swAnn = Ann

                      If swAnn.GetType = swAnnotationType_e.swGTol Then

                          EntArr = swAnn.GetAttachedEntities2

                          For Each Ent In EntArr

                              Set swEnt = Ent

                              If swEnt.GetType = swSelectType_e.swSelFACES Then

                                  boolstatus = swEnt.Select4(True, Nothing)

                              End If

                          Next

                      End If

                  Next

              End If

          End Sub

          • Re: select geometrical tolerance in assembly and faces associated to these tolerances
            Korbi Anis

            Thank you very much dear friend its worked fine !

            Just I have another question , how can I get the name of the seledted face ? thank you

              • Re: select geometrical tolerance in assembly and faces associated to these tolerances
                Fifi Riri

                Faces don't have name.

                You can get their Persistent Reference ID with below code, but whatever you're trying to do: I wouldn't go that way.

                 

                Option Explicit

                Dim swApp As SldWorks.SldWorks

                Dim swModel As SldWorks.ModelDoc2

                Dim swModelDocExt As SldWorks.ModelDocExtension

                Dim boolstatus As Boolean

                Dim AnnArr As Variant

                Dim Ann As Variant

                Dim swAnn As SldWorks.Annotation

                Dim EntArr As Variant

                Dim Ent As Variant

                Dim swEnt As SldWorks.Entity

                Sub main()

                    Set swApp = Application.SldWorks

                    Set swModel = swApp.ActiveDoc

                    swModel.ClearSelection2 True

                    Set swModelDocExt = swModel.Extension

                    AnnArr = swModelDocExt.GetAnnotations

                    If swModelDocExt.GetAnnotationCount > 0 Then

                        For Each Ann In AnnArr

                            Set swAnn = Ann

                            'Debug.Print swAnn.GetType

                            If swAnn.GetType = swAnnotationType_e.swGTol Then

                                EntArr = swAnn.GetAttachedEntities2

                                For Each Ent In EntArr

                                    Set swEnt = Ent

                                    If swEnt.GetType = swSelectType_e.swSelFACES Then

                                        boolstatus = swEnt.Select4(True, Nothing)

                                        Dim vId As Variant

                                        vId = swModel.Extension.GetPersistReference3(swEnt)

                                        Debug.Print "Face ID:" & vbCr & ConvertToBase64String(vId)

                                        Debug.Print

                                    End If

                                Next

                            End If

                        Next

                    End If

                End Sub

                Function ConvertToBase64String(vArr As Variant) As String

                    Dim xmlDoc As Object

                    Dim xmlNode As Object

                    Set xmlDoc = CreateObject("MSXML2.DOMDocument")

                    Set xmlNode = xmlDoc.createElement("b64")

                    xmlNode.DataType = "bin.base64"

                    xmlNode.nodeTypedValue = vArr

                    ConvertToBase64String = xmlNode.Text

                End Function