-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandSnap.cs
151 lines (113 loc) · 4.69 KB
/
CommandSnap.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
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
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SnapMe
{
// Attributes
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class CommandSnap: IExternalCommand
{
// Main method
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
try
{
string imageName = uidoc.ActiveView.Name;
// All images exported have a suffix followed by an index
string searchName_underscore = imageName + "_";
string search_suffix = ".png";
string fileDirectory = SettingsData.FolderDirectory;
int maxNumber = DirectoryChecker(fileDirectory, searchName_underscore, search_suffix);
int nextNum = maxNumber + 1;
string filepath = fileDirectory + @"\" + searchName_underscore + nextNum + search_suffix;
ImageExportOptions export = ExportSettings(filepath);
doc.ExportImage(export);
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
TaskDialog.Show("Success", $"View exported successfully!");
return Result.Succeeded;
}
public ImageExportOptions ExportSettings(string filePath)
{
// instanitate the image exportoptions class
ImageExportOptions export = new ImageExportOptions();
export.ExportRange = ExportRange.VisibleRegionOfCurrentView;
export.FilePath = filePath;
export.ShadowViewsFileType = ImageFileType.PNG;
export.HLRandWFViewsFileType = ImageFileType.PNG;
export.ImageResolution = ImageResolution.DPI_300;
export.ZoomType = ZoomFitType.Zoom;
export.Zoom = 100;
return export;
}
public int DirectoryChecker(string folderPath, string searchName, string searchSuffix)
{
//searchName with highest index set 0
int maxNumber = 0;
//list of files in folders
string[] files = Directory.GetFiles(folderPath);
foreach (string filepath in files)
{
//name with suffix i.e. symon.png
string fileName_suffix = Path.GetFileName(filepath);
//name without suffix
string fileName = RemoveSuffix(fileName_suffix, searchSuffix);
//split names
string[] prefix_suffix = SplitName(fileName);
string fileNamePrefix = prefix_suffix[0];
string fileNameSuffix = prefix_suffix[1];
if (searchName == fileNamePrefix)
{
int fileNameSuffix_int = int.Parse(fileNameSuffix);
//check the number of the prefix
if (fileNameSuffix_int > maxNumber)
{
maxNumber = fileNameSuffix_int;
}
}
}
return maxNumber;
}
static string RemoveSuffix(string Input, string suffix)
{
//checkk if input and suffix are not null as well as input ends with suffix
if (!string.IsNullOrEmpty(Input) && !string.IsNullOrEmpty(suffix) && Input.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
{
//remove suffix by replacing it with empty string
return Input.Replace(suffix, string.Empty);
}
//if above conditions are not met then return the input string
return Input;
}
static string[] SplitName(string input)
{
string prefix = "";
string suffix = "";
//find the last underscore index in the string
int underscoreIndex = input.LastIndexOf("_");
// ensure underscoe was found
if (underscoreIndex >= 0)
{
//separate string using the underscore index
prefix = input.Substring(0, underscoreIndex + 1);
suffix = input.Substring(underscoreIndex + 1);
}
//if name has no underscore then the strings will remain empty
string[] prefix_suffix = new string[] { prefix, suffix };
return prefix_suffix;
}
}
}