1 Reply Latest reply on Apr 9, 2014 3:44 PM by Jesse Seger

    Question about adding mates using a Macro

    Drew Adams

      Hi Everyone,

      I'm still pretty new to the API, so pardon me if this is a dumb question, but I have been working on a macro to quickly add some common mates between components in an assembly, and the assembly's top plane.  Basically placing components "on the floor" for layouts and the like. 

      Each time I run the macro, it creates the mates again, redundantly......and the redundant mates do not cause any errors/warnings.  I was wondering if this is common behavior?  Is there a way to work around this?  Like a chunk of code to use that detects if a mate already exists between the two selections (I'm using SelectbyID2) and that could then tell SolidWorks to not create the mate again?


      Trying to find out the best way to do this without re-inventing the wheel.





        • Re: Question about adding mates using a Macro
          Jesse Seger

          I think the best way is to name you mates a specific name.  Then you can check to see if that mate already exists.  Here is a macro I created in C# that shows that method. 





            public partial class SolidWorksMacro    {
                  public void Main()
                      IModelDoc2 swDoc = (IModelDoc2)swApp.ActiveDoc;
                      IModelDocExtension swDocExt = (IModelDocExtension)swDoc.Extension;
                      IAssemblyDoc swAssy = (IAssemblyDoc)swDoc;
                      string assyName = swDoc.GetTitle().Split('.')[0];
                      // loop through the components and mate the top planes            // to the assembly top plane            // we'll also check for existing mates            foreach (Object o in (Object[])swAssy.GetComponents(true))
                          IComponent2 comp = o as IComponent2;
                          IModelDoc2 compDoc = (IModelDoc2)comp.GetModelDoc2();
                          string firstSelection = "Top@" + assyName;
                          string secondSelection = "Top@" + comp.Name2 + "@" + assyName;
                          swDocExt.SelectByID(firstSelection, "PLANE", 0, 0, 0, true, 1, null);
                          swDocExt.SelectByID(secondSelection, "PLANE", 0, 0, 0, true, 1, null);
                          int mateErr = 0;
                          string mateName = "Top_Coincident_" + comp.Name2;
                          if (FindFeatureByName(swDoc, mateName) == null)
                              IFeature mateFeat = (IFeature)swAssy.AddMate3((int)swMateType_e.swMateCOINCIDENT,
                                  (int)swMateAlign_e.swMateAlignALIGNED, false, 0, 0, 0, 0, 0, 0, 0, 0, false, out mateErr);
                              mateFeat.Name = mateName;
                  private IFeature FindFeatureByName(IModelDoc2 doc, string name)
                      IFeatureManager featMgr = (IFeatureManager)doc.FeatureManager;
                      foreach (Object f in (Object[])featMgr.GetFeatures(true))
                          IFeature feat = f as IFeature;
                          if (feat.Name == name)
                              return feat;
                      return null;
                  /// <summary>        ///  The SldWorks swApp variable is pre-assigned for you.        /// </summary>        public SldWorks swApp;





          Another thing you could try is add a mate ANTI-ALIGNED.  If is returns an error, then you know to remove it.  If it doesn't return an error, then you can align the mate.  There are some caveats with that method and I haven't tried it, but it might work for your situation.