private void InventorcopyDocuments_Click(object sender, RibbonControlEventArgs e)
        {
            if (!InventorApplication.Attached)
            {
                InventorApplication.Attach();
            }

            var activeDocument = InventorApplication.ActiveDocument;

            var valid = activeDocument != null && activeDocument.IsAssemblyDoc;

            if (!valid)
            {
                MessageBox.Show("Please open an assembly document");
            }

            var adoc = activeDocument.GetAssemblyDocument();

            if (Globals.ThisAddIn.Application.ActiveWorkbook.WorkSheetExists("Copy Tool"))
            {
                Globals.ThisAddIn.Application.ActiveWorkbook.ActivateSheet("Copy Tool");
            }
            else
            {
                return;
            }

            Excel.Worksheet workSheet = Globals.ThisAddIn.Application.ActiveSheet;

            try
            {
                var outTable = workSheet.GetListObjects().FirstOrDefault(x => x.Name == "Copy Table");

                if (outTable == null)
                {
                    throw new Exception("Could not find table Copy Table");
                }

                var oldPath = outTable.GetListColumns().FirstOrDefault(x => x.Name == "Old Path");

                var newPath = outTable.GetListColumns().FirstOrDefault(x => x.Name == "New Path");

                var oldPathList = oldPath.Range.GetFilePaths();

                var newPathList = newPath.Range.GetFilePaths();

                if (oldPathList.Count != newPathList.Count)
                {
                    throw new Exception("Old path list and new path list do not match in qty");
                }

                var paths = new List <Tuple <string, string> >();

                for (var i = 0; i < oldPathList.Count; i++)
                {
                    paths.Add(new Tuple <string, string>(oldPathList[i], newPathList[i]));
                }

                var mainDocPath = paths.FirstOrDefault(x => x.Item1.ToUpper() == adoc.FileName.ToUpper());

                if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(mainDocPath.Item2)))
                {
                    System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(mainDocPath.Item2));
                }

                adoc.SaveAs(mainDocPath.Item2);

                foreach (var doc in adoc.ReferencedDocuments)
                {
                    if (paths.Any(x => x.Item1.ToUpper() == doc.FileName.ToUpper()))
                    {
                        var path = paths.First(x => x.Item1.ToUpper() == doc.FileName.ToUpper());

                        if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(path.Item2)))
                        {
                            System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path.Item2));
                        }

                        doc.SaveAs(path.Item2);
                    }
                }

                adoc.Save();

                adoc.Close();

                adoc.Dispose();

                var newDoc = InventorApplication.Open(mainDocPath.Item2, "");

                CopyHelpers.ReplaceReferences(newDoc, paths);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
Ejemplo n.º 2
0
        public void OpenDocument(string name, string sourceLocation, string textToFind, string textToReplace)
        {
            var replaceList = new List <string>();
            var searchList  = new List <string>();
            var paths       = new List <ReferenceDto>();

            if (textToFind.Contains(","))
            {
                searchList.AddRange(textToFind.Split(','));
            }
            else
            {
                searchList.Add(textToFind);
            }

            if (replaceList.Contains(","))
            {
                replaceList.AddRange(textToReplace.Split(','));
            }
            else
            {
                replaceList.Add(textToReplace);
            }

            if (searchList.Count != replaceList.Count)
            {
                throw new Exception("Search list and replace list don't match");
            }

            if (System.IO.File.Exists(name))
            {
                InventorApplication.Open(name, true);
            }
            else
            {
                if (System.IO.File.Exists(sourceLocation))
                {
                    var document = InventorApplication.Open(sourceLocation, true);

                    var newMainDocPath = document.FileName;

                    for (var i = 0; i < searchList.Count; i++)
                    {
                        newMainDocPath = newMainDocPath.Replace(searchList[i].Trim(), replaceList[i].Trim());
                    }

                    if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(newMainDocPath)))
                    {
                        System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(newMainDocPath));
                    }

                    document.SaveAs(newMainDocPath);

                    if (document.IsAssemblyDoc)
                    {
                        var adoc = document.GetAssemblyDocument();

                        foreach (var doc in adoc.ReferencedDocuments)
                        {
                            var oldPath = doc.FileName;

                            var newPath = oldPath;

                            for (var i = 0; i < searchList.Count; i++)
                            {
                                newPath = newPath.Replace(searchList[i].Trim(), replaceList[i].Trim());
                            }

                            if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(newPath)))
                            {
                                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(newPath));
                            }

                            doc.SaveAs(newPath);

                            paths.Add(new ReferenceDto {
                                OriginalReference = oldPath, NewReference = newPath
                            });
                        }
                    }

                    document.Save();

                    document.Close();

                    document.Dispose();

                    var newDoc = InventorApplication.Open(newMainDocPath);

                    if (newDoc.IsAssemblyDoc)
                    {
                        CopyHelpers.ReplaceReferences(newDoc, paths);
                    }
                }
            }
        }