Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
256 lines (224 loc) · 9.65 KB
/
Copy pathProgram.cs
File metadata and controls
256 lines (224 loc) · 9.65 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
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Diagnostics;
usingSystem.IO;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.ComponentModel.Composition;
usingDB.Extensibility.Contracts;
usingDB.Api;
namespaceDBSaveTableToFileExample
{
[Export(typeof(IPlugin2))]
publicclassExamplePlugin:PluginBase2,IPlugin2
{
classMenuKeys
{
publicconststringRoot="root";
publicconststringListTables="listTables";
publicconststringSaveTable="saveTable";
publicconststringSaveAllTables="saveAllTables";
}
classMenuItem
{
publicActionAction{get;set;}
publicboolIsEnabled{get;set;}
publicboolIsVisible{get;set;}
publicMenuItem(
Actionaction=null,
boolenabled=true,
boolvisible=true)
{
Action=action??delegate{};
IsEnabled=enabled;
IsVisible=visible;
}
}
privatereadonlyDictionary<string,MenuItem>mMenuItems=newDictionary<string,MenuItem>();
publicoverrideboolHasMenu
{
get{returntrue;}
}
publicoverridestringMenuLayout
{
get
{
StringBuildermenu=newStringBuilder();
menu.AppendFormat("*Table Export,{0}",MenuKeys.Root);
menu.AppendFormat("*>List Available Tables,{0}",MenuKeys.ListTables);
menu.AppendFormat("*>Save Table To File...,{0}",MenuKeys.SaveTable);
menu.AppendFormat("*>Save All Tables To Folder...,{0}",MenuKeys.SaveAllTables);
returnmenu.ToString();
}
}
publicoverrideboolIsMenuItemVisible(stringkey)
{
returnmMenuItems[key].IsVisible;
}
publicoverrideboolIsMenuItemEnabled(stringkey)
{
returnmMenuItems[key].IsEnabled;
}
publicoverridevoidOnMenuItemPressed(stringkey)
{
mMenuItems[key].Action();
}
publicoverridevoidCreate()
{
mMenuItems.Add(MenuKeys.Root,newMenuItem());
// TableOfTables lives on ApplicationTemplates, so listing tables doesn't need a model loaded.
mMenuItems.Add(MenuKeys.ListTables,newMenuItem(ListAvailableTables));
mMenuItems.Add(MenuKeys.SaveTable,newMenuItem(SaveTableToFile));
mMenuItems.Add(MenuKeys.SaveAllTables,newMenuItem(SaveAllTablesToFolder));
}
/// <summary>
/// TableOfTables is the master table listing every table DesignBuilder knows about (component
/// databases, template databases, application-only data, etc.) - see Data\TableOfTables.dat in
/// the DesignBuilder install. Each record's "Name" field is the string you'd pass to GetTable.
/// </summary>
privateList<KeyValuePair<string,string>>GetAvailableTableNames()
{
TabletableOfTables=ApiEnvironment.ApplicationTemplates.GetTable("TableOfTables");
List<KeyValuePair<string,string>>tables=newList<KeyValuePair<string,string>>();
foreach(RecordrecordintableOfTables.Records)
{
tables.Add(newKeyValuePair<string,string>(record["Name"],record["Title"]));
}
returntables;
}
publicvoidListAvailableTables()
{
StringBuilderreport=newStringBuilder();
report.AppendLine("Available tables");
report.AppendLine("-----------------");
report.AppendLine();
foreach(KeyValuePair<string,string>tableinGetAvailableTableNames())
{
report.AppendFormat("{0,-40} {1}{2}",table.Key,table.Value,System.Environment.NewLine);
}
SaveFileDialogsaveFileDialog=newSaveFileDialog
{
Filter="Text files (*.txt)|*.txt|All files (*.*)|*.*",
FileName="AvailableTables.txt",
InitialDirectory=System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)
};
if(saveFileDialog.ShowDialog()==DialogResult.OK)
{
File.WriteAllText(saveFileDialog.FileName,report.ToString());
if(MessageBox.Show(@"List generated successfully. Would you like to open it?",@"List Complete",
MessageBoxButtons.YesNo)==DialogResult.Yes)
{
Process.Start(saveFileDialog.FileName);
}
}
}
/// <summary>
/// Lets the user pick a table name from a filterable list (rather than type one, which is
/// easy to misspell), resolves it to a Table (trying the model's Site tables first, then the
/// two library/template scopes that work without a model loaded), and writes it out via the
/// DB API's own Table.SaveToFile.
/// </summary>
publicvoidSaveTableToFile()
{
frmTablePickerpicker=newfrmTablePicker(GetAvailableTableNames());
if(!picker.ShowMe(outstringtableName))
{
return;
}
Tabletable=GetTableByName(tableName);
if(table==null)
{
MessageBox.Show(string.Format(
"Table '{0}' was not found. It may require a model to be loaded, or the name may be misspelled.",
tableName));
return;
}
SaveFileDialogsaveFileDialog=newSaveFileDialog
{
Filter="DesignBuilder table files (*.dat)|*.dat|All files (*.*)|*.*",
FileName=tableName+".dat",
InitialDirectory=System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)
};
if(saveFileDialog.ShowDialog()==DialogResult.OK)
{
boolsaved=table.SaveToFile(saveFileDialog.FileName);
MessageBox.Show(saved
?string.Format("Table '{0}' saved to {1}",tableName,saveFileDialog.FileName)
:string.Format("Failed to save table '{0}'.",tableName));
}
}
/// <summary>
/// Saves every table listed in TableOfTables into a chosen folder in one go, one .dat file
/// per table (named after the table). Tables that can't be resolved in the current context
/// (e.g. Site-scope component tables when no model is loaded) are skipped rather than failing
/// the whole batch, and are called out in the summary so nothing silently goes missing.
/// </summary>
publicvoidSaveAllTablesToFolder()
{
using(FolderBrowserDialogfolderDialog=newFolderBrowserDialog
{
Description="Select a folder to save all tables into"
})
{
if(folderDialog.ShowDialog()!=DialogResult.OK)
{
return;
}
List<string>skipped=newList<string>();
List<string>failed=newList<string>();
intsavedCount=0;
foreach(KeyValuePair<string,string>tableInfoinGetAvailableTableNames())
{
stringtableName=tableInfo.Key;
Tabletable=GetTableByName(tableName);
if(table==null)
{
skipped.Add(tableName);
continue;
}
stringdestPath=Path.Combine(folderDialog.SelectedPath,tableName+".dat");
if(table.SaveToFile(destPath))
{
savedCount++;
}
else
{
failed.Add(tableName);
}
}
StringBuildersummary=newStringBuilder();
summary.AppendFormat("Saved {0} table(s) to {1}",savedCount,folderDialog.SelectedPath);
if(skipped.Count>0)
{
summary.AppendFormat("{0}{1} skipped (not available without a model / in this context): {2}",
System.Environment.NewLine,skipped.Count,string.Join(", ",skipped));
}
if(failed.Count>0)
{
summary.AppendFormat("{0}{1} failed to save: {2}",
System.Environment.NewLine,failed.Count,string.Join(", ",failed));
}
MessageBox.Show(summary.ToString(),"Save All Tables Complete");
}
}
/// <summary>
/// Tables live in one of three scopes depending on type (see the "Tables" section of the
/// DesignBuilder extensibility guide): component tables on the loaded model's Site, template
/// tables on ApplicationTemplates, and library component tables on ApplicationComponents.
/// </summary>
privateTableGetTableByName(stringtableName)
{
Tabletable=ApiEnvironment.Site.GetTable(tableName);
if(table!=null)
{
returntable;
}
table=ApiEnvironment.ApplicationTemplates.GetTable(tableName);
if(table!=null)
{
returntable;
}
returnApiEnvironment.ApplicationComponents.GetTable(tableName);
}
}
}