-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathCommands.cs
66 lines (61 loc) · 2.37 KB
/
Commands.cs
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
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.Runtime;
using Newtonsoft.Json;
using System.IO;
[assembly: CommandClass(typeof(CrxApp.Commands))]
[assembly: ExtensionApplication(null)]
namespace CrxApp
{
public class Parameters
{
public bool ExtractBlockNames { get; set; }
public bool ExtractLayerNames { get; set; }
}
public class Commands
{
[CommandMethod("MyTestCommands", "test", CommandFlags.Modal)]
static public void Test()
{
//prompt for input json and output folder
var doc = Application.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var res1 = ed.GetFileNameForOpen("Specify parameter file");
if (res1.Status != Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
return;
var res2 = ed.GetString("Specify output sub-folder name");
if (res2.Status != Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
return;
try
{
//get parameter from input json
var parameters = JsonConvert.DeserializeObject<Parameters>(File.ReadAllText(res1.StringResult));
Directory.CreateDirectory(res2.StringResult);
//extract layer names and block names from drawing as requested and place the results in the
//output folder
var db = doc.Database;
if (parameters.ExtractLayerNames)
{
using (var writer = File.CreateText(Path.Combine(res2.StringResult, "layers.txt")))
{
dynamic layers = db.LayerTableId;
foreach (dynamic layer in layers)
writer.WriteLine(layer.Name);
}
}
if (parameters.ExtractBlockNames)
{
using (var writer = File.CreateText(Path.Combine(res2.StringResult, "blocks.txt")))
{
dynamic blocks = db.BlockTableId;
foreach (dynamic block in blocks)
writer.WriteLine(block.Name);
}
}
}
catch (System.Exception e)
{
ed.WriteMessage("Error: {0}", e);
}
}
}
}