hytos / DTI_PID / APIDConverter / AutoModeling.cs @ a19bd6e2
이력 | 보기 | 이력해설 | 다운로드 (28.9 KB)
1 | 74a0c9d6 | gaqhf | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | 016701e5 | gaqhf | using System.Collections; |
4 | 74a0c9d6 | gaqhf | 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 | 1c5bd296 | gaqhf | using AVEVA.PID.Components; |
21 | using AVEVA.PID.GUI; |
||
22 | using AVEVA.PID.Common; |
||
23 | using AVEVA.PID.DWGSelector; |
||
24 | 495bb8f5 | gaqhf | using AVEVA.PID.Utilities; |
25 | 1c5bd296 | gaqhf | |
26 | 14540282 | gaqhf | using AVEVA.PID.CustomizationUtility.DB; |
27 | using AVEVA.PID.CustomizationUtility.Model; |
||
28 | using AVEVA.PID.CustomizationUtility.Properties; |
||
29 | 74a0c9d6 | gaqhf | |
30 | aba318eb | gaqhf | using DevExpress.XtraSplashScreen; |
31 | |||
32 | 74a0c9d6 | gaqhf | namespace AVEVA.PID.CustomizationUtility |
33 | { |
||
34 | public class AutoModeling |
||
35 | { |
||
36 | 1c5bd296 | gaqhf | public static AutoModeling Running { get; set; } |
37 | 14540282 | gaqhf | Model.Document document; |
38 | 74a0c9d6 | gaqhf | |
39 | 495bb8f5 | gaqhf | System.Data.DataTable AvevaSymbolTable = null; |
40 | |||
41 | a19bd6e2 | gaqhf | private void SetConvertRule() |
42 | { |
||
43 | #region OPC Setting |
||
44 | document.OPCs.Sort(SortOPC); |
||
45 | int SortOPC(OPC a, OPC b) |
||
46 | { |
||
47 | if (a.FlowType == FlowType.In) |
||
48 | return 1; |
||
49 | else |
||
50 | return 0; |
||
51 | } |
||
52 | #endregion |
||
53 | } |
||
54 | |||
55 | 495bb8f5 | gaqhf | public AutoModeling(Model.Document document, System.Data.DataTable AvevaSymbolTable) |
56 | 74a0c9d6 | gaqhf | { |
57 | 14540282 | gaqhf | this.document = document; |
58 | 495bb8f5 | gaqhf | this.AvevaSymbolTable = AvevaSymbolTable; |
59 | 74a0c9d6 | gaqhf | } |
60 | |||
61 | 1c5bd296 | gaqhf | public void CreateDrawing() |
62 | 46756c13 | gaqhf | { |
63 | 1c5bd296 | gaqhf | string outPath = Project_DB.GetDirectiveValue("DRGPTH"); |
64 | string tempPath = Path.GetTempPath(); |
||
65 | string selectedFullPath = Path.Combine(tempPath, document.AvevaTemplateName + ".DWT"); |
||
66 | |||
67 | DWTSelector.strDrawingNumber = document.AvevaDrawingNumber; |
||
68 | DWTSelector.strSheetNumber = document.AvevaSheetNumber; |
||
69 | DWTSelector.strCadFileName = document.AvevaDrawingNumber + document.AvevaSheetNumber; |
||
70 | DWTSelector.strCadFilePath = outPath; |
||
71 | |||
72 | try |
||
73 | 46756c13 | gaqhf | { |
74 | 1c5bd296 | gaqhf | string text = ReadWriteDrawingData.ReadTemplateData(document.AvevaTemplateName, document.AvevaTemplateID, selectedFullPath, false); |
75 | Autodesk.AutoCAD.Interop.AcadApplication acadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication; |
||
76 | Autodesk.AutoCAD.Interop.AcadDocument acadDocument = acadApplication.Documents.Add(text); |
||
77 | |||
78 | Commands.addLatestPidvesrionToDictionary(); |
||
79 | AVVPropCmd.ActivateAcad(); |
||
80 | |||
81 | if (acadDocument != null) |
||
82 | { |
||
83 | Running = this; |
||
84 | acadApplication.ActiveDocument = acadDocument; |
||
85 | acadDocument.SendCommand("QSAVE "); |
||
86 | acadDocument.SendCommand("APPIDRun "); |
||
87 | 9dab7146 | gaqhf | acadDocument.SendCommand("QSAVE "); |
88 | 1c5bd296 | gaqhf | Running = null; |
89 | } |
||
90 | 46756c13 | gaqhf | } |
91 | 1c5bd296 | gaqhf | catch (System.Exception ex) |
92 | { |
||
93 | |||
94 | } |
||
95 | |||
96 | GC.Collect(); |
||
97 | } |
||
98 | public void Run() |
||
99 | { |
||
100 | 016701e5 | gaqhf | try |
101 | { |
||
102 | aba318eb | gaqhf | SplashScreenManager.ShowForm(typeof(APIDSplashScreen), true, true); |
103 | SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllStepCount, 2); |
||
104 | SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetDocumentName, document.AvevaDrawingNumber + document.AvevaSheetNumber); |
||
105 | a19bd6e2 | gaqhf | SetConvertRule(); |
106 | aba318eb | gaqhf | |
107 | 016701e5 | gaqhf | RunLineModeling(); |
108 | a19bd6e2 | gaqhf | RunOPCModeling(); |
109 | 016701e5 | gaqhf | RunSymbolModeling(); |
110 | } |
||
111 | catch (System.Exception ex) |
||
112 | { |
||
113 | System.Windows.Forms.MessageBox.Show(ex.Message + @"\n" + ex.StackTrace); |
||
114 | } |
||
115 | aba318eb | gaqhf | finally |
116 | { |
||
117 | SplashScreenManager.CloseForm(false); |
||
118 | } |
||
119 | 46756c13 | gaqhf | } |
120 | |||
121 | 9dab7146 | gaqhf | #region Run Method |
122 | private void RunLineModeling() |
||
123 | 74a0c9d6 | gaqhf | { |
124 | aba318eb | gaqhf | SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count); |
125 | SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling"); |
||
126 | 9dab7146 | gaqhf | foreach (var item in document.LINES) |
127 | 74a0c9d6 | gaqhf | { |
128 | 53344e2c | gaqhf | if (item.Aveva.Handle == 0) |
129 | LineModeling(item); |
||
130 | aba318eb | gaqhf | |
131 | SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
||
132 | 74a0c9d6 | gaqhf | } |
133 | 9dab7146 | gaqhf | } |
134 | a19bd6e2 | gaqhf | private void RunOPCModeling() |
135 | { |
||
136 | SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.OPCs.Count); |
||
137 | SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "OPC Modeling"); |
||
138 | foreach (var item in document.OPCs) |
||
139 | { |
||
140 | if (item.Aveva.Handle == 0) |
||
141 | SymbolModeling(item); |
||
142 | SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
||
143 | } |
||
144 | } |
||
145 | 016701e5 | gaqhf | private void RunSymbolModeling() |
146 | { |
||
147 | aba318eb | gaqhf | SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count); |
148 | SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling"); |
||
149 | b90890eb | gaqhf | foreach (var item in document.SYMBOLS) |
150 | { |
||
151 | if (item.Aveva.Handle == 0) |
||
152 | SymbolModeling(item); |
||
153 | aba318eb | gaqhf | SplashScreenManager.Default.SendCommand(APIDSplashScreen.SplashScreenCommand.UpProgress, null); |
154 | b90890eb | gaqhf | } |
155 | 016701e5 | gaqhf | } |
156 | 9dab7146 | gaqhf | #endregion |
157 | 74a0c9d6 | gaqhf | |
158 | 9dab7146 | gaqhf | #region Modeling Method |
159 | private void LineModeling(Model.Line line) |
||
160 | { |
||
161 | 53344e2c | gaqhf | List<Model.Line> groupLine = new List<Model.Line>(); |
162 | 9dab7146 | gaqhf | List<string> points = new List<string>(); |
163 | 016701e5 | gaqhf | GetConnectedGroupLine(line, groupLine, false); |
164 | 53344e2c | gaqhf | GetGroupLinePoints(groupLine, points); |
165 | 9dab7146 | gaqhf | |
166 | 53344e2c | gaqhf | long handle = 0; |
167 | 9dab7146 | gaqhf | if (line.Aveva.Type == Model.Type.Pipe) |
168 | 53344e2c | gaqhf | handle = DrawPipe(line.Aveva.Name, points); |
169 | 9dab7146 | gaqhf | else if (line.Aveva.Type == Model.Type.Signal) |
170 | 53344e2c | gaqhf | handle = DrawSignal(line.Aveva.Name, points); |
171 | |||
172 | foreach (var item in groupLine) |
||
173 | item.Aveva.Handle = handle; |
||
174 | 74a0c9d6 | gaqhf | } |
175 | b90890eb | gaqhf | private void SymbolModeling(Symbol symbol) |
176 | { |
||
177 | 495bb8f5 | gaqhf | DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name)); |
178 | if (allRows.Length == 1) |
||
179 | 2d09df82 | gaqhf | { |
180 | 495bb8f5 | gaqhf | DataRow symbolRow = allRows[0]; |
181 | bool bAngle = CheckAngle(symbolRow); |
||
182 | bool bBalloon = CheckBalloon(symbol); |
||
183 | |||
184 | long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon); |
||
185 | if (handle != 0) |
||
186 | 2d09df82 | gaqhf | { |
187 | 495bb8f5 | gaqhf | symbol.Aveva.Handle = handle; |
188 | |||
189 | if (!bAngle) |
||
190 | { |
||
191 | ObjectId objectId = GetObjectIdByHandle(handle); |
||
192 | if (objectId != ObjectId.Null) |
||
193 | { |
||
194 | SetBlockReferenceRotation(objectId, symbol.ANGLE); |
||
195 | } |
||
196 | } |
||
197 | 2d09df82 | gaqhf | } |
198 | } |
||
199 | b90890eb | gaqhf | } |
200 | 9dab7146 | gaqhf | #endregion |
201 | 74a0c9d6 | gaqhf | |
202 | #region Drawing Method |
||
203 | 9dab7146 | gaqhf | private long DrawPipe(string style, List<string> coordinates) |
204 | 74a0c9d6 | gaqhf | { |
205 | 9dab7146 | gaqhf | List<long> prevHandles = GetAllGroupHandles(); |
206 | 74a0c9d6 | gaqhf | |
207 | 9dab7146 | gaqhf | GUIUtils.SetPipeStyle(style); |
208 | 14540282 | gaqhf | Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
209 | 74a0c9d6 | gaqhf | Editor editor = acDoc.Editor; |
210 | List<object> commandParam = new List<object>(); |
||
211 | //command name |
||
212 | commandParam.Add("DRPIPE"); |
||
213 | //coordinate |
||
214 | foreach (var item in coordinates) |
||
215 | commandParam.Add(item); |
||
216 | //enter |
||
217 | commandParam.Add(null); |
||
218 | //enter |
||
219 | commandParam.Add(null); |
||
220 | |||
221 | editor.Command(commandParam.ToArray()); |
||
222 | |||
223 | 9dab7146 | gaqhf | List<long> newHandles = GetAllGroupHandles(); |
224 | List<long> otherHandles = new List<long>(); |
||
225 | foreach (var item in newHandles) |
||
226 | if (!prevHandles.Contains(item)) |
||
227 | otherHandles.Add(item); |
||
228 | 74a0c9d6 | gaqhf | |
229 | 9dab7146 | gaqhf | if (otherHandles.Count == 1) |
230 | return otherHandles[0]; |
||
231 | else |
||
232 | return 0; |
||
233 | 74a0c9d6 | gaqhf | } |
234 | 9dab7146 | gaqhf | private long DrawPipe(string style, List<string> coordinates, ObjectId objectId) |
235 | 74a0c9d6 | gaqhf | { |
236 | 9dab7146 | gaqhf | List<long> prevHandles = GetAllGroupHandles(); |
237 | 14540282 | gaqhf | GUIUtils.SetPipeStyle(style); |
238 | 74a0c9d6 | gaqhf | |
239 | 14540282 | gaqhf | Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
240 | 74a0c9d6 | gaqhf | Editor editor = acDoc.Editor; |
241 | List<object> commandParam = new List<object>(); |
||
242 | //command name |
||
243 | commandParam.Add("DRPIPE"); |
||
244 | //coordinate |
||
245 | foreach (var item in coordinates) |
||
246 | commandParam.Add(item); |
||
247 | //enter |
||
248 | commandParam.Add(null); |
||
249 | |||
250 | //Input Object ID |
||
251 | commandParam.Add(objectId); |
||
252 | |||
253 | //enter |
||
254 | commandParam.Add(null); |
||
255 | |||
256 | editor.Command(commandParam.ToArray()); |
||
257 | 9dab7146 | gaqhf | |
258 | List<long> newHandles = GetAllGroupHandles(); |
||
259 | List<long> otherHandles = new List<long>(); |
||
260 | foreach (var item in newHandles) |
||
261 | if (!prevHandles.Contains(item)) |
||
262 | otherHandles.Add(item); |
||
263 | |||
264 | if (otherHandles.Count == 1) |
||
265 | return otherHandles[0]; |
||
266 | else |
||
267 | return 0; |
||
268 | } |
||
269 | private long DrawSignal(string style, List<string> coordinates) |
||
270 | { |
||
271 | List<long> prevHandles = GetAllGroupHandles(); |
||
272 | |||
273 | GUIUtils.SetPipeStyle(style); |
||
274 | Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
||
275 | Editor editor = acDoc.Editor; |
||
276 | List<object> commandParam = new List<object>(); |
||
277 | //command name |
||
278 | commandParam.Add("VPESIGNAL"); |
||
279 | //coordinate |
||
280 | foreach (var item in coordinates) |
||
281 | commandParam.Add(item); |
||
282 | //enter |
||
283 | commandParam.Add(null); |
||
284 | |||
285 | editor.Command(commandParam.ToArray()); |
||
286 | |||
287 | List<long> newHandles = GetAllGroupHandles(); |
||
288 | List<long> otherHandles = new List<long>(); |
||
289 | foreach (var item in newHandles) |
||
290 | if (!prevHandles.Contains(item)) |
||
291 | otherHandles.Add(item); |
||
292 | |||
293 | if (otherHandles.Count == 1) |
||
294 | return otherHandles[0]; |
||
295 | else |
||
296 | return 0; |
||
297 | 74a0c9d6 | gaqhf | } |
298 | 495bb8f5 | gaqhf | private long InsertSymbol(Symbol symbol, DataRow symbolRow, bool needAngle, bool needBalloon) |
299 | 6b3cb476 | gaqhf | { |
300 | 73152510 | gaqhf | long handle = 0; |
301 | 6b3cb476 | gaqhf | try |
302 | { |
||
303 | 495bb8f5 | gaqhf | string insertSymbolName = symbol.Aveva.Name; |
304 | double x = symbol.Aveva.X; |
||
305 | double y = symbol.Aveva.Y; |
||
306 | |||
307 | 73152510 | gaqhf | List<long> prevHandles = GetAllBlockHandlesByName(insertSymbolName); |
308 | |||
309 | 6b3cb476 | gaqhf | AVEVA.PID.Utilities.DrawingData drawingData = new AVEVA.PID.Utilities.DrawingData(); |
310 | drawingData.InsertSymbolName = insertSymbolName; |
||
311 | Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
||
312 | Editor editor = acDoc.Editor; |
||
313 | List<object> commandParam = new List<object>(); |
||
314 | commandParam.Add("INSSYM"); |
||
315 | 495bb8f5 | gaqhf | |
316 | // Insert Point |
||
317 | 6b3cb476 | gaqhf | Point3d point = new Point3d(x, y, 0); |
318 | commandParam.Add(point); |
||
319 | 495bb8f5 | gaqhf | |
320 | // Check Insert Point |
||
321 | if (needAngle) |
||
322 | commandParam.Add(symbol.ANGLE); |
||
323 | |||
324 | // Check Balloon |
||
325 | if (needBalloon) |
||
326 | { |
||
327 | point = new Point3d(x + 20, y + 20, 0); |
||
328 | commandParam.Add(point); |
||
329 | |||
330 | // Check LeaderLine |
||
331 | if (GraphicalUtility.IsLeaderReqOnSymbolBalloons()) |
||
332 | commandParam.Add(x + "," + y); |
||
333 | } |
||
334 | |||
335 | for (int i = 0; i < 8; i++) |
||
336 | commandParam.Add(null); |
||
337 | 6b3cb476 | gaqhf | |
338 | editor.Command(commandParam.ToArray()); |
||
339 | 73152510 | gaqhf | |
340 | List<long> newHandles = GetAllBlockHandlesByName(insertSymbolName); |
||
341 | List<long> otherHandles = new List<long>(); |
||
342 | foreach (var item in newHandles) |
||
343 | if (!prevHandles.Contains(item)) |
||
344 | otherHandles.Add(item); |
||
345 | |||
346 | if (otherHandles.Count == 1) |
||
347 | return otherHandles[0]; |
||
348 | else |
||
349 | return 0; |
||
350 | 6b3cb476 | gaqhf | } |
351 | catch (System.Exception ex) |
||
352 | { |
||
353 | 74a0c9d6 | gaqhf | |
354 | 6b3cb476 | gaqhf | } |
355 | 73152510 | gaqhf | |
356 | return handle; |
||
357 | 6b3cb476 | gaqhf | } |
358 | 74a0c9d6 | gaqhf | #endregion |
359 | |||
360 | 73152510 | gaqhf | #region Autocad Utils |
361 | private List<long> GetAllBlockHandlesByName(string name) |
||
362 | { |
||
363 | List<long> handles = new List<long>(); |
||
364 | |||
365 | Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
||
366 | Database acCurDb = acDoc.Database; |
||
367 | using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
||
368 | { |
||
369 | BlockTable gd = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead); |
||
370 | foreach (var entry in gd) |
||
371 | { |
||
372 | BlockTableRecord blockTableRecord = acTrans.GetObject(entry, OpenMode.ForRead, true) as BlockTableRecord; |
||
373 | if (blockTableRecord != null) |
||
374 | { |
||
375 | IEnumerable<BlockReference> records = AcDbLinqExtensionMethods.GetBlockReferences(blockTableRecord); |
||
376 | foreach (var item in records) |
||
377 | { |
||
378 | if (item.Name == name) |
||
379 | handles.Add(item.Handle.Value); |
||
380 | } |
||
381 | } |
||
382 | } |
||
383 | |||
384 | acTrans.Commit(); |
||
385 | } |
||
386 | return handles; |
||
387 | } |
||
388 | 9dab7146 | gaqhf | private List<long> GetAllGroupHandles() |
389 | { |
||
390 | List<long> handles = new List<long>(); |
||
391 | |||
392 | Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
||
393 | Database acCurDb = acDoc.Database; |
||
394 | using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
||
395 | { |
||
396 | DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
||
397 | foreach (var entry in gd) |
||
398 | handles.Add(entry.Value.Handle.Value); |
||
399 | acTrans.Commit(); |
||
400 | } |
||
401 | return handles; |
||
402 | } |
||
403 | private ObjectId GetFirstPolyLine(long groupHandle) |
||
404 | { |
||
405 | ObjectId result = ObjectId.Null; |
||
406 | |||
407 | Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
||
408 | Database acCurDb = acDoc.Database; |
||
409 | using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
||
410 | { |
||
411 | DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
||
412 | foreach (var entry in gd) |
||
413 | { |
||
414 | if (entry.Value.Handle.Value == groupHandle) |
||
415 | { |
||
416 | Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group; |
||
417 | foreach (var item in group.GetAllEntityIds()) |
||
418 | { |
||
419 | if (acTrans.GetObject(item, OpenMode.ForRead).GetType() == typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)) |
||
420 | { |
||
421 | result = item; |
||
422 | break; |
||
423 | } |
||
424 | } |
||
425 | break; |
||
426 | } |
||
427 | } |
||
428 | acTrans.Commit(); |
||
429 | } |
||
430 | |||
431 | return result; |
||
432 | } |
||
433 | 2d09df82 | gaqhf | private ObjectId GetObjectIdByHandle(long lHandle) |
434 | { |
||
435 | Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
||
436 | Database acCurDb = acDoc.Database; |
||
437 | Handle handle = new Handle(lHandle); |
||
438 | |||
439 | return acCurDb.GetObjectId(false, handle, 0); |
||
440 | } |
||
441 | private void SetBlockReferenceRotation(ObjectId objectId, double rotation) |
||
442 | { |
||
443 | Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
||
444 | Database acCurDb = acDoc.Database; |
||
445 | Editor acDocEd = acDoc.Editor; |
||
446 | |||
447 | using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
||
448 | { |
||
449 | DBObject objDB = acTrans.GetObject(objectId, OpenMode.ForWrite); |
||
450 | if (objDB != null) |
||
451 | { |
||
452 | if (objDB.GetType() == typeof(BlockReference)) |
||
453 | { |
||
454 | BlockReference block = objDB as BlockReference; |
||
455 | block.Rotation = rotation; |
||
456 | } |
||
457 | } |
||
458 | acTrans.Commit(); |
||
459 | } |
||
460 | } |
||
461 | 016701e5 | gaqhf | #endregion |
462 | |||
463 | 73152510 | gaqhf | #region Modeling Utils |
464 | 016701e5 | gaqhf | private void GetConnectedGroupLine(Model.Line line, List<Model.Line> group, bool isInsert) |
465 | 53344e2c | gaqhf | { |
466 | if (!group.Contains(line)) |
||
467 | { |
||
468 | 016701e5 | gaqhf | if (isInsert) |
469 | 53344e2c | gaqhf | group.Insert(0, line); |
470 | else |
||
471 | group.Add(line); |
||
472 | |||
473 | 016701e5 | gaqhf | if (line.CONNECTORS[0].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[0].ConnectedObject)) |
474 | { |
||
475 | object connObj = line.CONNECTORS[0].ConnectedObject; |
||
476 | if (connObj.GetType() == typeof(Model.Line) && |
||
477 | APIDUtils.IsConnectedLine(connObj as Model.Line, line)) |
||
478 | GetConnectedGroupLine(connObj as Model.Line, group, true); |
||
479 | else if (connObj.GetType() == typeof(Model.Symbol)) |
||
480 | { |
||
481 | Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol); |
||
482 | if (connLine != null) |
||
483 | GetConnectedGroupLine(connLine as Model.Line, group, true); |
||
484 | } |
||
485 | } |
||
486 | if (line.CONNECTORS[1].ConnectedObject != null && !IsExistEndBreak(line, line.CONNECTORS[1].ConnectedObject)) |
||
487 | { |
||
488 | object connObj = line.CONNECTORS[1].ConnectedObject; |
||
489 | if (connObj.GetType() == typeof(Model.Line) && |
||
490 | APIDUtils.IsConnectedLine(connObj as Model.Line, line)) |
||
491 | GetConnectedGroupLine(connObj as Model.Line, group, false); |
||
492 | else if (connObj.GetType() == typeof(Model.Symbol)) |
||
493 | { |
||
494 | Model.Line connLine = FindOtherLineByRun(line, connObj as Symbol); |
||
495 | if (connLine != null) |
||
496 | GetConnectedGroupLine(connLine as Model.Line, group, false); |
||
497 | } |
||
498 | } |
||
499 | 53344e2c | gaqhf | } |
500 | } |
||
501 | private void GetGroupLinePoints(List<Model.Line> groupLine, List<string> points) |
||
502 | { |
||
503 | for (int i = 0; i < groupLine.Count; i++) |
||
504 | { |
||
505 | Model.Line line = groupLine[i]; |
||
506 | 016701e5 | gaqhf | if (i == 0) |
507 | f7e09e14 | gaqhf | { |
508 | object connObj = line.CONNECTORS[0].ConnectedObject; |
||
509 | if (connObj != null && connObj.GetType() == typeof(Symbol)) |
||
510 | { |
||
511 | Symbol connSymbol = connObj as Symbol; |
||
512 | Connector connector = connSymbol.CONNECTORS.Find(x => x.ConnectedObject == line); |
||
513 | |||
514 | SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y); |
||
515 | if (slopeType == SlopeType.HORIZONTAL) |
||
516 | ad3d070a | gaqhf | points.Add(connSymbol.Aveva.X + "," + line.Aveva.Start_Y); |
517 | f7e09e14 | gaqhf | else if (slopeType == SlopeType.VERTICAL) |
518 | ad3d070a | gaqhf | points.Add(line.Aveva.Start_X + "," + connSymbol.Aveva.Y); |
519 | f7e09e14 | gaqhf | else |
520 | points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
||
521 | } |
||
522 | else |
||
523 | points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
||
524 | } |
||
525 | 016701e5 | gaqhf | else |
526 | { |
||
527 | Model.Line prevLine = groupLine[i - 1]; |
||
528 | if (line.SlopeType == SlopeType.HORIZONTAL && |
||
529 | prevLine.SlopeType == SlopeType.VERTICAL) |
||
530 | points.Add(prevLine.Aveva.End_X + "," + line.Aveva.Start_Y); |
||
531 | else if (line.SlopeType == SlopeType.VERTICAL && |
||
532 | prevLine.SlopeType == SlopeType.HORIZONTAL) |
||
533 | points.Add(line.Aveva.Start_X + "," + prevLine.Aveva.End_Y); |
||
534 | else |
||
535 | points.Add(line.Aveva.Start_X + "," + line.Aveva.Start_Y); |
||
536 | } |
||
537 | |||
538 | 53344e2c | gaqhf | if (i == groupLine.Count - 1) |
539 | f7e09e14 | gaqhf | { |
540 | object connObj = line.CONNECTORS[1].ConnectedObject; |
||
541 | if (connObj != null && connObj.GetType() == typeof(Symbol)) |
||
542 | { |
||
543 | Symbol connSymbol = connObj as Symbol; |
||
544 | Connector connector = connSymbol.CONNECTORS.Find(x => x.ConnectedObject == line); |
||
545 | |||
546 | SlopeType slopeType = APIDUtils.CalcSlope(connSymbol.X, connSymbol.Y, connector.X, connector.Y); |
||
547 | if (slopeType == SlopeType.HORIZONTAL) |
||
548 | ad3d070a | gaqhf | points.Add(connSymbol.Aveva.X + "," + line.Aveva.End_Y); |
549 | f7e09e14 | gaqhf | else if (slopeType == SlopeType.VERTICAL) |
550 | ad3d070a | gaqhf | points.Add(line.Aveva.End_X + "," + connSymbol.Aveva.Y); |
551 | f7e09e14 | gaqhf | else |
552 | points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y); |
||
553 | } |
||
554 | else |
||
555 | points.Add(line.Aveva.End_X + "," + line.Aveva.End_Y); |
||
556 | } |
||
557 | 53344e2c | gaqhf | } |
558 | } |
||
559 | 016701e5 | gaqhf | private bool IsExistEndBreak(object item1, object item2) |
560 | { |
||
561 | bool result = false; |
||
562 | if (item1 != null && item2 != null) |
||
563 | { |
||
564 | EndBreak endBreak = document.EndBreaks.Find(x => |
||
565 | (APIDUtils.FindObjectByUID(document, x.OWNER) == item1 && |
||
566 | APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item2) || |
||
567 | (APIDUtils.FindObjectByUID(document, x.OWNER) == item2 && |
||
568 | APIDUtils.FindObjectByUID(document, x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE) == item1)); |
||
569 | |||
570 | if (endBreak != null) |
||
571 | result = true; |
||
572 | } |
||
573 | |||
574 | return result; |
||
575 | } |
||
576 | private LineRun FindLineRun(Model.Line line) |
||
577 | { |
||
578 | List<LineRun> allLineRun = new List<LineRun>(); |
||
579 | foreach (var item in document.LINENUMBERS) |
||
580 | allLineRun.AddRange(item.RUNS); |
||
581 | foreach (var item in document.TRIMLINES) |
||
582 | allLineRun.AddRange(item.RUNS); |
||
583 | |||
584 | return allLineRun.Find(x => x.RUNITEMS.Contains(line)); |
||
585 | } |
||
586 | private Model.Line FindOtherLineByRun(Model.Line line, Symbol symbol) |
||
587 | { |
||
588 | Model.Line result = null; |
||
589 | LineRun run = FindLineRun(line); |
||
590 | Connector connector = symbol.CONNECTORS.Find(x => |
||
591 | x.ConnectedObject != null && |
||
592 | x.ConnectedObject != line && |
||
593 | run.RUNITEMS.Contains(x.ConnectedObject) && |
||
594 | x.ConnectedObject.GetType() == typeof(Model.Line)); |
||
595 | |||
596 | if (connector != null) |
||
597 | result = connector.ConnectedObject as Model.Line; |
||
598 | 2d09df82 | gaqhf | |
599 | 016701e5 | gaqhf | return result; |
600 | } |
||
601 | 495bb8f5 | gaqhf | private bool CheckAngle(DataRow symbolRow) |
602 | { |
||
603 | return int.Parse(symbolRow["Insert_Points"].ToString()).Equals(2); |
||
604 | } |
||
605 | private bool CheckBalloon(Symbol symbol) |
||
606 | { |
||
607 | bool result = false; |
||
608 | |||
609 | string text = string.Empty; |
||
610 | Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name); |
||
611 | if (symbolDefinitions.Count != 0) |
||
612 | { |
||
613 | text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper(); |
||
614 | |||
615 | if (text.Equals("VLP")) |
||
616 | text = "VPO"; |
||
617 | else if (text.Equals("IVP")) |
||
618 | text = "IPO"; |
||
619 | |||
620 | if (SymbolCategory.astrLabelBalloonBlockNames.Contains(text)) |
||
621 | result = true;//dumbDwgUpgrade.addLabelBalloon(symbolForInsertion, objectId); |
||
622 | } |
||
623 | |||
624 | return result; |
||
625 | } |
||
626 | 53344e2c | gaqhf | #endregion |
627 | 9dab7146 | gaqhf | |
628 | 74a0c9d6 | gaqhf | #region Test Source |
629 | public void test() |
||
630 | { |
||
631 | 14540282 | gaqhf | //InitGUI(); |
632 | 9dab7146 | gaqhf | //DrawSignal("SONIC", new List<string>() { "2,100", "100,100" }); |
633 | 74a0c9d6 | gaqhf | |
634 | DrawPipe("Main Pipe", new List<string>() { "2,100", "100,100" }); |
||
635 | 495bb8f5 | gaqhf | Symbol symbol = new Symbol(); |
636 | symbol.Aveva = new AvevaSymbolInfo(); |
||
637 | symbol.Aveva.Name = "INVB"; |
||
638 | symbol.Aveva.X = 50; |
||
639 | symbol.Aveva.Y = 100; |
||
640 | |||
641 | DataRow[] allRows = AvevaSymbolTable.Select(string.Format("Symbol_Name = '{0}'", symbol.Aveva.Name)); |
||
642 | if (allRows.Length == 1) |
||
643 | { |
||
644 | DataRow symbolRow = allRows[0]; |
||
645 | bool bAngle = CheckAngle(symbolRow); |
||
646 | bool bBalloon = CheckBalloon(symbol); |
||
647 | bBalloon = false; |
||
648 | long handle = InsertSymbol(symbol, symbolRow, bAngle, bBalloon); |
||
649 | } |
||
650 | |||
651 | Hashtable symbolDefinitions = GraphicsUtility.GetSymbolDefinitions(symbol.Aveva.Name); |
||
652 | string text = symbolDefinitions[SymbolDefinition.symAflowType].ToString().ToUpper(); |
||
653 | if (int.Parse(symbolDefinitions[SymbolDefinition.symNoOfInsertPoints].ToString()).Equals(2)) |
||
654 | { |
||
655 | bool bIsPromptRotate = true; |
||
656 | } |
||
657 | int num = int.Parse(symbolDefinitions[SymbolDefinition.symFixedOrUserDefined].ToString()); |
||
658 | |||
659 | AssociateSymbolType eAssosiateSym = AssociateSymbolType.None; |
||
660 | bool bIsSymbolHasAssociation = GraphicalUtility.FindSymbolHasAssociation(text, ref eAssosiateSym); |
||
661 | bool bPlaceSimpleBlocks = SymbolCategory.astrSimplePlacedBlocks.Contains(text); |
||
662 | |||
663 | 9dab7146 | gaqhf | //DrawPipe("Main Pipe", new List<string>() { "2,200", "100,200" }); |
664 | 74a0c9d6 | gaqhf | |
665 | 9dab7146 | gaqhf | //DrawPipe("Sub Pipe", new List<string>() { "50,100", "50,200" }); |
666 | 74a0c9d6 | gaqhf | } |
667 | 9dab7146 | gaqhf | public static void TESTStatic() |
668 | 74a0c9d6 | gaqhf | { |
669 | 495bb8f5 | gaqhf | System.Data.DataTable avevaSymbolTable = Project_DB.SelectSymbolTable(); |
670 | AutoModeling auto = new AutoModeling(null, avevaSymbolTable); |
||
671 | 74a0c9d6 | gaqhf | auto.test(); |
672 | } |
||
673 | #endregion |
||
674 | } |
||
675 | 73152510 | gaqhf | |
676 | |||
677 | public static class AcDbLinqExtensionMethods |
||
678 | { |
||
679 | /// <summary> |
||
680 | /// Get all references to the given BlockTableRecord, including |
||
681 | /// references to anonymous dynamic BlockTableRecords. |
||
682 | /// </summary> |
||
683 | |||
684 | public static IEnumerable<BlockReference> GetBlockReferences( |
||
685 | this BlockTableRecord btr, |
||
686 | OpenMode mode = OpenMode.ForRead, |
||
687 | bool directOnly = true) |
||
688 | { |
||
689 | if (btr == null) |
||
690 | throw new ArgumentNullException("btr"); |
||
691 | var tr = btr.Database.TransactionManager.TopTransaction; |
||
692 | if (tr == null) |
||
693 | throw new InvalidOperationException("No transaction"); |
||
694 | var ids = btr.GetBlockReferenceIds(directOnly, true); |
||
695 | int cnt = ids.Count; |
||
696 | for (int i = 0; i < cnt; i++) |
||
697 | { |
||
698 | yield return (BlockReference) |
||
699 | tr.GetObject(ids[i], mode, false, false); |
||
700 | } |
||
701 | if (btr.IsDynamicBlock) |
||
702 | { |
||
703 | BlockTableRecord btr2 = null; |
||
704 | var blockIds = btr.GetAnonymousBlockIds(); |
||
705 | cnt = blockIds.Count; |
||
706 | for (int i = 0; i < cnt; i++) |
||
707 | { |
||
708 | btr2 = (BlockTableRecord)tr.GetObject(blockIds[i], |
||
709 | OpenMode.ForRead, false, false); |
||
710 | ids = btr2.GetBlockReferenceIds(directOnly, true); |
||
711 | int cnt2 = ids.Count; |
||
712 | for (int j = 0; j < cnt2; j++) |
||
713 | { |
||
714 | yield return (BlockReference) |
||
715 | tr.GetObject(ids[j], mode, false, false); |
||
716 | } |
||
717 | } |
||
718 | } |
||
719 | } |
||
720 | } |
||
721 | 74a0c9d6 | gaqhf | } |