3 Replies Latest reply on Aug 6, 2015 8:53 PM by Abdul Rauf Aman

    Select plane from selected part in assembly

    Abdul Rauf Aman

      Hi,

      I want to create a macro to select X plane from a selected parts in an assembly. can anybody help me? Still new in programming skill.

       

      Thank you.

        • Re: Select plane from selected part in assembly
          Ivana Kolin
          Option Explicit
          Const ReqPlane              As Long = 3
          Dim swApp As SldWorks.SldWorks
          Dim Part As SldWorks.ModelDoc2
          Dim boolstatus As Boolean
          Dim longstatus As Long, longwarnings As Long
          
          Sub main()
          Dim swSelMgr As SldWorks.SelectionMgr
          Dim swComp As SldWorks.Component2
          Dim swCompModel As SldWorks.ModelDoc2
          Dim swFeat As SldWorks.Feature
          Dim swCorrFeature As SldWorks.Feature
          Dim PlaneCount              As Long
          Dim bRet As Boolean
          
          Set swApp = Application.SldWorks
          Set Part = swApp.ActiveDoc
          Set swSelMgr = Part.SelectionManager
          
          If swSelMgr.GetSelectedObjectCount2(-1) = 0 Then Exit Sub
          
          Select Case swSelMgr.GetSelectedObjectType3(1, -1)
          Case swSelCOMPONENTS
              Set swComp = swSelMgr.GetSelectedObject6(1, -1)
          Case Else
              Set swComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)
          End Select
          
          If swComp Is Nothing Then Exit Sub
          
          Set swCompModel = swComp.GetModelDoc2
          
          Set swFeat = swCompModel.FirstFeature
          
          Do While Not swFeat Is Nothing
              If "RefPlane" = swFeat.GetTypeName Then
                  PlaneCount = PlaneCount + 1
                  If ReqPlane = PlaneCount Then
                      Set swCorrFeature = swComp.GetCorresponding(swFeat)
                      If swCorrFeature Is Nothing Then Exit Sub
                      bRet = swCorrFeature.Select2(False, 0)
                      Exit Do
                  End If
              End If
              Set swFeat = swFeat.GetNextFeature
          Loop
          
          End Sub
          
          
          • Re: Select plane from selected part in assembly
            Patrick Jensen

            Dim swApp As SldWorks.SldWorks

            Dim swModel As SldWorks.ModelDoc2

            Dim swSelMgr As ISelectionMgr

            Dim swSelGroup As New Collection

            Dim swComp As SldWorks.Component2

            Dim swFeat As Feature

             

            Set swApp = Application.SldWorks

            Set swModel = swApp.ActiveDoc

            Set swSelMgr = swModel.SelectionManager

             

            For i = 1 To swSelMgr.GetSelectedObjectCount

                swSelGroup.Add swSelMgr.GetSelectedObjectsComponent3(i, -1)

            Next i

             

            swModel.ClearSelection2 True

             

            For j = 1 To swSelGroup.Count

                 Set swComp = swSelGroup.Item(j)

                 Set swFeat = swComp.FeatureByName("X Plane")

                 val = swFeat.Select(True)

            Next j

             

             

             

            Dont completely know what you were looking for but that selects the "X Plane" of every selected component. Does not leave the components selected though. If you wanted both the components and their plane selected you just have to delete "swModel.ClearSelection2 True"

            • Re: Select plane from selected part in assembly
              Abdul Rauf Aman

              Thank you Ivana and Patrick. Both macro can be used and it is what I'm looking for. Patrick's macro is much simpler and easy to modified. One again thank guys for your help. now it is much faster for me to do the selection of the plane.