public bool Run(BackgroundWorker worker) { int succesfulModified = 0; var dh = new Tekla.Structures.Drawing.DrawingHandler(); dh.CloseActiveDrawing(); var selectedDrawings = dh.GetDrawingSelector().GetSelected(); var drawingsCount = selectedDrawings.GetSize(); if (drawingsCount == 0) { MessageBox.Show("No drawings selected", "No drawings", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } DialogResult result = MessageBox.Show( "Are you sure do you want to change attributes in " + selectedDrawings.GetSize() + " drawings?", "Are you sure ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2 ); if (result == DialogResult.Yes) { var stopwatch = new System.Diagnostics.Stopwatch(); try { UpdateProgress(0, 1, "Trying to start"); stopwatch.Start(); int currentNumber = data._StartNumber; int checkedDrawings = 0; double medTimeForOne = 0; while (selectedDrawings.MoveNext()) { if (worker.CancellationPending) { break; } var currentDrawing = selectedDrawings.Current as Tekla.Structures.Drawing.Drawing; currentDrawing.Select(); bool modified = false; var currentNumberString = GetCurrentNumberWithPrefixAndPostFix(data._Prefix, data._Postfix, currentNumber); switch (data._Title) { case (0): if (currentDrawing.Name != currentNumberString) { currentDrawing.Name = currentNumberString; modified = true; } break; case (1): if (currentDrawing.Title1 != currentNumberString) { currentDrawing.Title1 = currentNumberString; modified = true; } break; case (2): if (currentDrawing.Title2 != currentNumberString) { currentDrawing.Title2 = currentNumberString; modified = true; } break; case (3): if (currentDrawing.Title3 != currentNumberString) { currentDrawing.Title3 = currentNumberString; modified = true; } break; default: try { var udaHandler = new UDAHandler(); var udaName = udaHandler.GetUDAByIndex(data._Title - 4); if (udaName != "") { modified = currentDrawing.SetUserProperty(udaName, currentNumberString); } } catch { } break; } if (modified) { currentDrawing.Modify(); succesfulModified++; } currentNumber++; checkedDrawings++; medTimeForOne = stopwatch.Elapsed.TotalMinutes / checkedDrawings; string message = checkedDrawings + "/" + drawingsCount + " " + Math.Round(medTimeForOne * (drawingsCount - checkedDrawings)) + " minutes left"; UpdateProgress(checkedDrawings, drawingsCount, message); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { stopwatch.Stop(); string message = "Task has been completed. Modified drawings: " + succesfulModified.ToString() + ". Time elapsed: " + Math.Round(stopwatch.Elapsed.TotalSeconds).ToString() + " seconds"; UpdateProgress(0, 1, message); } } return(true); }
public override bool Run(List <InputDefinition> Input) { GetValuesFromDialog(); int succesfulModified = 0; var selectedDrawings = new Tekla.Structures.Drawing.DrawingHandler().GetDrawingSelector().GetSelected(); var drawingsCount = selectedDrawings.GetSize(); if (drawingsCount == 0) { MessageBox.Show("No drawings selected", "No drawings", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } DialogResult result = MessageBox.Show( "Are you sure do you want to change attributes in " + selectedDrawings.GetSize() + " drawings?", "Are you sure ?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2 ); if (result == DialogResult.Yes) { var progress = new Tekla.Structures.Model.Operations.Operation.ProgressBar(); var stopwatch = new System.Diagnostics.Stopwatch(); try { progress.Display(10, "Task is in progress", "Task is in progress", "Cancel", " "); stopwatch.Start(); int currentNumber = _StartNumber; int checkedDrawings = 0; double medTimeForOne = 0; while (selectedDrawings.MoveNext()) { if (progress.Canceled()) { break; } var currentDrawing = selectedDrawings.Current as Tekla.Structures.Drawing.Drawing; bool modified = false; var currentNumberString = GetCurrentNumberWithPrefixAndPostFix(_Prefix, _Postfix, currentNumber); switch (_Title) { case (0): if (currentDrawing.Name != currentNumberString) { currentDrawing.Name = currentNumberString; modified = true; } break; case (1): if (currentDrawing.Title1 != currentNumberString) { currentDrawing.Title1 = currentNumberString; modified = true; } break; case (2): if (currentDrawing.Title2 != currentNumberString) { currentDrawing.Title2 = currentNumberString; modified = true; } break; case (3): if (currentDrawing.Title3 != currentNumberString) { currentDrawing.Title3 = currentNumberString; modified = true; } break; default: try { var udaHandler = new UDAHandler(); var udaName = udaHandler.GetUDAByIndex(_Title - 4); if (udaName != "") { modified = currentDrawing.SetUserProperty(udaName, currentNumberString); } } catch (Exception) { } break; } if (modified) { currentDrawing.Modify(); succesfulModified++; } currentNumber++; checkedDrawings++; medTimeForOne = stopwatch.Elapsed.TotalMinutes / checkedDrawings; progress.SetProgress(checkedDrawings + "/" + drawingsCount + "\t" + Math.Round(medTimeForOne * (drawingsCount - checkedDrawings)) + " minutes left", 100 * checkedDrawings / drawingsCount); } } catch (Exception e) { MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { progress.Close(); stopwatch.Stop(); MessageBox.Show("Task has been completed.\nModified drawings: " + succesfulModified.ToString() + ".\nTime elapsed: " + Math.Round(stopwatch.Elapsed.TotalSeconds).ToString() + " seconds"); } } return(true); }