hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 73152510
이력 | 보기 | 이력해설 | 다운로드 (19.7 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Collections; |
4 |
using System.Configuration; |
5 |
using System.Data; |
6 |
using System.Data.Common; |
7 |
using System.Data.SqlClient; |
8 |
using System.IO; |
9 |
|
10 |
using Autodesk.AutoCAD.ApplicationServices; |
11 |
using Autodesk.AutoCAD.ApplicationServices.Core; |
12 |
using Autodesk.AutoCAD.DatabaseServices; |
13 |
using Autodesk.AutoCAD.EditorInput; |
14 |
using Autodesk.AutoCAD.Geometry; |
15 |
using Autodesk.AutoCAD.Interop; |
16 |
using Autodesk.AutoCAD.Interop.Common; |
17 |
using Autodesk.AutoCAD.Runtime; |
18 |
using Autodesk.AutoCAD.Windows; |
19 |
|
20 |
using AVEVA.PID.Components; |
21 |
using AVEVA.PID.GUI; |
22 |
using AVEVA.PID.Common; |
23 |
using AVEVA.PID.DWGSelector; |
24 |
|
25 |
using AVEVA.PID.CustomizationUtility.DB; |
26 |
using AVEVA.PID.CustomizationUtility.Model; |
27 |
using AVEVA.PID.CustomizationUtility.Properties; |
28 |
|
29 |
namespace AVEVA.PID.CustomizationUtility |
30 |
{ |
31 |
public class AutoModeling |
32 |
{ |
33 |
public static AutoModeling Running { get; set; } |
34 |
Model.Document document; |
35 |
|
36 |
public AutoModeling(Model.Document document) |
37 |
{ |
38 |
this.document = document; |
39 |
} |
40 |
|
41 |
public void CreateDrawing() |
42 |
{ |
43 |
string outPath = Project_DB.GetDirectiveValue("DRGPTH"); |
44 |
string tempPath = Path.GetTempPath(); |
45 |
string selectedFullPath = Path.Combine(tempPath, document.AvevaTemplateName + ".DWT"); |
46 |
|
47 |
DWTSelector.strDrawingNumber = document.AvevaDrawingNumber; |
48 |
DWTSelector.strSheetNumber = document.AvevaSheetNumber; |
49 |
DWTSelector.strCadFileName = document.AvevaDrawingNumber + document.AvevaSheetNumber; |
50 |
DWTSelector.strCadFilePath = outPath; |
51 |
|
52 |
try |
53 |
{ |
54 |
string text = ReadWriteDrawingData.ReadTemplateData(document.AvevaTemplateName, document.AvevaTemplateID, selectedFullPath, false); |
55 |
Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication; |
56 |
Autodesk.AutoCAD.Interop.AcadDocument acadDocument = acadApplication.Documents.Add(text); |
57 |
|
58 |
Commands.addLatestPidvesrionToDictionary(); |
59 |
AVVPropCmd.ActivateAcad(); |
60 |
|
61 |
if (acadDocument != null) |
62 |
{ |
63 |
Running = this; |
64 |
acadApplication.ActiveDocument = acadDocument; |
65 |
acadDocument.SendCommand("QSAVE "); |
66 |
acadDocument.SendCommand("APPIDRun "); |
67 |
acadDocument.SendCommand("QSAVE "); |
68 |
Running = null; |
69 |
} |
70 |
} |
71 |
catch (System.Exception ex) |
72 |
{ |
73 |
|
74 |
} |
75 |
|
76 |
GC.Collect(); |
77 |
} |
78 |
public void Run() |
79 |
{ |
80 |
try |
81 |
{ |
82 |
RunLineModeling(); |
83 |
RunSymbolModeling(); |
84 |
} |
85 |
catch (System.Exception ex) |
86 |
{ |
87 |
System.Windows.Forms.MessageBox.Show(ex.Message + @"\n" + ex.StackTrace); |
88 |
} |
89 |
} |
90 |
|
91 |
#region Run Method |
92 |
private void RunLineModeling() |
93 |
{ |
94 |
foreach (var item in document.LINES) |
95 |
{ |
96 |
if (item.Aveva.Handle == 0) |
97 |
LineModeling(item); |
98 |
} |
99 |
} |
100 |
private void RunSymbolModeling() |
101 |
{ |
102 |
foreach (var item in document.SYMBOLS) |
103 |
{ |
104 |
if (item.Aveva.Handle == 0) |
105 |
SymbolModeling(item); |
106 |
} |
107 |
} |
108 |
#endregion |
109 |
|
110 |
#region Modeling Method |
111 |
private void LineModeling(Model.Line line) |
112 |
{ |
113 |
List<Model.Line> groupLine = new List<Model.Line>(); |
114 |
List<string> points = new List<string>(); |
115 |
GetConnectedGroupLine(line, groupLine, false); |
116 |
GetGroupLinePoints(groupLine, points); |
117 |
|
118 |
long handle = 0; |
119 |
if (line.Aveva.Type == Model.Type.Pipe) |
120 |
handle = DrawPipe(line.Aveva.Name, points); |
121 |
else if (line.Aveva.Type == Model.Type.Signal) |
122 |
handle = DrawSignal(line.Aveva.Name, points); |
123 |
|
124 |
foreach (var item in groupLine) |
125 |
item.Aveva.Handle = handle; |
126 |
} |
127 |
private void SymbolModeling(Symbol symbol) |
128 |
{ |
129 |
long handle = InsertSymbol(symbol.Aveva.Name, symbol.Aveva.X, symbol.Aveva.Y); |
130 |
if (handle != 0) |
131 |
symbol.Aveva.Handle = handle; |
132 |
} |
133 |
#endregion |
134 |
|
135 |
#region Drawing Method |
136 |
private long DrawPipe(string style, List<string> coordinates) |
137 |
{ |
138 |
List<long> prevHandles = GetAllGroupHandles(); |
139 |
|
140 |
GUIUtils.SetPipeStyle(style); |
141 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
142 |
Editor editor = acDoc.Editor; |
143 |
List<object> commandParam = new List<object>(); |
144 |
//command name |
145 |
commandParam.Add("DRPIPE"); |
146 |
//coordinate |
147 |
foreach (var item in coordinates) |
148 |
commandParam.Add(item); |
149 |
//enter |
150 |
commandParam.Add(null); |
151 |
//enter |
152 |
commandParam.Add(null); |
153 |
|
154 |
editor.Command(commandParam.ToArray()); |
155 |
|
156 |
List<long> newHandles = GetAllGroupHandles(); |
157 |
List<long> otherHandles = new List<long>(); |
158 |
foreach (var item in newHandles) |
159 |
if (!prevHandles.Contains(item)) |
160 |
otherHandles.Add(item); |
161 |
|
162 |
if (otherHandles.Count == 1) |
163 |
return otherHandles[0]; |
164 |
else |
165 |
return 0; |
166 |
} |
167 |
private long DrawPipe(string style, List<string> coordinates, ObjectId objectId) |
168 |
{ |
169 |
List<long> prevHandles = GetAllGroupHandles(); |
170 |
GUIUtils.SetPipeStyle(style); |
171 |
|
172 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
173 |
Editor editor = acDoc.Editor; |
174 |
List<object> commandParam = new List<object>(); |
175 |
//command name |
176 |
commandParam.Add("DRPIPE"); |
177 |
//coordinate |
178 |
foreach (var item in coordinates) |
179 |
commandParam.Add(item); |
180 |
//enter |
181 |
commandParam.Add(null); |
182 |
|
183 |
//Input Object ID |
184 |
commandParam.Add(objectId); |
185 |
|
186 |
//enter |
187 |
commandParam.Add(null); |
188 |
|
189 |
editor.Command(commandParam.ToArray()); |
190 |
|
191 |
List<long> newHandles = GetAllGroupHandles(); |
192 |
List<long> otherHandles = new List<long>(); |
193 |
foreach (var item in newHandles) |
194 |
if (!prevHandles.Contains(item)) |
195 |
otherHandles.Add(item); |
196 |
|
197 |
if (otherHandles.Count == 1) |
198 |
return otherHandles[0]; |
199 |
else |
200 |
return 0; |
201 |
} |
202 |
private long DrawSignal(string style, List<string> coordinates) |
203 |
{ |
204 |
List<long> prevHandles = GetAllGroupHandles(); |
205 |
|
206 |
GUIUtils.SetPipeStyle(style); |
207 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
208 |
Editor editor = acDoc.Editor; |
209 |
List<object> commandParam = new List<object>(); |
210 |
//command name |
211 |
commandParam.Add("VPESIGNAL"); |
212 |
//coordinate |
213 |
foreach (var item in coordinates) |
214 |
commandParam.Add(item); |
215 |
//enter |
216 |
commandParam.Add(null); |
217 |
|
218 |
editor.Command(commandParam.ToArray()); |
219 |
|
220 |
List<long> newHandles = GetAllGroupHandles(); |
221 |
List<long> otherHandles = new List<long>(); |
222 |
foreach (var item in newHandles) |
223 |
if (!prevHandles.Contains(item)) |
224 |
otherHandles.Add(item); |
225 |
|
226 |
if (otherHandles.Count == 1) |
227 |
return otherHandles[0]; |
228 |
else |
229 |
return 0; |
230 |
} |
231 |
private long InsertSymbol(string insertSymbolName, double x, double y) |
232 |
{ |
233 |
long handle = 0; |
234 |
try |
235 |
{ |
236 |
List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName); |
237 |
|
238 |
AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData(); |
239 |
drawingData.InsertSymbolName = insertSymbolName; |
240 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
241 |
Editor editor = acDoc.Editor; |
242 |
List<object> commandParam = new List<object>(); |
243 |
commandParam.Add("INSSYM"); |
244 |
Point3d point = new Point3d(x, y, 0); |
245 |
commandParam.Add(point); |
246 |
commandParam.Add(null); |
247 |
|
248 |
editor.Command(commandParam.ToArray()); |
249 |
|
250 |
List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName); |
251 |
List<long> otherHandles = new List<long>(); |
252 |
foreach (var item in newHandles) |
253 |
if (!prevHandles.Contains(item)) |
254 |
otherHandles.Add(item); |
255 |
|
256 |
if (otherHandles.Count == 1) |
257 |
return otherHandles[0]; |
258 |
else |
259 |
return 0; |
260 |
} |
261 |
catch (System.Exception ex) |
262 |
{ |
263 |
|
264 |
} |
265 |
|
266 |
return handle; |
267 |
} |
268 |
#endregion |
269 |
|
270 |
#region Autocad Utils |
271 |
private List<long> GetAllBlockHandlesByName(string name) |
272 |
{ |
273 |
List<long> handles = new List<long>(); |
274 |
|
275 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
276 |
Database acCurDb = acDoc.Database; |
277 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
278 |
{ |
279 |
BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead); |
280 |
foreach (var entry in gd) |
281 |
{ |
282 |
BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord; |
283 |
if (blockTableRecord != null) |
284 |
{ |
285 |
IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord); |
286 |
foreach (var item in records) |
287 |
{ |
288 |
if (item.Name == name) |
289 |
handles.Add(item.Handle.Value); |
290 |
} |
291 |
} |
292 |
} |
293 |
|
294 |
acTrans.Commit(); |
295 |
} |
296 |
return handles; |
297 |
} |
298 |
private List<long> GetAllGroupHandles() |
299 |
{ |
300 |
List<long> handles = new List<long>(); |
301 |
|
302 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
303 |
Database acCurDb = acDoc.Database; |
304 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
305 |
{ |
306 |
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
307 |
foreach (var entry in gd) |
308 |
handles.Add(entry.Value.Handle.Value); |
309 |
acTrans.Commit(); |
310 |
} |
311 |
return handles; |
312 |
} |
313 |
private ObjectId GetFirstPolyLine(long groupHandle) |
314 |
{ |
315 |
ObjectId result = ObjectId.Null; |
316 |
|
317 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
318 |
Database acCurDb = acDoc.Database; |
319 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
320 |
{ |
321 |
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
322 |
foreach (var entry in gd) |
323 |
{ |
324 |
if (entry.Value.Handle.Value == groupHandle) |
325 |
{ |
326 |
Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group; |
327 |
foreach (var item in group.GetAllEntityIds()) |
328 |
{ |
329 |
if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)) |
330 |
{ |
331 |
result = item; |
332 |
break; |
333 |
} |
334 |
} |
335 |
break; |
336 |
} |
337 |
} |
338 |
acTrans.Commit(); |
339 |
} |
340 |
|
341 |
return result; |
342 |
} |
343 |
#endregion |
344 |
|
345 |
#region Modeling Utils |
346 |
private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert) |
347 |
{ |
348 |
if (!group.Contains(line)) |
349 |
{ |
350 |
if (isInsert) |
351 |
group.Insert(0, line); |
352 |
else |
353 |
group.Add(line); |
354 |
|
355 |
if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject)) |
356 |
{ |
357 |
object connObj = line.CONNECTORS[0].ConnectedObject; |
358 |
if (connObj.GetType() == typeof(Model.Line) && |
359 |
APIDUtils.IsConnectedLine(connObj as Model.Line, line)) |
360 |
GetConnectedGroupLine(connObj as Model.Line, group, true); |
361 |
else if (connObj.GetType() == typeof(Model.Symbol)) |
362 |
{ |
363 |
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol); |
364 |
if (connLine != null) |
365 |
GetConnectedGroupLine(connLine as Model.Line, group, true); |
366 |
} |
367 |
} |
368 |
if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject)) |
369 |
{ |
370 |
object connObj = line.CONNECTORS[1].ConnectedObject; |
371 |
if (connObj.GetType() == typeof(Model.Line) && |
372 |
APIDUtils.IsConnectedLine(connObj as Model.Line, line)) |
373 |
GetConnectedGroupLine(connObj as Model.Line, group, false); |
374 |
else if (connObj.GetType() == typeof(Model.Symbol)) |
375 |
{ |
376 |
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol); |
377 |
if (connLine != null) |
378 |
GetConnectedGroupLine(connLine as Model.Line, group, false); |
379 |
} |
380 |
} |
381 |
} |
382 |
} |
383 |
private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points) |
384 |
{ |
385 |
for (int i = 0; i < groupLine.Count; i++) |
386 |
{ |
387 |
Model.Line line = groupLine[i]; |
388 |
if (i == 0) |
389 |
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
390 |
else |
391 |
{ |
392 |
Model.Line prevLine = groupLine[i - 1]; |
393 |
if (line.SlopeType == SlopeType.HORIZONTAL && |
394 |
prevLine.SlopeType == SlopeType.VERTICAL) |
395 |
points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y); |
396 |
else if (line.SlopeType == SlopeType.VERTICAL && |
397 |
prevLine.SlopeType == SlopeType.HORIZONTAL) |
398 |
points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y); |
399 |
else |
400 |
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
401 |
} |
402 |
|
403 |
if (i == groupLine.Count - 1) |
404 |
points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y); |
405 |
} |
406 |
} |
407 |
private bool IsExistEndBreak(object item1, object item2) |
408 |
{ |
409 |
bool result = false; |
410 |
if (item1 != null && item2 != null) |
411 |
{ |
412 |
EndBreak endBreak = document.EndBreaks.Find(x => |
413 |
(APIDUtils.FindObjectByUID(document, x.OWNER) == item1 && |
414 |
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) || |
415 |
(APIDUtils.FindObjectByUID(document, x.OWNER) == item2 && |
416 |
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1)); |
417 |
|
418 |
if (endBreak != null) |
419 |
result = true; |
420 |
} |
421 |
|
422 |
return result; |
423 |
} |
424 |
private LineRun FindLineRun(Model.Line line) |
425 |
{ |
426 |
List<LineRun> allLineRun = new List<LineRun>(); |
427 |
foreach (var item in document.LINENUMBERS) |
428 |
allLineRun.AddRange(item.RUNS); |
429 |
foreach (var item in document.TRIMLINES) |
430 |
allLineRun.AddRange(item.RUNS); |
431 |
|
432 |
return allLineRun.Find(x => x.RUNITEMS.Contains(line)); |
433 |
} |
434 |
private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol) |
435 |
{ |
436 |
Model.Line result = null; |
437 |
LineRun run = FindLineRun(line); |
438 |
Connector connector = symbol.CONNECTORS.Find(x => |
439 |
x.ConnectedObject != null && |
440 |
x.ConnectedObject != line && |
441 |
run.RUNITEMS.Contains(x.ConnectedObject) && |
442 |
x.ConnectedObject.GetType() == typeof(Model.Line)); |
443 |
|
444 |
if (connector != null) |
445 |
result = connector.ConnectedObject as Model.Line; |
446 |
|
447 |
return result; |
448 |
} |
449 |
#endregion |
450 |
|
451 |
#region Test Source |
452 |
public void test() |
453 |
{ |
454 |
//InitGUI(); |
455 |
//DrawSignal("SONIC", new List<string>() { "2,100", "100,100" }); |
456 |
|
457 |
DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }); |
458 |
InsertSymbol("BAVA", 50, 100); |
459 |
//DrawPipe("Main Pipe", new List<string>() { "2,200", "100,200" }); |
460 |
|
461 |
//DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" }); |
462 |
} |
463 |
|
464 |
|
465 |
public static void TESTStatic() |
466 |
{ |
467 |
AutoModeling auto = new AutoModeling(null); |
468 |
auto.test(); |
469 |
} |
470 |
#endregion |
471 |
} |
472 |
|
473 |
|
474 |
public static class AcDbLinqExtensionMethods |
475 |
{ |
476 |
/// <summary> |
477 |
/// Get all references to the given BlockTableRecord, including |
478 |
/// references to anonymous dynamic BlockTableRecords. |
479 |
/// </summary> |
480 |
|
481 |
public static IEnumerable<BlockReference> GetBlockReferences( |
482 |
this BlockTableRecord btr, |
483 |
OpenMode mode = OpenMode.ForRead, |
484 |
bool directOnly = true) |
485 |
{ |
486 |
if (btr == null) |
487 |
throw new ArgumentNullException("btr"); |
488 |
var tr = btr.Database.TransactionManager.TopTransaction; |
489 |
if (tr == null) |
490 |
throw new InvalidOperationException("No transaction"); |
491 |
var ids = btr.GetBlockReferenceIds(directOnly, true); |
492 |
int cnt = ids.Count; |
493 |
for (int i = 0; i < cnt; i++) |
494 |
{ |
495 |
yield return (BlockReference) |
496 |
tr.GetObject(ids[i], mode, false, false); |
497 |
} |
498 |
if (btr.IsDynamicBlock) |
499 |
{ |
500 |
BlockTableRecord btr2 = null; |
501 |
var blockIds = btr.GetAnonymousBlockIds(); |
502 |
cnt = blockIds.Count; |
503 |
for (int i = 0; i < cnt; i++) |
504 |
{ |
505 |
btr2 = (BlockTableRecord)tr.GetObject(blockIds[i], |
506 |
OpenMode.ForRead, false, false); |
507 |
ids = btr2.GetBlockReferenceIds(directOnly, true); |
508 |
int cnt2 = ids.Count; |
509 |
for (int j = 0; j < cnt2; j++) |
510 |
{ |
511 |
yield return (BlockReference) |
512 |
tr.GetObject(ids[j], mode, false, false); |
513 |
} |
514 |
} |
515 |
} |
516 |
} |
517 |
} |
518 |
} |