3 Replies Latest reply on Mar 8, 2014 3:18 AM by Brant Williams

    Selecting Named Faces, etc

    Brant Williams

      I am playing around with an AddIn, just trying to learn the funcationality behind selecting entities that have been named.  Under the UI Callbacks region of my AddIn, I created a new method called CheckName.

       

      Pre-Conditions to using this method, which is called from my AddIn Menu

      1.  There is an open part document / model

      2.  I have named a face

      3.  I select that named face before I execute the CheckName method

       

      Here is the method.  Basically, it starts with a face I have selected.  The face I selected, has been named.  It is the only face in the model that has been named.  I retrieve the name and save to a string.  I then clear the selection list.  Then, using the name save to the string, I get the named entity, add it back to the selection list (which I cleared).  Along the way I throw in a few MSGBox statements to confirm thngs are working. 

       

      public void CheckName()

              {           

                  IModelDoc2 iModDoc = (IModelDoc2)iSwApp.ActiveDoc;//Get our active document

                  iSwSelect = (ISelectionMgr)iModDoc.SelectionManager;//Gets the Selection Manager for the active doc

                  IPartDoc iPart = (IPartDoc)iModDoc;//Get the more specific Part object as well.  This is needed to the GetEntityByName method

       

                  if (iSwSelect.GetSelectedObjectCount2(-1)==1)//If we have ONE and only one component (face, edge, vertex, etc) selected....

                  {

                      object entity = (object)iSwSelect.GetSelectedObject6(1,-1);//Gets reference variable to the selected face/edge, etc.  We do not know type, so we cast to object

                      System.Windows.Forms.MessageBox.Show("Selected Entity Name is " +iModDoc.GetEntityName(entity) + "." );//MSGBox to confirm...

                      string retName = iModDoc.GetEntityName(entity);//Save the retrieved name to a string

                      iModDoc.ClearSelection2(true);//Clear the selection list

                      IEntity iSwEnt = (IEntity)iPart.GetEntityByName(retName, 2);//Get the named entity/face. When we NAMED the entity, we noted the type = 2

                      SelectData iSelData; //HERE IS MY Question....Why does Select Data work...but ISelect does not?

                      iSelData= iSwSelect.CreateSelectData();//Create the select data object for our named face.

                      bool newSelect = iSwEnt.Select4(true, iSelData);//Now, finally, select the named face.  We are right back where we started, with the named face selected.

                     

                      if (newSelect)

                      {

                          entity = (object)iSwSelect.GetSelectedObject6(1, -1);//Gets reference variable to the selected object

                          System.Windows.Forms.MessageBox.Show("Selected Entity Name is " + iModDoc.GetEntityName(entity) + ".");//Check objects name   

                      }

                      else System.Windows.Forms.MessageBox.Show("Entity was not successfully selected by ID");

                  }

                  else System.Windows.Forms.MessageBox.Show("Please select only one face." );

              }

       

      Here is my question.  This code work perfect with

      SelectData iSelData;

       

      Originally, I wrote it with the ISelect interface as follows:

      ISelectData iSelData;

       

      The former works...ISelectData does not!  Why is this.  Documentation on the help only shows the Interface objects, the the class objects...I can not figure out whey SelectData works...but ISelectData will not. 

       

      Thanks!

        • Re: Selecting Named Faces, etc
          Brant Williams

          Here...this should be easier to read...

           

          public void CheckName()

                  {          

                      IModelDoc2 iModDoc = (IModelDoc2)iSwApp.ActiveDoc;

                      iSwSelect = (ISelectionMgr)iModDoc.SelectionManager;

                      IPartDoc iPart = (IPartDoc)iModDoc;

           

                      if (iSwSelect.GetSelectedObjectCount2(-1)==1)

                      {

                          object entity = (object)iSwSelect.GetSelectedObject6(1,-1);

                          string retName = iModDoc.GetEntityName(entity);

                          System.Windows.Forms.MessageBox.Show("Name is "+iModDoc.GetEntityName(entity)+"." );

                          iModDoc.ClearSelection2(true);

                          IEntity iSwEnt = (IEntity)iPart.GetEntityByName(retName, 2);

                          SelectData iSelData;//This is my question.  Why won't ISelectData work?

                          iSelData= iSwSelect.CreateSelectData();

                          bool newSelect = iSwEnt.Select4(true, iSelData);

           

                          if (newSelect)

                          {

                              entity = (object)iSwSelect.GetSelectedObject6(1, -1);

                              System.Windows.Forms.MessageBox.Show("Name is "+iModDoc.GetEntityName(entity)+"."); 

                          }

                          else System.Windows.Forms.MessageBox.Show("Not successfully selected by ID");

                      }

                      else System.Windows.Forms.MessageBox.Show("Please select only one face." );

           

                  }

            • Re: Selecting Named Faces, etc
              Simon Turner

              From the API Help:

               

              In-process methods typically begin with the letter I and get or set pointers to arrays that only unmanaged C++ applications can handle

                • Re: Selecting Named Faces, etc
                  Brant Williams

                  I am not talking about the method.  I am talking about the type (interface).

                   

                  Declaring the type as ISelectData, like this, fails:

                  ISelectData iSelData;

                  iSelData= iSwSelect.CreateSelectData();

                   

                  But this works...

                  SelectData iSelData;

                  iSelData= iSwSelect.CreateSelectData();

                   

                   

                   

                  From the API Help

                   

                  ISelectionMgr Interface Members

                   MethodCreateSelectDataCreates a ISelectData object to use as argument with Select methods. 

                   

                  Since the ISelectionMgr documentation notes that the CreateSelectData() method creates an ISelectData object for use with Selection methods,...declared iSelData as an ISelectData object.  But that does not work.  The ISelectData can NOT be passed as an argument with to Select4().  It needs to be a SelectData object.

                   

                  The following error occurs...

                   

                  Argument 2: cannot convert from 'SolidWorks.Interop.sldworks.ISelectData' to 'SolidWorks.Interop.sldworks.SelectData'. This relates to the IEntity Select4 method.  The Select4 method will not take an ISelectData object...but it will take a SelectData object.

                   

                  I seems that the Select4 method should overloaded to take either a SelectData object...or and ISelectData object.  Either that...or they should fix the documentation to make is clear that when you use the  ISelectionManager::CreateSelectData method, you should declare the instanced object as a SelectData object...not an ISelectData object.

                   

                  Either that...or I am missing something obvious.