hytos / DTI_PID / APIDConverter / PIDCustomization.cs @ faba1fc7
이력 | 보기 | 이력해설 | 다운로드 (19.2 KB)
1 | 6348c496 | gaqhf | #region namespaces |
---|---|---|---|
2 | using System; |
||
3 | using System.Collections.Generic; |
||
4 | using System.Configuration; |
||
5 | using System.Data; |
||
6 | using System.Data.Common; |
||
7 | using System.Data.SqlClient; |
||
8 | using System.IO; |
||
9 | using System.Linq; |
||
10 | using System.Runtime.InteropServices; |
||
11 | using System.Text; |
||
12 | using System.Threading; |
||
13 | using System.Windows.Forms; |
||
14 | using Autodesk.AutoCAD.ApplicationServices; |
||
15 | using Autodesk.AutoCAD.DatabaseServices; |
||
16 | using Autodesk.AutoCAD.EditorInput; |
||
17 | using Autodesk.AutoCAD.Geometry; |
||
18 | using Autodesk.AutoCAD.Interop; |
||
19 | using Autodesk.AutoCAD.Interop.Common; |
||
20 | using Autodesk.AutoCAD.Runtime; |
||
21 | using Autodesk.AutoCAD.Windows; |
||
22 | using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application; |
||
23 | 8562d7dc | gaqhf | using AVEVA.PID.CustomizationUtility.DB; |
24 | using AVEVA.PID.CustomizationUtility.Model; |
||
25 | using AVEVA.PID.CustomizationUtility.Properties; |
||
26 | |||
27 | 6348c496 | gaqhf | #endregion using namespaces |
28 | namespace AVEVA.PID.CustomizationUtility |
||
29 | { |
||
30 | /// <summary> |
||
31 | /// Singleton class used for customization of P&ID application |
||
32 | /// </summary> |
||
33 | public class PIDCustomization |
||
34 | { |
||
35 | public string ProjectName { get; set; } |
||
36 | public string DrawingID { get; set; } |
||
37 | public string DrawingName { get; set; } |
||
38 | public string MajorRevision { get; set; } |
||
39 | public string MinorRevision { get; set; } |
||
40 | public string Description { get; set; } |
||
41 | public string NameOfUser { get; set; } |
||
42 | public string ClientDwgNo { get; set; } |
||
43 | public string RevisedBy { get; set; } |
||
44 | public string CheckedBy { get; set; } |
||
45 | public string ApprovedBy { get; set; } |
||
46 | public string ApprovedBy2 { get; set; } |
||
47 | public string ApprovedBy3 { get; set; } |
||
48 | public string ClientApprovedBy { get; set; } |
||
49 | public byte[] DrawingData { get; set; } |
||
50 | |||
51 | private static PIDCustomization _instance; |
||
52 | /// <summary> |
||
53 | /// private Constructor make it singleton |
||
54 | /// </summary> |
||
55 | private PIDCustomization() |
||
56 | { |
||
57 | DrawingData = null; |
||
58 | } |
||
59 | |||
60 | public static PIDCustomization GetInstance() |
||
61 | { |
||
62 | if (_instance == null) |
||
63 | { |
||
64 | _instance = new PIDCustomization(); |
||
65 | } |
||
66 | return _instance; |
||
67 | } |
||
68 | |||
69 | /// <summary> |
||
70 | /// Public method called on issue drawing in P&ID |
||
71 | /// </summary> |
||
72 | /// <param name="strRecordId">unique database table id for revision</param> |
||
73 | public void ProcessIssueDrawing(string strRecordId) |
||
74 | { |
||
75 | if (!string.IsNullOrEmpty(strRecordId)) |
||
76 | { |
||
77 | ReadDrawingDetails(strRecordId); |
||
78 | SaveBinaryDrawing(); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /// <summary> |
||
83 | /// public method used for processing block |
||
84 | /// </summary> |
||
85 | /// <param name="BlockId"></param> |
||
86 | public void ProcessBlock(ObjectId BlockId) |
||
87 | { |
||
88 | SetColorForEntity(BlockId,0);// white color |
||
89 | } |
||
90 | |||
91 | d327a608 | gaqhf | public static void GetProjectInfo() |
92 | { |
||
93 | Project_Info _ProjectInfo = Project_Info.GetInstance(); |
||
94 | _ProjectInfo.DefaultPath = Settings.Default.ProjectPath; |
||
95 | _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType; |
||
96 | _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP; |
||
97 | _ProjectInfo.Port = Settings.Default.ProjectPort; |
||
98 | _ProjectInfo.DBUser = Settings.Default.ProjectDBUser; |
||
99 | _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword; |
||
100 | |||
101 | if (Project_DB.ConnTestAndCreateTable()) |
||
102 | { |
||
103 | _ProjectInfo.Enable = true; |
||
104 | } |
||
105 | else |
||
106 | { |
||
107 | _ProjectInfo.Enable = false; |
||
108 | MessageBox.Show("Fail project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
109 | } |
||
110 | } |
||
111 | e3ced1c9 | gaqhf | #region Command |
112 | 6348c496 | gaqhf | /// <summary> |
113 | /// This is test command which gives the idea regarding selection of an entity and assigning different color to it |
||
114 | /// </summary> |
||
115 | 74a0c9d6 | gaqhf | [CommandMethod("APIDConverter")] |
116 | public static void APIDConverter() |
||
117 | 6348c496 | gaqhf | { |
118 | d327a608 | gaqhf | GetInstance(); |
119 | GetProjectInfo(); |
||
120 | 88cb9898 | gaqhf | APIDUtils.SetAvevaExplorer(); |
121 | aabc2b2d | gaqhf | } |
122 | dc270d65 | gaqhf | [CommandMethod("AC")] |
123 | public static void AC() |
||
124 | { |
||
125 | e3ced1c9 | gaqhf | GetInstance(); |
126 | d327a608 | gaqhf | GetProjectInfo(); |
127 | 88cb9898 | gaqhf | APIDUtils.SetAvevaExplorer(); |
128 | dc270d65 | gaqhf | } |
129 | aabc2b2d | gaqhf | [CommandMethod("ConverterForm")] |
130 | 74a0c9d6 | gaqhf | public static void ConverterForm() |
131 | aabc2b2d | gaqhf | { |
132 | dc270d65 | gaqhf | APIDConverter form = new APIDConverter(); |
133 | if (AcadApp.ShowModalDialog(form) == DialogResult.OK) |
||
134 | { |
||
135 | |||
136 | } |
||
137 | } |
||
138 | [CommandMethod("ProjectForm")] |
||
139 | public static void ProjectForm() |
||
140 | { |
||
141 | ProjectForm form = new ProjectForm(); |
||
142 | if (AcadApp.ShowModalDialog(form) == DialogResult.OK) |
||
143 | { |
||
144 | 8562d7dc | gaqhf | Project_Info _ProjectInfo = Project_Info.GetInstance(); |
145 | _ProjectInfo.DefaultPath = Settings.Default.ProjectPath; |
||
146 | _ProjectInfo.DBType = (ID2DB_Type)Settings.Default.ProjectDBType; |
||
147 | _ProjectInfo.ServerIP = Settings.Default.ProjectServerIP; |
||
148 | _ProjectInfo.Port = Settings.Default.ProjectPort; |
||
149 | _ProjectInfo.DBUser = Settings.Default.ProjectDBUser; |
||
150 | _ProjectInfo.DBPassword = Settings.Default.ProjectDBPassword; |
||
151 | dc270d65 | gaqhf | |
152 | 8562d7dc | gaqhf | if (Project_DB.ConnTestAndCreateTable()) |
153 | { |
||
154 | _ProjectInfo.Enable = true; |
||
155 | MessageBox.Show("Success project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Information); |
||
156 | } |
||
157 | else |
||
158 | { |
||
159 | _ProjectInfo.Enable = false; |
||
160 | MessageBox.Show("Fail project setting", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
161 | } |
||
162 | dc270d65 | gaqhf | } |
163 | } |
||
164 | [CommandMethod("MappingForm")] |
||
165 | public static void MappingForm() |
||
166 | { |
||
167 | MappingForm form = new MappingForm(); |
||
168 | if (AcadApp.ShowModalDialog(form) == DialogResult.OK) |
||
169 | 74a0c9d6 | gaqhf | { |
170 | |||
171 | } |
||
172 | 6348c496 | gaqhf | } |
173 | 1c5bd296 | gaqhf | [CommandMethod("APPIDRun")] |
174 | public static void APIDRun() |
||
175 | { |
||
176 | if (AutoModeling.Running != null) |
||
177 | { |
||
178 | 932933ed | gaqhf | GUIUtils.SetAutoLabelCheck(false); |
179 | 1c5bd296 | gaqhf | AutoModeling.Running.Run(); |
180 | } |
||
181 | } |
||
182 | d03dde83 | gaqhf | [CommandMethod("GIFF")] |
183 | public static void GetAttributeInformationFromForm() |
||
184 | { |
||
185 | // Get the current document and database, and start a transaction |
||
186 | Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
||
187 | Database acCurDb = acDoc.Database; |
||
188 | Editor acDocEd = acDoc.Editor; |
||
189 | |||
190 | // Request for objects to be selected in the drawing area |
||
191 | PromptSelectionResult acSSPrompt = acDocEd.GetSelection(); |
||
192 | |||
193 | // If the prompt status is OK, objects were selected |
||
194 | if (acSSPrompt.Status == PromptStatus.OK) |
||
195 | { |
||
196 | ObjectId[] selectedObjectIds = acSSPrompt.Value.GetObjectIds(); |
||
197 | |||
198 | if (selectedObjectIds.Length > 0) |
||
199 | { |
||
200 | ObjectId objIdBlock = selectedObjectIds[0]; |
||
201 | Entity entity = APIDUtils.GetEntityByHandle(objIdBlock.Handle.Value); |
||
202 | |||
203 | GUI.ComponentPropertiesHelper componentPropertiesHelper = new GUI.ComponentPropertiesHelper(); |
||
204 | AvevaThread.Run(ThreadType.GetAttributeInformationFromForm, null); |
||
205 | if (!componentPropertiesHelper.DisplayComponentProperties(entity)) |
||
206 | { |
||
207 | AvevaThread.Stop(ThreadType.GetAttributeInformationFromForm); |
||
208 | MessageBox.Show("Fail Save Attribute!", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
209 | } |
||
210 | else |
||
211 | MessageBox.Show("Success Save Attribute!", "APID Converter", MessageBoxButtons.OK, MessageBoxIcon.Information); |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | e3ced1c9 | gaqhf | #endregion |
216 | 6348c496 | gaqhf | #region private methods |
217 | private string SaveBinaryDrawing() |
||
218 | { |
||
219 | FileStream fs = null; |
||
220 | string strDrawing = string.Empty; |
||
221 | try |
||
222 | { |
||
223 | if (!string.IsNullOrEmpty(this.DrawingName)) |
||
224 | { |
||
225 | string strDrawingname = this.DrawingName; |
||
226 | //if(!string.IsNullOrEmpty(this.MajorRevision)) |
||
227 | //{ |
||
228 | // strDrawingname = strDrawingname + "_" + this.MajorRevision; |
||
229 | //} |
||
230 | |||
231 | byte[] drawingData = this.DrawingData; |
||
232 | if (null != drawingData) |
||
233 | { |
||
234 | try |
||
235 | { |
||
236 | if (File.Exists(strDrawingname) == true) |
||
237 | { |
||
238 | Microsoft.VisualBasic.FileSystem.Kill(strDrawingname); |
||
239 | } |
||
240 | } |
||
241 | catch (System.Exception ) |
||
242 | { |
||
243 | } |
||
244 | |||
245 | strDrawing = strDrawingname; |
||
246 | string path = GetDrawingSavePath(); |
||
247 | if (!string.IsNullOrEmpty(path)) |
||
248 | { |
||
249 | strDrawing = path + "\\" + strDrawingname; |
||
250 | } |
||
251 | strDrawing += ".dwg"; |
||
252 | |||
253 | fs = new FileStream(strDrawing, FileMode.Create, FileAccess.Write); |
||
254 | int ArraySize = new int(); |
||
255 | ArraySize = drawingData.GetUpperBound(0); |
||
256 | fs.Write(drawingData, 0, ArraySize); |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | catch (System.Exception ) |
||
261 | { } |
||
262 | finally |
||
263 | { |
||
264 | if (fs != null) |
||
265 | fs.Close(); |
||
266 | } |
||
267 | return strDrawing; |
||
268 | } |
||
269 | |||
270 | private void ReadDrawingDetails(string strRecordId) |
||
271 | { |
||
272 | SqlConnection connection = null; |
||
273 | try |
||
274 | { |
||
275 | int ID = 0; |
||
276 | int.TryParse(strRecordId, out ID); |
||
277 | if (ID > 0) |
||
278 | { |
||
279 | connection = new SqlConnection(); |
||
280 | connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString(); |
||
281 | connection.Open(); |
||
282 | |||
283 | string strQry = "Select * from SynchroniseDetails where ID = " + ID; |
||
284 | SqlCommand cmd = new SqlCommand(); |
||
285 | cmd.Connection = connection; |
||
286 | cmd.CommandText = strQry; |
||
287 | cmd.CommandType = CommandType.Text; |
||
288 | SqlDataReader reader = cmd.ExecuteReader(); |
||
289 | |||
290 | if (null != reader && reader.HasRows) |
||
291 | { |
||
292 | while (reader.Read()) |
||
293 | { |
||
294 | ProjectName = reader["ProjectName"].ToString(); |
||
295 | DrawingID = reader["DrawingId"].ToString(); |
||
296 | DrawingName = reader["DrawingName"].ToString(); |
||
297 | MajorRevision = reader["Revision"].ToString(); |
||
298 | MinorRevision = reader["MinorRevision"].ToString(); |
||
299 | Description = reader["Description"].ToString(); |
||
300 | NameOfUser = reader["NameOfUser"].ToString(); |
||
301 | ClientDwgNo = reader["ClientDwgNo"].ToString(); |
||
302 | RevisedBy = reader["RevisedBy"].ToString(); |
||
303 | CheckedBy = reader["CheckedBy"].ToString(); |
||
304 | ApprovedBy = reader["ApprovedBy"].ToString(); |
||
305 | ApprovedBy2 = reader["ApprovedBy2"].ToString(); |
||
306 | ApprovedBy3 = reader["ApprovedBy3"].ToString(); |
||
307 | ClientApprovedBy= reader["ClientApprovedBy"].ToString(); |
||
308 | DrawingData = reader["DrawingData"] != null ? (byte[])reader["DrawingData"] : null; |
||
309 | } |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 | catch (System.Exception ) |
||
314 | { } |
||
315 | finally |
||
316 | { |
||
317 | if (connection != null) |
||
318 | connection.Close(); |
||
319 | } |
||
320 | } |
||
321 | private string GetRecordId() |
||
322 | { |
||
323 | string strRecordId = string.Empty; |
||
324 | SqlConnection connection = null; |
||
325 | try |
||
326 | { |
||
327 | connection = new SqlConnection(); |
||
328 | connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString(); |
||
329 | connection.Open(); |
||
330 | |||
331 | string strQry = "Select Max(ID) from SynchroniseDetails"; |
||
332 | SqlCommand cmd = new SqlCommand(); |
||
333 | cmd.Connection = connection; |
||
334 | cmd.CommandText = strQry; |
||
335 | cmd.CommandType = CommandType.Text; |
||
336 | SqlDataReader reader = cmd.ExecuteReader(); |
||
337 | if (null != reader && reader.HasRows) |
||
338 | { |
||
339 | strRecordId = reader.GetString(0).ToString(); |
||
340 | } |
||
341 | } |
||
342 | catch (System.Exception ) |
||
343 | { } |
||
344 | finally |
||
345 | { |
||
346 | if (connection != null) |
||
347 | connection.Close(); |
||
348 | } |
||
349 | return strRecordId; |
||
350 | } |
||
351 | private string GetConnectionString() |
||
352 | { |
||
353 | string strConn = string.Empty; |
||
354 | if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
||
355 | { |
||
356 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName); |
||
357 | } |
||
358 | else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
||
359 | { |
||
360 | string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
||
361 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString); |
||
362 | } |
||
363 | |||
364 | return strConn; |
||
365 | } |
||
366 | |||
367 | private string GetConnectionString_Reports() |
||
368 | { |
||
369 | string strConn = string.Empty; |
||
370 | if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
||
371 | { |
||
372 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName); |
||
373 | } |
||
374 | else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
||
375 | { |
||
376 | string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
||
377 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString); |
||
378 | } |
||
379 | |||
380 | return strConn; |
||
381 | } |
||
382 | |||
383 | e9ed6b7b | gaqhf | private string GetConnectionString_Admin() |
384 | { |
||
385 | string strConn = string.Empty; |
||
386 | if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
||
387 | { |
||
388 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName); |
||
389 | } |
||
390 | else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
||
391 | { |
||
392 | string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
||
393 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString); |
||
394 | } |
||
395 | |||
396 | return strConn; |
||
397 | } |
||
398 | |||
399 | 6348c496 | gaqhf | private string GetDrawingSavePath() |
400 | { |
||
401 | return System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); |
||
402 | } |
||
403 | |||
404 | /// <summary> |
||
405 | /// Sets the color of the entity |
||
406 | /// </summary> |
||
407 | /// <param name="objIdBlockId">block id</param> |
||
408 | /// <param name="iColor">color to be assigned</param> |
||
409 | private void SetColorForEntity(ObjectId objIdBlockId, short iColor) |
||
410 | { |
||
411 | Transaction trans = null; |
||
412 | try |
||
413 | { |
||
414 | if (objIdBlockId != ObjectId.Null) |
||
415 | { |
||
416 | 8562d7dc | gaqhf | Autodesk.AutoCAD.ApplicationServices.Document doc = AcadApp.DocumentManager.MdiActiveDocument; |
417 | 6348c496 | gaqhf | Database db = HostApplicationServices.WorkingDatabase; |
418 | trans = doc.TransactionManager.StartTransaction(); |
||
419 | using (doc.LockDocument()) |
||
420 | { |
||
421 | using (trans) |
||
422 | { |
||
423 | if (objIdBlockId.IsErased == false) |
||
424 | { |
||
425 | Entity objDb = (Entity)trans.GetObject(objIdBlockId, OpenMode.ForWrite); |
||
426 | if (objDb != null) |
||
427 | { |
||
428 | objDb.ColorIndex = iColor; |
||
429 | } |
||
430 | } |
||
431 | trans.Commit(); |
||
432 | } |
||
433 | } |
||
434 | } |
||
435 | } |
||
436 | catch (System.Exception ) |
||
437 | { |
||
438 | } |
||
439 | finally |
||
440 | { |
||
441 | if (trans != null) |
||
442 | { |
||
443 | trans.Dispose(); |
||
444 | } |
||
445 | } |
||
446 | } |
||
447 | #endregion |
||
448 | 6b3cb476 | gaqhf | |
449 | |||
450 | |||
451 | #region TEST |
||
452 | [CommandMethod("TestS", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)] |
||
453 | public static void TestSelection() |
||
454 | { |
||
455 | c94252bf | gaqhf | AvevaThread.Run(ThreadType.AddProperty | ThreadType.LineNumberModeling, null); |
456 | c8b025e0 | gaqhf | AvevaThread.Run(ThreadType.AddProperty, null); |
457 | AvevaThread.Run(ThreadType.AddProperty | ThreadType.None, null); |
||
458 | AvevaThread.Run(ThreadType.AddProperty | ThreadType.AddProperty, null); |
||
459 | 9dab7146 | gaqhf | } |
460 | |||
461 | [CommandMethod("PicTest", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)] |
||
462 | public static void PicTest() |
||
463 | { |
||
464 | c94252bf | gaqhf | // Get the current document and database, and start a transaction |
465 | Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
||
466 | Database acCurDb = acDoc.Database; |
||
467 | Editor acDocEd = acDoc.Editor; |
||
468 | |||
469 | // Request for objects to be selected in the drawing area |
||
470 | PromptSelectionResult acSSPrompt = acDocEd.GetSelection(); |
||
471 | 6b3cb476 | gaqhf | |
472 | |||
473 | c94252bf | gaqhf | // If the prompt status is OK, objects were selected |
474 | if (acSSPrompt.Status == PromptStatus.OK) |
||
475 | { |
||
476 | ObjectId[] selectedObjectIds = acSSPrompt.Value.GetObjectIds(); |
||
477 | // Get the last selected entity |
||
478 | |||
479 | if (selectedObjectIds.Length > 0) |
||
480 | { |
||
481 | ObjectId objIdBlock = selectedObjectIds[0]; |
||
482 | AutoModeling.TESTStatic(objIdBlock.Handle.Value); |
||
483 | } |
||
484 | } |
||
485 | } |
||
486 | 6b3cb476 | gaqhf | #endregion |
487 | 6348c496 | gaqhf | } |
488 | } |