protected override void CaptureDesignFormn_FormClosing(object sender, FormClosingEventArgs e) { SolidworksApplication.StopListening(); SolidworksApplication.DocumentChanged -= DocumentChangedHandler; base.CaptureDesignFormn_FormClosing(sender, e); }
/// <summary> /// Closes the document /// </summary> /// <param name="save">If true the document will be saved</param> public void Close(bool save = false) { if (save) { Save(); } SolidworksApplication.Close(Title); }
public ProcessRunBlockSolidworks(Excel.Worksheet worksheet, SolidworksDocument topDocument = null) : base(worksheet) { if (!SolidworksApplication.Attached) { SolidworksApplication.Attach(); } _methods = new SolidworksMethods(); }
private void SetUp() { if (!SolidworksApplication.Attached) { SolidworksApplication.Attach(); } SolidworksApplication.Listen(); DocumentChangedHandler = new SolidworksApplication.DocumentChangedHandler(UpdateWorkingDocument); SolidworksApplication.DocumentChanged += DocumentChangedHandler; Globals.ThisAddIn.Application.ActiveWorkbook.SheetActivate += UpdateSelectedSheet; UpdateWorkingDocument(); }
private void Recurse(SolidworksDocument document, List <Tuple <string, string> > paths) { foreach (var c in document.Children(true)) { var path = c.SolidworksDocument.FullFileName; var refPath = paths.FirstOrDefault(x => x.Item1.ToUpper() == path.ToUpper()); if (refPath == null) { continue; } document.ClearSelection(); c.Select(); var adoc = document._doc as AssemblyDoc; if (adoc == null) { continue; } adoc.ReplaceComponents(refPath.Item2, c.ReferencedConfiguration, false, true); if (c.SolidworksDocument.IsAssemblyDoc) { SolidworksApplication.ActivateDocument(c.SolidworksDocument.FileName); var doc = SolidworksApplication.ActiveDocument; Recurse(doc, paths); doc.Close(); } } }
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 solidworksLoadRefDocs_Click(object sender, RibbonControlEventArgs e) { if (!SolidworksApplication.Attached) { SolidworksApplication.Attach(); } var activeDocument = SolidworksApplication.ActiveDocument; if (activeDocument == null || !activeDocument.IsAssemblyDoc) { MessageBox.Show("Please open an assembly document"); } var docs = new List <CopyDocumentItem>(); docs.Add(new CopyDocumentItem(activeDocument.FullFileName)); var filesPathsToAvoid = Settings.Default.PathsToAvoid.SettingsToList(); foreach (var doc in activeDocument.Children(false).GetReferencedDocumentsNames()) { if (filesPathsToAvoid.Any(x => doc.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"); }
public List <string> Run(bool startMethod, string rangeName = "", string subRangeName = "", string subRangeValue = "") { Excel.Range startCell = null; if (string.IsNullOrEmpty(rangeName)) { startCell = _worksheet.Range[_worksheet.Name + "Type"]; } else { startCell = _worksheet.Range[rangeName]; } if (!string.IsNullOrEmpty(subRangeName)) { var subRangeCell = _worksheet.Range[subRangeName]; subRangeCell.Value = subRangeValue; } if (startCell == null) { throw new Exception("Could not find start range"); } var typeCol = startCell.Column; var nameCol = typeCol + 1; var parentCol = nameCol + 1; var value = parentCol + 1; var value2 = value + 1; var i = startCell.Row + 1; while (!string.IsNullOrEmpty(GetString(i, typeCol))) { var command = GetString(i, typeCol).ToUpper(); if (command == Commands.Comment) { i++; continue; } var workingDocumentName = GetString(i, parentCol); if (command != Commands.Sub) { if (string.IsNullOrEmpty(workingDocumentName)) { if (_topDocument == null) { _topDocument = SolidworksApplication.ActiveDocument; } else { if (SolidworksApplication.ActiveDocument.Name != _topDocument.Name) { SolidworksApplication.ActiveDocument.Close(); } SolidworksApplication.ActivateDocument(_topDocument.Name); } _workingDocument = _topDocument; } else { if (_workingDocument == null) { _workingDocument = SolidworksApplication.ActivateDocument(workingDocumentName); if (_workingDocument == null) { throw new Exception($"Could not find document {workingDocumentName}"); } } else { if (_workingDocument.Name != workingDocumentName) { if (_workingDocument.Name != _topDocument.Name) { _workingDocument.Close(); } _workingDocument = SolidworksApplication.ActivateDocument(workingDocumentName); } if (_workingDocument == null) { throw new Exception($"Could not find document {workingDocumentName}"); } } } } switch (command) { case Commands.TopLevelName: var topLevelName = GetString(i, nameCol); if (SolidworksApplication.ActiveDocument.Name != topLevelName) { throw new Exception("Top level name does not match active model"); } else { _topDocument = SolidworksApplication.ActiveDocument; } break; case Commands.Dimension: _methods.SetDimValue(_workingDocument, GetString(i, nameCol), "", GetDouble(i, value)); break; case Commands.Equation: _methods.SetEquation(_workingDocument, GetString(i, nameCol), GetDouble(i, value)); break; case Commands.SetProperty: _methods.SetProperty(_workingDocument, GetString(i, nameCol), GetString(i, value)); break; case Commands.GetProperty: var propertyValue = _methods.GetProperty(_workingDocument, GetString(i, nameCol)); SetValue(i, value, propertyValue); break; case Commands.ComponentActivity: _methods.Suppression(_workingDocument, GetString(i, nameCol), GetString(i, value), SuppresionType.Component); break; case Commands.ConstraintActivity: _methods.Suppression(_workingDocument, GetString(i, nameCol), GetString(i, value), SuppresionType.Constraint); break; case Commands.PatternActivity: _methods.Suppression(_workingDocument, GetString(i, nameCol), GetString(i, value), SuppresionType.Pattern); break; case Commands.FeatureActivity: _methods.Suppression(_workingDocument, GetString(i, nameCol), GetString(i, value), SuppresionType.Feature); break; case Commands.ShowConfiguration: _methods.ShowConfiguration(_workingDocument, GetString(i, nameCol)); break; case Commands.SetComponentConfiguration: _methods.SetComponentConfiguration(_workingDocument, GetString(i, nameCol), GetString(i, value)); break; case Commands.SetWeldmentConfiguration: _methods.SetWeldmentMemberConfiguration(_workingDocument, GetString(i, nameCol), GetString(i, value)); break; case Commands.ComponentVisiblity: _methods.SetVisiblity(_workingDocument, GetString(i, nameCol), GetBoolean(i, value), FeatureTypes.Component); break; case Commands.DocumentReferenceVisiblity: _methods.SetDocumentReferenceVisibility(_workingDocument, GetString(i, nameCol), GetBoolean(i, value), FeatureTypes.Component); break; case Commands.DeleteComponent: // _methods.Delete(_workingDocument, GetString(i, nameCol)); break; case Commands.DeleteReferencedDocuments: // _methods.DeleteReferenced(_workingDocument, GetString(i, nameCol)); break; case Commands.Stop: throw new Exception("Program Stopped"); case Commands.UpdateDocument: _workingDocument.ForceRebuildAll(); break; case Commands.Sub: ProcessRunBlockSolidworks runBlock = null; var subName = GetString(i, nameCol); var parameterName = GetString(i, parentCol); var parameter = GetString(i, value); var workSheetName = ""; if (subName.Contains("!")) { var subSplit = subName.Split('!'); workSheetName = subSplit[0]; subName = subSplit[1]; runBlock = new ProcessRunBlockSolidworks(Globals.ThisAddIn.Application.ActiveWorkbook.GetWorksheets().FirstOrDefault(x => x.Name == workSheetName), _topDocument); } else { runBlock = new ProcessRunBlockSolidworks(_worksheet, _topDocument); } runBlock.Run(false, subName, parameterName, parameter); break; case Commands.If: ValidateIf(i, typeCol); var booleanValue = GetBoolean(i, value); if (!booleanValue) { i = GetEndIfRow(i, typeCol); } break; case Commands.Repeat: ValidateRepeat(i, typeCol); repeatStart = i; repeatEnd = GetEndRepeatRow(i, typeCol); repeatCount = GetInt(i, value); repeatIndex = 1; SetValue(i, value2, repeatIndex.ToString()); inRepeat = true; break; } i++; if (inRepeat) { if (i == repeatEnd) { if (repeatIndex == repeatCount) { i = repeatEnd + 1; repeatStart = 0; repeatEnd = 0; repeatCount = 0; repeatIndex = 0; inRepeat = false; } else { i = repeatStart + 1; repeatIndex++; SetValue(repeatStart, value2, repeatIndex.ToString()); } } } } if (startMethod) { if (_topDocument != null && SolidworksApplication.ActiveDocument.Name != _topDocument.Name) { SolidworksApplication.ActiveDocument.Save(); SolidworksApplication.ActiveDocument.Close(); } SolidworksApplication.ActiveDocument.ForceRebuildAll(); SolidworksApplication.ActiveDocument.Save(); } return(_methods.Logs); }