9 Replies Latest reply on Nov 23, 2016 5:01 PM by Roman Mettler

    Copy Assembly and replace references with Document Manager API (C#)

    Roman Mettler

      Hello, im new in the forum, already made a search, found some answers but cant get it to work, would be great if someone could point me in the right direction...

      Here is a bit of my code, it is part of it so i commented some parts just to clarify what is going on, the goal is to copy an assembly file, get the referenced component names, copy them too with updated names(later will do the same with sub assemblies recursively), replace the references in the new copied assembly...

       

      public void CopyAssembly(string Path)

      {

         int count = 0;

         SwDMConfiguration8 config;

         Helpers.documentSerials.Part srlPrt = new documentSerials.Part(); // Serial Number Generator for parts

         Helpers.documentSerials.Assembly srlAsm = new documentSerials.Assembly(); // Serial Number Generator for Assemblies

         string newAssyFileName = SwAddin.CurrentProject.Path + "\\Data\\" + srlAsm.GetFormatedSerial(srlAsm.GetNewSerial().ToString()) + ".SLDASM"; // Assembly Name    generated on the fly using an automatic serial system

         System.IO.File.Copy(Path, newAssyFileName, true);

       

         //int x = swDocMgr.CopyDocument(Path, newAssyFileName, null, null, 1, null);

         swDoc = swDocMgr.GetDocument(newAssyFileName, SwDmDocumentType.swDmDocumentAssembly, true, out nRetVal) as SwDMDocument12;

               

         SwDMConfigurationMgr configMgr = swDoc.ConfigurationManager;

         string currConfig = configMgr.GetActiveConfigurationName();

         config = (SwDMConfiguration8)configMgr.GetConfigurationByName(currConfig);

         object comps = config.GetComponents();

          Array arrComps = (Array)comps;

         foreach (SwDMComponent6 swComp in arrComps)

         {

              if(!rplFiles.ContainsKey(swComp.Name)) //RplFiles is a Dictionary<string, string>

              {

                   if (Helpers.SWPMHelpers.GetDocumentType(swComp.PathName) == swDocumentType.swPart)

                   {

                        string newPartFileName = SwAddin.CurrentProject.Path + "\\Data\\" + srlPrt.GetFormatedSerial(srlPrt.GetNewSerial().ToString()) + ".SLDPRT";

                        CopyPart(swComp.PathName, newPartFileName, true);

                        bool result = swComp.Replace(newPartFileName, swComp.ConfigurationName, true);

                        if(result)

                           {

                               count++;

                           }

                              rplFiles.Add(swComp.Name, newPartFileName);

                          }

                      }

                      else

                      {

                          bool result = swComp.Replace(rplFiles[swComp.Name], swComp.ConfigurationName, true);

                          if (result)

                          {

                              count++;

                          }

                      }

                  }

                  SwDmDocumentSaveError err = swDoc.Save();

                  swDoc.CloseDoc();

              }

       

       

      All the moving operations are done, no errors returned by the api, but references never get updated.

       

      thanks in advance,

      Best regards,

      Bruno Frade.