10 Replies Latest reply on Feb 18, 2016 1:56 AM by Sina Mohseni

    Selecting faces in Macro,

    Sina Mohseni

      Hi guys,


         I'm trying to record a macro;
      1) while working with a non .SLDPRT CAD file (like X_T or STP)


      2) using functions in Direct Editing tab, like moving faces changing diameters.

      but unfortunately Solidworks can not follow me and record the very first step. "SelectByID2" function (for selecting faces) doesn't work, So, all other steps are fail.


      --Example for "SelectByID2" function in regular macros using SLDPRT files with feature tree:

      >> boolstatus = Part.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)


      >> boolstatus = Part.Extension.SelectByID2("D9@Sketch1@MyExample.SLDPRT", "DIMENSION", 0.388868242092195, 0.598798981386682, -0.56327146244613, False, 0, Nothing, 0)


      --Example for SelectByID2 when I try to work with X_T files with no feature tree:

      >> boolstatus = Part.Extension.SelectByID2("", "FACE", -0.397196525943571, 0.406399999999906, 0.178747848011696, False, 0, Nothing, 0)



      There is no ID for the selected face!

      Any suggestions?

      Any other functions or examples to  start with?

         I appreciate if you could help me on this matter.


      Best Regards,


        • Re: Selecting faces in Macro,
          Artem Taturevych

          Faces and other entities do not have assigned ids to them so SelectByID2 recorded by macro tries to select the face by coordinate. And this is very sensitive (so you need to make sure that the target face appears within the view so it can be selected). You need to use the IEntity::Select4 method instead for selecting faces, edges and vertices.


          So in your macro you need to decide what face are you going to modify and find it via API. Check this article for some guidance: Re: Selecting faces in a Part




            • Re: Selecting faces in Macro,
              Sina Mohseni

                  Thank you Artem, I've seen the article you mention, It seems Select4 is a proper function for selecting faces.

              But in discussion, faces are selected by their normal vector or assigned names. In my case, I'm using X_T files, which there is no name for faces. So, I'm planning to select faces with mouse clicks manually.


              I found such this code in three steps:

              (you can find complete code here: 2013 SOLIDWORKS API Help - Get Reference Point Data Example (VBA)


              1) Set swRefPtData = swFeat.GetDefinition

              2) bRet = swRefPtData.AccessSelections(swModel, MyFace)

              3) bRet = swEnt.Select4(True, Myface)


              which "MyFace" is the selected face.


                  Is there any other useful functions you might suggest to select faces with mouse?




                • Re: Selecting faces in Macro,
                  Artem Taturevych

                  You say that you are planning to select the face with mouse click (so they are already selected). If you want to reselect them later you can retrieve the pointer to IEntity via ISelectionManager::GetSelecetdObject6, store it and then use IEntity::Select4 to reselect the face at any time. if you want to keep the pointer persistent across sessions/modeling operations you may look at this discussion: How to attach Attribute to a Face



                    • Re: Selecting faces in Macro,
                      Sina Mohseni

                      Artem, Thank you for your fast response, your notes are very helpful.

                          Yes, I was thinking how can I access to faces while there is no name or anything tagged (in X_T files).

                      I should have implement these, will ask again if seeing problem.




                      • Re: Selecting faces in Macro,
                        Sina Mohseni


                        I still have problem with this simple task:

                           I draw a box, select an edge, press record, apply a fillet on it, press stop and save the macro.
                        next, I undo the fillet, click on another edge and run my macro, solidworks applies fillet on previous edge, not the one I'm selecting now.
                        What should I do about it?


                        1) I open a X_T file,
                        2) then select a face, press record button,
                        4) translate (in a way to increase volume) a face from “direct editing” tab,
                        5) press stop button and save the macro,
                        6) undo my translation, select a face, and play the macro.

                        It doesn’t work again! it applies the translation on some random faces, not the one I selec,
                        It seems my macro doesn’t understand which face/edge I’m selecting, so it could apply do its written task.

                            In fact my question is, how can I “select” faces (plane, non-plane) and edges (which none have names/attributes) by mouse click, then I can apply my functions, like translation(Part.FeatureManager.InsertMoveFace), or fillet (Part.FeatureManager.FeatureFillet3).

                        Thank you,

                          • Re: Selecting faces in Macro,
                            Artem Taturevych

                            Just remove the 'SelectById' statements from your macro so it is not trying to reselect while using the already selected objects.

                            • Re: Selecting faces in Macro,
                              Artem Taturevych

                              For example. This what I have when record the fillet. As you can see the macro clears the selection and reselecting the edges again - so that's why when you undo you have the same result.


                              Now if you delete the marked lines it will work as expected:


                              Dim swApp As Object

                              Dim Part As Object

                              Dim boolstatus As Boolean

                              Dim longstatus As Long, longwarnings As Long


                              Sub main()


                              Set swApp = _



                              Set Part = swApp.ActiveDoc

                              Dim myModelView As Object

                              Set myModelView = Part.ActiveView

                              myModelView.FrameState = swWindowState_e.swWindowMaximized

                              boolstatus = Part.Extension.SelectByID2("", "EDGE", -6.74126596481983E-03, 6.58561314139661E-02, 0.048660836410761, True, 1, Nothing, 0)

                              Part.ClearSelection2 True

                              boolstatus = Part.Extension.SelectByID2("", "EDGE", -6.74126596481983E-03, 6.58561314139661E-02, 0.048660836410761, False, 1, Nothing, 0)

                              Dim radiiArray0 As Variant

                              Dim radiis0 As Double

                              Dim dist2Array0 As Variant

                              Dim dists20 As Double

                              Dim conicRhosArray0 As Variant

                              Dim coniRhos0 As Double

                              Dim setBackArray0 As Variant

                              Dim setBacks0 As Double

                              Dim pointArray0 As Variant

                              Dim points0 As Double

                              Dim pointDist2Array0 As Variant

                              Dim pointsDist20 As Double

                              Dim pointRhoArray0 As Variant

                              Dim pointsRhos0 As Double

                              radiiArray0 = radiis0

                              dist2Array0 = dists20

                              conicRhosArray0 = coniRhos0

                              setBackArray0 = setBacks0

                              pointArray0 = points0

                              pointDist2Array0 = pointsDist20

                              pointRhoArray0 = pointsRhos0

                              Dim myFeature As Object

                              Set myFeature = Part.FeatureManager.FeatureFillet3(195, 0.00254, 0.01, 0, 0, 0, 0, (radiiArray0), (dist2Array0), (conicRhosArray0), (setBackArray0), (pointArray0), (pointDist2Array0), (pointRhoArray0))

                              End Sub

                                • Re: Selecting faces in Macro,
                                  Sina Mohseni



                                      I'm trying this, works very well on fillets, but not for face, macro does nothing when I comment that line, pretty simple but unknown to me:


                                  '////////// Sample code

                                  Sub main()

                                  Set swApp = Application.SldWorks


                                  Set Part = swApp.ActiveDoc

                                  'Dim myModelView As Object

                                  'Set myModelView = Part.ActiveView

                                  'myModelView.FrameState = swWindowState_e.swWindowMaximized

                                  'Part.ClearSelection2 True

                                  'boolstatus = Part.Extension.SelectByID2("", "FACE", -0.030497371551121, 5.07999999999811E-02, 1.13329892172374E-02, False, 1, Nothing, 0)

                                  Dim myFeature As Object

                                  Set myFeature = Part.FeatureManager.InsertMoveFace(0, False, 0, 0.0254)

                                  End Sub

                                  '/////////////End Sample code


                                  Could you take a look and suggest me a solution,


                                  Thank you,