forked from chuongmep/CadPythonShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnoopCommand.cs
More file actions
111 lines (108 loc) · 3.8 KB
/
SnoopCommand.cs
File metadata and controls
111 lines (108 loc) · 3.8 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using CADSnoop.Model;
using CADSnoop.View;
using CADSnoop.ViewModel;
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
using Exception = System.Exception;
namespace CADSnoop
{
public class SnoopCommand
{
[CommandMethod("Snoop")]
public void Snoop()
{
Snoop(new List<ObjectId>());
}
/// <summary>
/// Snoop Snoop a object by id
/// </summary>
/// <param name="objectId"></param>
public void Snoop(ObjectId objectId)
{
Snoop(new List<ObjectId>(){objectId});
}
/// <summary>
/// snoop by dbobject
/// </summary>
/// <param name="dbObject"></param>
/// <exception cref="ArgumentException"></exception>
public void Snoop(DBObject dbObject)
{
if (dbObject == null) throw new ArgumentException(nameof(dbObject));
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
using (DocumentLock lockDoc = doc.LockDocument())
{
using (Transaction tran = db.TransactionManager.StartTransaction())
{
{
SnoopViewModel vm = new SnoopViewModel(doc, db, dbObject);
MainWindow form = new MainWindow(vm);
form.SetCadAsWindowOwner();
form.Show();
}
tran.Commit();
}
}
}
/// <summary>
/// Snoop snoop object by list object id
/// </summary>
/// <param name="objectIds"></param>
public void Snoop(List<ObjectId> objectIds)
{
try
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
using (DocumentLock lockDoc = doc.LockDocument())
{
using (Transaction tran = db.TransactionManager.StartTransaction())
{
if (objectIds == null || objectIds.Count==0) objectIds = PickObjectBySelect(doc);
if (objectIds != null)
{
SnoopViewModel vm = new SnoopViewModel(doc, db, objectIds);
MainWindow form = new MainWindow(vm);
form.SetCadAsWindowOwner();
form.Show();
}
tran.Commit();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
List<ObjectId> PickObjectBySelect(Document doc)
{
try
{
//PromptSelectionOptions poOptions = new PromptSelectionOptions();
//poOptions.SingleOnly = true;
PromptSelectionResult promptSelectionResult = doc.Editor.GetSelection();
if (promptSelectionResult.Status != PromptStatus.OK) return null;
SelectionSet selectionSet = promptSelectionResult.Value;
return selectionSet.GetObjectIds().ToList();
}
catch (ArgumentNullException) { }
catch (NullReferenceException) { }
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
return null;
}
}
}