-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
277 lines (236 loc) · 8.41 KB
/
Copy pathProgram.cs
File metadata and controls
277 lines (236 loc) · 8.41 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Office.Interop.Visio;
using VisioApp = Microsoft.Office.Interop.Visio.Application;
namespace VisioAutomation
{
public class Program
{
[STAThread]
public static void Main(string[] args)
{
string title = null;
if (args != null && args.Length > 0)
{
title = args[0];
if (title != null) title = title.Trim();
if (title != null && title.Length == 0) title = null;
}
string scriptText;
try
{
scriptText = Clipboard.GetText();
}
catch (Exception ex)
{
Console.WriteLine("ERROR: Unable to read clipboard text. " + ex.Message);
return;
}
if (scriptText == null) scriptText = "";
scriptText = scriptText.Trim();
if (scriptText.Length == 0)
{
Console.WriteLine("ERROR: Clipboard is empty. Copy your chart script to the clipboard and try again.");
return;
}
VisioApp app;
try
{
app = GetOrCreateVisio();
}
catch (Exception ex)
{
Console.WriteLine("ERROR: Unable to start/connect to Visio. " + ex.Message);
return;
}
app.Visible = true;
Document doc = null;
Page page = null;
try
{
if (app.Documents != null && app.Documents.Count > 0)
{
doc = app.ActiveDocument;
}
else
{
if (title == null)
{
Console.WriteLine("ERROR: No Visio document is open, and no title argument was provided to create a new one.");
return;
}
doc = app.Documents.Add("");
try
{
doc.Title = title;
}
catch
{
// shield: Some Visio/interop setups may not allow setting Title; ignore and continue.
}
}
page = app.ActivePage ?? (doc.Pages.Count > 0 ? doc.Pages[1] : doc.Pages.Add());
}
catch (Exception ex)
{
Console.WriteLine("ERROR: Unable to get/create document/page. " + ex.Message);
return;
}
Document stencil;
try
{
stencil = OpenAnyBasicFlowchartStencil(app);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: Could not open a Basic Flowchart stencil. " + ex.Message);
return;
}
Master startEndMaster;
Master processMaster;
object dynamicConnector;
try
{
startEndMaster = TryGetMaster(stencil, "Start/End", "Start-End", "Terminator");
processMaster = TryGetMaster(stencil, "Process", "Process (rectangle)");
dynamicConnector = app.ConnectorToolDataObject;
}
catch (Exception ex)
{
Console.WriteLine("ERROR: Could not access required masters/connectors. " + ex.Message);
return;
}
try
{
BuildForkableChartFromClipboard(page, scriptText, startEndMaster, processMaster, dynamicConnector);
Console.WriteLine("Done. Chart created in the active document. Visio left open.");
}
catch (Exception ex)
{
Console.WriteLine("ERROR: Failed while building the chart. " + ex.Message);
return;
}
}
private static VisioApp GetOrCreateVisio()
{
try
{
return (VisioApp)Marshal.GetActiveObject("Visio.Application");
}
catch
{
return new VisioApp();
}
}
private static Document OpenAnyBasicFlowchartStencil(VisioApp app)
{
// shield: Try common stencil names; Visio resolves installed stencils by name.
// shield: This avoids hardcoding a filesystem path.
var candidates = new[]
{
"BASFLO_U.VSSX",
"BASFLO_U.VSS",
"Basic Flowchart Shapes.vssx",
"Basic Flowchart Shapes.vss",
"Basic Flowchart Shapes (US units).vssx",
"Basic Flowchart Shapes (US units).vss"
};
foreach (var name in candidates)
{
try
{
return app.Documents.OpenEx(name, (short)(VisOpenSaveArgs.visOpenDocked | VisOpenSaveArgs.visOpenRO));
}
catch
{
}
}
throw new Exception("None of the known Basic Flowchart stencil names could be opened on this machine.");
}
private static Master TryGetMaster(Document stencil, params string[] names)
{
foreach (var n in names)
{
try
{
return stencil.Masters[n];
}
catch
{
}
}
throw new Exception("Master not found. Tried: " + string.Join(", ", names));
}
private static void BuildForkableChartFromClipboard(
Page page,
string scriptText,
Master startEndMaster,
Master processMaster,
object dynamicConnector)
{
var lines = scriptText.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');
var chains = new List<List<string>>();
foreach (var raw in lines)
{
var line = raw.Trim();
if (line.Length == 0) continue;
var tokens = ParseChain(line);
if (tokens.Count > 0) chains.Add(tokens);
}
if (chains.Count == 0)
throw new Exception("No valid lines found.");
var nodes = new Dictionary<string, Shape>(StringComparer.OrdinalIgnoreCase);
double startX = 2.0;
double startY = 10.0;
double colStep = 3.5;
double rowStep = 1.5;
for (int i = 0; i < chains.Count; i++)
{
var tokens = chains[i];
double x = startX + (i * colStep);
double y = startY;
Shape prev = null;
for (int t = 0; t < tokens.Count; t++)
{
var label = tokens[t];
Shape current;
if (!nodes.TryGetValue(label, out current))
{
Master masterToUse = processMaster;
if (IsStartOrEnd(label))
masterToUse = startEndMaster;
current = page.Drop(masterToUse, x, y);
current.Text = label;
nodes[label] = current;
}
if (prev != null)
{
Shape connector = page.Drop(dynamicConnector, 0, 0);
connector.CellsU["BeginX"].GlueTo(prev.CellsU["PinX"]);
connector.CellsU["EndX"].GlueTo(current.CellsU["PinX"]);
}
prev = current;
y -= rowStep;
}
}
}
private static List<string> ParseChain(string line)
{
var parts = line.Split(new[] { "->" }, StringSplitOptions.RemoveEmptyEntries);
var tokens = new List<string>();
foreach (var p in parts)
{
var t = p.Trim();
if (t.Length > 0) tokens.Add(t);
}
return tokens;
}
private static bool IsStartOrEnd(string label)
{
var v = label.Trim().ToLowerInvariant();
return v == "start" || v == "end";
}
}
}