监视用户是保存用户编辑还是放弃参照编辑
【原文】
1 using Autodesk.AutoCAD.ApplicationServices;
2 using Autodesk.AutoCAD.DatabaseServices;
3 using Autodesk.AutoCAD.EditorInput;
4 using Autodesk.AutoCAD.Runtime;
5
6 // 此行不是强制性的,但可以提高装载性能
7 [assembly: CommandClass(typeof(监视用户是保存用户编辑还是放弃参照编辑.XrefLongTrans))]
8
9 namespace 监视用户是保存用户编辑还是放弃参照编辑
10 {
11 public class XrefLongTrans
12 {
13 enum EditInPlaceXrefState
14 {
15 Discarded,
16 Saved
17 };
18
19 static EditInPlaceXrefState state = EditInPlaceXrefState.Discarded;
20
21 [CommandMethod("WatchXref")]
22 public void WatchXref()
23 {
24 Document doc = Application.DocumentManager.MdiActiveDocument;
25 if (doc == null) return;
26 Editor ed = doc.Editor;
27 LongTransactionManager longTranMan = Application.LongTransactionManager;
28 longTranMan.CheckedIn += TrMan_CheckedIn;
29 longTranMan.Aborted += TrMan_Aborted;
30 doc.CommandEnded += Doc_CommandEnded;
31 }
32 [CommandMethod("UnWatchXref")]
33 public void UnWatchXref()
34 {
35 Document doc = Application.DocumentManager.MdiActiveDocument;
36 if (doc == null) return;
37 Editor ed = doc.Editor;
38 LongTransactionManager longTranMan = Application.LongTransactionManager;
39 longTranMan.CheckedIn -= TrMan_CheckedIn;
40 longTranMan.Aborted -= TrMan_Aborted;
41 doc.CommandEnded -= Doc_CommandEnded;
42 }
43
44 private void TrMan_Aborted(object sender, LongTransactionEventArgs e)
45 {
46 Document doc = Application.DocumentManager.MdiActiveDocument;
47 if (doc == null) return;
48 Editor ed = doc.Editor;
49 if (e.Transaction.Type == LongTransactionType.XRefDb)
50 {
51 ed.WriteMessage("\nLong transaction {0} aborted\n", e.Transaction.LongTransactionName);
52 state = EditInPlaceXrefState.Discarded;
53 }
54 }
55
56 private void TrMan_CheckedIn(object sender, LongTransactionEventArgs e)
57 {
58 Document doc = Application.DocumentManager.MdiActiveDocument;
59 if (doc == null) return;
60 Editor ed = doc.Editor;
61 if (e.Transaction.Type == LongTransactionType.XRefDb)
62 {
63 ed.WriteMessage("\nLong transaction {0} commited\n", e.Transaction.LongTransactionName);
64 state = EditInPlaceXrefState.Saved;
65 }
66 }
67
68 private void Doc_CommandEnded(object sender, CommandEventArgs e)
69 {
70 if (e.GlobalCommandName.ToUpper() == "REFCLOSE")
71 {
72 Document doc = Application.DocumentManager.MdiActiveDocument;
73 if (doc == null) return;
74 Editor ed = doc.Editor;
75 ed.WriteMessage("\nModification of XREF {0}\n", (state == EditInPlaceXrefState.Saved) ? "Saved" : "Discarded");
76 }
77 }
78 }
79 }