public void UpdateLineColor() { try { if (ErrorHandler.IsActiveSelection() == false) { Excel.ShapeRange shapeObjects = Globals.ThisAddIn.Application.Selection.ShapeRange; foreach (Excel.Shape shape in shapeObjects) { if (shape.Name.StartsWith("RevTri") && shape.Type != Microsoft.Office.Core.MsoShapeType.msoTextBox) { shape.Select(); Globals.ThisAddIn.Application.Selection.ShapeRange.Line.ForeColor.RGB = Properties.Settings.Default.Markup_ShapeLineColor; Globals.ThisAddIn.Application.Selection.Font.Color = Properties.Settings.Default.Markup_ShapeLineColor; } else { if (shape.Type != Microsoft.Office.Core.MsoShapeType.msoTextBox) { SetLineColor(); } } } } } catch (Exception ex) { ErrorHandler.DisplayMessage(ex); } }
private Excel.Shape[] Sort(Excel.ShapeRange shapeRange, Func <Excel.Shape, Excel.Shape, float> testFunc) { var sorted = new Excel.Shape[shapeRange.Count]; for (int i = 0; i < sorted.Length; i++) { sorted[i] = shapeRange.Item(i + 1); } // FIXME: GroupByで一発でソートする bool swapped = true; while (swapped) { swapped = false; for (int i = 0, length = sorted.Length; i < length - 1; i++) { if (testFunc(sorted[i], sorted[i + 1]) > 0) { var tmp = sorted[i + 1]; sorted[i + 1] = sorted[i]; sorted[i] = tmp; swapped = true; } } } return(sorted); }
/// <summary> /// Explicitly release all com objects /// </summary> static void ReleaseAllObjects() { if (LogCalls) { DebugLog.Message("ReleaseAllObjects"); } ReleaseObject(XlRange); ReleaseObject(XlShapeRange); ReleaseObject(XlShapes); // ReleaseObject(XlSelection); // ReleaseObject(XlTemp); ReleaseObject(XlQueryTables); ReleaseObject(XlPictures); ReleaseObject(XlSheet); ReleaseObject(XlBook); ReleaseObject(XlSheet2); ReleaseObject(XlBook2); ReleaseObject(XlBooks); XlRange = null; XlShapeRange = null; XlShapes = null; // XlSelection = null; // XlTemp = null; XlQueryTables = null; XlPictures = null; XlSheet = null; XlBook = null; XlSheet2 = null; XlBook2 = null; XlBooks = null; }
/// <summary> /// Get Selected Shape /// </summary> /// <returns>Return Selected Shape</returns> private MyShape GetShapeSelected() { Excel.Shape selectedShape = null; MyShape myselectedShape = null; Excel.ShapeRange selectedShapeRange = null; try { selectedShapeRange = this.Application.Selection.ShapeRange; } catch { if (selecteTypeNameNow != selectedTypeNameLastTime) { customTaskPanel.AddMessage("No shape selected"); } } if (selectedShapeRange != null) { selectedShape = selectedShapeRange.Item(1) as Excel.Shape; if (selectedShape != null) { myselectedShape = new MyShape(selectedShape); return(myselectedShape); } } return(myselectedShape); }
public void SetSequentialNumber(Excel.ShapeRange shapeRange, uint startNumber, uint step, SequenceDirection direction) { var testFunc = GetTestFunc(direction); var sortedShapes = Sort(shapeRange, testFunc); for (int i = 0; i < sortedShapes.Length; i++) { sortedShapes[i].TextFrame2.TextRange.Text = string.Format("{0}", startNumber + (step * i)); } }
public void CreateCloudHatch() { Excel.Shape cloudPart = null; Excel.Shape hatchArea = null; Excel.ShapeRange shapeRange = null; try { if (ErrorHandler.IsEnabled(true) == false) { return; } ErrorHandler.CreateLogRecord(); string shapeName = AssemblyInfo.Title.ToLower(); double x = Globals.ThisAddIn.Application.Selection.Left; double y = Globals.ThisAddIn.Application.Selection.Top; double h = Globals.ThisAddIn.Application.Selection.Height; double w = Globals.ThisAddIn.Application.Selection.Width; cloudPart = CreateCloudPart("ALL"); hatchArea = CreateHatchArea(x, y, h, w); if (cloudPart != null && hatchArea != null) { object[] shapes = { cloudPart.Name, hatchArea.Name }; shapeRange = Globals.ThisAddIn.Application.ActiveSheet.Shapes.Range(shapes); //.Group(); shapeRange.Group(); shapeRange.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat); Properties.Settings.Default.Markup_LastShapeName = shapeRange.Name; Marshal.FinalReleaseComObject(cloudPart); Marshal.FinalReleaseComObject(hatchArea); } } catch (Exception ex) { ErrorHandler.DisplayMessage(ex); } finally { if (cloudPart != null) { Marshal.ReleaseComObject(cloudPart); } if (hatchArea != null) { Marshal.ReleaseComObject(hatchArea); } if (shapeRange != null) { Marshal.ReleaseComObject(shapeRange); } } }
public Excel.Shape CreateCloudLine(double x1, double y1, double x2, double y2) { Excel.Shape cloudArc = null; Excel.Shape cloudLine = null; Excel.ShapeRange shapeRange = null; try { string shapeName = AssemblyInfo.Title.ToLower(); double length = Properties.Settings.Default.Markup_ShapeLineSpacing; int i = 0; double x = 0; double y = 0; double dx = x2 - x1; double dy = y2 - y1; double d = Math.Sqrt(dx * dx + dy * dy); double segments = Math.Ceiling(d / length); if (segments < 2) { segments = 2; } double deltax = (dx / segments); double deltay = (dy / segments); double xp = x1; double yp = y1; object[] shapes = new object[Convert.ToInt32(segments)]; for (i = 1; i <= Convert.ToInt32(segments); i++) { x = xp + deltax; y = yp + deltay; cloudArc = CreateArc(xp, yp, x, y, length); shapes[i - 1] = cloudArc.Name; xp = x; yp = y; } shapeRange = Globals.ThisAddIn.Application.ActiveSheet.Shapes.Range(shapes); //.Group(); shapeRange.Group(); shapeRange.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + LineNbr.ToString(); LineNbr += 1; cloudLine = Globals.ThisAddIn.Application.ActiveSheet.Shapes(shapeRange.Name); Properties.Settings.Default.Markup_LastShapeName = shapeRange.Name; return(cloudLine); } catch (Exception ex) { ErrorHandler.DisplayMessage(ex, true); return(null); } }
public static void SelectionShapeToOpenSCAD() { XL.Application xl = (XL.Application)ExcelDnaUtil.Application; dynamic sel = xl.Selection; XL.ShapeRange sr = (XL.ShapeRange)sel.ShapeRange; //WF.MessageBox.Show(sr.Name); var vecs = new string[sr.Nodes.Count]; var i = 0; foreach (XL.ShapeNode n in sr.Nodes) { float[,] ps = n.Points; //Excelは左上に左上に原点,OpenSCADは左下に原点のためyを反転する意味で-1をかける vecs[i++] = $"[{ps[1, 1]},-{ps[1, 2]}]"; } WF.Clipboard.SetText($@"points = [ {string.Join(",\n\t", vecs)} ]; polygon(points=points);"); sr = null; xl = null; }
private void _FormatThem(object _range, Func <string, IEnumerable <PCommand> > commandMaker, bool normalize) { dynamic range = (dynamic)_range; switch (range) { case Excel.Range cells: switch (cells.Count) { case 0: return; case 1: FormatIt(cells, commandMaker, normalize); break; default: foreach (Excel.Range cell in cells) { _FormatThem(cell, commandMaker, normalize); } break; } break; case Office.TextRange2 textRange: FormatIt(textRange, commandMaker, normalize); break; case Excel.Shape shape: _FormatThem(shape.TextFrame2.TextRange, commandMaker, normalize); break; case Excel.ShapeRange shapeRange: for (int i = 1; i <= shapeRange.Count; i++) { _FormatThem(shapeRange.Item(i), commandMaker, normalize); } break; case Excel.ChartArea chartArea: Debug.Assert(chartArea.Parent is Excel.Chart); _FormatThem(chartArea.Parent, commandMaker, normalize); break; case Excel.Chart chart: _FormatThem(chart.ChartTitle, commandMaker, normalize); break; case Excel.ChartTitle chartTitle: _FormatThem(chartTitle.Format.TextFrame2.TextRange, commandMaker, normalize); break; default: try { dynamic o = Globals.ThisAddIn.Application.Selection; Excel.ShapeRange shapeRange = o.ShapeRange; _FormatThem(shapeRange, commandMaker, normalize); } catch (Exception) { } break; } ProgressDialog.Current.ReportWithCancellationCheck(""); }
public Excel.Shape CreateHatchArea(double x, double y, double h, double w) { string shapeName = AssemblyInfo.Title.ToLower(); double length = Properties.Settings.Default.Markup_ShapeLineSpacing; double xx1 = 0; double yy1 = 0; double xx2 = 0; double yy2 = 0; double x1 = x + y; double x2 = Math.Floor(x1 / length); double x3 = (x2 + 1) * length; double xDiff = x3 - x1; double xl = x; double xr = x + w; double xw = w; double yt = y; double yb = y + h; double yw = h; double xsp = x + xDiff; double ysp = yt + xDiff; Excel.Shape hatchLine1 = null; Excel.Shape hatchLine2 = null; Excel.Shape hatchArea = null; Excel.ShapeRange shapeRange = null; List <object> shapesList = new List <object>(); try { if (xw >= yw) { while (xsp < xr + yw) { if (xsp - xl < yb - yt) { xx1 = xsp; yy1 = yt; xx2 = xl; yy2 = yt + (xx1 - xl); } else { if (xsp <= xr) { xx1 = xsp; yy1 = yt; xx2 = xx1 - yw; yy2 = yb; } else { xx2 = xsp - yw; yy2 = yb; xx1 = xr; yy1 = yb - (xr - xx2); } } hatchLine1 = Globals.ThisAddIn.Application.ActiveSheet.Shapes.AddLine(Convert.ToSingle(xx1), Convert.ToSingle(yy1), Convert.ToSingle(xx2), Convert.ToSingle(yy2)); hatchLine1.Select(); hatchLine1.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + LineNbr.ToString(); shapesList.Add(hatchLine1.Name); SetLineColor(); xsp = xsp + length; LineNbr += 1; } } else { while (ysp < yb + xw) { if (ysp - yt < xw) { xx2 = xl; yy2 = ysp; xx1 = xl + (yy2 - yt); yy1 = yt; } else { if (ysp <= yb) { xx2 = xl; yy2 = ysp; xx1 = xr; yy1 = yy2 - xw; } else { xx1 = xr; xx2 = xl + (ysp - yb); yy2 = yb; yy1 = yb - (xr - xx2); } } hatchLine2 = Globals.ThisAddIn.Application.ActiveSheet.Shapes.AddLine(Convert.ToSingle(xx1), Convert.ToSingle(yy1), Convert.ToSingle(xx2), Convert.ToSingle(yy2)); hatchLine2.Select(); hatchLine2.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + LineNbr.ToString(); shapesList.Add(hatchLine2.Name); SetLineColor(); ysp = ysp + length; LineNbr += 1; } } object[] shapes = shapesList.ToArray(); shapeRange = Globals.ThisAddIn.Application.ActiveSheet.Shapes.Range(shapes); shapeRange.Group(); shapeRange.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat); hatchArea = Globals.ThisAddIn.Application.ActiveSheet.Shapes(shapeRange.Name); Properties.Settings.Default.Markup_LastShapeName = shapeRange.Name; return(hatchArea); } catch (System.Runtime.InteropServices.COMException ex) { ErrorHandler.DisplayMessage(ex, true); return(null); } catch (Exception ex) { ErrorHandler.DisplayMessage(ex, true); return(null); } finally { if (shapeRange != null) { Marshal.FinalReleaseComObject(shapeRange); } if (hatchLine1 != null) { Marshal.FinalReleaseComObject(hatchLine1); } if (hatchLine2 != null) { Marshal.FinalReleaseComObject(hatchLine2); } } }
public Excel.Shape CreateCloudPart(string cloudPart) { if (ErrorHandler.IsEnabled(true) == false) { return(null); } Excel.Shape cloudLineBottom = null; Excel.Shape cloudLineTop = null; Excel.Shape cloudLineLeft = null; Excel.Shape cloudLineRight = null; Excel.Shape cloudLine = null; Excel.ShapeRange shapeRange = null; try { double x = Globals.ThisAddIn.Application.Selection.Left; double y = Globals.ThisAddIn.Application.Selection.Top; double h = Globals.ThisAddIn.Application.Selection.Height; double w = Globals.ThisAddIn.Application.Selection.Width; if (cloudPart == "B" | cloudPart == "ALL") { cloudLineBottom = CreateCloudLine(x, y + h, x + w, y + h); } if (cloudPart == "T" | cloudPart == "ALL") { cloudLineTop = CreateCloudLine(x + w, y, x, y); } if (cloudPart == "L" | cloudPart == "ALL") { cloudLineLeft = CreateCloudLine(x, y, x, y + h); } if (cloudPart == "R" | cloudPart == "ALL") { cloudLineRight = CreateCloudLine(x + w, y + h, x + w, y); } if (cloudPart == "ALL" && cloudLineBottom != null && cloudLineTop != null && cloudLineLeft != null && cloudLineRight != null) { string shapeName = AssemblyInfo.Title.ToLower(); object[] shapes = { cloudLineBottom.Name, cloudLineTop.Name, cloudLineLeft.Name, cloudLineRight.Name }; shapeRange = Globals.ThisAddIn.Application.ActiveSheet.Shapes.Range(shapes); //.Group(); shapeRange.Group(); shapeRange.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat); cloudLine = Globals.ThisAddIn.Application.ActiveSheet.Shapes(shapeRange.Name); Properties.Settings.Default.Markup_LastShapeName = shapeRange.Name; return(cloudLine); } else { return(null); } } catch (Exception ex) { ErrorHandler.DisplayMessage(ex); return(null); } finally { if (cloudLineBottom != null) { Marshal.ReleaseComObject(cloudLineBottom); } if (cloudLineTop != null) { Marshal.ReleaseComObject(cloudLineTop); } if (cloudLineLeft != null) { Marshal.ReleaseComObject(cloudLineLeft); } if (cloudLineRight != null) { Marshal.ReleaseComObject(cloudLineRight); } if (cloudLine != null) { Marshal.ReleaseComObject(cloudLine); } if (shapeRange != null) { Marshal.ReleaseComObject(shapeRange); } } }
public void CreateCloudHold() { Excel.Shape cloudLineTop = null; Excel.Shape cloudLineRight = null; Excel.Shape cloudLineBottom = null; Excel.Shape cloudLineLeft = null; Excel.ShapeRange shapeRange = null; try { if (ErrorHandler.IsEnabled(true) == false) { return; } ErrorHandler.CreateLogRecord(); string shapeName = AssemblyInfo.Title.ToLower(); double x = Globals.ThisAddIn.Application.Selection.Left; double y = Globals.ThisAddIn.Application.Selection.Top; double h = Globals.ThisAddIn.Application.Selection.Height; double w = Globals.ThisAddIn.Application.Selection.Width; double off = 7.5; x = x - off / 2; w = w + off; y = y - off / 2; h = h + off; cloudLineTop = CreateCloudLine(x, y, x + w, y); cloudLineRight = CreateCloudLine(x + w, y, x + w, y + h); cloudLineBottom = CreateCloudLine(x + w, y + h, x, y + h); cloudLineLeft = CreateCloudLine(x, y + h, x, y); if (cloudLineBottom != null && cloudLineTop != null && cloudLineLeft != null && cloudLineRight != null) // only if there are no errors in returning an Excel shape { object[] shapes = { cloudLineBottom.Name, cloudLineTop.Name, cloudLineLeft.Name, cloudLineRight.Name }; shapeRange = Globals.ThisAddIn.Application.ActiveSheet.Shapes.Range(shapes); shapeRange.Group(); shapeRange.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat); Properties.Settings.Default.Markup_LastShapeName = shapeRange.Name; } } catch (Exception ex) { ErrorHandler.DisplayMessage(ex); } finally { if (cloudLineTop != null) { Marshal.ReleaseComObject(cloudLineTop); } if (cloudLineRight != null) { Marshal.ReleaseComObject(cloudLineRight); } if (cloudLineBottom != null) { Marshal.ReleaseComObject(cloudLineBottom); } if (cloudLineLeft != null) { Marshal.ReleaseComObject(cloudLineLeft); } if (shapeRange != null) { Marshal.ReleaseComObject(shapeRange); } } }
public void CreateRevisionTriangle() { Excel.Shape shpTriangle = null; Excel.Shape txtTriangle = null; Excel.ShapeRange shapeRange = null; try { if (ErrorHandler.IsEnabled(true) == false) { return; } ErrorHandler.CreateLogRecord(); string shapeName = AssemblyInfo.Title.ToLower(); Single[,] triArray = new Single[4, 2]; double x = 0; double y = Globals.ThisAddIn.Application.Selection.Top; double h = Globals.ThisAddIn.Application.Selection.RowHeight; double w = Convert.ToInt32(h * 2.2 / Math.Sqrt(3)); double f = Globals.ThisAddIn.Application.Selection.Font.Size; double selWidth = Globals.ThisAddIn.Application.Selection.Width; double selLeft = Globals.ThisAddIn.Application.Selection.Left; double selHorAli = Globals.ThisAddIn.Application.Selection.HorizontalAlignment; double xlAliCntr = Convert.ToDouble(Excel.XlHAlign.xlHAlignCenter); if (selHorAli == xlAliCntr & selWidth > w) { x = selLeft + (selWidth - w) / 2; } else { x = selLeft; } triArray[0, 0] = Convert.ToSingle(x + w / 2); triArray[0, 1] = Convert.ToSingle(y); triArray[1, 0] = Convert.ToSingle(x); triArray[1, 1] = Convert.ToSingle(y + h); triArray[2, 0] = Convert.ToSingle(x + w); triArray[2, 1] = Convert.ToSingle(y + h); triArray[3, 0] = Convert.ToSingle(x + w / 2); triArray[3, 1] = Convert.ToSingle(y); shpTriangle = Globals.ThisAddIn.Application.ActiveSheet.Shapes.AddPolyline(triArray); shpTriangle.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + "1"; shpTriangle.Line.Weight = Convert.ToSingle(1.5); //add a textbox to the triangle txtTriangle = Globals.ThisAddIn.Application.ActiveSheet.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, Convert.ToSingle(x), Convert.ToSingle(y + h * 0.2), Convert.ToSingle(w), Convert.ToSingle(h * 0.8)); txtTriangle.Select(); txtTriangle.TextEffect.Text = Properties.Settings.Default.Markup_TriangleRevisionCharacter; Globals.ThisAddIn.Application.Selection.Font.Color = Properties.Settings.Default.Markup_ShapeLineColor; Globals.ThisAddIn.Application.Selection.Font.Size = f; Globals.ThisAddIn.Application.Selection.Border.LineStyle = Excel.Constants.xlNone; Globals.ThisAddIn.Application.Selection.Interior.ColorIndex = Excel.Constants.xlNone; Globals.ThisAddIn.Application.Selection.Shadow = false; Globals.ThisAddIn.Application.Selection.RoundedCorners = false; txtTriangle.TextFrame.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; txtTriangle.TextFrame.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; txtTriangle.TextFrame.AutoSize = true; txtTriangle.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + "2"; //group both shapes together object[] shapes = { shpTriangle.Name, txtTriangle.Name }; shapeRange = Globals.ThisAddIn.Application.ActiveSheet.Shapes.Range(shapes); shapeRange.Group(); shapeRange.Name = shapeName + new string(' ', 1) + DateTime.Now.ToString(Properties.Settings.Default.Markup_ShapeDateFormat) + "3"; shpTriangle.Select(); Globals.ThisAddIn.Application.Selection.Interior.Pattern = Excel.XlPattern.xlPatternNone; SetLineColor(); Globals.ThisAddIn.Application.ActiveCell.Select(); Properties.Settings.Default.Markup_LastShapeName = shapeRange.Name; } catch (Exception ex) { ErrorHandler.DisplayMessage(ex); } finally { if (shapeRange != null) { Marshal.ReleaseComObject(shapeRange); } if (txtTriangle != null) { Marshal.ReleaseComObject(txtTriangle); } if (shpTriangle != null) { Marshal.ReleaseComObject(shpTriangle); } } }