You need to be logged in to view this content. Please login or become a member.

<<  Prev   |   Index   |   Next  >>


This important lesson explores the means of getting pre-selected objects and selecting objects using the SolidWorks API.


Marking is used to specify which entities are used in a particular aspect of feature creation. For example, in order to create a revolved feature you need to have a profile and an axis selected. To specify which selected object is the profile and which is the axis, you need to mark the axis with 16, since this is what the member page for IFeatureManager::FeatureRevolve2 states. You will see marking used in the lesson on feature creation.

You can mark objects using the Mark argument in IModelDocExtension::SelectByID2 or by using the ISelectData::Mark property.

If you intend to select a dimension, display dimension, feature, or component with IModelDocExtension::SelectByID2, use one of these methods to quickly get the name string: IComponent2::GetSelectByIDString, IDimension::GetNameForSelection, IDisplayDimension::GetNameForSelection, IFeature::GetNameForSelection.

Lesson Files

Lesson 3.4 select methods code
Lesson 3.4 SelectionManager code


Lesson 3.4 homework part A solution
Lesson 3.4 homework part B solution


  1. yona1652

    Hello! I am trying to take part and another part put them in assy and make mates between them, the problem is choosing a face in one part WITHOUT the coordinates, so I have to specify (in selectbyid2) object name and type and in the video u said I need to use ipartdoc::getentitybyname
    can u please explain how?
    do I need to dim entities before?
    or dim faces?


    • The Name argument in ISelectionMgr::SelectByID2 is not the same as an entity name. An entity name you create by right clicking on the entity (body, face, edge, vertex) and entering a name is the Name property. Then you can select that entity using IPartDoc::GetEntityByName. Here is an example.

      With SelectByID2, however, I would not recommend trying to select faces because you do not know the name, and you may not know the coordinates. To locate faces I would recommend you traverse all of the faces. You can learn how to do this in Lesson 4.3. In Lesson 5.1 you will also learn how to mate components using pre-named faces.

  2. gik

    Dear Sir,
    Sometime I cannot pass data from Variant to Body:

    Dim vBody As Variant
    Dim swBody As SldWorks.Body2
    Set vBody = swSelMng.GetSelectedObject6(1, -1)
    Set swBody = vBody ( Run-Time error 13 )

    What am I missing ?

    • This code works fine for me when I select a body in the feature manager tree:

      Dim swApp As SldWorks.SldWorks
      Dim swModel As SldWorks.ModelDoc2
      Dim swSelMgr As SldWorks.SelectionMgr
      Dim swBody As SldWorks.Body2
      Dim vBody As Variant
      Sub main()
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          Set swSelMgr = swModel.SelectionManager
          Set vBody = swSelMgr.GetSelectedObject6(1, -1)
          Set swBody = vBody
          Debug.Print swBody.Name
      End Sub

      But I don’t understand why you would do this. It would make more sense to simply cast ISelectionManager.GetSelectedObject6 to swBody.

Questions and Comments

You must be logged in to post a comment.