-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisioAutomation.cs
128 lines (110 loc) · 5.04 KB
/
VisioAutomation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Linq;
using Visio = Microsoft.Office.Interop.Visio;
namespace VisioPlugin
{
public class VisioAutomation
{
private readonly Visio.Application _visioApplication;
private Visio.Document _activeDocument;
private Visio.Page _activePage;
private Dictionary<string, Visio.Document> _loadedStencils;
public VisioAutomation(Visio.Application visioApplication)
{
_visioApplication = visioApplication;
_loadedStencils = new Dictionary<string, Visio.Document>();
UpdateActiveDocumentAndPage();
}
private void UpdateActiveDocumentAndPage()
{
_activeDocument = _visioApplication.ActiveDocument;
_activePage = _activeDocument?.Pages.Cast<Visio.Page>().FirstOrDefault(p => p.ID == _activeDocument.Application.ActivePage.ID);
}
public void LoadStencil(string stencilPath, string stencilName)
{
try
{
if (!_loadedStencils.ContainsKey(stencilName))
{
var stencil = _visioApplication.Documents.OpenEx(stencilPath, (short)Visio.VisOpenSaveArgs.visOpenDocked);
_loadedStencils[stencilName] = stencil;
}
}
catch (Exception ex)
{
throw new Exception($"Failed to load stencil: {ex.Message}", ex);
}
}
public void AddShapeFromStencil(string stencilName, string masterName, double x, double y)
{
UpdateActiveDocumentAndPage();
if (_activePage == null)
{
throw new InvalidOperationException("No active page found.");
}
if (!_loadedStencils.TryGetValue(stencilName, out var stencil))
{
throw new ArgumentException($"Stencil '{stencilName}' not loaded.");
}
Visio.Master master = stencil.Masters[masterName];
if (master == null)
{
throw new ArgumentException($"Master '{masterName}' not found in the stencil '{stencilName}'.");
}
_activePage.Drop(master, x, y);
}
public List<string> GetLoadedStencils()
{
return new List<string>(_loadedStencils.Keys);
}
public List<string> GetMastersInStencil(string stencilName)
{
if (!_loadedStencils.TryGetValue(stencilName, out var stencil))
{
throw new ArgumentException($"Stencil '{stencilName}' not loaded.");
}
var masters = new List<string>();
foreach (Visio.Master master in stencil.Masters)
{
masters.Add(master.Name);
}
return masters;
}
public void AddShape(string masterName, double x, double y)
{
UpdateActiveDocumentAndPage();
if (_activePage == null)
{
throw new InvalidOperationException("No active page found.");
}
Visio.Master master = _activeDocument.Masters[masterName];
if (master == null)
{
throw new ArgumentException($"Master '{masterName}' not found in the active document.");
}
_activePage.Drop(master, x, y);
}
public void ConnectShapes(Visio.Shape shape1, Visio.Shape shape2)
{
UpdateActiveDocumentAndPage();
if (_activePage == null)
{
throw new InvalidOperationException("No active page found.");
}
Visio.Shape connector = _activePage.Drop(_activeDocument.Masters["Dynamic connector"], 0, 0);
connector.CellsU["BeginX"].GlueTo(shape1.CellsSRC[(short)Visio.VisSectionIndices.visSectionObject, (short)Visio.VisRowIndices.visRowXFormOut, (short)Visio.VisCellIndices.visXFormPinX]);
connector.CellsU["EndX"].GlueTo(shape2.CellsSRC[(short)Visio.VisSectionIndices.visSectionObject, (short)Visio.VisRowIndices.visRowXFormOut, (short)Visio.VisCellIndices.visXFormPinX]);
}
public void HighlightShape(Visio.Shape shape, System.Drawing.Color color)
{
shape.CellsSRC[(short)Visio.VisSectionIndices.visSectionObject, (short)Visio.VisRowIndices.visRowLine, (short)Visio.VisCellIndices.visLineColor].FormulaU = $"RGB({color.R},{color.G},{color.B})";
shape.CellsSRC[(short)Visio.VisSectionIndices.visSectionObject, (short)Visio.VisRowIndices.visRowLine, (short)Visio.VisCellIndices.visLineWeight].FormulaU = "2 pt";
}
public void ResetShapeHighlight(Visio.Shape shape)
{
shape.CellsSRC[(short)Visio.VisSectionIndices.visSectionObject, (short)Visio.VisRowIndices.visRowLine, (short)Visio.VisCellIndices.visLineColor].FormulaU = "Guard(0)";
shape.CellsSRC[(short)Visio.VisSectionIndices.visSectionObject, (short)Visio.VisRowIndices.visRowLine, (short)Visio.VisCellIndices.visLineWeight].FormulaU = "Guard(0)";
}
}
}