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