Engineering / Computers / Part 4 of 9
Working with Selected Objects
A selection is a contract. The user says "these entities, in this order"; the program must verify that what it received matches what it needs before acting. Programs that skip the verification step are the ones that mate the wrong faces and move the wrong dimension.
- Part 4 · Applied
- Marks & ordering
- Mates, materials, dimensions
- Filters
KL-ENG-COMP-1504 · KEVOS® Knowledge Library · Australian English
Selection-driven tools are the most natural fit for engineering work, because the engineer already knows which entities matter. The program's job is to take that judgement and apply a rule to it exactly. Doing that safely means interrogating the selection before trusting it.
01The selection manager and marks
Selection state belongs to the document, and is reached through its selection manager. Everything about the current selection is read from there.
ISelectionMgr swSelMgr = (ISelectionMgr)swModel.SelectionManager;
int count = swSelMgr.GetSelectedObjectCount2(-1);
if (count < 2)
{
MessageBox.Show("Select two faces, then run this command.");
return;
}What a mark is
A mark is an integer label attached to a selection when it is made, used to separate selections that belong to different roles. A PropertyManager page with two selection boxes gives each box a different mark, so the program can ask for "the selections belonging to box one" without the user having to select in a particular sequence.
Selections are returned in the order the user made them, and for many operations that order is meaningful — which face is the reference and which is being positioned, for instance. If your tool depends on order, say so plainly in the prompt. If it does not, determine roles from geometry rather than from sequence.
02Identifying what has been selected
Before acting, establish that every selection is a type your operation can accept. The type is reported as a member of the selection type enumeration.
for (int i = 1; i <= count; i++)
{
int selType = swSelMgr.GetSelectedObjectType3(i, -1);
bool acceptable =
selType == (int)swSelectType_e.swSelFACES ||
selType == (int)swSelectType_e.swSelEDGES ||
selType == (int)swSelectType_e.swSelVERTICES;
if (!acceptable)
{
MessageBox.Show("Select faces, edges or vertices only.");
return;
}
}Validating everything before acting on anything matters more than it appears. A loop that acts as it goes and fails on the third item leaves the model half-modified, and the user with no clear way back. Validate, then act.
| Enumeration member | Selects | Typical use |
|---|---|---|
| swSelFACES | A face of a solid or surface body | Mating, material application, measurement |
| swSelEDGES | An edge | Mating, fillet and chamfer input, dimensioning |
| swSelVERTICES | A vertex | Coincident mates, reference points |
| swSelCOMPONENTS | A component instance in an assembly | Suppression, replacement, property editing |
| swSelDIMENSIONS | A dimension | Reading and driving values |
| swSelDRAWINGVIEWS | A view on a drawing sheet | Auditing, repositioning, scale changes |
03Retrieving the objects themselves
Once the types are confirmed, retrieve the objects and cast them to the interface the operation needs.
object firstSel = swSelMgr.GetSelectedObject6(1, -1);
object secondSel = swSelMgr.GetSelectedObject6(2, -1);
// In an assembly, an entity also belongs to a component instance:
IComponent2 owner = (IComponent2)swSelMgr.GetSelectedObjectsComponent4(1, -1);In an assembly, a selected face belongs both to a body and to the component instance containing it. Which one you need depends on the operation: mating works with the entity, while suppressing or renaming works with the component. Retrieving the wrong one produces an operation that fails without any obvious reason.
The selection retrieval members carry version numbers that have incremented over successive releases. Use the highest-numbered variant your target release exposes, and note that number in the source. The behaviour is broadly consistent; the argument lists are not.
04Worked pattern: mating two selected entities
Mating is the classic selection-driven operation and shows the full shape of the pattern: guard, validate, act, verify.
The mate call takes the mate type and an alignment condition, along with distance and angle values that are ignored for mate types that do not use them. Alignment deserves particular care: an aligned and an anti-aligned coincident mate are both valid and produce opposite results. Where the correct choice depends on geometry rather than on convention, letting the application choose the closest alignment is usually right for an interactive tool, while an explicit value is right for a tool applying a company standard.
The mate call reports failure through an output argument. A returned object with a non-zero error means the mate was created but is not solving — over-defined, or conflicting with existing mates. Read the error, and where the operation should have changed geometry, rebuild before reporting success so that what the user sees matches what you tell them.
05Applying a material to a selection
Material assignment is a strong automation candidate: the rule is simple, the volume is high, and manual application is inconsistent across a team.
Material is set on the part document, naming the material database and the material within it, and optionally a configuration. A material named in the call must exist in the named database, and names are matched exactly — a trailing space or a differing case will fail without an obvious cause.
Reading what is applied
The document reports both an internal material identifier and the user-visible material name. The identifier is what you compare against in an audit; the name is what you put in a report.
Where selections come in
In an assembly, resolve each selected component to its underlying part document before applying material. Applying to the assembly is not the same operation and will not do what the user expected.
Custom databases
Company material libraries live in their own database file. Automation that assumes the default library will fail on any site using its own — take the database path as configuration.
Configuration scope
Material can differ between configurations. Decide whether your rule applies to the active configuration or to all of them, and state it in the tool's documentation.
Before writing a tool that applies material, write one that reports it across an assembly. In most businesses the report finds enough inconsistency to justify the application tool without further argument — and it carries no risk of modifying anything.
06Driving dimension values
Reading and writing dimensions is where the units trap does the most damage, because a wrong value rebuilds successfully and looks plausible on screen.
IDisplayDimension dispDim = (IDisplayDimension)swSelMgr.GetSelectedObject6(1, -1);
IDimension dim = dispDim.GetDimension2(0);
// Values arrive as an array; the first entry is the value itself, in METRES.
double[] current = (double[])dim.GetSystemValue3(
(int)swInConfigurationOpts_e.swThisConfiguration, null);
double newValueMetres = current[0] * 1.10; // grow by ten per cent
dim.SetSystemValue3(newValueMetres,
(int)swInConfigurationOpts_e.swThisConfiguration, null);
swModel.EditRebuild3();Three points carry most of the risk.
- A selected dimension is a display dimension. The underlying dimension is obtained from it. Working with the wrong one produces members that do not exist or values that do not change.
- Configuration scope is explicit. The configuration option argument decides whether you are changing this configuration, all configurations, or a named set. The default is rarely what a company standard requires — state it deliberately.
- Nothing has changed until you rebuild. Setting a value updates the model definition; the geometry follows on rebuild. Reading a downstream measurement before rebuilding returns the previous state.
Where a dimension value comes from a user or a spreadsheet, validate the range before writing. A value entered in millimetres and written as metres is a factor of a thousand, and a model rebuilt at that scale can take a very long time to fail.
07Selecting from code, and narrowing what can be picked
Two capabilities complete the picture: selecting entities without the user, and constraining what the user is able to select.
Programmatic selection
Entities can be selected by name and type through the document extension, optionally appending to the existing selection and assigning a mark. This is what replaces the coordinate-based selection a recorded macro produces, and it is the mechanism behind almost every robust selection-driven tool.
- Append or replace. An append flag controls whether the new selection joins the existing one or replaces it. Getting this wrong is the usual cause of a tool that works on the first entity only.
- Assign marks deliberately when the selection will be consumed by a page with several selection roles.
- Clear first. Beginning with a cleared selection removes any dependence on what the user happened to have picked beforehand.
Selection filters
Filters restrict what the pointer will pick up. Enabling a filter for faces and edges while your tool is active means the user cannot accidentally select a plane or a sketch, and the validation you wrote is far less likely to reject them.
A selection filter changes application behaviour, not just your tool's. Record what was set when you started, apply what you need, and restore it on every exit path including the failure paths. A tool that leaves filters on is remembered as the tool that broke selection.
