Selections in SOLIDWORKS API
Selections in SOLIDWORKS API

Selection is a vital part of SOLIDWORKS API development process. In this article different selection methods and selection options will be discussed.

Application

Mainly selections are used for

  • Features creation (e.g. Extrude feature requires selection of the sketch and optional selection of direction)
  • Mating
  • Evaluation (i.e. surface area or perimeter calculation)
  • Dimensioning
  • Highlighting

Selection Marks

Selection marks for Extrude feature
Selection marks for Extrude feature

Selection marks are integer attributes which can be associated with the selected entity in order to differentiate different group of objects by purpose. For example selection used in different selection boxes in the property manager page will have different selection marks. Selection marks can be assigned while selection of new objects via IModelDocExtension::SelectByID2 method, or direct Select methods such as IFeature::Select2 where the mark is passed as an Mark parameter.

Selection mark can also be passed within the Selection Data ISelectData::Mark property as some select methods expect this object to be passed to the method as Data parameter (e.g. IComponent2::Select4, IAnnotation::Select3 methods).

Use ISelectionMgr::SetSelectedObjectMark method to change the selection mark of already selected object.

Selection Data

Selection data is an object created via ISelectionMgr::CreateSelectData SOLIDWORKS API method which can be passed to various selection methods (e.g. IBody2::Select2).

Selection data allows to provide additional instruction to the selection:

Selection data is an optional parameter. Pass NULL to methods if it is not used.

Refer the ISelectData SOLIDWORKS API interface members for more information.

ISelectData Interface Members
ISelectData Interface Members

Selection Methods

There are multiple ways of selecting entities in SOLIDWORKS. The following list is the most common ways of selecting elements:

  • By name or by coordinate. This method is usually used in macro recording and selects the object ‘as is’ (i.e. as it would be selected from the User Interface). This approach introduces potential issues and usually a reason for macro instability and inconsistency and should be avoided. Refer the Selecting Objects By Name And Coordinates article for pros and cons of this approach.

  • By direct SelectX method. Majority of selectable objects in SOLIDWORKS provide direct SelectX method which allows to select the element from its pointer:

Direct SelectX methods for SOLIDWORKS objects
Direct SelectX methods for SOLIDWORKS objects

This is a preferable method of selection as it provides the consistency and it is not dependent on the view’s orientation. Refer the Select All Sketch Elements example which shows how different sketch elements can be selected.

Selecting Entities In Drawing Views

Entities in the drawing view (i.e. elements which are created in the 3D models) can be selected via IModelDocExtension::SelectByID2 method but it introduces the same issues as using this method in 3D models (refer Selection Methods section). Refer Selecting Entities In Drawing View article for examples of different approaches for selecting entities in the drawing views.