8 Replies Latest reply on Sep 5, 2017 5:11 PM by Nilesh Patel

    Select an Edge with SelectByID2

    Nadav Gover


      Is there anyway of using SelectByID2 to select an Edge?

      I need this in order to use InsertFillSurface2 (2013 SOLIDWORKS API Help - InsertFillSurface2 Method (IFeatureManager) ) which takes as an argument an array of Edges which must be selected with SelectByID2 as it states in the remarks.


      Or is there a way around it?

      Casting the Edge to an Entity an then use Select4 didn't do the work for me :/



        • Re: Select an Edge with SelectByID2
          Simon Turner

          When you used Entity.Select4, did you use a SelectData object to set the Mark?

            • Re: Select an Edge with SelectByID2
              Nadav Gover

              Hi Simon,

              I currently have this (shortened, C#)

              SelectData swSelData = swSelMgr.CreateSelectData();

              swSelData.Mark = 257;



              Entity swEntity = (Entity)swEdge;

              status = swEntity.Select4(true, swSelData);


              And the Select4 returns false for some reason thus the status = false.

              Afterwards I try to call the InsertFillSurface2 method with swSelData as an argument for the boundries, obviously not working because the selection was unsuccessful.

              Any idea?



                • Re: Select an Edge with SelectByID2
                  Nilesh Patel

                  Hi Nadav,


                  A specified in API help, edges MUST be selected using IModelDocExtension::SelectByID2 method. I don't think selecting edges using IEntity::Select4 method will work. Could you attach your part, so someone can have a look?



                    • Re: Select an Edge with SelectByID2
                      Simon Turner

                      I think it should work with Select4, because the example it gives in the reference page for InsertFillSurface2 shows exactly that.

                      Look at "Get and Fill Gaps in Body Example (VBA)".

                      • Re: Select an Edge with SelectByID2
                        Nadav Gover

                        The part is Attached now.

                        I agree with what Simon said, also in my case I need to select more than 1 Edge for the boundary.

                        I thought of making a new sketch with converting the entities but this probably will take much longer.

                        As this is only a minor sub routine in my whole macro I would want to avoid time consuming procedures.


                        By the way, the only reason I want to do that (select edges) is to find which contour is the most outer contour, by filling all contours and than check which has the greatest area.

                        So if you guys have any other idea of how to achieve this, it will be great.



                          • Re: Select an Edge with SelectByID2
                            Simon Turner

                            This VBA code selects the outer loop of any selected face:


                            Dim swApp As SldWorks.SldWorks

                            Dim Part As ModelDoc2


                            Sub main()

                                Dim selMgr As SelectionMgr

                                Dim selData As SelectData

                                Dim myFace As Face2

                                Dim myLoop As Loop2


                                Set swApp = Application.SldWorks

                                Set Part = swApp.ActiveDoc

                                If Part Is Nothing Then Exit Sub

                                Set selMgr = Part.SelectionManager

                                If selMgr.GetSelectedObjectCount2(-1) <> 1 Then Exit Sub

                                If selMgr.GetSelectedObjectType3(1, -1) <> swSelFACES Then Exit Sub

                                Set selData = selMgr.CreateSelectData

                                Set myFace = selMgr.GetSelectedObject6(1, -1)

                                Set myLoop = myFace.GetFirstLoop

                                While Not myLoop Is Nothing

                                    If myLoop.IsOuter Then ProcessLoop myLoop

                                    Set myLoop = myLoop.GetNext



                            End Sub


                            Sub ProcessLoop(myLoop As Loop2)

                                Dim myCoEdges As Variant

                                Dim myCoEdge As CoEdge

                                Dim myEdge As Edge

                                Dim myEnt As Entity

                                Dim i As Integer


                                myCoEdges = myLoop.GetCoEdges

                                If IsEmpty(myCoEdges) Then Exit Sub


                                Part.ClearSelection2 True

                                For i = 0 To UBound(myCoEdges)

                                    Set myCoEdge = myCoEdges(i)

                                    Set myEdge = myCoEdge.GetEdge

                                    Set myEnt = myEdge

                                    myEnt.Select4 True, Nothing



                            End Sub

                            • Re: Select an Edge with SelectByID2
                              Nilesh Patel

                              Hi Nadav,


                              I am still on SW2015 and can't open your part. But I will play with both options.