Learn how to stay in touch with CodeStack latest updates about SOLIDWORKS API, C#, Visual Basic, DevOps and software development tutorials and tips & trick. Never miss the releases of new useful macros or applications to enhance your SOLIDWORKS experience.
Selection is a vital part of SOLIDWORKS API development process. In this article different selection methods and selection options will be discussed.
Mainly selections are used for
- Features creation (e.g. Extrude feature requires selection of the sketch and optional selection of direction)
- Evaluation (i.e. surface area or perimeter calculation)
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 allows to provide additional instruction to the selection:
- Associate callouts elements with the selection
- Assign the selection mark attributes
- Specify the drawing view to select element in
- Set the selection point. This can be also set via ISelectionMgr::SetSelectionPoint2 method for already selected object.
- Set the cells range to select in the table annotation.
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.
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:
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.
By object dispatches in a batch mode. This is useful way of selecting any selectable object without the need to cast it to the specific interface. Refer the Select Any SOLIDWORKS Objects In A Batch for an example
By type (i.e. standard plane or view). Refer the Select Standard Plane By Type for an example
By name of named entities (face, edge or vertex). Refer Select Named Entity
By adding objects directly to selection list. Read Selecting Objects For API Only article for more information
By using the Advanced Component Selection in assembly
By vector using intersection rays
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.