How to work with selections in the SOLIDWORKS API (part 8)

Introduction

User interfaces are built for clicking and selecting things. But when you try to automate a task via an API, you need other ways of selecting objects.

In this blog post, I’ll share everything I know about selecting objects in the SOLIDWORKS API, plus how to get the objects that a user selected.

In this blog post, you’ll find

  1. Which objects can you select via the API?
  2. How to select an object
    1. Select an object by its name
    2. Select an object by its Select method
    3. Select an object by its position
    4. Selecting by ray
  3. Tricky selections
    1. How to select assembly components
    2. How to select entities
    3. How to select mates
    4. How to select origins
    5. How to select sketch segments
  4. Multi-select
  5. Select all
  6. Appending an existing selection
  7. Selection marks
  8. Selection data
  9. Get a selected object
    1. Iterating over selected objects
  10. Deleting selected objects

1. Which objects can you select via the API?

Every object that the user can select via the user interface, you can select via the API. The best overview is on the help page of the swSelectType enumeration.

The list includes geometry like faces and edges, features in the feature tree, sketch segments and assembly components.

2.How to select an object

There are four main ways of selecting objects and I will talk about each one, from most-used to least-used:

  1. By name
  2. By its Select method
  3. By its position
  4. By intersection of a ray

2.1. Select an object by its name

You will do this often. You call SelectByID2 on the ModelExtension object, pass the name and type of the object and set the position to the default (0,0,0). The method call will look something like this in C#:

There are nine parameters:

  1. Object name
  2. Object type
  3. X-coordinate: not used, so zero
  4. Y-coordinate: not used
  5. Z-coordinate: not used
  6. Append boolean: see Appending an existing selection
  7. Mark integer: see Selection marks
  8. Callout object: I still have no idea what this is
  9. Selection option: I have only ever used the default of zero

2.2. Select an object by its Select method

A few objects have a Select method (or a newer version with Select2, Select3…), so if you already have access to the object, you can call that method directly. These are the ones I use the most:

I don’t know why some objects have a Select method and others don’t.

2.3. Select an object by its position

When the two methods above don’t work for your application, you can try selecting an object by its position. Since you only have a point and no direction, I only use this method when working with drawings.

You call the same SelectById2 method, but you pass an empty string for the object name and three coordinates:

2.4. Selecting by ray

This method does have a position, whereas selecting an object by position didn’t have one. You can only use it to select geometry, though:

The twelve parameters are the following:

  1. Position, x-coordinate
  2. Position, y-coordinate
  3. Position, z-coordinate
  4. Direction, x-coordinate
  5. Direction, y-coordinate
  6. Direction, z-coordinate
  7. Radius of the cylinder that should hit the object. I usually keep this constant at, for example, 1e-4 (0.001). If it consistently fails, I increase this radius.
  8. The object type you want to select
  9. Append boolean: see Appending an existing selection
  10. Mark integer: see Selection marks
  11. Callout object: I still have no idea what this is
  12. Selection option: I have only ever used the default of zero

SelectByRay select silhouette edge in solidworks

I only use this method when all of these conditions are true:

  • I have full control over the geometry
  • There are no named geometry objects
  • I do not have access to the objects I want to select

I do this in the software that creates our Fastener Models library, where I select edges (and sometimes silhouette edges) in an isometric view so I can add mate references to them.

Be aware, though, this method is very hard to get right consistently.

Ditch the Toolbox, once and for all

Start using the fastener library that actually follows the standards
  • No more mate errors
  • Made for speed
  • No yearly license fees

3. Tricky selections

3.1. How to select assembly components

If you have access to the IComponent2 object, you can always call Select4 directly. But if you want to select the component by its name, you need to call the GetSelectByIDString method to get the full component name that includes all sub-assembly levels. Pass that string as the name when you call SelectById2.

Make sure you get the component from the top-level assembly directly. If you have the ModelDoc2 of a sub-assembly and get the components from there, the component objects are different and will behave unpredictably.

3.2. How to select entities

There are five object types that are secretly entities, and therefore implement IEntity:

  1. IEdge
  2. IFace2
  3. IFeature
  4. ILoop2
  5. IVertex

A Face2 object has no Select method, but Entity has. So to select these objects directly, cast them to an Entity, then call Select4.

