6 Replies Latest reply on May 16, 2016 3:11 PM by Josh Brady

    SelectByID2 get component name

    Vlad Shevchenko

      Hi everyone!

       

      I'm trying to get component name for method SelectByID2 from assembly. I have simple assembly with one mate, and I want select those components which were selected in this mate.

      Here is my code:

      var aseembly = iSwApp.ActiveDoc as ModelDoc2;
      
      
                  Feature swFeat = aseembly.FirstFeature();
                  Feature mates = null;
      
      
                  while (swFeat != null)
                  {
                      if (swFeat.GetTypeName2() == "MateGroup")
                      {
                          mates = swFeat;
                          break;
                      }
      
      
                      swFeat = swFeat.GetNextFeature();
                  }
      
      
                  Feature swMateFeature = mates.GetFirstSubFeature();
                  var swMate = swMateFeature.GetSpecificFeature2() as Mate2;
                  for (int i = 0; i < swMate.GetMateEntityCount(); i++)
                  {
                      var entity = swMate.MateEntity(i);
      
      
                      var comp = entity.ReferenceComponent as Component2;
                      var param = entity.EntityParams as double[];
                      var selected = aseembly.Extension.SelectByID2("Boss-Extrude1@"+comp.GetSelectByIDString(), "", param[0], param[1], param[2], true, 1, null, (int)swSelectOption_e.swSelectOptionDefault);
                  }
      

       

      This code selects wrong points (it should be point and edge). SelectByID2 always returns false, which means that name parameter is incorrect. Anybody know how I can get full name of component programmatically?

        • Re: SelectByID2 get component name
          Vlad Shevchenko

          It's looks like first three values of IMateEntity2.EntityParams doesn't keep information about point which was selected for mate creation. How can I get access for this information?

            • Re: SelectByID2 get component name
              Steven Barry

              Vlad,

              Here's a couple of things you could try:

              - Verify that you actually select the "MateGroup" feature from the while loop.  I imagine it would throw an error on the next line if it didn't, but it doesn't hurt to check

              - In your SelectByID2, try replacing your "mark" parameter of 1 with "i" or "i+1".  Honestly not sure how this parameter is used, but it may need to be a unique value for each selected item?

              - In your SelectByID2, extend your "name" parameter by adding something like "@Assem1" or whatever suits your particular assembly.  You could also probably make this dynamic if you want by pulling the name from your existing variables.  The reason I am thinking this might be the culprit is the "EntityParams" method returns an X, Y, Z that are in the assembly space.

               

              Good luck!

              -Steven

                • Re: SelectByID2 get component name
                  Vlad Shevchenko

                  Thank you for your answer. I think that my problem related not with "name" parameter but with EntityParams. First three values are coordinates of "mate entity" not of point which was selected. I'm interesting how I can get this original point to select the same part of body which was selected in that mate. I afraid that SolidWorks doesn't keep this information.

                  Please look at the picture.

                   

                  mate.png

              • Re: SelectByID2 get component name
                Josh Brady

                Hi, Vlad,

                 

                Basically, you should almost never use SelectByID.

                 

                It sounds to me like your final goal is to select the entities used for the mate.

                 

                Please study the code below.  Sorry, it's VBA rather than C.

                 

                Preconditions:  User has selected a mate

                Postconditions: The mate is un-selected and the entities used for the mate are selected.

                 

                Dim swApp As SldWorks.SldWorks
                Dim tmpFeat As SldWorks.Feature
                Dim swSelMgr As SldWorks.SelectionMgr
                Dim tmpMent As SldWorks.MateEntity2
                Dim i As Long
                
                Sub main()
                
                    Set swApp = Application.SldWorks
                    Set swSelMgr = swApp.ActiveDoc.SelectionManager
                    Set tmpFeat = swSelMgr.GetSelectedObject6(1, -1)
                    Set swMate = tmpFeat.GetSpecificFeature2
                    Set tmpFeat = Nothing
                    swApp.ActiveDoc.ClearSelection2 True
                
                    For i = 0 To swMate.GetMateEntityCount - 1
                        Set tmpMent = swMate.MateEntity(i)
                        tmpMent.Reference.Select2 True, 1
                    Next i
                End Sub
                
                
                
                
                
                
                  • Re: SelectByID2 get component name
                    Vlad Shevchenko

                    Thank you. Your code works fine. But I need to select the same entity on another document and this component may has another name. But I know that geometry location on that document will be the same as in the original document with mates. So if I get this point I will be able to select the same entity on other document.

                      • Re: SelectByID2 get component name
                        Josh Brady

                        So, just to make sure I understand, you are saying that:

                         

                        Comp1.sldprt and Comp2.sldprt are in an assembly.  There is a mate between faces of these components.

                         

                        Comp3.sldprt is a different file, but is "similar" geometry to Comp2.sldprt.  You want to select a face on Comp3 that is similar to a face on Comp2.

                         

                        Is this correct?

                         

                        This is certainly tricky.  As you have found, SolidWorks doesn't remember the point selected when mates are created. 

                         

                        It sounds like you are always looking for a face? One approach that may work to find some point close to the center of the face:

                         

                        1. Get the face

                        2. Get the bounding box for the face.

                        3. Get the midpoint of the bounding box

                        3. Use GetClosestPointOn with the midpoint coordinates to get a point that is actually on the face.