-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form_Legend_Renderer.cs
4676 lines (3761 loc) · 227 KB
/
Form_Legend_Renderer.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GSC_Legend_Renderer.Dictionaries;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using stdole;
using Newtonsoft.Json;
using System.IO;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Framework;
using System.Drawing.Imaging;
using System.Globalization;
namespace GSC_Legend_Renderer
{
public partial class Form_Legend_Renderer : Form
{
#region Main Variables
//Public variables
public string dataPath { get; set; }
public string dataExtension { get; set; }
public List<string> dataFieldList { get; set; } //Alias will be used inside this list
public ITable inputDataTable { get; set; }
public ITable inMemoryDataTable { get; set; }
List<IElement> legendElementList = new List<IElement>(); //Will hold all legend items to group them at the end of the process.
public IElement originalCGMLegend { get; set; } //Will be used to move legend right if a left bracket is found for CGM maps only.
public IMxDocument currentDoc { get; set; } //Wil be used to keep track of current document throughout methods
public Dictionary<int, double> arialCharactersWidth { get; set; } //Will be used to calculate text box height based on total lenght of characters
//Extensions
public const string gdbExt = Dictionaries.Constants.Extensions.gdbExt;
public const string mdbExt = Dictionaries.Constants.Extensions.mdbExt;
public const string xlExt = Dictionaries.Constants.Extensions.xlExt;
public const string txtExt = Dictionaries.Constants.Extensions.txtExt;
public const string csvExt = Dictionaries.Constants.Extensions.csvExt;
public const string dbfExt = Dictionaries.Constants.Extensions.dbfExt;
//Legend table
public const string fieldOrder = Dictionaries.Constants.LegendTable.legendOrderField;
public const string fieldColumn = Dictionaries.Constants.LegendTable.legendColumnField;
public const string fieldElement = Dictionaries.Constants.LegendTable.legendElementField;
public const string fieldStyle1 = Dictionaries.Constants.LegendTable.legendStyle1Field;
public const string fieldStyle2 = Dictionaries.Constants.LegendTable.legendStyle2Field;
public const string fieldLabel1 = Dictionaries.Constants.LegendTable.legendLabel1Field;
public const string fieldLabel1Style = Dictionaries.Constants.LegendTable.legendLabel1StyleField;
public const string fieldLabel2 = Dictionaries.Constants.LegendTable.legendLabel2Field;
public const string fieldLabel2Style = Dictionaries.Constants.LegendTable.legendLabel2StyleField;
public const string fieldHeading = Dictionaries.Constants.LegendTable.legendHeadingField;
public const string fieldDescription = Dictionaries.Constants.LegendTable.legendDescriptionField;
//UI
public List<CboxTables> _cboxlayers = new List<CboxTables>();
#endregion
#region PROPERTIES
//Graphics
public Dictionary<string, IElement> templateGraphicDico { get; set; }
//Fields
public string orderFieldUser { get; set; }
public string columnFieldUser { get; set; }
public string elementFieldUser { get; set; }
public string style1FieldUser { get; set; }
public string style2FieldUser { get; set; }
public string label1FieldUser { get; set; }
public string label1StyleFieldUser { get; set; }
public string label2FieldUser { get; set; }
public string label2StyleFieldUser { get; set; }
public string headingFieldUser { get; set; }
public string descrîptionFieldUser { get; set; }
//Symbols
public Dictionary<string, object> fillSymbolDico { get; set; } //Will hold symbol name (01.01.0l) and it's associate style object
public Dictionary<string, object> lineSymbolDico { get; set; } //Will hold symbol name (01.01.0l) and it's associate style object
public Dictionary<string, object> markerSymbolDico { get; set; } //Will hold symbol name (01.01.0l) and it's associate style object
public Dictionary<string, object> textSymbolDico { get; set; } //Will hold symbol name and style object
//Style
public IStyleGallery gscStyle { get; set; } //Whole arc map style gallery that holds all style files
public string gscStylePath { get; set; } //For search purposes inside style gallery
//JSON
public string jsonYSpacingFilePath { get; set; }
public string jsonXSpacingFilePath { get; set; }
public string jsonOtherFilePath { get; set; }
public Dictionary<string, Dictionary<string, string>> ySpacings { get; set; }
public Dictionary<string, string> xSpacings { get; set; }
public Dictionary<string, string> otherComponents { get; set; }
//OTHER
public double columnWidth { get; set; }
public double elementWidth { get; set; }
public double elementDescriptGapWidth { get; set; }
public double descriptionWidth { get; set; }
public double columnColumnGapWidth { get; set; }
public double smallDescriptionHeight { get; set; }
public double smallDescriptionHeightLine { get; set; }
public double groupDescriptionWidth { get; set; }
public List<string> heading5Text { get; set; } //Will be used to detect heading 5 elements, which will see their description made italic and indented of 10 points
public bool isCGMTemplateMXD { get; set; } //Will be used to prevent legend grouping in a CGM template to prevent weird behavior.
//UI
public class CboxTables
{
public string cboxDataName { get; set; }
public IStandaloneTable cboxSTLTable { get; set; } //This field can hold a layer object or will be set to null if a string path is available inside layer name
}
#endregion
#region INIT
public Form_Legend_Renderer()
{
InitializeComponent();
FillTableViewCombobox();
this.comboBox_SelectTable.SelectedIndexChanged += comboBox_SelectTable_SelectedIndexChanged;
//Init of dictionaries
templateGraphicDico = new Dictionary<string, IElement>();
ySpacings = new Dictionary<string, Dictionary<string, string>>();
xSpacings = new Dictionary<string, string>();
otherComponents = new Dictionary<string, string>();
}
#endregion
#region EVENTS
/// <summary>
/// Will be triggered when user selects a new table path or layer
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBox_SelectTable_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox senderBox = sender as ComboBox;
if (senderBox.SelectedIndex != -1)
{
//Init field list
InitFieldList();
//Fill the fields comboboxes
FillFieldsComboboxes();
}
}
/// <summary>
/// Will close the form
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_Cancel_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// Will initiate a graphic legend creation
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_Start_Click(object sender, EventArgs e)
{
//Process
CreateLegend();
}
/// <summary>
/// Will open a generic browse dialog in which the user can select the data that he wants to load inside the form
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_selectTable_Click(object sender, EventArgs e)
{
//Open dialog
dataPath = Services.Dialog.GetDataPrompt(this.Handle.ToInt32(), Properties.GSC_LegendRenderer_Resources.Dialog_SelectTableTitle);
//Unset list before change
this.comboBox_SelectTable.DataSource = null;
//Update
_cboxlayers.Add(new CboxTables { cboxDataName = dataPath, cboxSTLTable = null });
this.comboBox_SelectTable.DataSource = _cboxlayers;
this.comboBox_SelectTable.DisplayMember = "cboxDataName";
this.comboBox_SelectTable.ValueMember = "cboxSTLTable";
this.comboBox_SelectTable.SelectedIndex = _cboxlayers.Count - 1;
}
private void comboBox_orderField_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox_orderField.SelectedIndex != -1)
{
orderFieldUser = this.comboBox_orderField.SelectedItem.ToString();
}
}
private void comboBox_ColumnField_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox_ColumnField.SelectedIndex != -1)
{
columnFieldUser = this.comboBox_ColumnField.SelectedItem.ToString();
}
}
private void comboBox_ElementField_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox_ElementField.SelectedIndex != -1)
{
elementFieldUser = this.comboBox_ElementField.SelectedItem.ToString();
}
}
private void comboBox_Style1Field_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox_Style1Field.SelectedIndex != -1)
{
style1FieldUser = this.comboBox_Style1Field.SelectedItem.ToString();
}
}
private void comboBox_Style2Field_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox_Style2Field.SelectedIndex != -1)
{
style2FieldUser = this.comboBox_Style2Field.SelectedItem.ToString();
}
}
private void comboBox_Label1Field_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox_Label1Field.SelectedIndex != -1)
{
label1FieldUser = this.comboBox_Label1Field.SelectedItem.ToString();
}
}
private void comboBox_Label1StyleField_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox_Label1StyleField.SelectedIndex != -1)
{
label1StyleFieldUser = this.comboBox_Label1StyleField.SelectedItem.ToString();
}
}
private void comboBox_Label2Field_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox_Label2Field.SelectedIndex != -1)
{
label2FieldUser = this.comboBox_Label2Field.SelectedItem.ToString();
}
}
private void comboBox_Label2StyleField_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox_Label2StyleField.SelectedIndex != -1)
{
label2StyleFieldUser = this.comboBox_Label2StyleField.SelectedItem.ToString();
}
}
private void comboBox_HeadingField_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox_HeadingField.SelectedIndex != -1)
{
headingFieldUser = this.comboBox_HeadingField.SelectedItem.ToString();
}
}
private void comboBox_DescriptionField_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox_DescriptionField.SelectedIndex != -1)
{
descrîptionFieldUser = this.comboBox_DescriptionField.SelectedItem.ToString();
}
}
#endregion
#region METHODS
/// <summary>
/// Will retrieve a list of table fields depending on the data extension type
/// Will later be used to fill in the UI
/// </summary>
private void InitFieldList()
{
if (this.comboBox_SelectTable.SelectedIndex != -1)
{
CboxTables selectedlTables = this.comboBox_SelectTable.SelectedItem as CboxTables;
if (selectedlTables.cboxSTLTable != null)
{
#region FOR TABLE VIEWS IN TABLE OF CONTENT
IStandaloneTable selectedSTL = selectedlTables.cboxSTLTable;
//Fill the field list with field names
dataFieldList = Services.Tables.GetFieldList(selectedSTL.Table, true, selectedSTL);
#endregion
}
else if (selectedlTables.cboxDataName != null)
{
#region FOR EXTERNAL DATA
if (dataPath.Contains(gdbExt) || dataPath.Contains(mdbExt))
{
//Open as table
inputDataTable = Services.Tables.OpenTableFromStringFaster(dataPath);
//Fill the field list with field names
dataFieldList = Services.Tables.GetFieldList(inputDataTable, true);
}
else if (dataPath.Contains(dbfExt))
{
//Get the table name and extension only
string fileNameOnly = System.IO.Path.GetFileName(dataPath);
//Get the excel workspace factory
IWorkspace dbfWorkspace = Services.Workspace.AccessWorkspace(dataPath);
inputDataTable = Services.Tables.OpenTableFromWorkspace(dbfWorkspace, fileNameOnly);
//Fill the field list with field names
dataFieldList = Services.Tables.GetFieldList(inputDataTable, true);
//Release workspace so user can keep on editing
Services.ObjectManagement.ReleaseObject(dbfWorkspace);
}
else if (dataPath.Contains(txtExt) || dataPath.Contains(csvExt))
{
//Get the sheet name only
string fileNameOnly = System.IO.Path.GetFileName(dataPath);
//Get the excel workspace factory
IWorkspace txtFileWorkspace = Services.Workspace.AccessTextfileWorkspace(dataPath);
inputDataTable = Services.Tables.OpenTableFromWorkspace(txtFileWorkspace, fileNameOnly);
//Fill the field list with field names
dataFieldList = Services.Tables.GetFieldList(inputDataTable, true);
//Release workspace so user can keep on editing
Services.ObjectManagement.ReleaseObject(txtFileWorkspace);
}
else if (dataPath.Contains(xlExt))
{
//Get the sheet name
string[] splitedPath = dataPath.Split('\\');
//Build path to the file itself without the sheet
string dataPathFileOnly = string.Empty;
foreach (string parts in splitedPath)
{
if (parts != splitedPath[splitedPath.Length - 1])
{
if (dataPathFileOnly != string.Empty)
{
dataPathFileOnly = dataPathFileOnly + "\\" + parts;
}
else
{
dataPathFileOnly = parts;
}
}
}
//Get the sheet name only
string fileSheetName = splitedPath[splitedPath.Length - 1];
//Get the excel workspace factory
IWorkspace excelWorkspace = Services.Workspace.AccessExcelWorkspace(dataPathFileOnly);
inputDataTable = Services.Tables.OpenTableFromWorkspace(excelWorkspace, fileSheetName);
//Fill the field list with field names
dataFieldList = Services.Tables.GetFieldList(inputDataTable, true);
dataFieldList.Add(string.Empty);
//Release workspace so user can keep on editing
Services.ObjectManagement.ReleaseObject(inputDataTable);
Services.ObjectManagement.ReleaseObject(excelWorkspace);
}
#endregion
}
}
}
/// <summary>
/// Will create the legend.
/// </summary>
public void CreateLegend()
{
//Continu if table is valid
if (this.comboBox_SelectTable.Text != null && this.comboBox_SelectTable.Text != string.Empty)
{
//Get json config files - check properties for path
ValidateJsonFilesExistance();
BuildOtherComponentsDictionary();
//Validate if needed style has been loaded
if (ValidateStyleFile())
{
#region GET and BUILD information
//Get arial character widths
arialCharactersWidth = GetArialCharacterWidth();
//Set document units, else if it's not in mm the legend will be looking bad...
currentDoc = (IMxDocument)ArcMap.Application.Document;
esriUnits originalUnits = SetDocumentUnits(currentDoc, esriUnits.esriMillimeters);
//Force delay update
currentDoc.DelayUpdateContents = true;
//Variables
IElement parentElement = null; //Will be used to keep parent element that has embedded children
double originalYSpacing = 0;
double ySpacing = originalYSpacing;//Keep track of Y spacing
double xSpacing = 0; //Keep track of X spacing
bool firstIterationBreaker = true;
IElement lastElement = null;
string lastElementType = string.Empty;
int lastColumn = 1;
IElement waitingLeftBracket = null; //Will be used to move in Y axis an added left bracket that needs to know ySpacing for it's horizontal brother element.
IElement upLeftBracket = null; //Will be used to complete left bracket when end-point is reached
IElement waitingCenterLeftBracket = null; //Will be used to move bracket annotation when full bracket has been completed.
IElement annotationBracket = null; //Will be used to set text first and then move it.
IElement waitingRightBracket = null; //Will be used to move in XY axis an added right bracket
IElement upRightBracket = null; //Will be used to complete right bracket when end-point is reached.
IElement waitingCenterRightBracket = null; //Wil be used to move bracket associated map unit when full bracket has been completed
int howManyRightBrackets = 0; //Will be used to recalculate x spacing in case more columns are asked by user and that some right brackets are also found
Tuple<IElement, IElement, IElement, IElement> bracketMapUnit = new Tuple<IElement, IElement, IElement, IElement>(null, null, null, null); //Will be used to keep unit box for bracket and replace it at the right anchor when bracket is done drawing.
Tuple<double, double> anchorPoint = GetAnchorPointStart(); //TODO Find if mxd is a CGM one or not.
originalYSpacing = anchorPoint.Item2; //Synchronise with initial calculate anchor.
Tuple<double, double> anchorPointParent = new Tuple<double, double>(0, 0);
heading5Text = new List<string>(); //Init
double currentIteration = 0.0; //Will be used if user has forgot to enter an order.
bool nullOrderBreaker = false; //Will be used to show error message to user if null values are found, but only once.
//Get selection of graphic (will help doing a clear)
IPageLayout currentLayout = currentDoc.ActiveView as IPageLayout;
IGraphicsContainerSelect currentGrapSelection = currentLayout as IGraphicsContainerSelect;
//Get template graphics
GetTemplateGraphicList();
//Get legend table
ITable legendTable;
CboxTables selectedTable = this.comboBox_SelectTable.SelectedItem as CboxTables;
if (selectedTable.cboxSTLTable == null)
{
legendTable = Services.Tables.OpenTableFromString(selectedTable.cboxDataName);
}
else
{
legendTable = selectedTable.cboxSTLTable as ITable;
}
//Iterate through table and add elements
IQueryFilter ascendingOrderQuery = new QueryFilter();
IQueryFilterDefinition ascendingOrderQueryPostfix = ascendingOrderQuery as IQueryFilterDefinition;
ascendingOrderQueryPostfix.PostfixClause = "ORDER BY " + orderFieldUser;
ICursor legendCursor = legendTable.Search(ascendingOrderQuery, true);
//Get symbols
if (lineSymbolDico == null)
{
GetSymbols(Dictionaries.Constants.Styles.styleLineClass);
}
if (fillSymbolDico == null)
{
GetSymbols(Dictionaries.Constants.Styles.styleFillClass);
}
if (markerSymbolDico == null)
{
GetSymbols(Dictionaries.Constants.Styles.styleMarkerClass);
}
if (textSymbolDico == null)
{
GetSymbols(Dictionaries.Constants.Styles.styleTextClass);
}
//Get lower bound
double legendYLowerBound = GetCGMLegendLowerBound(Constants.YSpacings.legendEnd_Citation, Constants.Graphics.cgmCitation);
#endregion
#region CURSOR inside Legend Table
//Get fields indexes
int elementFieldIndex = legendCursor.FindField(elementFieldUser);
int orderFieldIndex = legendCursor.FindField(orderFieldUser);
int style1FieldIndex = legendCursor.FindField(style1FieldUser);
int style2FieldIndex = legendCursor.FindField(style2FieldUser);
int labelFieldIndex = legendCursor.FindField(label1FieldUser);
int descriptionFieldIndex = legendCursor.FindField(descrîptionFieldUser);
int headingFieldIndex = legendCursor.FindField(headingFieldUser);
int columnFieldIndex = legendCursor.FindField(columnFieldUser);
int label2FieldIndex = legendCursor.FindField(label2FieldUser);
int label1StyleFieldIndex = legendCursor.FindField(label1StyleFieldUser);
int label2StyleFieldIndex = legendCursor.FindField(label2StyleFieldUser);
int currentColumn = 1; //Default
IRow legendRow;
while ((legendRow = legendCursor.NextRow()) != null)
{
//Variables
currentIteration = currentIteration + 1.0;
double currentOrder = currentIteration;
Double.TryParse(legendRow.Value[orderFieldIndex].ToString(), out currentOrder);
string currentStyle1 = legendRow.Value[style1FieldIndex].ToString();
string currentStyle2 = legendRow.Value[style2FieldIndex].ToString();
string currentLabel1 = legendRow.Value[labelFieldIndex].ToString();
string currentLabel2 = legendRow.Value[label2FieldIndex].ToString();
string currentDescription = legendRow.Value[descriptionFieldIndex].ToString();
string currentHeading = legendRow.Value[headingFieldIndex].ToString();
string currentElement = legendRow.Value[elementFieldIndex].ToString();
string currentLabel1Style = legendRow.Value[label1StyleFieldIndex].ToString();
string currentLabel2Style = legendRow.Value[label2StyleFieldIndex].ToString();
//Clean and replace < characters from description
//Having <bol></bol> within description along an extra < symbol, breaks the bolding of the heading within the description
if (currentDescription != string.Empty && currentHeading != string.Empty && heading5Text.Count == 0)
{
currentDescription = currentDescription.Replace("<", "<");
}
//Get related graphic, if exists
IElement currentElementObject = null;
if (templateGraphicDico.ContainsKey(currentElement))
{
currentElementObject = Services.ObjectManagement.CopyInputObject(templateGraphicDico[currentElement]) as IElement;
}
//Manage null order
if (legendRow.Value[orderFieldIndex].ToString() == string.Empty || legendRow.Value[orderFieldIndex].ToString() == "<Null>" || legendRow.Value[orderFieldIndex] == null)
{
if (!nullOrderBreaker)
{
MessageBox.Show("Missing value found in " + Constants.LegendTable.legendOrderField + " field. This might cause some problems on item rendering. \n\nLook for:\n" +
currentElement + " " + currentHeading + " " + currentDescription); //TODO change this message for localized one and better text.
nullOrderBreaker = true;
}
}
//Set heading5 trigger for special style symbols
///Two case, either user repeats heading5 text in wanted embedded symbols, or
///uses the latest element named HEADING5_END without duplicating heading5 text in all symbols
if (heading5Text.Count > 0)
{
//Add any duplicate
if (heading5Text[0] == currentHeading)
{
heading5Text.Add(currentHeading);
}
//Detect suddent misrupt of heading 5 text in heading column
if (heading5Text[0] != currentHeading && heading5Text.Count > 1)
{
heading5Text = new List<string>(); //reinitialize
}
//Detect explicit use of a heading 5 end element
if (currentElement == Constants.Graphics.heading5_end)
{
heading5Text = new List<string>(); //reinitialize
}
}
//Get spacings dictionnary loaded up
if (!firstIterationBreaker && !currentElement.Contains(Constants.Graphics.keywordBracket))
{
ySpacing = GetYSpacing(lastElement, lastElementType, currentElement, anchorPoint.Item2);
xSpacing = GetXSpacing(currentElement);
}
else
{
BuildYSpacingsDictionary();
BuildXSpacingsDictionary();
//Widths
if (xSpacings != null)
{
columnWidth = GetXSpacing(Constants.Graphics.columnWidth);
elementWidth = GetXSpacing(Constants.Graphics.elementWidth);
elementDescriptGapWidth = GetXSpacing(Constants.Graphics.elementDescriptionGapWidth);
descriptionWidth = GetXSpacing(Constants.Graphics.descriptionWidth);
columnColumnGapWidth = GetXSpacing(Constants.Graphics.columnColumnGapWidth);
smallDescriptionHeight = Constants.YSpacings.smallDescriptionHeightLimit;
smallDescriptionHeightLine = Constants.YSpacings.smallDescriptionHeightLimitLines;
groupDescriptionWidth = GetXSpacing(Constants.Graphics.groupDescriptionWidth);
xSpacing = GetXSpacing(currentElement);
}
firstIterationBreaker = false;
}
//Manage columns
if (!this.checkBox_autoCalculateColumns.Checked)
{
//Track column number change in table
if (int.TryParse(legendRow.Value[columnFieldIndex].ToString(), out currentColumn))
{
currentColumn = Convert.ToInt32(legendRow.Value[columnFieldIndex]);
}
}
else
{
//Track column change with auto-calculate
if (legendYLowerBound != 0.0)
{
if ((anchorPoint.Item2 - ySpacing - currentElementObject.Geometry.Envelope.Height) < legendYLowerBound)
{
currentColumn++;
}
}
}
if (currentColumn > 1 && lastColumn != currentColumn)
{
//Get x spacing based on how many brackets were found in previous column
double rightBracketSpacing = 0;
if (howManyRightBrackets > 0)
{
rightBracketSpacing = (descriptionWidth + elementDescriptGapWidth + elementWidth + GetXSpacing(Constants.Graphics.bracketRightCenter) + GetXSpacing(Constants.Graphics.unitBoxBracket));
}
//Move to right and reset Y.
ySpacing = 0; //Reset y spacing so it appears at the top of the page
anchorPoint = new Tuple<double, double>(anchorPoint.Item1 + columnWidth + columnColumnGapWidth + rightBracketSpacing, originalYSpacing);
//Adjust anchorpoint in case current element as an inner centered y anchor (CC, CL and CR)
if (templateGraphicDico.ContainsKey(currentElement))
{
IElement newColumnFirstElement = Services.ObjectManagement.CopyInputObject(templateGraphicDico[currentElement]) as IElement;
//Get anchor type
IElementProperties3 newColumnProp = newColumnFirstElement as IElementProperties3;
esriAnchorPointEnum currentAnchorPointType = newColumnProp.AnchorPoint;
if (currentAnchorPointType == esriAnchorPointEnum.esriCenterPoint || currentAnchorPointType == esriAnchorPointEnum.esriLeftMidPoint || currentAnchorPointType == esriAnchorPointEnum.esriRightMidPoint)
{
ySpacing = (newColumnFirstElement.Geometry.Envelope.Height / 2.0);
}
}
lastColumn = currentColumn;
//Reset right bracket number
howManyRightBrackets = 0;
}
#region HEADINGS
if (currentElement.Contains(Constants.Graphics.heading1.Substring(0, 6)))
{
//Get appropriate element
if (templateGraphicDico.ContainsKey(currentElement))
{
IElement headElement = Services.ObjectManagement.CopyInputObject(templateGraphicDico[currentElement]) as IElement;
IElementProperties headElProp = headElement as IElementProperties;
string originalElementName = headElProp.Name;
//TODO special cases heading 3 like description, heading 4 UL
#region Move to right anchor
//Set new anchor
anchorPoint = new Tuple<double, double>(anchorPoint.Item1, anchorPoint.Item2 - ySpacing);
//Set height for heading3
if (currentElement.Contains(Constants.Graphics.heading3))
{
//Recalculate height
string tempGroupHeadingDescription = currentHeading;
if (currentDescription != null)
{
tempGroupHeadingDescription = currentHeading + currentDescription;
}
double heading3Height = GetTextHeight(tempGroupHeadingDescription, descriptionWidth, Constants.TextConfiguration.lineHeight);
//Set new envelope
SetRectnagularPolygonFromAnchorTypeAndHeight(headElement, anchorPoint, heading3Height);
}
else
{
//Set new envelope
SetRectangularPolygonFromAnchorType(headElement, anchorPoint);
}
//Move
ITransform2D transElement = headElement as ITransform2D;
transElement.Move(xSpacing, 0); //Move accordingly to x spacing if any
#endregion
//Rename
headElProp.Name = headElProp.Name + currentOrder.ToString();
//Add Heading text
ITextElement tElement = headElement as ITextElement;
if (currentHeading == null || currentHeading == string.Empty || currentHeading == " ")
{
currentHeading = Constants.TextConfiguration.missingText;
tElement.Symbol = Services.Symbols.GetMissingTextSymbol(tElement.Symbol);
}
//Special case for heading 3 since we can't have bolded all caps setting inside a graphic along
//no cap and not bolded description.
if (currentElement.Contains(Constants.Graphics.heading3))
{
currentHeading = Constants.TextConfiguration.tagAllCaps + Constants.TextConfiguration.tagBold + currentHeading + Constants.TextConfiguration.endTagBold + Constants.TextConfiguration.endTagAllCaps + " ";
//Add Description to text - Only for heading 3 in theory
if (!IsTextEmpty(currentDescription))
{
//Add header if needed
if (!IsTextEmpty(currentHeading))
{
currentHeading = currentHeading + currentDescription;
}
}
}
if (currentElement.Contains(Constants.Graphics.heading5))
{
//Keep heading text so it can be used for a trigger to modify description style for heading 5 only.
heading5Text.Add(currentHeading);
}
tElement.Text = currentHeading;
//Manage style if needed
if (currentStyle1 != "")
{
if (textSymbolDico.ContainsKey(currentStyle1))
{
ISimpleTextSymbol inStyleSymbol = textSymbolDico[currentStyle1] as ISimpleTextSymbol;
ISimpleTextSymbol currentStyleSymbol = tElement.Symbol as ISimpleTextSymbol;
currentStyleSymbol.Font = inStyleSymbol.Font;
currentStyleSymbol.Color = inStyleSymbol.Color;
currentStyleSymbol.Size = currentStyleSymbol.Size; //Force size else incoming style might be too big.
currentStyleSymbol.VerticalAlignment = currentStyleSymbol.VerticalAlignment; //Force vertical center for text else incoming style might be set to else where.
tElement.Symbol = Services.ObjectManagement.CopyInputObject(currentStyleSymbol) as ISimpleTextSymbol;
}
else
{
//Missing or wrong style
tElement.Symbol = Services.Symbols.GetMissingTextSymbol(tElement.Symbol);
}
}
//Add base element
currentDoc.ActiveView.GraphicsContainer.AddElement(headElement, 0);
currentDoc.ActiveView.GraphicsContainer.BringToFront(currentGrapSelection.SelectedElements);
//Unselect
currentGrapSelection.UnselectElement(headElement);
//Add to legend list
legendElementList.Add(headElement);
//Keep name
lastElement = headElement;
lastElementType = originalElementName;
}
}
#endregion
#region MAP UNITS
if (currentElement == Constants.Graphics.unitBox || currentElement == Constants.Graphics.unitSplit ||
currentElement == Constants.Graphics.unitindent1 || currentElement == Constants.Graphics.unitindent2)
{
//Get appropriate element
IElement unitBoxElement = Services.ObjectManagement.CopyInputObject(templateGraphicDico[currentElement]) as IElement;
IElementProperties unitBoxElProp = unitBoxElement as IElementProperties;
currentDoc.ActiveView.GraphicsContainer.AddElement(unitBoxElement as IElement, 0);
string originalElementName = unitBoxElProp.Name;
//Init empty dem element if ever needed
IElement demUnitBoxElement = null;
#region Move to right anchor
//Set new anchor
anchorPoint = new Tuple<double, double>(anchorPoint.Item1, anchorPoint.Item2 - ySpacing); //New anchor point with proper move inside it
SetRectangularPolygonFromAnchorType(unitBoxElement, anchorPoint);
//Move
if (currentElement == Constants.Graphics.unitindent1 || currentElement == Constants.Graphics.unitindent2)
{
ITransform2D transElement = unitBoxElement as ITransform2D;
transElement.Move(xSpacing, 0); //Move accordingly to x spacing if any
}
#endregion
//Rename
unitBoxElProp.Name = unitBoxElProp.Name + currentOrder.ToString();
//Symbolize
IElement unitBoxLabelElement = new MarkerElement();
IGroupElement inGroupElement = unitBoxElement as IGroupElement;
//Unselect
currentGrapSelection.UnselectElement(unitBoxElement);
if (inGroupElement != null)
{
//Check geometry of inner elements, if it's all lines
for (int el = 0; el < inGroupElement.ElementCount; el++)
{
IElement innerElement = inGroupElement.Element[el];
if (el == 0)
{
SetPolygonFill(innerElement, currentStyle1, true);
//Add label
if (currentLabel1 == null || currentLabel1 == string.Empty || currentLabel1 == " ")
{
currentLabel1 = Constants.TextConfiguration.missingText;
}
unitBoxLabelElement = AddLabelInUnitBox(currentLabel1, innerElement, currentDoc, anchorPoint, Constants.Graphics.UnitBoxType.split1, currentLabel1Style);
}
else if (el > 0)
{
SetPolygonFill(innerElement, currentStyle2, true);
//Add label
if (currentLabel2 == null || currentLabel2 == string.Empty || currentLabel2 == " ")
{
currentLabel2 = Constants.TextConfiguration.missingText;
}
unitBoxLabelElement = AddLabelInUnitBox(currentLabel2, innerElement, currentDoc, anchorPoint, Constants.Graphics.UnitBoxType.split2, currentLabel2Style);
}
}
}
else
{
//Symbolize
demUnitBoxElement = SetPolygonFill(unitBoxElement, currentStyle1, true, true, anchorPoint, currentStyle2);
//Add
currentDoc.ActiveView.GraphicsContainer.AddElement(demUnitBoxElement as IElement, 0);
//Add label
if (currentLabel1 == null || currentLabel1 == string.Empty || currentLabel1 == " ")
{
currentLabel1 = Constants.TextConfiguration.missingText;
}
unitBoxLabelElement = AddLabelInUnitBox(currentLabel1, unitBoxElement, currentDoc, anchorPoint, Constants.Graphics.UnitBoxType.normal, currentLabel1Style);
}
//Move label and/or dem
if (currentElement == Constants.Graphics.unitindent1 || currentElement == Constants.Graphics.unitindent2)
{
//DEM
if (this.checkBox_DEMBoxes.Checked)
{
ITransform2D transDEMElement = demUnitBoxElement as ITransform2D;
transDEMElement.Move(xSpacing, 0); //Move accordingly to x spacing if any
}
//LABEL
ITransform2D transLabelElement = unitBoxLabelElement as ITransform2D;
transLabelElement.Move(xSpacing, 0); //Move accordingly to x spacing if any
}
//Keep name
lastElement = unitBoxElement;
lastElementType = originalElementName;
//Add header if needed
if (currentHeading != null && currentHeading != string.Empty && currentHeading != " ")
{
currentDescription = Constants.TextConfiguration.tagBold + currentHeading + Constants.TextConfiguration.endTagBold + " " + currentDescription;
}
//Add Description
IElement newDescriptionElement = AddDescription(currentDescription, unitBoxElement, currentDoc, anchorPoint, originalElementName);
double descriptionHeight = newDescriptionElement.Geometry.Envelope.Height;
if (descriptionHeight > smallDescriptionHeight)
{
//Reset anchor point for next element
if (currentColumn != 0)
{
anchorPoint = new Tuple<double, double>(anchorPoint.Item1, anchorPoint.Item2 - descriptionHeight); //New anchor point with proper move inside it
}
//Keep name
lastElement = newDescriptionElement;
lastElementType = Constants.Graphics.description;
}
//Move description
if (currentElement == Constants.Graphics.unitindent1 || currentElement == Constants.Graphics.unitindent2)
{
ITransform2D transDescElement = newDescriptionElement as ITransform2D;
transDescElement.Move(xSpacing, 0); //Move accordingly to x spacing if any
}
//Keep element if for bracket
if (currentColumn == 0)
{
bracketMapUnit = new Tuple<IElement, IElement, IElement, IElement>(unitBoxElement, unitBoxLabelElement, newDescriptionElement, demUnitBoxElement);
//Reset anchor point
anchorPoint = new Tuple<double, double>(anchorPoint.Item1, anchorPoint.Item2 + ySpacing);
}
//Add to legend list
if (demUnitBoxElement != null)
{
legendElementList.Add(demUnitBoxElement as IElement);
}
legendElementList.Add(unitBoxLabelElement as IElement);
legendElementList.Add(unitBoxElement as IElement);
}
#endregion
#region THIN UNITS (MAP UNIT LINE)
if (currentElement == Constants.Graphics.unitLine)
{
//Get appropriate element
IElement thinUnitElement = Services.ObjectManagement.CopyInputObject(templateGraphicDico[currentElement]) as IElement;