Move Assembly Components to New Folder Example (C#)
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.
//--------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Runtime.InteropServices;
namespace ReOrderMacroCSharp.csproj
{
public
partial class SolidWorksMacro
{
public
void Main()
{
int
errors = 0;
int
warnings = 0;
bool
status = false;
//Open
assembly document
swApp.OpenDoc6("C:\\Program Files\\SolidWorks
Corp\\SolidWorks\\samples\\tutorial\\motionstudies\\valve_cam.sldasm",
(int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent,
"", ref errors, ref warnings);
ModelDoc2
modelDoc2 = (ModelDoc2)swApp.ActiveDoc;
AssemblyDoc
assemblyDoc = (AssemblyDoc)modelDoc2;
FeatureManager
featureMgr = (FeatureManager)modelDoc2.FeatureManager;
//Select
and get the two valve-related components to move to the new folder
ModelDocExtension
modelDocExt = modelDoc2.Extension;
SelectionMgr
selectionMgr = (SelectionMgr)modelDoc2.SelectionManager;
status
= modelDocExt.SelectByID2("valve-1@valve_cam",
"COMPONENT", 0, 0, 0, true, 0, null, 0);
object
selObj = selectionMgr.GetSelectedObject6(1,
-1);
status
= modelDocExt.SelectByID2("valve_guide-1@valve_cam",
"COMPONENT", 0, 0, 0, true, 0, null, 0);
selObj
= selectionMgr.GetSelectedObject6(2,
-1);
int
count = selectionMgr.GetSelectedObjectCount2(0);
object[]
componentsToMove = new object[count];
for
(int i = 0; i < count; i++)
{
componentsToMove[i]
= selectionMgr.GetSelectedObjectsComponent4(i
+ 1, 0);
}
//Create
the folder where to move the selected components
Feature
feature = featureMgr.InsertFeatureTreeFolder2((int)swFeatureTreeFolderType_e.swFeatureTreeFolder_EmptyBefore);
feature
= (Feature)assemblyDoc.FeatureByName("Folder1");
//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, (int)swReorderComponentsWhere_e.swReorderComponents_LastInFolder);
}
public
DispatchWrapper[] ObjectArrayToDispatchWrapperArray(object[] SwObjects)
{
int
arraySize;
arraySize
= SwObjects.GetUpperBound(0);
DispatchWrapper[]
dispwrap = new DispatchWrapper[arraySize + 1];
int
arrayIndex;
for
(arrayIndex = 0; arrayIndex < arraySize + 1; arrayIndex++)
{
dispwrap[arrayIndex]
= new DispatchWrapper(SwObjects[arrayIndex]);
}
return
dispwrap;
}
///
<summary>
///
The SldWorks
swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
public
bool retVal;
public
DispatchWrapper[] compsToMove;
}
}