forked from chuongmep/CadPythonShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionHelper.cs
More file actions
309 lines (254 loc) · 9.35 KB
/
Copy pathTransactionHelper.cs
File metadata and controls
309 lines (254 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// (C) Copyright 2004 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Diagnostics;
namespace MgdDbg.CompBuilder
{
/// <summary>
/// With the removal of ObjectId.Open/Close, it means that you need to
/// have a Transaction active to do just about anything. This is the
/// base class for a number of Helper objects that help manage a Transaction
/// and a set of operations around them. For instance, when creating a
/// BlockTableRecord, its nice to have someone manage the containing block,
/// Xforms to the WCS, etc.
/// </summary>
public class TransactionHelper : IDisposable
{
// member variables
protected Database m_db = null;
protected Transaction m_trans = null;
public
TransactionHelper()
{
m_db = Utils.Db.GetCurDwg();
}
public
TransactionHelper(Database db)
{
Debug.Assert(db != null);
m_db = db;
}
~TransactionHelper()
{
Dispose(false);
}
public void
Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void
Dispose(bool disposing)
{
if (disposing)
{
if (m_trans != null)
{
m_trans.Dispose();
}
}
}
public virtual void
Start()
{
Debug.Assert(m_trans == null);
m_trans = m_db.TransactionManager.StartTransaction();
}
public virtual void
Commit()
{
Debug.Assert(m_trans != null);
m_trans.Commit();
m_trans = null;
}
public virtual void
Abort()
{
Debug.Assert(m_trans != null);
m_trans.Abort();
m_trans = null;
}
/// <summary>
/// return the underlying Transaction
/// </summary>
public Autodesk.AutoCAD.DatabaseServices.Transaction
Transaction
{
get { return m_trans; }
}
/// <summary>
/// return the Database that this TransactionHelper goes with
/// </summary>
public Autodesk.AutoCAD.DatabaseServices.Database
Database
{
get { return m_db; }
}
/// <summary>
/// See if a SymbolTableRecord of the given name already exists
/// </summary>
/// <param name="classType">Type of the SymbolTableRecord (e.g., LayerTableRecord)</param>
/// <param name="name">Name of the symbol</param>
/// <returns>True if the symbol already exists, False if it doesn't</returns>
public bool
SymbolTableRecExists(System.Type classType, string name)
{
ObjectId tblId = Utils.SymTbl.GetSymbolTableId(classType, m_db);
SymbolTable tbl = (SymbolTable)m_trans.GetObject(tblId, OpenMode.ForRead);
return tbl.Has(name);
}
/// <summary>
/// Add a new SymbolTableRecord to the database (and the current transaction)
/// </summary>
/// <param name="newRec">A newly allocated SymbolTableRecord which hasn't yet been added to the database</param>
public virtual void
AddNewSymbolRec(SymbolTableRecord newRec)
{
Debug.Assert(m_trans != null);
ObjectId tblId = Utils.SymTbl.GetSymbolTableId(newRec.GetType(), m_db);
SymbolTable tbl = (SymbolTable)m_trans.GetObject(tblId, OpenMode.ForWrite);
Debug.Assert(tbl.Has(newRec.Name) == false);
tbl.Add(newRec);
m_trans.AddNewlyCreatedDBObject(newRec, true);
}
/// <summary>
/// Define a new named block and add it to the BlockTable. If the block
/// definition already exists, its contents will be emptied out so that
/// the block can be re-defined.
/// </summary>
/// <param name="blkName">Name of the BlockDef</param>
/// <param name="blkRec">New or existing BlockTableRecord</param>
/// <param name="db">Specific database we are using</param>
public BlockTableRecord
DefineNewBlockRec(string blkName)
{
Debug.Assert(m_trans != null);
BlockTableRecord blkRec = null;
BlockTable tbl = (BlockTable)m_trans.GetObject(m_db.BlockTableId, OpenMode.ForWrite);
// if it already exists, we are re-defining it and we need to
// erase all the existing entities
if (tbl.Has(blkName))
{
blkRec = (BlockTableRecord)m_trans.GetObject(tbl[blkName], OpenMode.ForWrite);
// erase all
DBObject tmpObj = null;
foreach (ObjectId objId in blkRec)
{
tmpObj = m_trans.GetObject(objId, OpenMode.ForWrite);
if (tmpObj != null)
{
tmpObj.Erase(true);
}
}
}
else
{
blkRec = new BlockTableRecord();
blkRec.Name = blkName;
tbl.Add(blkRec);
m_trans.AddNewlyCreatedDBObject(blkRec, true);
}
return blkRec;
}
/// <summary>
/// Define a new Anonymous block and add it to the BlockTable
/// </summary>
/// <param name="db"></param>
/// <returns>an open BlockTableRecord, ready to add Entities to</returns>
public BlockTableRecord
DefineNewAnonymousBlockRec()
{
Debug.Assert(m_trans != null);
BlockTableRecord blkRec = null;
BlockTable tbl = (BlockTable)m_trans.GetObject(m_db.BlockTableId, OpenMode.ForWrite);
blkRec = new BlockTableRecord();
blkRec.Name = "*U";
tbl.Add(blkRec);
m_trans.AddNewlyCreatedDBObject(blkRec, true);
return blkRec;
}
/// <summary>
/// Given the ObjectId of a SymbolTableRecord, get its name
/// </summary>
/// <param name="symId">ObjectId of the SymbolTableRecord</param>
/// <returns>name of the SymbolTableRecord</returns>
public string
SymbolIdToName(ObjectId symId)
{
Debug.Assert(m_trans != null);
string str = null;
DBObject tmpObj = m_trans.GetObject(symId, OpenMode.ForRead);
SymbolTableRecord rec = tmpObj as SymbolTableRecord;
if (rec != null)
str = rec.Name;
return str;
}
public bool
IsOnLockedLayer(Entity ent, bool printMsg)
{
Debug.Assert(m_trans != null);
LayerTableRecord layer = (LayerTableRecord)m_trans.GetObject(ent.LayerId, OpenMode.ForRead);
if (printMsg && layer.IsLocked)
{
Utils.AcadUi.PrintToCmdLine("\nSelected entity is on a locked layer.");
}
return layer.IsLocked;
}
public ObjectIdCollection
CollectBlockIds(bool excludeMsPs, bool excludeXref, bool excludeXrefDep, bool excludeAnonymous)
{
Debug.Assert(m_trans != null);
BlockTable blkTbl = (BlockTable)m_trans.GetObject(m_db.BlockTableId, OpenMode.ForRead);
// iterate over each BlockTableRecord in the BlockTable
ObjectIdCollection blkRecIds = new ObjectIdCollection();
foreach (ObjectId tblRecId in blkTbl)
{
BlockTableRecord blkRec = (BlockTableRecord)m_trans.GetObject(tblRecId, OpenMode.ForRead);
if (excludeMsPs && blkRec.IsLayout)
{
; // do nothing
}
else if ((excludeXrefDep) && blkRec.IsDependent)
{
; // do nothing
}
else if ((excludeXref) &&
((blkRec.IsFromExternalReference) ||
(blkRec.IsFromOverlayReference)))
{
; // do nothing
}
else if (excludeAnonymous && blkRec.IsAnonymous)
{
; // do nothing
}
else
{
blkRecIds.Add(tblRecId);
}
}
return blkRecIds;
}
}
}