-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
321 lines (300 loc) · 12.9 KB
/
Copy pathProgram.cs
File metadata and controls
321 lines (300 loc) · 12.9 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Tekla.Structural.InteropAssemblies.Tedds;
namespace TedToPdf
{
/// <summary>
/// Ted to Pdf conversion options
/// </summary>
struct ConvertOptions
{
/// <summary>
/// Recursively process child directories
/// </summary>
public bool recursive;
/// <summary>
/// Overwrite output file if it already exists
/// </summary>
public bool? overwrite;
}
/// <summary>
/// Program wich converts Tekla Tedds document files (.ted) to Adobe PDF format (.pdf).
/// Output files are created at the same location as the source files.
/// </summary>
class Program
{
/// <summary>
/// Tedds document file extension
/// </summary>
public const string FileExtension_TeddsDocument = ".ted";
/// <summary>
/// Adobe PDF file extension
/// </summary>
public const string FileExtension_Pdf = ".pdf";
/// <summary>
/// Characters which are trimmed from file paths
/// </summary>
public static readonly char[] FilePathTrimChars = { ' ', '\'', '\"' };
/// <summary>
/// Main program
/// </summary>
/// <param name="args">Command line arguments.</param>
static void Main(string[] args)
{
ConvertOptions options = new ConvertOptions();
List<string> filePaths = new List<string>();
if (args.Length == 0)
{
if (!PromptForUsage(filePaths, ref options))
return;
}
else if (!ProcessCommandLine(args, filePaths, ref options))
{
ShowUsage();
return;
}
IApplication teddsApp = null;
try
{
//Connect to Tedds application
teddsApp = new Application();
#if DEBUG
teddsApp.Visible = true;
#endif
}
catch (COMException e)
{
Console.WriteLine($"Error attempting to start or connect to the Tedds Application\n", e.StackTrace);
return;
}
bool cancel = false;
//Process all file paths
foreach (string filePath in filePaths)
{
ConvertTedToPdf(teddsApp, filePath, ref options, ref cancel);
if (cancel)
break;
}
//Explicitely release reference to COM object, if App is visible it will remain open; otherwise the process will exit
Marshal.ReleaseComObject(teddsApp);
}
/// <summary>
/// Prompt user to enter path of file or folder to convert and to confirm options.
/// </summary>
/// <param name="filePaths">Returns the list of file paths to be processed.</param>
/// <param name="options">Returns to options to use for the conversion process.</param>
/// <returns>true is user entered required parameters; otherwise false.</returns>
public static bool PromptForUsage(List<string> filePaths, ref ConvertOptions options)
{
//Source file
string filePath;
do
{
//File or directory
Console.WriteLine("Enter path of a Tedds document file or a directory to convert");
filePath = Console.ReadLine().Trim(FilePathTrimChars);
}
while (!File.Exists(filePath) && !Directory.Exists(filePath));
filePaths.Add(filePath);
//Recursive
if (Directory.Exists(filePath))
{
do
{
Console.WriteLine("Do you want to convert all files in child directories?\n" +
"Y = Yes, N = No, ESC = Cancel");
ConsoleKey key = Console.ReadKey().Key;
Console.WriteLine();
switch (key)
{
case ConsoleKey.Y:
options.recursive = true;
return true;
case ConsoleKey.N:
options.recursive = false;
return true;
case ConsoleKey.Escape:
return false;
default:
break;
}
}
while (true);
}
return true;
}
/// <summary>
/// Process the command line arguments to initialise the list of files and folders to process and the options to use.
/// </summary>
/// <param name="args">Command line arguments.</param>
/// <param name="filePaths">Returns the list of file paths to be processed.</param>
/// <param name="options">Returns to options to use for the conversion process.</param>
/// <returns>true if the command line arguments were processed and are valid; otherwise false.</returns>
public static bool ProcessCommandLine(string[] args, List<string> filePaths, ref ConvertOptions options)
{
//Process each argument
foreach (string arg in args)
{
//Support '-' or '/' formatted command line options
switch (arg.TrimStart("-/".ToCharArray()).ToUpper())
{
//Recursive
case "R":
options.recursive = true;
break;
//Overwrite existing files
case "O":
options.overwrite = true;
break;
//Drive, path, filename
default:
filePaths.Add(arg.Trim(FilePathTrimChars));
break;
}
}
return (filePaths.Count > 0);
}
/// <summary>
/// Output application usage to the command line
/// </summary>
public static void ShowUsage()
{
Console.WriteLine(
$"Converts Tedds document files ({FileExtension_TeddsDocument}) to Adobe PDF ({FileExtension_Pdf}).\n\n" +
"TEDTOPDF [drive:][path][filename] [/R] [/O]\n\n" +
"[drive:][path][filename]\n" +
"\tSpecifies drive, directory, and/or files to convert\n" +
"/R\tIf path is a directory then recursively convert all files in child directories\n" +
"/O\tOverwrite existing files\n");
}
/// <summary>
/// Convert the specified Tedds document file or all files in the specified directory to PDF.
/// </summary>
/// <param name="teddsApp">Tedds application object.</param>
/// <param name="filePath">Full path of Tedds document file or a directory of files to convert.</param>
/// <param name="options">Conversion options.</param>
/// <param name="cancel">Returns true if user cancels operation.</param>
public static void ConvertTedToPdf(IApplication teddsApp, string filePath, ref ConvertOptions options, ref bool cancel)
{
if (Directory.Exists(filePath))
DirectoryConvertTedToPdf(teddsApp, filePath, ref options, ref cancel);
else
FileConvertTedToPdf(teddsApp, filePath, ref options, ref cancel);
}
/// <summary>
/// Convert all Tedds documents in the specified directory to PDF. If the recursive option is enabled then all process all child directories.
/// </summary>
/// <param name="teddsApp">Tedds application object.</param>
/// <param name="filePath">Full path of Tedds document file or a directory of files to convert.</param>
/// <param name="options">Conversion options.</param>
/// <param name="cancel">Returns true if user cancels operation.</param>
public static void DirectoryConvertTedToPdf(IApplication teddsApp, string filePath, ref ConvertOptions options, ref bool cancel)
{
//Path must be a directory
Debug.Assert(Directory.Exists(filePath));
//Process all Tedds document files in specified directory
foreach (string file in Directory.GetFiles(filePath, $"*{FileExtension_TeddsDocument}", new EnumerationOptions { RecurseSubdirectories = options.recursive }))
{
FileConvertTedToPdf(teddsApp, file, ref options, ref cancel);
if (cancel)
return;
}
}
/// <summary>
/// Prompt user to confirm whether an existing file shuld be overwritten.
/// </summary>
/// <param name="fileName">Name of file to overwrite.</param>
/// <param name="options">Convert options. MOdifies options.overwrite if user chooses Yes to All or No to All.</param>
/// <param name="cancel">Returns true if user cancels operation.</param>
/// <returns>true if file should be overwritten; otherwise false.</returns>
public static bool PromptToOverwrite(string fileName, ref ConvertOptions options, ref bool cancel)
{
do
{
Console.WriteLine($"\nWarning! '{fileName}' already exists.\n" +
"Do you want to continue and overwrite the existing file?\n" +
"Y = Yes, N = No, C = Cancel, A = Yes to All, O = No To All");
ConsoleKey key = Console.ReadKey().Key;
Console.WriteLine();
switch (key)
{
case ConsoleKey.C:
cancel = true;
return false;
case ConsoleKey.N:
return false;
case ConsoleKey.O:
options.overwrite = false;
return false;
case ConsoleKey.Y:
return true;
case ConsoleKey.A:
options.overwrite = true;
return true;
}
} while (true);
}
/// <summary>
/// Convert Tedds document file to PDF. Output file is created in the same location as the input file.
/// </summary>
/// <param name="teddsApp">Tedds application object.</param>
/// <param name="filePath">Full path of Tedds document file or a directory of files to convert.</param>
/// <param name="options">Conversion options.</param>
/// <param name="cancel">Returns true if user cancels operation.</param>
public static void FileConvertTedToPdf(IApplication teddsApp, string fileName, ref ConvertOptions options, ref bool cancel)
{
//Create output file in the same location as the input file but with the PDF file extension
string outputFileName = Path.ChangeExtension(fileName, FileExtension_Pdf);
//Verify whether output file already exists
if (File.Exists(outputFileName))
{
if (options.overwrite == false)
return;
if (options.overwrite != true && !PromptToOverwrite(outputFileName, ref options, ref cancel))
return;
}
bool closeDocument = false;
ITeddsDocument document = null;
ITeddsDocuments documents = null;
try
{
documents = teddsApp.Documents;
//Determine if file is already open
try { document = documents[fileName]; }
catch (COMException) { }
if (document == null)
{
document = documents.Open(fileName);
closeDocument = true;
}
if (document != null)
{
document.SaveAsPdf(outputFileName);
if (closeDocument)
{
document.Close();
//Closing the document explicitly destroys the object
//Do not try to release it at the end of this method
document = null;
}
}
Console.WriteLine($"Saved '{fileName}'\n as '{outputFileName}'");
}
catch (COMException e)
{
Console.WriteLine($"Error converting document '{fileName}'\n{e}");
}
finally
{
//Explicitely release references to COM objects
if (document != null)
Marshal.ReleaseComObject(document);
if (documents != null)
Marshal.ReleaseComObject(documents);
}
}
}
}