hytos / DTI_PID / APIDConverter / PIDCustomization.cs @ 932933ed
이력 | 보기 | 이력해설 | 다운로드 (19.1 KB)
1 |
#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 |
using AVEVA.PID.CustomizationUtility.DB; |
24 |
using AVEVA.PID.CustomizationUtility.Model; |
25 |
using AVEVA.PID.CustomizationUtility.Properties; |
26 |
|
27 |
#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 |
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 |
#region Command |
112 |
/// <summary> |
113 |
/// This is test command which gives the idea regarding selection of an entity and assigning different color to it |
114 |
/// </summary> |
115 |
[CommandMethod("APIDConverter")] |
116 |
public static void APIDConverter() |
117 |
{ |
118 |
GetInstance(); |
119 |
GetProjectInfo(); |
120 |
APIDUtils.SetAvevaExplorer(); |
121 |
} |
122 |
[CommandMethod("AC")] |
123 |
public static void AC() |
124 |
{ |
125 |
GetInstance(); |
126 |
GetProjectInfo(); |
127 |
APIDUtils.SetAvevaExplorer(); |
128 |
} |
129 |
[CommandMethod("ConverterForm")] |
130 |
public static void ConverterForm() |
131 |
{ |
132 |
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 |
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 |
|
152 |
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 |
} |
163 |
} |
164 |
[CommandMethod("MappingForm")] |
165 |
public static void MappingForm() |
166 |
{ |
167 |
MappingForm form = new MappingForm(); |
168 |
if (AcadApp.ShowModalDialog(form) == DialogResult.OK) |
169 |
{ |
170 |
|
171 |
} |
172 |
} |
173 |
[CommandMethod("APPIDRun")] |
174 |
public static void APIDRun() |
175 |
{ |
176 |
if (AutoModeling.Running != null) |
177 |
{ |
178 |
GUIUtils.SetAutoLabelCheck(false); |
179 |
AutoModeling.Running.Run(); |
180 |
} |
181 |
} |
182 |
#endregion |
183 |
#region private methods |
184 |
private string SaveBinaryDrawing() |
185 |
{ |
186 |
FileStream fs = null; |
187 |
string strDrawing = string.Empty; |
188 |
try |
189 |
{ |
190 |
if (!string.IsNullOrEmpty(this.DrawingName)) |
191 |
{ |
192 |
string strDrawingname = this.DrawingName; |
193 |
//if(!string.IsNullOrEmpty(this.MajorRevision)) |
194 |
//{ |
195 |
// strDrawingname = strDrawingname + "_" + this.MajorRevision; |
196 |
//} |
197 |
|
198 |
byte[] drawingData = this.DrawingData; |
199 |
if (null != drawingData) |
200 |
{ |
201 |
try |
202 |
{ |
203 |
if (File.Exists(strDrawingname) == true) |
204 |
{ |
205 |
Microsoft.VisualBasic.FileSystem.Kill(strDrawingname); |
206 |
} |
207 |
} |
208 |
catch (System.Exception ) |
209 |
{ |
210 |
} |
211 |
|
212 |
strDrawing = strDrawingname; |
213 |
string path = GetDrawingSavePath(); |
214 |
if (!string.IsNullOrEmpty(path)) |
215 |
{ |
216 |
strDrawing = path + "\\" + strDrawingname; |
217 |
} |
218 |
strDrawing += ".dwg"; |
219 |
|
220 |
fs = new FileStream(strDrawing, FileMode.Create, FileAccess.Write); |
221 |
int ArraySize = new int(); |
222 |
ArraySize = drawingData.GetUpperBound(0); |
223 |
fs.Write(drawingData, 0, ArraySize); |
224 |
} |
225 |
} |
226 |
} |
227 |
catch (System.Exception ) |
228 |
{ } |
229 |
finally |
230 |
{ |
231 |
if (fs != null) |
232 |
fs.Close(); |
233 |
} |
234 |
return strDrawing; |
235 |
} |
236 |
|
237 |
private void ReadDrawingDetails(string strRecordId) |
238 |
{ |
239 |
SqlConnection connection = null; |
240 |
try |
241 |
{ |
242 |
int ID = 0; |
243 |
int.TryParse(strRecordId, out ID); |
244 |
if (ID > 0) |
245 |
{ |
246 |
connection = new SqlConnection(); |
247 |
connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString(); |
248 |
connection.Open(); |
249 |
|
250 |
string strQry = "Select * from SynchroniseDetails where ID = " + ID; |
251 |
SqlCommand cmd = new SqlCommand(); |
252 |
cmd.Connection = connection; |
253 |
cmd.CommandText = strQry; |
254 |
cmd.CommandType = CommandType.Text; |
255 |
SqlDataReader reader = cmd.ExecuteReader(); |
256 |
|
257 |
if (null != reader && reader.HasRows) |
258 |
{ |
259 |
while (reader.Read()) |
260 |
{ |
261 |
ProjectName = reader["ProjectName"].ToString(); |
262 |
DrawingID = reader["DrawingId"].ToString(); |
263 |
DrawingName = reader["DrawingName"].ToString(); |
264 |
MajorRevision = reader["Revision"].ToString(); |
265 |
MinorRevision = reader["MinorRevision"].ToString(); |
266 |
Description = reader["Description"].ToString(); |
267 |
NameOfUser = reader["NameOfUser"].ToString(); |
268 |
ClientDwgNo = reader["ClientDwgNo"].ToString(); |
269 |
RevisedBy = reader["RevisedBy"].ToString(); |
270 |
CheckedBy = reader["CheckedBy"].ToString(); |
271 |
ApprovedBy = reader["ApprovedBy"].ToString(); |
272 |
ApprovedBy2 = reader["ApprovedBy2"].ToString(); |
273 |
ApprovedBy3 = reader["ApprovedBy3"].ToString(); |
274 |
ClientApprovedBy= reader["ClientApprovedBy"].ToString(); |
275 |
DrawingData = reader["DrawingData"] != null ? (byte[])reader["DrawingData"] : null; |
276 |
} |
277 |
} |
278 |
} |
279 |
} |
280 |
catch (System.Exception ) |
281 |
{ } |
282 |
finally |
283 |
{ |
284 |
if (connection != null) |
285 |
connection.Close(); |
286 |
} |
287 |
} |
288 |
private string GetRecordId() |
289 |
{ |
290 |
string strRecordId = string.Empty; |
291 |
SqlConnection connection = null; |
292 |
try |
293 |
{ |
294 |
connection = new SqlConnection(); |
295 |
connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString(); |
296 |
connection.Open(); |
297 |
|
298 |
string strQry = "Select Max(ID) from SynchroniseDetails"; |
299 |
SqlCommand cmd = new SqlCommand(); |
300 |
cmd.Connection = connection; |
301 |
cmd.CommandText = strQry; |
302 |
cmd.CommandType = CommandType.Text; |
303 |
SqlDataReader reader = cmd.ExecuteReader(); |
304 |
if (null != reader && reader.HasRows) |
305 |
{ |
306 |
strRecordId = reader.GetString(0).ToString(); |
307 |
} |
308 |
} |
309 |
catch (System.Exception ) |
310 |
{ } |
311 |
finally |
312 |
{ |
313 |
if (connection != null) |
314 |
connection.Close(); |
315 |
} |
316 |
return strRecordId; |
317 |
} |
318 |
private string GetConnectionString() |
319 |
{ |
320 |
string strConn = string.Empty; |
321 |
if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
322 |
{ |
323 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName); |
324 |
} |
325 |
else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
326 |
{ |
327 |
string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
328 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString); |
329 |
} |
330 |
|
331 |
return strConn; |
332 |
} |
333 |
|
334 |
private string GetConnectionString_Reports() |
335 |
{ |
336 |
string strConn = string.Empty; |
337 |
if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
338 |
{ |
339 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName); |
340 |
} |
341 |
else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
342 |
{ |
343 |
string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
344 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString); |
345 |
} |
346 |
|
347 |
return strConn; |
348 |
} |
349 |
|
350 |
private string GetConnectionString_Admin() |
351 |
{ |
352 |
string strConn = string.Empty; |
353 |
if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
354 |
{ |
355 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName); |
356 |
} |
357 |
else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
358 |
{ |
359 |
string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
360 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString); |
361 |
} |
362 |
|
363 |
return strConn; |
364 |
} |
365 |
|
366 |
private string GetDrawingSavePath() |
367 |
{ |
368 |
return System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); |
369 |
} |
370 |
|
371 |
/// <summary> |
372 |
/// Sets the color of the entity |
373 |
/// </summary> |
374 |
/// <param name="objIdBlockId">block id</param> |
375 |
/// <param name="iColor">color to be assigned</param> |
376 |
private void SetColorForEntity(ObjectId objIdBlockId, short iColor) |
377 |
{ |
378 |
Transaction trans = null; |
379 |
try |
380 |
{ |
381 |
if (objIdBlockId != ObjectId.Null) |
382 |
{ |
383 |
Autodesk.AutoCAD.ApplicationServices.Document doc = AcadApp.DocumentManager.MdiActiveDocument; |
384 |
Database db = HostApplicationServices.WorkingDatabase; |
385 |
trans = doc.TransactionManager.StartTransaction(); |
386 |
using (doc.LockDocument()) |
387 |
{ |
388 |
using (trans) |
389 |
{ |
390 |
if (objIdBlockId.IsErased == false) |
391 |
{ |
392 |
Entity objDb = (Entity)trans.GetObject(objIdBlockId, OpenMode.ForWrite); |
393 |
if (objDb != null) |
394 |
{ |
395 |
objDb.ColorIndex = iColor; |
396 |
} |
397 |
} |
398 |
trans.Commit(); |
399 |
} |
400 |
} |
401 |
} |
402 |
} |
403 |
catch (System.Exception ) |
404 |
{ |
405 |
} |
406 |
finally |
407 |
{ |
408 |
if (trans != null) |
409 |
{ |
410 |
trans.Dispose(); |
411 |
} |
412 |
} |
413 |
} |
414 |
#endregion |
415 |
|
416 |
|
417 |
|
418 |
#region TEST |
419 |
[CommandMethod("TestS", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)] |
420 |
public static void TestSelection() |
421 |
{ |
422 |
AccessPropertyForm.Run(); |
423 |
|
424 |
//GUIUtils.SetAutoLabelCheck(false); |
425 |
////APIDUtils.CreateDrawingAndOpen("testA1", "1", "Aveva-Metric", "1"); |
426 |
AutoModeling.TESTStatic(); |
427 |
|
428 |
AccessPropertyForm.Stop(); |
429 |
} |
430 |
|
431 |
[CommandMethod("PicTest", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)] |
432 |
public static void PicTest() |
433 |
{ |
434 |
// Get the current document and database, and start a transaction |
435 |
Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; |
436 |
Database acCurDb = acDoc.Database; |
437 |
Editor acDocEd = acDoc.Editor; |
438 |
|
439 |
// Request for objects to be selected in the drawing area |
440 |
PromptSelectionResult acSSPrompt = acDocEd.GetSelection(); |
441 |
|
442 |
// If the prompt status is OK, objects were selected |
443 |
if (acSSPrompt.Status == PromptStatus.OK) |
444 |
{ |
445 |
ObjectId[] selectedObjectIds = acSSPrompt.Value.GetObjectIds(); |
446 |
// Get the last selected entity |
447 |
|
448 |
if (selectedObjectIds.Length > 0) |
449 |
{ |
450 |
ObjectId objIdBlock = selectedObjectIds[0]; |
451 |
|
452 |
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) |
453 |
{ |
454 |
DBDictionary gd = (DBDictionary)acTrans.GetObject(acCurDb.GroupDictionaryId, OpenMode.ForRead); |
455 |
|
456 |
DBObject objDB = acTrans.GetObject(objIdBlock, OpenMode.ForRead, true); |
457 |
if (objDB != null) |
458 |
{ |
459 |
|
460 |
foreach (var entry in gd) |
461 |
{ |
462 |
Autodesk.AutoCAD.DatabaseServices.Group group = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Group; |
463 |
ObjectId[] ids = group.GetAllEntityIds(); |
464 |
foreach (var item in group.GetAllEntityIds()) |
465 |
{ |
466 |
|
467 |
} |
468 |
} |
469 |
// check if the selected entity is of Type BlockReference |
470 |
//if (objDB.GetType() == typeof(BlockReference)) |
471 |
//{ |
472 |
// BlockReference block = objDB as BlockReference; |
473 |
|
474 |
// // open an entity in write mode so that we can modify its color |
475 |
// //Entity objDb = (Entity)acTrans.GetObject(objIdBlock, OpenMode.ForWrite); |
476 |
// //if (objDb != null) |
477 |
// //{ |
478 |
// // objDb.ColorIndex = 0;// white color |
479 |
// //} |
480 |
//} |
481 |
} |
482 |
acTrans.Commit(); |
483 |
} |
484 |
} |
485 |
} |
486 |
} |
487 |
|
488 |
|
489 |
#endregion |
490 |
} |
491 |
} |