Add Objects to Selection List Example (C#)
This example shows how to populate the selection list with objects that have
not been preselected through
the user interface.
//----------------------------------------------------------------------------
// Preconditions: Open a model that contains Sketch1 and Sketch2.
//
// Postconditions:
// 1. Inspect the Immediate Window.
// 2. The selection list containing Sketch1 is suspended.
// 3. Another selection list is populated with two objects.
// 4. The type of the last object in the selection list is reported.
// 5. The previous selection list with Sketch1 is resumed.
// 6. Sketch1 is placed in edit mode.
//
---------------------------------------------------------------------------
using
Microsoft.VisualBasic;
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Data;
using
System.Diagnostics;
using
System.Runtime.InteropServices;
using
SolidWorks.Interop.sldworks;
using
SolidWorks.Interop.swconst;
namespace
AddObjectToSelectionList_CSharp.csproj
{
partial
class
SolidWorksMacro
{
ModelDoc2
swModel;
SelectionMgr
selMgr;
SelectData
selData;
FeatureManager
featMgr;
Feature
feat;
DispatchWrapper[]
arrObjIn = new
DispatchWrapper[2];
Feature[]
selObjs = new
Feature[2];
object
selobj;
int
featCount;
int
i;
string
typeName;
int
j;
int
numAdded;
bool
boolstatus;
int
ret;
int
count;
public
void Main()
{
swModel = (ModelDoc2)swApp.ActiveDoc;
selMgr = (SelectionMgr)swModel.SelectionManager;
selData = (SelectData)selMgr.CreateSelectData();
featMgr = swModel.FeatureManager;
featCount = featMgr.GetFeatureCount(true);
feat = (Feature)swModel.FirstFeature();
j = 0;
for
(i = 0; i <= featCount; i++)
{
if
((feat != null))
{
typeName = feat.Name;
typeName = typeName.ToUpper();
if
("SKETCH"
== typeName.Substring(0, 6))
{
selObjs[j] = feat;
arrObjIn[j] = new
DispatchWrapper(selObjs[j]);
j = j + 1;
}
feat = (Feature)feat.GetNextFeature();
}
}
// Add one object to the
current selection list
boolstatus = swModel.Extension.SelectByID2("Sketch1",
"SKETCH",
0, 0, 0, false,
0, null,
0);
// Start a new selection list
ret = selMgr.SuspendSelectionList();
Debug.Print("The
current selection list with " + ret +
" object (Sketch1) is suspended.");
// Add two objects to the new
selection list
numAdded = selMgr.AddSelectionListObjects((arrObjIn),
selData);
Debug.Print("A
new selection list is started.");
// Get number of objects in the
new selection list (should be 2)
count = selMgr.GetSelectedObjectCount();
Debug.Print("The
selection list now contains " + count +
" objects.");
// Get the last object in the
new selection list
selobj = selMgr.GetSelectedObject6(count,
-1);
Debug.Print("The
last object in the selection list is of swSelectType_e = "
+ selMgr.GetSelectedObjectType3(count, -1) +
".");
// Go back to the previous
selection list
selMgr.ResumeSelectionList();
Debug.Print("The
previous selection list is resumed.");
// Get the number of objects in
the selection list (should be 1)
count = selMgr.GetSelectedObjectCount();
Debug.Print("The
selection list now contains " + count +
" object (Sketch1).");
swModel.EditSketch();
Debug.Print("Sketch1
is in edit mode.");
}
public
SldWorks
swApp;
}
}