Move Assembly Components to New Folder Example (VB.NET)
This example shows how to move selected assembly components to a newly
created folder in the FeatureManager design tree.
'-------------------------------------------------------
' Preconditions: Specified assembly document to open exists.
'
' Postconditions:
' 1. Assembly document is opened.
' 2. The valve<1> and valve_guide<1> components
are selected.
' 3. Folder named Folder1 is created in the FeatureManager
design tree.
' 4. The valve<1> and valve_guide<1> components
are moved to Folder1,
' which
you can verify by expanding the Folder1 folder.
'
' NOTE: Because the assembly document is used elsewhere,
' do not save any changes when closing the document.
'--------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Runtime.InteropServices
Partial Public Class SolidWorksMacro
Public
Sub Main()
Dim
errors As Integer = 0
Dim
warnings As Integer = 0
Dim
status As Boolean = False
'Open
assembly document
swApp.OpenDoc6("C:\Program Files\SolidWorks
Corp\SolidWorks\samples\tutorial\motionstudies\valve_cam.sldasm",
CInt(swDocumentTypes_e.swDocASSEMBLY), CInt(swOpenDocOptions_e.swOpenDocOptions_Silent),
"", errors, warnings)
Dim
modelDoc2 As ModelDoc2 = DirectCast(swApp.ActiveDoc,
ModelDoc2)
Dim
assemblyDoc As AssemblyDoc = DirectCast(modelDoc2, AssemblyDoc)
Dim
featureMgr As FeatureManager = DirectCast(modelDoc2.FeatureManager,
FeatureManager)
'Select
and get the two valve-related components to move to the new folder
Dim
modelDocExt As ModelDocExtension = modelDoc2.Extension
Dim
selectionMgr As SelectionMgr = DirectCast(modelDoc2.SelectionManager,
SelectionMgr)
status
= modelDocExt.SelectByID2("valve-1@valve_cam",
"COMPONENT", 0, 0, 0, True, _
0,
Nothing, 0)
Dim
selObj As Object = selectionMgr.GetSelectedObject6(1,
-1)
status
= modelDocExt.SelectByID2("valve_guide-1@valve_cam",
"COMPONENT", 0, 0, 0, True, _
0,
Nothing, 0)
selObj
= selectionMgr.GetSelectedObject6(2,
-1)
Dim
count As Integer = selectionMgr.GetSelectedObjectCount2(0)
Dim
componentsToMove As Object() = New Object(count - 1) {}
For
i As Integer = 0 To count - 1
componentsToMove(i)
= selectionMgr.GetSelectedObjectsComponent4(i
+ 1, 0)
Next
'Create
the folder where to move the selected components
Dim
feature As Feature = featureMgr.InsertFeatureTreeFolder2(CInt(swFeatureTreeFolderType_e.swFeatureTreeFolder_EmptyBefore))
feature
= DirectCast(assemblyDoc.FeatureByName("Folder1"),
Feature)
'Convert
.NET objects to IDispatch by using DispatchWrapper
compsToMove
= ObjectArrayToDispatchWrapperArray(componentsToMove)
modelDoc2.ClearSelection2(True)
'Move
the selected components to the new folder
retVal
= assemblyDoc.ReorderComponents(compsToMove,
feature, CInt(swReorderComponentsWhere_e.swReorderComponents_LastInFolder))
End
Sub
Public
Function ObjectArrayToDispatchWrapperArray(ByVal SwObjects As Object())
As DispatchWrapper()
Dim
arraySize As Integer
arraySize
= SwObjects.GetUpperBound(0)
Dim
dispwrap As DispatchWrapper() = New DispatchWrapper(arraySize) {}
Dim
arrayIndex As Integer
For
arrayIndex = 0 To arraySize
dispwrap(arrayIndex)
= New DispatchWrapper(SwObjects(arrayIndex))
Next
Return
dispwrap
End
Function
'''
<summary>
'''
The SldWorks swApp variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
Public
retVal As Boolean
Public
compsToMove As DispatchWrapper()
End Class