private void solidWorksCopyButton_Click(object sender, RibbonControlEventArgs e) { if (!SolidworksApplication.Attached) { SolidworksApplication.Attach(); } var activeDocument = SolidworksApplication.ActiveDocument; var valid = activeDocument != null && activeDocument.IsAssemblyDoc; if (!valid) { MessageBox.Show("Please open an assembly document"); } var adoc = activeDocument; 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.FullFileName.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); adoc.Save(); adoc.Close(); adoc.Dispose(); var newDoc = SolidworksApplication.Open(mainDocPath.Item2, DocumentTypes.ASSEMBLY); var children = newDoc.Children(); // capture the current state of suppression children.CaptureSuppressionState(); children.UnsuppressAll(); foreach (var doc in children.GetReferencedDocuments()) { if (paths.Any(x => x.Item1.ToUpper() == doc.FullFileName.ToUpper())) { var path = paths.First(x => x.Item1.ToUpper() == doc.FullFileName.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); } } SolidworksCopyHelpers solidworksCopy = new SolidworksCopyHelpers(); solidworksCopy.References(newDoc, paths); children.RestoreSuppressionState(); //CopyHelpers.ReplaceReferences(newDoc, paths); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }
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 <ReferenceDto>(); for (var i = 0; i < oldPathList.Count; i++) { paths.Add(new ReferenceDto { OriginalReference = oldPathList[i], NewReference = newPathList[i] }); } var mainDocPath = paths.FirstOrDefault(x => x.OriginalReference.ToUpper() == adoc.FileName.ToUpper()); if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(mainDocPath.NewReference))) { System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(mainDocPath.NewReference)); } adoc.SaveAs(mainDocPath.NewReference); foreach (var doc in adoc.ReferencedDocuments) { if (paths.Any(x => x.OriginalReference.ToUpper() == doc.FileName.ToUpper())) { var path = paths.First(x => x.OriginalReference.ToUpper() == doc.FileName.ToUpper()); if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(path.NewReference))) { System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path.NewReference)); } doc.SaveAs(path.NewReference); } } adoc.Save(); adoc.Close(); adoc.Dispose(); var newDoc = InventorApplication.Open(mainDocPath.NewReference, true); CopyHelpers.ReplaceReferences(newDoc, paths); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }