protected override void CaptureDesignFormn_FormClosing(object sender, FormClosingEventArgs e)
        {
            InventorApplication.StopListening();

            InventorApplication.DocumentChanged -= DocumentChangedHandler;

            base.CaptureDesignFormn_FormClosing(sender, e);
        }
        /// <summary>
        /// Attaches to inventor and setups up the methods
        /// </summary>
        /// <param name="worksheet"></param>
        /// <param name="topDocument"></param>
        public ProcessRunBlockInventor(Excel.Worksheet worksheet, InventorDocument topDocument = null) : base(worksheet)
        {
            if (!InventorApplication.Attached)
            {
                InventorApplication.Attach();
            }

            _methods     = new InventorMethods();
            _topDocument = topDocument;
        }
        private void InventorCaptureDrawingData_Click(object sender, RibbonControlEventArgs e)
        {
            if (!InventorApplication.Attached)
            {
                InventorApplication.Attach();
            }

            var captureForm = new DrawingCaptureForm();

            captureForm.ShowDialog();
        }
        private void SetUp()
        {
            if (!InventorApplication.Attached)
            {
                InventorApplication.Attach();
            }

            InventorApplication.Listen();

            DocumentChangedHandler = new InventorApplication.DocumentChangedHandler(UpdateWorkingDocument);

            InventorApplication.DocumentChanged += DocumentChangedHandler;

            Globals.ThisAddIn.Application.ActiveWorkbook.SheetActivate += UpdateSelectedSheet;

            UpdateWorkingDocument();
        }
        private void InventorGetRefButton_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();

            var docs = new List <CopyDocumentItem>();

            docs.Add(new CopyDocumentItem(adoc));

            var filesPathsToAvoid = Settings.Default.PathsToAvoid.SettingsToList();

            foreach (var doc in adoc.ReferencedDocuments)
            {
                if (filesPathsToAvoid.Any(x => doc.FileName.ToUpper().Contains(x.ToUpper())))
                {
                    continue;
                }

                docs.Add(new CopyDocumentItem(doc));
            }

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

                Globals.ThisAddIn.Application.ActiveSheet.Name = "Copy Tool";
            }

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

            workSheet.Cells.Clear();

            workSheet.Name = "Copy Tool";

            workSheet.Range["A1"].Value = "Old Path";
            workSheet.Range["B1"].Value = "New Path";

            var i = 2;

            foreach (var d in docs)
            {
                workSheet.Range[$"A{i}"].Value = d.OldPath;
                workSheet.Range[$"B{i}"].Value = d.NewPath;

                i++;
            }

            var copyTable = workSheet.Range[$"A1:B{i}"];

            workSheet.ListObjects.AddEx(XlListObjectSourceType.xlSrcRange, copyTable, null, XlYesNoGuess.xlYes).Name = "Copy Table";

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

            var old = outTable.GetListColumns().FirstOrDefault(x => x.Name == "Old Path");
        }
        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;
            }
        }
예제 #7
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);
                    }
                }
            }
        }