-
Notifications
You must be signed in to change notification settings - Fork 64
/
CtlConcept.cs
373 lines (330 loc) · 12.8 KB
/
CtlConcept.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
// Name: CtlConcept.cs
// Description: Concept diagram editor
// Author: Tim Chipman
// Origination: Work performed for BuildingSmart by Constructivity.com LLC.
// Copyright: (c) 2013 BuildingSmart International Ltd.
// License: http://www.buildingsmart-tech.org/legal
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IfcDoc.Format.PNG;
using IfcDoc.Schema.DOC;
namespace IfcDoc
{
public partial class CtlConcept : ScrollableControl
{
Image m_image;
DocProject m_project;
DocTemplateDefinition m_template;
DocConceptRoot m_conceptroot;
DocAttribute m_attribute;
DocModelRule m_selection;
DocModelRule m_highlight;
int m_iSelection; // index of attribute within selection, or -1 if entity
int m_iHighlight;
Rectangle m_rcSelection;
Rectangle m_rcHighlight;
Dictionary<string, DocObject> m_map;
Dictionary<Rectangle, DocModelRule> m_hitmap;
public event EventHandler SelectionChanged;
public CtlConcept()
{
InitializeComponent();
this.DoubleBuffered = true;
this.ResizeRedraw = true;
this.AutoScroll = true;
}
public Dictionary<string, DocObject> Map
{
get
{
return this.m_map;
}
set
{
this.m_map = value;
}
}
public DocProject Project
{
get
{
return this.m_project;
}
set
{
this.m_project = value;
this.m_rcSelection = Rectangle.Empty;
this.m_rcHighlight = Rectangle.Empty;
}
}
public DocTemplateDefinition Template
{
get
{
return this.m_template;
}
set
{
this.m_template = value;
this.m_hitmap = new Dictionary<Rectangle, DocModelRule>();
Redraw();
}
}
public DocConceptRoot ConceptRoot
{
get
{
return this.m_conceptroot;
}
set
{
this.m_conceptroot = value;
this.m_hitmap = new Dictionary<Rectangle, DocModelRule>();
this.Redraw();
}
}
public DocModelRule Selection
{
get
{
return this.m_selection;
}
set
{
if (this.m_selection == value)
return;
this.m_selection = value;
this.m_attribute = null;
// determine rectangle
foreach(Rectangle rc in this.m_hitmap.Keys)
{
DocModelRule mr = this.m_hitmap[rc];
if(mr == value)
{
this.m_rcSelection = rc;
this.m_iSelection = -1;
break;
}
else if(mr is DocModelRuleEntity && mr.Rules != null && mr.Rules.Contains(value))
{
this.m_rcSelection = rc;
this.m_iSelection = -1;
DocEntity docEntity = this.m_map[mr.Name] as DocEntity;
if (docEntity != null)
{
List<DocAttribute> listAttr = new List<DocAttribute>();
FormatPNG.BuildAttributeList(docEntity, listAttr, this.m_map);
for (int i = 0; i < listAttr.Count; i++)
{
DocAttribute docAttr = listAttr[i];
if (docAttr.Name.Equals(value.Name))
{
this.m_attribute = docAttr;
this.m_iSelection = i;
break;
}
}
}
break;
}
else if(mr == null && this.m_template != null && this.m_template.Rules.Contains(value))
{
this.m_rcSelection = rc;
this.m_iSelection = -1;
DocEntity docEntity = this.m_map[this.m_template.Type] as DocEntity;
List<DocAttribute> listAttr = new List<DocAttribute>();
FormatPNG.BuildAttributeList(docEntity, listAttr, this.m_map);
for (int i = 0; i < listAttr.Count; i++)
{
DocAttribute docAttr = listAttr[i];
if (docAttr.Name.Equals(value.Name))
{
this.m_attribute = docAttr;
this.m_iSelection = i;
break;
}
}
}
}
this.Invalidate();
if(this.SelectionChanged != null)
{
this.SelectionChanged(this, EventArgs.Empty);
}
}
}
public DocAttribute CurrentAttribute
{
get
{
return this.m_attribute;
}
}
public void Redraw()
{
if(this.m_image != null)
{
this.m_image.Dispose();
this.m_image = null;
}
this.m_rcSelection = Rectangle.Empty;
this.m_rcHighlight = Rectangle.Empty;
this.m_hitmap.Clear();
if (this.m_template != null && this.m_project != null && this.m_map != null)
{
this.m_image = FormatPNG.CreateTemplateDiagram(this.m_template, this.m_map, this.m_hitmap, this.m_project);
if (this.m_image != null)
{
this.AutoScrollMinSize = new Size(this.m_image.Width, this.m_image.Height);
}
}
else if (this.m_conceptroot != null && this.m_project != null && this.m_map != null && this.m_conceptroot.ApplicableEntity != null)
{
DocModelView docView = null;
foreach (DocModelView eachView in this.m_project.ModelViews)
{
if (eachView.ConceptRoots.Contains(this.m_conceptroot))
{
docView = eachView;
break;
}
}
this.m_image = FormatPNG.CreateEntityDiagram(this.m_conceptroot.ApplicableEntity, docView, this.m_map, this.m_hitmap, this.m_project);
if (this.m_image != null)
{
this.AutoScrollMinSize = new Size(this.m_image.Width, this.m_image.Height);
}
}
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs pe)
{
if (this.m_image != null)
{
Graphics g = pe.Graphics;
g.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
g.DrawImage(this.m_image, Point.Empty);
if (this.m_rcSelection != Rectangle.Empty)
{
Rectangle rc = this.m_rcSelection;
rc.Inflate(3, 3);
rc.Width++;
rc.Height++;
using (Pen pen = new Pen(Color.Red,6.0f))
{
g.DrawRectangle(pen, rc);
}
if (this.m_iSelection >= 0 && this.m_attribute != null)
{
Rectangle rcAttr = new Rectangle(m_rcSelection.Left, m_rcSelection.Top + (this.m_iSelection + 1) * FormatPNG.CY, FormatPNG.CX - FormatPNG.DX, FormatPNG.CY);
g.FillRectangle(Brushes.Red, rcAttr);
g.DrawString(this.m_attribute.Name, this.Font, Brushes.White, rcAttr);
}
}
if (this.m_rcHighlight != Rectangle.Empty)
{
Rectangle rc = this.m_rcHighlight;
rc.Inflate(2, 2);
rc.Width++;
rc.Height++;
using (Pen pen = new Pen(Color.Orange, 4.0f))
{
g.DrawRectangle(pen, rc);
}
if (this.m_iHighlight >= 0)
{
Rectangle rcAttr = new Rectangle(m_rcHighlight.Left, m_rcHighlight.Top + (this.m_iHighlight + 1) * FormatPNG.CY, FormatPNG.CX - FormatPNG.DX, FormatPNG.CY);
g.DrawRectangle(Pens.Orange, rcAttr);
}
}
}
}
private DocModelRule Pick(Point pt, out int iAttr, out DocAttribute docAttribute, out Rectangle rc)
{
docAttribute = null;
iAttr = -1;
rc = new Rectangle();
foreach (Rectangle rect in this.m_hitmap.Keys)
{
if (rect.Contains(pt))
{
rc = rect;
iAttr = (pt.Y - rc.Top) / FormatPNG.CY - 1;
DocModelRuleEntity ruleEntity = this.m_hitmap[rc] as DocModelRuleEntity;
DocEntity docEntity = null;
if (ruleEntity != null)
{
docEntity = this.m_map[ruleEntity.Name] as DocEntity;
if (docEntity != null)
{
List<DocAttribute> listAttr = new List<DocAttribute>();
FormatPNG.BuildAttributeList(docEntity, listAttr, this.m_map);
if (iAttr >= 0 && iAttr < listAttr.Count)
{
docAttribute = listAttr[iAttr];
foreach (DocModelRule ruleAttr in ruleEntity.Rules)
{
if (ruleAttr is DocModelRuleAttribute && ruleAttr.Name.Equals(docAttribute.Name))
{
return ruleAttr;
}
}
}
}
}
else if (this.m_template != null)
{
docEntity = this.m_map[this.m_template.Type] as DocEntity;
List<DocAttribute> listAttr = new List<DocAttribute>();
FormatPNG.BuildAttributeList(docEntity, listAttr, this.m_map);
if (iAttr >= 0 && iAttr < listAttr.Count)
{
docAttribute = listAttr[iAttr];
if (this.m_template.Rules != null)
{
foreach (DocModelRule ruleAttr in this.m_template.Rules)
{
if (ruleAttr is DocModelRuleAttribute && ruleAttr.Name.Equals(docAttribute.Name))
{
return ruleAttr;
}
}
}
}
}
return ruleEntity;
}
}
return null;
}
private void CtlConcept_MouseDown(object sender, MouseEventArgs e)
{
Point pt = e.Location;
pt.X -= this.AutoScrollPosition.X;
pt.Y -= this.AutoScrollPosition.Y;
this.m_selection = Pick(pt, out this.m_iSelection, out this.m_attribute, out this.m_rcSelection);
this.Invalidate();
if (this.SelectionChanged != null)
{
this.SelectionChanged(this, EventArgs.Empty);
}
}
private void CtlConcept_MouseMove(object sender, MouseEventArgs e)
{
Point pt = e.Location;
pt.X -= this.AutoScrollPosition.X;
pt.Y -= this.AutoScrollPosition.Y;
DocAttribute attr;
this.m_highlight = Pick(pt, out this.m_iHighlight, out attr, out this.m_rcHighlight);
this.Invalidate();
}
private void CtlConcept_MouseUp(object sender, MouseEventArgs e)
{
}
}
}