8 Replies Latest reply on Jul 11, 2016 10:58 AM by Alexandre Gragnano

    Select bodies in drawing document

    Alexandre Gragnano

      If I have an array of Body2 taken from referenced PartDoc instances,

      how can I select them into the DrawingDoc views of the active sheet ?

        • Re: Select bodies in drawing document
          Amen Allah Jlili

          Interesting question!

           

          You can't really select a body (3D) inside a (2D). What you might able to do is to select a vertex, an edge or a face that belong to body in the drawing view. That would make more sense to me.

          Here's my suggestion (Assuming this is a one drawing view of a single part doc with multiple bodies. Not an assembly. With assemblies, it's a little longer):
          1. You need to get the visible components from the drawing view.

          2. Get all the visible entities (faces for example) of the visible components in the drawing view.
          3.Loop through the visible entities casting each one into a Face2 object and use the GetBody method from IFace2 to get the body object from the face and check if the body's name is in your referenced part doc array of bodies.
          4. If True, add the the entity to a collection object.
          5. Loop through the collection of entities that are now from the bodies of your referenced part and use the IView.SelectEntity to select the entity in the drawing view.

          6. Et voilà Monsieur!

           

          This way you will be able to select faces that belong to a bodies in your referenced part.
          The same would be apply to vertex and edges.

           

          Hope this helps 
          Send me a private message if you want my professional help

            • Re: Select bodies in drawing document
              Alexandre Gragnano

              Thank you very much for you help.

               

              I have a problem with the method IView::GetVisibleEntities2 and IView::GetVisibleEntityCount2 with the swViewEntityType_e.swViewEntityType_SilhouetteEdge argument. SolidWorks seems to fall in an infinite loop.

               

              The source code (un-comment the lines 40 & 41 the see the problem):

              Sub main()
                 
                  Dim swApp As SldWorks.SldWorks
                  Dim swDoc As SldWorks.ModelDoc2
                  Dim swDrw As SldWorks.DrawingDoc
                  Dim swSheet As SldWorks.sheet
                  Dim vViews As Variant
                  Dim vView As Variant
                  Dim swView As SldWorks.view
                  Dim vComps As Variant
                  Dim vComp As Variant
                  Dim swComp As SldWorks.Component2
                  Dim vEntities As Variant
                  Dim vEntity As Variant
                  Dim swEntity As SldWorks.Entity
                  Dim iCount As Integer
                  Dim lCount As Long
                 
                  Set swApp = Application.SldWorks
                  Set swDoc = swApp.ActiveDoc
                  If swDoc Is Nothing Then Exit Sub
                  If swDoc.GetType <> swDocumentTypes_e.swDocDRAWING Then Exit Sub
                  Set swDrw = swDoc
                  Set swSheet = swDrw.GetCurrentSheet
                 
                  vViews = swSheet.GetViews
                  If Not IsEmpty(vViews) Then
                      iCount = UBound(vViews) - LBound(vViews) + 1
                      Debug.Print iCount & " view(s):"
                      For Each vView In vViews
                          Set swView = vView
                          Debug.Print "- " & swView.GetName2
                          vComps = swView.GetVisibleComponents
                          If Not IsEmpty(vComps) Then
                              iCount = UBound(vComps) - LBound(vComps) + 1
                              Debug.Print "  " & iCount & " component(s):"
                              For Each vComp In vComps
                                  Set swComp = vComp
                                  Debug.Print "  - " & swComp.Name2
              '                    lCount = swView.GetVisibleEntityCount2(swComp, swViewEntityType_e.swViewEntityType_SilhouetteEdge)
              '                    Debug.Print "    " & lCount & " silhouette edge(s)"
                              Next vComp
                          End If
                      Next vView
                  End If
                 
              End Sub
              

              Running with SolidWorks 2016

              Have you got the problem too ?

                • Re: Select bodies in drawing document
                  Amen Allah Jlili

                  Try replacing wViewEntityType_e.swViewEntityType_SilhouetteEdge with 3 (Faces).

                    • Re: Select bodies in drawing document
                      Alexandre Gragnano

                      My objective is to highlight silhouette edges,

                      however swViewEntityType_Face is not enough reliable.

                       

                      Macro:

                      Sub main()
                         
                          Dim swApp As SldWorks.SldWorks
                          Dim swDoc As SldWorks.ModelDoc2
                          Dim swDrw As SldWorks.DrawingDoc
                          Dim swSheet As SldWorks.Sheet
                          Dim vViews As Variant
                          Dim vView As Variant
                          Dim swView As SldWorks.View
                          Dim vCpns As Variant
                          Dim vCpn As Variant
                          Dim swCpn As SldWorks.Component2
                          Dim vEntities As Variant
                          Dim vEntity As Variant
                          Dim swEntity As SldWorks.Entity
                          Dim vSelectables As Variant
                          Dim vSelectable As Variant
                          Dim swSelectables() As SldWorks.Entity
                          Dim swSelectable As SldWorks.Entity
                          Dim iCount As Integer
                          Dim iIndex As Integer
                         
                          Set swApp = Application.SldWorks
                          Set swDoc = swApp.ActiveDoc
                          If swDoc Is Nothing Then Exit Sub
                          If swDoc.GetType <> swDocumentTypes_e.swDocDRAWING Then Exit Sub
                          Set swDrw = swDoc
                          Set swSheet = swDrw.GetCurrentSheet
                         
                          vViews = swSheet.GetViews
                          If Not IsEmpty(vViews) Then
                              iCount = UBound(vViews) - LBound(vViews) + 1
                              Debug.Print iCount & " views:"
                              For Each vView In vViews
                                  Set swView = vView
                                  Debug.Print "- " & swView.GetName2
                                  vCpns = swView.GetVisibleComponents
                                  If Not IsEmpty(vCpns) Then
                                      iCount = UBound(vCpns) - LBound(vCpns) + 1
                                      Debug.Print "  " & iCount & " components:"
                                      For Each vCpn In vCpns
                                          Set swCpn = vCpn
                                          Debug.Print "  - " & swCpn.Name2
                                          vEntities = swView.GetVisibleEntities2(swCpn, swViewEntityType_e.swViewEntityType_Face)
                                          If Not IsEmpty(vEntities) Then
                                              iCount = UBound(vEntities) - LBound(vEntities) + 1
                                              Debug.Print "    " & iCount & " face edges"
                                              For Each vEntity In vEntities
                                                  Set swEntity = vEntity
                                                  If IsEmpty(vSelectables) Then
                                                      ReDim vSelectables(0 To 0)
                                                      Set vSelectables(0) = swEntity
                                                  Else
                                                      ReDim Preserve vSelectables(LBound(vSelectables) To UBound(vSelectables) + 1)
                                                      Set vSelectables(UBound(vSelectables)) = swEntity
                                                  End If
                                              Next vEntity
                                          End If
                                      Next vCpn
                                  End If
                              Next vView
                          End If
                         
                          If Not IsEmpty(vSelectables) Then
                              ReDim swSelectables(LBound(vSelectables) To UBound(vSelectables))
                              For iIndex = LBound(vSelectables) To UBound(vSelectables)
                                  Set swSelectable = vSelectables(iIndex)
                                  Set swSelectables(iIndex) = swSelectable
                              Next iIndex
                              iCount = UBound(swSelectables) - LBound(swSelectables) + 1
                              Debug.Print iCount & " selectable entities"
                              iCount = swDoc.Extension.MultiSelect2(swSelectables, False, Nothing)
                              Debug.Print iCount & " selected entities"
                          End If
                         
                      End Sub
                      

                       

                      Obtained result:

                      Capture67.JPG

                       

                      Not all the faces are selected.