프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / PIDCustomization.cs @ 6348c496

이력 | 보기 | 이력해설 | 다운로드 (13.6 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
#endregion using namespaces
24
namespace AVEVA.PID.CustomizationUtility
25
{
26
    /// <summary>
27
    /// Singleton class used for customization of P&ID application
28
    /// </summary>
29
    public class PIDCustomization
30
    {
31
        public string ProjectName   { get; set; }
32
        public string DrawingID     { get; set; }
33
        public string DrawingName   { get; set; }
34
        public string MajorRevision { get; set; }
35
        public string MinorRevision { get; set; }
36
        public string Description   { get; set; }
37
        public string NameOfUser    { get; set; }
38
        public string ClientDwgNo   { get; set; }
39
        public string RevisedBy     { get; set; }
40
        public string CheckedBy     { get; set; }
41
        public string ApprovedBy    { get; set; }
42
        public string ApprovedBy2   { get; set; }
43
        public string ApprovedBy3   { get; set; }
44
        public string ClientApprovedBy { get; set; }
45
        public byte[] DrawingData   { get; set; }
46

    
47
        private static PIDCustomization _instance;
48
        /// <summary>
49
        /// private Constructor make it singleton
50
        /// </summary>
51
        private PIDCustomization()
52
        {
53
            DrawingData = null;
54
        }
55

    
56
        public static PIDCustomization GetInstance()
57
        {
58
            if (_instance == null)
59
            {
60
                _instance = new PIDCustomization();
61
            }
62
            return _instance;           
63
        }
64

    
65
        /// <summary>
66
        /// Public method called on issue drawing in P&ID
67
        /// </summary>
68
        /// <param name="strRecordId">unique database table id for revision</param>
69
        public void ProcessIssueDrawing(string strRecordId)
70
        {            
71
            if (!string.IsNullOrEmpty(strRecordId))
72
            {
73
                ReadDrawingDetails(strRecordId);
74
                SaveBinaryDrawing();
75
            }
76
        }
77

    
78
        /// <summary>
79
        /// public method used for processing block
80
        /// </summary>
81
        /// <param name="BlockId"></param>
82
        public void ProcessBlock(ObjectId BlockId)
83
        {
84
            SetColorForEntity(BlockId,0);// white color
85
        }
86

    
87
        /// <summary>
88
        /// This is test command which gives the idea regarding selection of an entity and assigning different color to it
89
        /// </summary>
90
        [CommandMethod("TestS", Autodesk.AutoCAD.Runtime.CommandFlags.UsePickSet)]
91
        public static void TestSelection()
92
        {
93
            // Get the current document and database, and start a transaction
94
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
95
            Database acCurDb = acDoc.Database;
96
            Editor acDocEd = acDoc.Editor;
97

    
98
            // Request for objects to be selected in the drawing area
99
            PromptSelectionResult acSSPrompt = acDocEd.GetSelection();
100

    
101
            // If the prompt status is OK, objects were selected
102
            if (acSSPrompt.Status == PromptStatus.OK)
103
            {
104
                ObjectId[] selectedObjectIds = acSSPrompt.Value.GetObjectIds();
105
                // Get the last selected entity
106

    
107
                if (selectedObjectIds.Length > 0)
108
                {
109
                    ObjectId objIdBlock = selectedObjectIds[0];
110
                    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
111
                    {
112
                        DBObject objDB = acTrans.GetObject(objIdBlock, OpenMode.ForRead, true);
113
                        if (objDB != null)
114
                        {
115
                            // check if the selected entity is of Type BlockReference
116
                            if (objDB.GetType() == typeof(BlockReference))
117
                            {
118
                                // open an entity in write mode so that we can modify its color
119
                                Entity objDb = (Entity)acTrans.GetObject(objIdBlock, OpenMode.ForWrite);
120
                                if (objDb != null)
121
                                {
122
                                    objDb.ColorIndex = 0;// white color
123
                                }
124
                            }
125
                        }
126
                        acTrans.Commit();
127
                    }
128
                }
129
            }
130
        }
131

    
132
        #region private methods
133
        private string SaveBinaryDrawing()
134
        {
135
            FileStream fs = null;
136
            string strDrawing = string.Empty;
137
            try
138
            {
139
                if (!string.IsNullOrEmpty(this.DrawingName))
140
                {
141
                    string strDrawingname = this.DrawingName;
142
                    //if(!string.IsNullOrEmpty(this.MajorRevision))
143
                    //{
144
                    //    strDrawingname = strDrawingname + "_" + this.MajorRevision;
145
                    //}
146

    
147
                    byte[] drawingData = this.DrawingData;
148
                    if (null != drawingData)
149
                    {
150
                        try
151
                        {                            
152
                            if (File.Exists(strDrawingname) == true)
153
                            {
154
                                Microsoft.VisualBasic.FileSystem.Kill(strDrawingname);
155
                            }
156
                        }
157
                        catch (System.Exception )
158
                        {
159
                        }
160

    
161
                        strDrawing = strDrawingname;
162
                        string path = GetDrawingSavePath();                        
163
                        if (!string.IsNullOrEmpty(path))
164
                        {
165
                            strDrawing = path + "\\" + strDrawingname;
166
                        }
167
                        strDrawing += ".dwg";
168

    
169
                        fs = new FileStream(strDrawing, FileMode.Create, FileAccess.Write);
170
                        int ArraySize = new int();
171
                        ArraySize = drawingData.GetUpperBound(0);
172
                        fs.Write(drawingData, 0, ArraySize);
173
                    }
174
                }
175
            }
176
            catch (System.Exception )
177
            { }
178
            finally
179
            {
180
                if (fs != null)
181
                    fs.Close();
182
            }
183
            return strDrawing;
184
        }
185

    
186
        private void ReadDrawingDetails(string strRecordId)
187
        {
188
            SqlConnection connection = null;
189
            try
190
            {
191
                int ID = 0;
192
                int.TryParse(strRecordId, out ID);
193
                if (ID > 0)
194
                {
195
                    connection = new SqlConnection();
196
                    connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
197
                    connection.Open();
198

    
199
                    string strQry = "Select * from SynchroniseDetails where ID = " + ID;
200
                    SqlCommand cmd = new SqlCommand();
201
                    cmd.Connection = connection;
202
                    cmd.CommandText = strQry;
203
                    cmd.CommandType = CommandType.Text;
204
                    SqlDataReader reader = cmd.ExecuteReader();
205

    
206
                    if (null != reader && reader.HasRows)
207
                    {
208
                        while (reader.Read())
209
                        {
210
                            ProjectName     = reader["ProjectName"].ToString();
211
                            DrawingID       = reader["DrawingId"].ToString();
212
                            DrawingName     = reader["DrawingName"].ToString();
213
                            MajorRevision   = reader["Revision"].ToString();
214
                            MinorRevision   = reader["MinorRevision"].ToString();
215
                            Description     = reader["Description"].ToString();
216
                            NameOfUser      = reader["NameOfUser"].ToString();
217
                            ClientDwgNo     = reader["ClientDwgNo"].ToString();
218
                            RevisedBy       = reader["RevisedBy"].ToString();
219
                            CheckedBy       = reader["CheckedBy"].ToString();
220
                            ApprovedBy      = reader["ApprovedBy"].ToString();
221
                            ApprovedBy2     = reader["ApprovedBy2"].ToString();
222
                            ApprovedBy3     = reader["ApprovedBy3"].ToString();
223
                            ClientApprovedBy= reader["ClientApprovedBy"].ToString();
224
                            DrawingData     = reader["DrawingData"] != null ? (byte[])reader["DrawingData"] : null;
225
                        }
226
                    }
227
                }
228
            }
229
            catch (System.Exception )
230
            { }
231
            finally
232
            {
233
                if (connection != null)
234
                    connection.Close();
235
            }
236
        }
237
        private string GetRecordId()
238
        {
239
            string strRecordId = string.Empty;
240
            SqlConnection connection = null;
241
            try
242
            {
243
                connection = new SqlConnection();
244
                connection.ConnectionString = GetConnectionString_Reports(); // GetConnectionString();
245
                connection.Open();
246

    
247
                string strQry = "Select Max(ID) from SynchroniseDetails";
248
                SqlCommand cmd = new SqlCommand();
249
                cmd.Connection = connection;
250
                cmd.CommandText = strQry;
251
                cmd.CommandType = CommandType.Text;
252
                SqlDataReader reader = cmd.ExecuteReader();
253
                if (null != reader && reader.HasRows)
254
                {
255
                    strRecordId = reader.GetString(0).ToString();
256
                }
257
            }
258
            catch (System.Exception )
259
            { }
260
            finally
261
            {
262
                if (connection != null)
263
                    connection.Close();
264
            }
265
            return strRecordId;
266
        }
267
        private string GetConnectionString()
268
        {
269
            string strConn = string.Empty;            
270
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
271
            {
272
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName);               
273
            }
274
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
275
            {
276
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
277
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString);
278
            }           
279

    
280
            return strConn;            
281
        }
282

    
283
        private string GetConnectionString_Reports()
284
        {
285
            string strConn = string.Empty;
286
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
287
            {
288
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
289
            }
290
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
291
            {
292
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
293
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
294
            }
295

    
296
            return strConn;
297
        }
298

    
299
        private string GetDrawingSavePath()
300
        {
301
            return System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
302
        }
303

    
304
        /// <summary>
305
        /// Sets the color of the entity
306
        /// </summary>
307
        /// <param name="objIdBlockId">block id</param>
308
        /// <param name="iColor">color to be assigned</param>
309
        private void SetColorForEntity(ObjectId objIdBlockId, short iColor)
310
        {
311
            Transaction trans = null;
312
            try
313
            {
314
                if (objIdBlockId != ObjectId.Null)
315
                {
316
                    Document doc = AcadApp.DocumentManager.MdiActiveDocument;
317
                    Database db = HostApplicationServices.WorkingDatabase;
318
                    trans = doc.TransactionManager.StartTransaction();
319
                    using (doc.LockDocument())
320
                    {
321
                        using (trans)
322
                        {
323
                            if (objIdBlockId.IsErased == false)
324
                            {
325
                                Entity objDb = (Entity)trans.GetObject(objIdBlockId, OpenMode.ForWrite);
326
                                if (objDb != null)
327
                                {
328
                                    objDb.ColorIndex = iColor;                                    
329
                                }
330
                            }
331
                            trans.Commit();
332
                        }
333
                    }
334
                }
335
            }
336
            catch (System.Exception )
337
            {       
338
            }
339
            finally
340
            {
341
                if (trans != null)
342
                {
343
                    trans.Dispose();
344
                }
345
            }
346
        }
347
        #endregion
348
    }
349
}
클립보드 이미지 추가 (최대 크기: 500 MB)