hytos / DTI_PID / APIDConverter / AutoModeling.cs @ 016701e5
이력 | 보기 | 이력해설 | 다운로드 (15.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 |
|
103 |
} |
104 |
#endregion |
105 |
|
106 |
#region Modeling Method |
107 |
private void LineModeling(Model.Line line) |
108 |
{ |
109 |
List<Model.Line> groupLine = new List<Model.Line>(); |
110 |
List<string> points = new List<string>(); |
111 |
GetConnectedGroupLine(line, groupLine, false); |
112 |
GetGroupLinePoints(groupLine, points); |
113 |
|
114 |
long handle = 0; |
115 |
if (line.Aveva.Type == Model.Type.Pipe) |
116 |
handle = DrawPipe(line.Aveva.Name, points); |
117 |
else if (line.Aveva.Type == Model.Type.Signal) |
118 |
handle = DrawSignal(line.Aveva.Name, points); |
119 |
|
120 |
foreach (var item in groupLine) |
121 |
item.Aveva.Handle = handle; |
122 |
} |
123 |
|
124 |
#endregion |
125 |
|
126 |
#region Drawing Method |
127 |
private long DrawPipe(string style, List<string> coordinates) |
128 |
{ |
129 |
List<long> prevHandles = GetAllGroupHandles(); |
130 |
|
131 |
GUIUtils.SetPipeStyle(style); |
132 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
133 |
Editor editor = acDoc.Editor; |
134 |
List<object> commandParam = new List<object>(); |
135 |
//command name |
136 |
commandParam.Add("DRPIPE"); |
137 |
//coordinate |
138 |
foreach (var item in coordinates) |
139 |
commandParam.Add(item); |
140 |
//enter |
141 |
commandParam.Add(null); |
142 |
//enter |
143 |
commandParam.Add(null); |
144 |
|
145 |
editor.Command(commandParam.ToArray()); |
146 |
|
147 |
List<long> newHandles = GetAllGroupHandles(); |
148 |
List<long> otherHandles = new List<long>(); |
149 |
foreach (var item in newHandles) |
150 |
if (!prevHandles.Contains(item)) |
151 |
otherHandles.Add(item); |
152 |
|
153 |
if (otherHandles.Count == 1) |
154 |
return otherHandles[0]; |
155 |
else |
156 |
return 0; |
157 |
} |
158 |
private long DrawPipe(string style, List<string> coordinates, ObjectId objectId) |
159 |
{ |
160 |
List<long> prevHandles = GetAllGroupHandles(); |
161 |
GUIUtils.SetPipeStyle(style); |
162 |
|
163 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
164 |
Editor editor = acDoc.Editor; |
165 |
List<object> commandParam = new List<object>(); |
166 |
//command name |
167 |
commandParam.Add("DRPIPE"); |
168 |
//coordinate |
169 |
foreach (var item in coordinates) |
170 |
commandParam.Add(item); |
171 |
//enter |
172 |
commandParam.Add(null); |
173 |
|
174 |
//Input Object ID |
175 |
commandParam.Add(objectId); |
176 |
|
177 |
//enter |
178 |
commandParam.Add(null); |
179 |
|
180 |
editor.Command(commandParam.ToArray()); |
181 |
|
182 |
List<long> newHandles = GetAllGroupHandles(); |
183 |
List<long> otherHandles = new List<long>(); |
184 |
foreach (var item in newHandles) |
185 |
if (!prevHandles.Contains(item)) |
186 |
otherHandles.Add(item); |
187 |
|
188 |
if (otherHandles.Count == 1) |
189 |
return otherHandles[0]; |
190 |
else |
191 |
return 0; |
192 |
} |
193 |
private long DrawSignal(string style, List<string> coordinates) |
194 |
{ |
195 |
List<long> prevHandles = GetAllGroupHandles(); |
196 |
|
197 |
GUIUtils.SetPipeStyle(style); |
198 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
199 |
Editor editor = acDoc.Editor; |
200 |
List<object> commandParam = new List<object>(); |
201 |
//command name |
202 |
commandParam.Add("VPESIGNAL"); |
203 |
//coordinate |
204 |
foreach (var item in coordinates) |
205 |
commandParam.Add(item); |
206 |
//enter |
207 |
commandParam.Add(null); |
208 |
|
209 |
editor.Command(commandParam.ToArray()); |
210 |
|
211 |
List<long> newHandles = GetAllGroupHandles(); |
212 |
List<long> otherHandles = new List<long>(); |
213 |
foreach (var item in newHandles) |
214 |
if (!prevHandles.Contains(item)) |
215 |
otherHandles.Add(item); |
216 |
|
217 |
if (otherHandles.Count == 1) |
218 |
return otherHandles[0]; |
219 |
else |
220 |
return 0; |
221 |
} |
222 |
private void InsertSymbol(string insertSymbolName, double x, double y) |
223 |
{ |
224 |
try |
225 |
{ |
226 |
AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData(); |
227 |
drawingData.InsertSymbolName = insertSymbolName; |
228 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
229 |
Editor editor = acDoc.Editor; |
230 |
List<object> commandParam = new List<object>(); |
231 |
commandParam.Add("INSSYM"); |
232 |
Point3d point = new Point3d(x, y, 0); |
233 |
commandParam.Add(point); |
234 |
commandParam.Add(null); |
235 |
|
236 |
editor.Command(commandParam.ToArray()); |
237 |
} |
238 |
catch (System.Exception ex) |
239 |
{ |
240 |
|
241 |
} |
242 |
} |
243 |
#endregion |
244 |
|
245 |
#region |
246 |
private List<long> GetAllGroupHandles() |
247 |
{ |
248 |
List<long> handles = new List<long>(); |
249 |
|
250 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
251 |
Database acCurDb = acDoc.Database; |
252 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
253 |
{ |
254 |
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
255 |
foreach (var entry in gd) |
256 |
handles.Add(entry.Value.Handle.Value); |
257 |
acTrans.Commit(); |
258 |
} |
259 |
return handles; |
260 |
} |
261 |
private ObjectId GetFirstPolyLine(long groupHandle) |
262 |
{ |
263 |
ObjectId result = ObjectId.Null; |
264 |
|
265 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
266 |
Database acCurDb = acDoc.Database; |
267 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
268 |
{ |
269 |
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
270 |
foreach (var entry in gd) |
271 |
{ |
272 |
if (entry.Value.Handle.Value == groupHandle) |
273 |
{ |
274 |
Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group; |
275 |
foreach (var item in group.GetAllEntityIds()) |
276 |
{ |
277 |
if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)) |
278 |
{ |
279 |
result = item; |
280 |
break; |
281 |
} |
282 |
} |
283 |
break; |
284 |
} |
285 |
} |
286 |
acTrans.Commit(); |
287 |
} |
288 |
|
289 |
return result; |
290 |
} |
291 |
|
292 |
|
293 |
|
294 |
#endregion |
295 |
|
296 |
#region |
297 |
private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert) |
298 |
{ |
299 |
if (!group.Contains(line)) |
300 |
{ |
301 |
if (isInsert) |
302 |
group.Insert(0, line); |
303 |
else |
304 |
group.Add(line); |
305 |
|
306 |
if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject)) |
307 |
{ |
308 |
object connObj = line.CONNECTORS[0].ConnectedObject; |
309 |
if (connObj.GetType() == typeof(Model.Line) && |
310 |
APIDUtils.IsConnectedLine(connObj as Model.Line, line)) |
311 |
GetConnectedGroupLine(connObj as Model.Line, group, true); |
312 |
else if (connObj.GetType() == typeof(Model.Symbol)) |
313 |
{ |
314 |
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol); |
315 |
if (connLine != null) |
316 |
GetConnectedGroupLine(connLine as Model.Line, group, true); |
317 |
} |
318 |
} |
319 |
if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject)) |
320 |
{ |
321 |
object connObj = line.CONNECTORS[1].ConnectedObject; |
322 |
if (connObj.GetType() == typeof(Model.Line) && |
323 |
APIDUtils.IsConnectedLine(connObj as Model.Line, line)) |
324 |
GetConnectedGroupLine(connObj as Model.Line, group, false); |
325 |
else if (connObj.GetType() == typeof(Model.Symbol)) |
326 |
{ |
327 |
Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol); |
328 |
if (connLine != null) |
329 |
GetConnectedGroupLine(connLine as Model.Line, group, false); |
330 |
} |
331 |
} |
332 |
} |
333 |
} |
334 |
private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points) |
335 |
{ |
336 |
for (int i = 0; i < groupLine.Count; i++) |
337 |
{ |
338 |
Model.Line line = groupLine[i]; |
339 |
if (i == 0) |
340 |
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
341 |
else |
342 |
{ |
343 |
Model.Line prevLine = groupLine[i - 1]; |
344 |
if (line.SlopeType == SlopeType.HORIZONTAL && |
345 |
prevLine.SlopeType == SlopeType.VERTICAL) |
346 |
points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y); |
347 |
else if (line.SlopeType == SlopeType.VERTICAL && |
348 |
prevLine.SlopeType == SlopeType.HORIZONTAL) |
349 |
points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y); |
350 |
else |
351 |
points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
352 |
} |
353 |
|
354 |
if (i == groupLine.Count - 1) |
355 |
points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y); |
356 |
} |
357 |
} |
358 |
private bool IsExistEndBreak(object item1, object item2) |
359 |
{ |
360 |
bool result = false; |
361 |
if (item1 != null && item2 != null) |
362 |
{ |
363 |
EndBreak endBreak = document.EndBreaks.Find(x => |
364 |
(APIDUtils.FindObjectByUID(document, x.OWNER) == item1 && |
365 |
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) || |
366 |
(APIDUtils.FindObjectByUID(document, x.OWNER) == item2 && |
367 |
APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1)); |
368 |
|
369 |
if (endBreak != null) |
370 |
result = true; |
371 |
} |
372 |
|
373 |
return result; |
374 |
} |
375 |
private LineRun FindLineRun(Model.Line line) |
376 |
{ |
377 |
List<LineRun> allLineRun = new List<LineRun>(); |
378 |
foreach (var item in document.LINENUMBERS) |
379 |
allLineRun.AddRange(item.RUNS); |
380 |
foreach (var item in document.TRIMLINES) |
381 |
allLineRun.AddRange(item.RUNS); |
382 |
|
383 |
return allLineRun.Find(x => x.RUNITEMS.Contains(line)); |
384 |
} |
385 |
private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol) |
386 |
{ |
387 |
Model.Line result = null; |
388 |
LineRun run = FindLineRun(line); |
389 |
Connector connector = symbol.CONNECTORS.Find(x => |
390 |
x.ConnectedObject != null && |
391 |
x.ConnectedObject != line && |
392 |
run.RUNITEMS.Contains(x.ConnectedObject) && |
393 |
x.ConnectedObject.GetType() == typeof(Model.Line)); |
394 |
|
395 |
if (connector != null) |
396 |
result = connector.ConnectedObject as Model.Line; |
397 |
|
398 |
return result; |
399 |
} |
400 |
#endregion |
401 |
|
402 |
#region Test Source |
403 |
public void test() |
404 |
{ |
405 |
//InitGUI(); |
406 |
//DrawSignal("SONIC", new List<string>() { "2,100", "100,100" }); |
407 |
|
408 |
DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }); |
409 |
|
410 |
//DrawPipe("Main Pipe", new List<string>() { "2,200", "100,200" }); |
411 |
|
412 |
//DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" }); |
413 |
} |
414 |
public static void TESTStatic() |
415 |
{ |
416 |
AutoModeling auto = new AutoModeling(null); |
417 |
auto.test(); |
418 |
} |
419 |
#endregion |
420 |
} |
421 |
} |