To learn more about working with entities in the API, check out part seven of this series: Entities and GetCorresponding in the SOLIDWORKS API.

3.3. How to select mates

Mates are features, so if you want to select a mate, you first need to cast it to a feature and get the name of that feature. But when you ultimately call SelectById2, you have to pass “MATE” as the type.

3.4. How to select origins

Origins are points, so the correct name is “Point1@Origin” and the type is “EXTSKETCHPOINT”. When no sketch is active, this selects the model’s origin. If a sketch is active, you can select the sketch origin this way, although you may have to pass “SKETCHPOINT” as the type. See the next section.

3.5. How to select sketch segments

If a sketch is active, you can select a sketch segment by its name and the type “SKETCHSEGMENT”.

But if no sketch is active and you want to select a segment within a sketch feature, you need to add the name of the sketch and use the type “EXTSKETCHSEGMENT”. For example:

Be careful when selecting sketch points. If you use the argument “SKETCHSEGMENT” and not “SKETCHPOINT”, SOLIDWORKS may crash.

select a sketch segment when no sketch is active

4. Multi-select

With IModelDocExtension.MultiSelect2, you can select multiple objects at once. I don’t use this method very often, but when I need it, it’s really useful and fast. Just pass an array of objects, they don’t even have to be of the same type, and call MultiSelect2.

I don’t think you need to wrap these objects in a Dispatch Wrapper, but if you call this method and it crashes SOLIDWORKS, you should try that.

5. Select all

You don’t often need this one, but when you do, call:

6. Appending an existing selection

Every select method has an Append parameter. To append means to add, so if you have an existing selection and want to add an object to that selection, set the Append parameter to true.

If you want to select multiple objects without using MultiSelect2, you can do something like this:

7. Selection marks

You may have already noticed that SelectByID2 has a Mark integer parameter. It lets you share your intention for each selected item with SOLIDWORKS.

SelectById2 mark

SOLIDWORKS also uses marks for Property Manager Pages, for example the one you see when you create a Revolve feature. Every geometry input box has its own mark setting, which you can read about in the docs.

  • The sketch to revolve, using Mark = 0.
  • The axis of revolution, using Mark = 4 or 16.
  • The up-to surface, up-to vertex, or offset-from surface, using Mark = 32.
  • One or more affected bodies or components (only if UseFeatScope is set to true and UseAutoSel is set to false), using Mark = 1 for each.

The default value is minus one (-1), which says “I don’t care what this selection is for”. The other one I remember is one (1), for when you select objects to create a Mate.

8. Selection data

Where SelectById2 uses a Mark integer parameter, all the objects that have their own Select method (see Select an object by its Select method) have a ISelectData parameter.

The SelectData object has a Mark property, but that is the only property that I have ever used on this object.

9. Get a selected object

To work with selected objects, you use the IModelDoc2.SelectionMgr (SelectionManager) object. There are four methods I use often:

  1. GetSelectedObject6: get the selected object for a certain index.
  2. GetSelectedObjectsComponent3: get the component that owns the selected geometry.
  3. GetSelectedObjectCount2: get the number of selected objects.
  4. GetSelectedObjectType3: get the type of object for a certain index.

9.1. Iterating over selected objects

Using the methods mentioned previously, it’s easy to create a for loop to iterate over the selected objects:

Please note that when these methods take an Index parameter, this index starts from one, not zero! That’s why we use “index <=” as well.

10. Deleting selected objects

This one is easy. Just select a bunch of objects and call IModelDocExtension.DeleteSelection2. You can pass an integer parameter to do advanced stuff like deleting child objects, but usually, a zero is all you need.

All blog posts in this series

  1. The SOLIDWORKS Object Model + API explained
  2. SOLIDWORKS API: the basics - SldWorks, ModelDoc2
  3. How to work with Features 
  4. Persistent ID and sketch segment ID and more
  5. All identifiers in the SOLIDWORKS API
  6. About return values
  7. Entities and GetCorresponding
  8. How to work with selections
  9. How to use custom properties
  10. Understanding math, MathVector and MathTransform
  11. Toolbars, menus and the Command Manager
  12. How to create task panes and Property Manager Pages
  13. How to create your own SOLIDWORKS add-in
  14. Creating add-ins with SolidDNA: the basics

Don't miss the next post. Get a free add-in.

Subscribe to our newsletter and get our TimeSavers add-in for free.