5 Replies Latest reply on Jul 9, 2017 1:50 PM by Deepak Gupta

    How to move an existing part to an exist folder using api?

    Marco Wu

      Dear all,

       

      I tried to find out a way to move existing parts into existing folders in my assembly. The folders are just below origin. The folders are created by templates.

       

      I have read 2013 SOLIDWORKS API Help - Move Assembly Components to New Folder Example (VBA)

       

      and

       

      2013 SOLIDWORKS API Help - InsertFeatureTreeFolder2 Method (IFeatureManager)

       

      However, I could not find a good answer.

       

      Would someone please make a simple program to do demonstration?

       

      Thanks

        • Re: How to move an existing part to an exist folder using api?
          Deepak Gupta

          Here are modified codes using the same example codes.

           

          Option Explicit
          Dim swApp As SldWorks.SldWorks
          Dim modelDoc2 As SldWorks.modelDoc2
          Dim assemblyDoc As SldWorks.assemblyDoc
          Dim featureMgr As SldWorks.FeatureManager
          Dim modelDocExt As SldWorks.ModelDocExtension
          Dim selectionMgr As SldWorks.selectionMgr
          Dim feature As SldWorks.feature
          Dim selObj As Object
          Dim feat As SldWorks.feature
          Dim folderFeat As SldWorks.feature
          Dim errors As Long
          Dim warnings As Long
          Dim status As Long
          Dim count As Long
          Dim componentToMove As SldWorks.Component2
          Dim componentsToMove() As Object
          Dim i As Long
          Dim retVal As Boolean
          
          Sub Main()      
              Set swApp = Application.SldWorks
              Set modelDoc2 = swApp.ActiveDoc
              Set assemblyDoc = modelDoc2
          
              'Select and get the two valve-related components to move to the new folder
              Set modelDocExt = modelDoc2.Extension
              Set selectionMgr = modelDoc2.SelectionManager
              status = modelDocExt.SelectByID2("1-1@Assem1", "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)  'Change part, instance ID and assembly name here
              Set selObj = selectionMgr.GetSelectedObject6(1, -1)
              status = modelDocExt.SelectByID2("2-1@Assem1", "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)  'Change part, instance ID and assembly name here
              Set selObj = selectionMgr.GetSelectedObject6(2, -1)
              count = selectionMgr.GetSelectedObjectCount2(0)
             
              ReDim componentsToMove(count - 1)
              For i = 0 To count - 1
                  Set componentToMove = selectionMgr.GetSelectedObjectsComponent4(i + 1, 0)
                  Set componentsToMove(i) = componentToMove
              Next
          
              'Set the folder where to move the selected components
              Set feature = assemblyDoc.FeatureByName("Folder1") 'Change folder name here
          
              'Move the selected components to the new folder
              retVal = assemblyDoc.ReorderComponents(componentsToMove, feature, swReorderComponents_LastInFolder)
          
          End Sub