10 Replies Latest reply on Apr 19, 2018 4:50 AM by Ivana Kolin

    How to choose a face to mate distance in an assembly?

    Shi Xiaochen

      I am doing a project, the project’s function is that operators input some parameters and then it will produce parts and mate these parts automatically. The assembly relationship is to choose plane of two parts and then set distance mate, just like the pictures below show:

       

      111.png

       

      Now I have some troubles...

      1. How to get the specific planes and mate them? I have tried the SelectionMgr, but when debugging I click the parts and it doesn't react, it seems that after I stop debugging can those parts be chosen.

      2. I use AddMate5 but it just doesn't work , here are part of my codes:

       

       

       

                  tmpObj = (ModelDoc2)swApp.OpenDoc6(ADDRESS + "part"+i+".SLDPRT", (int)

                  swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);

       

                  strCompModelname = "part" + i + ".sldprt";

                  swDoc = (ModelDoc2)swApp.ActivateDoc3("gb_assembly.asmdot", true, (int)swRebuildOnActivation_e.swUserDecision, ref errors);

       

                    swComponent = (Component2)swAssemblyDoc.AddComponent5(

                    strCompModelname, (int)

                    swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig,

                    "", false, "", -1, -1, -1);

                

                  strCompName = swComponent.Name2;

             

                  FirstSelection = "FrontPlane@" + strCompName0 + "@" + AssemblyName;

                  SecondSelection = "FrontPlane@" + strCompName + "@" + AssemblyName;

       

                  swDoc.ClearSelection2(true);

                  swDocExt = (ModelDocExtension)swDoc.Extension;

                

                  boolstat = swDocExt.SelectByID2(FirstSelection, "PLANE", 0, 0, 0, true, 1, null, (int)

                      swSelectOption_e.swSelectOptionDefault);

                  boolstat = swDocExt.SelectByID2(SecondSelection, "PLANE", 0, 0, 0, true, 1, null, (int)

                  swSelectOption_e.swSelectOptionDefault);

                 

                  matefeature = (Feature)swAssemblyDoc.AddMate5((int)swMateType_e.swMateDISTANCE, (int)

                      swMateAlign_e.swMateAlignALIGNED, true, 0.020, 0.145, 0.012, 0.001, 0.001,

                      0, 0, 0, false, false, 0, out mateError);

                   swDoc.ClearSelection2(true);

       

      How to select specific faces and mate them? I do need help!

       

      Shi

        • Re: How to choose a face to mate distance in an assembly?
          Ivana Kolin

          are you sure that name of your planes is "FrontPlane". Our template is "Front" and I have seen "Front Plane"

          • Re: How to choose a face to mate distance in an assembly?
            Deepak Gupta

            Since the codes are not full, it's hard to find the issue.

             

            What is strCompName0 or where is it defined?

             

            Change true and 1 in the below line to False, 0 and see if that helps.

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

              • Re: How to choose a face to mate distance in an assembly?
                Shi Xiaochen

                okay, here are the full codes , and as you can see, there are two parts with almost the same codes because the first part is mated with the assembly not the part in the assembly.

                 

                  public void Assembly()
                        {
                            string ADDRESS = "C:\\ProgramData\\SolidWorks\\SOLIDWORKS 2017\\templates\\";
                            string strCompName0;
                            string strCompName;
                            //PART1
                            swDoc = (ModelDoc2)swApp.OpenDoc6(ADDRESS + "gb_assembly.asmdot", (int)          swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);

                            AssemblyName = "gb_assembly.asmdot";

                        
                            swAssemblyDoc = (AssemblyDoc)swDoc;       
                           
                            tmpObj = (ModelDoc2)swApp.OpenDoc6(ADDRESS + "零件1.SLDPRT", (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent,"", ref errors, ref warnings);
                            string strCompModelname = "零件1.sldprt";

                       
                            swDoc = (ModelDoc2)swApp.ActivateDoc3("gb_assembly.asmdot", true, (int)swRebuildOnActivation_e.swUserDecision, ref errors);

                           
                            swComponent = (Component2)swAssemblyDoc.AddComponent5(strCompModelname, (int)
                              swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig,"", false, "", -1, -1, -1);

                           
                            strCompName = swComponent.Name2;

                           
                            FirstSelection = "前视基准面@" + AssemblyName;
                            SecondSelection = "前视基准面@" + strCompName + "@" + AssemblyName;

                            swDoc.ClearSelection2(true);
                            swDocExt = (ModelDocExtension)swDoc.Extension;

                       
                            boolstat = swDocExt.SelectByID2(FirstSelection, "PLANE", 0, 0, 0, false, 0, null, 0);
                            boolstat = swDocExt.SelectByID2(SecondSelection, "PLANE", 0, 0, 0, false, 0, null, 0);

                         
                            matefeature = (Feature)swAssemblyDoc.AddMate5((int)swMateType_e.swMateCOINCIDENT, (int)swMateAlign_e.swMateAlignALIGNED, true, 0, 0, 0, 0, 0, 0, 0, 0, false, false, 0, out mateError);   
                            swDoc.ClearSelection2(true);
                            swDoc.EditRebuild3();

                           //PART2

                            for (int i = 2; i <= n; i++)
                            {
                                strCompName0 = strCompName;

                     
                            tmpObj = (ModelDoc2)swApp.OpenDoc6(ADDRESS + "零件"+i+".SLDPRT", (int)swDocumentTypes_e.swDocPART, (int)
                            swOpenDocOptions_e.swOpenDocOptions_Silent,"", ref errors, ref warnings);

                            strCompModelname = "零件" + i + ".sldprt";
                     
                            swDoc = (ModelDoc2)swApp.ActivateDoc3("gb_assembly.asmdot", true, (int)
                           swRebuildOnActivation_e.swUserDecision, ref errors);

                           
                            swComponent = (Component2)swAssemblyDoc.AddComponent5(strCompModelname, (int)        swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig,"", false, "", -1, -1, -1);

                         
                            strCompName = swComponent.Name2;

                           
                            FirstSelection = "前视基准面@" + strCompName0 + "@" + AssemblyName;
                            SecondSelection = "前视基准面@" + strCompName + "@" + AssemblyName;

                            swDoc.ClearSelection2(true);
                            swDocExt = (ModelDocExtension)swDoc.Extension;

                            // choose the mate plane
                            boolstat = swDocExt.SelectByID2(FirstSelection, "PLANE", 0, 0, 0, true, 0, null, (int)
                                swSelectOption_e.swSelectOptionDefault);
                            boolstat = swDocExt.SelectByID2(SecondSelection, "PLANE", 0, 0, 0, true, 0, null, (int)
                            swSelectOption_e.swSelectOptionDefault);

                            // add mate
                            matefeature = (Feature)swAssemblyDoc.AddMate5((int)swMateType_e.swMateDISTANCE, (int)
                                swMateAlign_e.swMateAlignALIGNED, true, 0.020, 0.145, 0.012, 0.001, 0.001,0, 0, 0, false, false, 0, out mateError);

                            swDoc.ClearSelection2(true);
                            swDoc.EditRebuild3();

                            }

                  • Re: How to choose a face to mate distance in an assembly?
                    Ivana Kolin

                    public void Assembly()
                            {
                                string ADDRESS = "C:\\ProgramData\\SolidWorks\\SOLIDWORKS 2017\\templates\\";
                                string strCompName0;
                                string strCompName;
                                //PART1

                    //  I would create new assembly doc and not edit template

                    // 2017 SOLIDWORKS API Help - NewDocument Method (ISldWorks)


                                swDoc = (ModelDoc2)swApp.OpenDoc6(ADDRESS + "gb_assembly.asmdot", (int)          swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);

                                AssemblyName = swDoc.GetTitle() ;

                           
                                swAssemblyDoc ="gb_assembly.asmdot";       
                               
                                tmpObj = (ModelDoc2)swApp.OpenDoc6(ADDRESS + "零件1.SLDPRT", (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent,"", ref errors, ref warnings);
                                string strCompModelname = "零件1.sldprt";

                           
                               swDoc = (ModelDoc2)swApp.ActivateDoc3(swAssemblyDoc , true, (int)swRebuildOnActivation_e.swUserDecision, ref errors);

                              

                              // isn't first component fixed automatically? 2017 SOLIDWORKS API Help - UnfixComponent Method (IAssemblyDoc)
                                swComponent = (Component2)swAssemblyDoc.AddComponent5(strCompModelname, (int)


                                  swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig,"", false, "", -1, -1, -1);

                               
                                strCompName = swComponent.Name2;

                               
                                FirstSelection = "前视基准面@" + AssemblyName;
                                SecondSelection = "前视基准面@" + strCompName + "@" + AssemblyName;

                                swDoc.ClearSelection2(true);
                                swDocExt = (ModelDocExtension)swDoc.Extension;

                           
                                boolstat = swDocExt.SelectByID2(FirstSelection, "PLANE", 0, 0, 0, false, 0, null, 0);
                                boolstat = swDocExt.SelectByID2(SecondSelection, "PLANE", 0, 0, 0, true, 0, null, 0);

                             
                                matefeature = (Feature)swAssemblyDoc.AddMate5((int)swMateType_e.swMateCOINCIDENT, (int)swMateAlign_e.swMateAlignALIGNED, true, 0, 0, 0, 0, 0, 0, 0, 0, false, false, 0, out mateError);   
                                swDoc.ClearSelection2(true);
                                swDoc.EditRebuild3();

                              //PART2

                                for (int i = 2; i <= n; i++)
                                {
                                    strCompName0 = strCompName;

                         
                                tmpObj = (ModelDoc2)swApp.OpenDoc6(ADDRESS + "零件"+i+".SLDPRT", (int)swDocumentTypes_e.swDocPART, (int)
                                swOpenDocOptions_e.swOpenDocOptions_Silent,"", ref errors, ref warnings);

                                strCompModelname = "零件" + i + ".sldprt";
                         
                                swDoc = (ModelDoc2)swApp.ActivateDoc3(swAssemblyDoc , true, (int)
                              swRebuildOnActivation_e.swUserDecision, ref errors);

                               
                                swComponent = (Component2)swAssemblyDoc.AddComponent5(strCompModelname, (int)        swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig,"", false, "", -1, -1, -1);

                             
                                strCompName = swComponent.Name2;

                               
                                FirstSelection = "前视基准面@" + strCompName0 + "@" + AssemblyName;
                                SecondSelection = "前视基准面@" + strCompName + "@" + AssemblyName;

                                swDoc.ClearSelection2(true);
                                swDocExt = (ModelDocExtension)swDoc.Extension;

                                // choose the mate plane
                                boolstat = swDocExt.SelectByID2(FirstSelection, "PLANE", 0, 0, 0, false, 0, null, (int)
                                    swSelectOption_e.swSelectOptionDefault);
                                boolstat = swDocExt.SelectByID2(SecondSelection, "PLANE", 0, 0, 0, true, 0, null, (int)
                                swSelectOption_e.swSelectOptionDefault);

                                // add mate
                                matefeature = (Feature)swAssemblyDoc.AddMate5((int)swMateType_e.swMateDISTANCE, (int)
                                    swMateAlign_e.swMateAlignALIGNED, true, 0.020, 0.145, 0.012, 0.001, 0.001,0, 0, 0, false, false, 0, out mateError);

                                swDoc.ClearSelection2(true);
                                swDoc.EditRebuild3();

                                }

                      • Re: How to choose a face to mate distance in an assembly?
                        Shi Xiaochen

                        Hi Ivana,

                        Thanks for your reply. I have tried your code but it doesn't work. I will try to  change other parameters and if I work it out I will tell you.

                        Difficult to find out bugs, sad

                          • Re: How to choose a face to mate distance in an assembly?
                            Ivana Kolin

                            can you check before AddMate5 call how many object are selected? 2017 SOLIDWORKS API Help - GetSelectedObjectCount2 Method (ISelectionMgr)

                            you can also try add this mate with by code preselected objects in SW - perhaps you can see an error

                             

                            and this was not complete correction FirstSelection = "前视基准面@" + AssemblyName@ must be also removed

                              • Re: How to choose a face to mate distance in an assembly?
                                Shi Xiaochen

                                Hi Ivana,

                                Amazing! I add the GetSelectedObjectCount2 Method as you told and the immediate window shows that the number of selected objects is 0!!! How come?

                                BTW, I had already removed the "@ + AssemblyName" code.

                                Here are my new codes:

                                           ......

                                            // choose the mating plane
                                            boolstat = swDocExt.SelectByID2(FirstSelection, "PLANE", 0, 0, 0, false, 0, null, 0);
                                            boolstat = swDocExt.SelectByID2(SecondSelection, "PLANE", 0, 0, 0, true, 0, null, 0);
                                           
                                            //count the selected objects
                                            swSelMgr = (SelectionMgr)swDoc.SelectionManager;
                                            int selCount = 0;
                                            selCount = swSelMgr.GetSelectedObjectCount2(-1);
                                            Debug.Print("The count of selected object:"+selCount);

                                 

                                            // add mate
                                            matefeature = (Feature)swAssemblyDoc.AddMate5((int)swMateType_e.swMateDISTANCE, (int)
                                                swMateAlign_e.swMateAlignALIGNED, false, 0.020, 0.145, 0.012, 0.001, 0.001,
                                                0, 0, 0, false, false, 0, out mateError);

                                            swDoc.ClearSelection2(true);