10 Replies Latest reply on Mar 27, 2013 8:37 PM by Wang Zhiwie

    How to get the name of Mate Entity by a set of Entity Parameters?

    Bomiao Li

      Hi there,

       

      I am trying to extract mate information from an open assembly. I referred to this example from SolidWorks API help: Get Mate and Mate Entities Example (VBA). For each mate under "Mates" feature in Feature Manage Design Tree, by using IMateEntity2.GetEntityParams() method, I get a set of parameters of each mate entity.

       

      But here is a problem that I couldn't overcome. It's about how to get the name of mate entities associated in a mate by their parameters. I've done some searching on internet but with no progress. Does anyone have  idea?

       

       

      //------------------------------Code--------------------------------------------------------------------//

       

      private void ExtractMateInformation_Click(object sender, EventArgs e)
              {
                  SldWorks.SldWorks swApp = new SldWorks.SldWorks();
                  ModelDoc2 swModel;
                  Feature swFeat;
                  Face2 swFace;
                  Feature swMateFeat=null;
                  Feature swSubFeat;
                  PathTitleName Sub = new PathTitleName();
                  Mate2 swMate;
                  MateEntity2 swMateEntity_1;
                  MateEntity2 swMateEntity_2;
                           
                  string MateFeatName = null;
                  swModel = (ModelDoc2)swApp.ActiveDoc;
                  swFeat = (Feature)swModel.FirstFeature();
      
                  //  Iterate over features in FeatureManager design tree
                  while (!(swFeat == null))
                  {
                      if ("MateGroup" == swFeat.GetTypeName())
                      {
                          swMateFeat = (Feature)swFeat;
                          break;
                      }
                      swFeat = (Feature)swFeat.GetNextFeature();
                  }
                  MateFeatName = swMateFeat.Name;
      
                  //  Get first mate, which is a subfeature
                  swSubFeat = (Feature)swMateFeat.GetFirstSubFeature();
                  while (!(swSubFeat == null))
                  {                
                      swMate = (Mate2)swSubFeat.GetSpecificFeature2();
                      if (!(swMate == null))
                      {
      
                          string MateName = swSubFeat.Name;
                          string Component1 = swMate.MateEntity(0).ReferenceComponent.Name2;
                          string Component2 = swMate.MateEntity(1).ReferenceComponent.Name2;
                          int MateType = swMate.Type;
                          int MateAlignment = swMate.Alignment;
                          bool CanBeFlipped = Flipped;
                          int Entity1Type =swMate.MateEntity(0).ReferenceType;
                          int Entity2Type =swMate.MateEntity(1).ReferenceType;
                          
                          //get two mate entities associated in this mate
                          swMateEntity_1 = (MateEntity2)swMate.MateEntity(0);
                          swMateEntity_2 = (MateEntity2)swMate.MateEntity(1);
                          
                          //get a set of entity parameters                    
                          double[] EntityParams_1 = swMateEntity_1.EntityParams;
                          double[] EntityParams_2 = swMateEntity_2.EntityParams;
                          
                      }
      
                      //  Get the next mate in MateGroup
                      swSubFeat = (Feature)swSubFeat.GetNextSubFeature();
                  }            
      
              }
      

       

       

      Cheers,

      Bomiao

        • Re: How to get the name of Mate Entity by a set of Entity Parameters?
          Artem Taturevych

          Try one of the following sequences.

           

          MateEntity2::Reference->MateReference::Name.

           

          MateEntity2::Reference->MateReference::ReferenceEntity2->ModelDoc2::GetEntityName.

           

          ______________________

          Artem Taturevych

          Application Engineer at Intercad

          http://intercad.com.au/

          Tel: +61 2 9454 4444

            • Re: How to get the name of Mate Entity by a set of Entity Parameters?
              Bomiao Li

              Hi Artem,

               

              Thanks for your help. It helps me out!~

               

              I have another problem now. If the name of a mate entity exists, I can get its name by ModelDoc2::GetEntityName; But, if there is no name for this mate entity, how to add a name for this entity? Here is my assumptions:

               

              1. use IPart::SetEntityName to name this entity, then use ModelDoc2::GetEntityName to get its name. this way need to interface IPart, which I don't know how....

               

              2. insert a geometry reference for this entity, name it, then select them for AddMate3()

               

              Please let me know if you have any idea about this.

               

              Cheers,

              Bomiao

                • Re: How to get the name of Mate Entity by a set of Entity Parameters?
                  Artem Taturevych

                  Hi Bomiao,

                   

                  What about your first question. You should find the component of this entity and its part:

                  Entity::GetComponent->Component2::GetModelDoc2. which would be IPartDoc. However please note that this name is the part level name. So if you have multiple instances of the same component you will be unable to find exact instance.

                   

                  I'm not sure what are you looking for in second question.

                   

                  Can you explain your general goal? I will be probably able to suggest you some other solution.

                  ______________________

                  Regards,

                  Artem Taturevych

                  Application Engineer at Intercad

                  http://intercad.com.au/

                  Tel: +61 2 9454 4444

                    • Re: How to get the name of Mate Entity by a set of Entity Parameters?
                      Bomiao Li

                      Hi Artem,

                       

                      I am new to this. My intension is as follows:

                       

                      1. extract mate information of an existing assembly, set mate entity names;

                      2. then, make changes of the associate parts;

                      3. finally, assemble these parts together to form a new assembly with the same mates as extracted in Step.1.

                       

                      I tried your method and successfully interface IPartDoc object and set a name for mate entity. It works. But when I tried to use SelectByID2() method, it fails. For example, here is a mate named "Coincident1", and its two mate entity are named "Coincident1" as well. The two component are named "Comp1-1" and "Comp2-1". I use the following code to select two entity for AddMate3.

                       

                      =======================================code===================================================

                      string FirstSelection = "Coincident1@Comp1-1 @ AssemblyName";

                      string SecondSelection = "Coincident1@Comp2-1@ AssemblyName";

                      swDocExt = (ModelDocExtension)swModel.Extension;

                      swModel.ClearSelection2(true);

                       

                      boolstatus = swDocExt.SelectByID2(FirstSelection, "FACE", 0, 0, 0, true, 1, null, (int)swSelectOption_e.swSelectOptionDefault);

                      boolstatus = swDocExt.SelectByID2(SecondSelection, "FACE", 0, 0, 0, true, 1, null, (int)swSelectOption_e.swSelectOptionDefault);

                      =====================================code end===============================================

                       

                      But the boolstatus is "False" which means the entities are not selected. I don't know why. Any idea?

                       

                      Cheers,

                      Bomiao

                        • Re: How to get the name of Mate Entity by a set of Entity Parameters?
                          Artem Taturevych

                          If I'm not mistaken you cannot use this name in SelectById2. You need to retrieve the pointer directly PartDoc::GetEntityByName and select the face via Entity::Select4. Please also refer to "Persistent Reference IDs" section in API Help. This may be solution for your purposes. Use ids instead of names for tracking.

                          ______________________

                          Regards,

                          Artem Taturevych

                          Application Engineer at Intercad

                          http://intercad.com.au/

                          Tel: +61 2 9454 4444

                            • Re: How to get the name of Mate Entity by a set of Entity Parameters?
                              Bomiao Li

                              Hi Artem,

                               

                              Yes, you are right. The Persistent Reference ID is a better way to do this. I've been trying to solve this by using Persistent IDs during the past two days. Now, object (Persistent IDs) can be obtained by IModelDocExtension::GetPersistReference3(). As there are more than one mates in assembly, so I repeatly use this method to get a list of objects (Persistent IDs). I also defined a "object[] swObj" to store them.

                               

                                          object[] PIDs = new object[100];

                                          int i = 0;

                                          while (!(swSubFeat == null))

                                          {

                                              swMate = (Mate2)swSubFeat.GetSpecificFeature2();               

                                              if (!(swMate == null))

                                              {                  

                                                  swMateEntity_1 = (MateEntity2)swMate.MateEntity(0);   //get entity_1

                                                  swEntity_1 = (Entity)swMateEntity_1.Reference;

                                                  PIDs[i] = swModelDocExt.GetPersistReference3(swEntity_1);

                               

                                                  swMateEntity_2 = (MateEntity2)swMate.MateEntity(1);   //get entity_2

                                                  swEntity_2 = (Entity)swMateEntity_2.Reference;

                                                  PIDs[i+1] = swModelDocExt.GetPersistReference3(swEntity_2);                  

                                              }

                               

                                              //  Get the next mate in MateGroup

                                              swSubFeat = (Feature)swSubFeat.GetNextSubFeature();

                                              i = i + 2;

                                          }

                               

                              Then IModelDocExtension::GetObjectByPersistReference3() is used to retrieve these mate entities for mating. This method also returns "object".

                               

                                          int errorCode;

                                          object swObj = swModelDocExt.GetObjectByPersistReference3(PIDs[0], out errorCode);

                               

                              If we want to use them for AssemblyDoc::AddMate3(), we need to select two entities at a same time. But I have no idea about select entity from obtained object.  Can this be used to replace IModelDocExtension::SelectByID4() method?

                                • Re: How to get the name of Mate Entity by a set of Entity Parameters?
                                  Artem Taturevych

                                  Hi Bomiao,


                                  You should cast it to IEntity and use Entity::Select4 or ModelDocExtension::MultiSelect2 to select any object. One important note. I think you approach may fail.

                                   

                                  Let's consider the following situation. You extracted the mate references and get the id. At this point you get the id of the entity in assembly space. For example you have two instances of the same component. So this entity will have different ids for these components. You stored this reference and created new assembly where inserted two instances of this component. Now when you call the select method, which face will be selected? Answer - it should fail.

                                   

                                  In order to properly organize this you should do the following:

                                   

                                  1) Get the entity from mate reference in assembly space like you are doing now.

                                  2) Convert this to part space (ModelDocExtension::GetCorresponding) so now this is the pointer to the entity in part space.

                                  3) Get the persistent reference

                                  4) Create new assembly and insert instance of the component.

                                  5) Convert the reference to the entity to new assembly space using the inserted component interface Component2::GetCorresponding.

                                  6) Use Entity::Select4 to select this entity in new context.

                                   

                                  I know this may be a bit complicated to understand for now because this is not fundamentals but advanced API I think... Please refer the forum threads/API Help where the context is clearly described or post questions. Just know that there may be two contexts of the same object in SolidWorks (context of the owner document and context of the assembly) where the same object has different pointers.

                                   

                                  ______________________

                                  Regards,

                                  Artem Taturevych

                                  Application Engineer at Intercad

                                  http://intercad.com.au/

                                  Tel: +61 2 9454 4444

                                    • Re: How to get the name of Mate Entity by a set of Entity Parameters?
                                      Bomiao Li

                                      Hi Artem,

                                       

                                      Thanks for your advices. I followed the steps in your last post.

                                       

                                      1) get the entity from mate reference in assembly space:

                                                          swMateEntity_1 = (MateEntity2)swMate.MateEntity(0);   //get entity_1

                                                          swEntity_1 = (Entity)swMateEntity_1.Reference;

                                       

                                      2) Convert this to part space:

                                                          Entity entity_1=(Entity)swModelDocExt.GetCorresponding(swEntity_1);

                                       

                                      3) Get the persistent reference:                  

                                                          PIDs[i] = swModelDocExt.GetPersistReference3(entity_1);

                                       

                                      4) Create new assembly and insert instance of the component:

                                                         swcomponent = (Component2)swAssembly.AddComponent5(comp_name,

                                                                                  (int)swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig, "", true,

                                                                                   "", 0, 0, 0);

                                      in this step, I added more than one components by doing AssemblyDoc.AddComponet5() method repeatly.

                                       

                                      5) Convert the reference to the entity to new assembly space using the inserted component interface Component2::GetCorresponding.

                                      6)  Use Entity::Select4 to select this entity in new context.  

                                          for these two step, how to get the inserted component objects? can you give me some code for interface Component2::GetCorresponding() and the restore pointer to entity in assembly context by Persistent IDs?  

                                       

                                      Cheers,

                                      Bomiao

                            • Re: How to get the name of Mate Entity by a set of Entity Parameters?
                              Wang Zhiwie

                              I'm just a beginner of solidworks,I met the same question .I know you have solved the problem that how to get the name of Mate Entity by a set of Entity Parameters.Can you help me to solve this problem?

                              Thank you for advanced.