-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReviewLinks.cs
More file actions
230 lines (208 loc) · 8.74 KB
/
ReviewLinks.cs
File metadata and controls
230 lines (208 loc) · 8.74 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
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
namespace RG_Tools
{
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class ReviewLinks : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
// Get UIDocument
UIDocument uidoc = commandData.Application.ActiveUIDocument;
// Get Document
Document doc = uidoc.Document;
//Fix unpinned
List<object> unp = GetUnpinned(CollectLinks(doc));
if (unp.Count > 0)
{
Transaction transaction1 = new Transaction(doc);
transaction1.Start("Fix Pinned Links");
TaskDialog fixunpinned = new TaskDialog("Instance(s) Not Pinned")
{
MainInstruction = "Instance(s) Not Pinned",
MainContent = ConcatList(unp)
};
fixunpinned.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Pin link(s)");
fixunpinned.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "Ignore warning");
fixunpinned.CommonButtons = TaskDialogCommonButtons.Close;
fixunpinned.DefaultButton = TaskDialogResult.Close;
TaskDialogResult t1Result = fixunpinned.Show();
if (TaskDialogResult.CommandLink1 == t1Result)
{
try
{
foreach (Element link in unp)
{
link.Pinned = true;
}
transaction1.Commit();
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException e)
{
message = e.Message;
transaction1.RollBack();
}
}
else if (TaskDialogResult.CommandLink2 == t1Result)
{
transaction1.RollBack();
}
else
{
transaction1.RollBack();
}
}
else
{
TaskDialog.Show("Check Unpinned", "There are no unpinned instances in project");
}
// Add Worksets
if (doc.IsWorkshared == true)
{
List<object> unw = GetLinksWithoutWorkset(doc, CollectLinks(doc));
if (unw.Any() == true)
{
Transaction transaction2 = new Transaction(doc);
transaction2.Start("Fix Worksets");
TaskDialog fixworksets = new TaskDialog("Instance(s) Not Pinned")
{
MainInstruction = "Links are not in a separated worksets",
MainContent = ConcatList(unw)
};
fixworksets.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Create separate worksets for links");
fixworksets.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "Ignore warning");
fixworksets.CommonButtons = TaskDialogCommonButtons.Close;
fixworksets.DefaultButton = TaskDialogResult.Close;
TaskDialogResult t2Result = fixworksets.Show();
if (TaskDialogResult.CommandLink1 == t2Result)
{
foreach (Element link in unw.Cast<Element>())
{
//Get LinkType
RevitLinkType linktype = (RevitLinkType)doc.GetElement(link.GetTypeId());
//Defy the new Workset Name
string workset_name = ConvertWSName(linktype);
//Create new workset
Workset wrks = Workset.Create(doc, workset_name);
//Put link type and link instance into new workset
Parameter wsparam = link.get_Parameter(BuiltInParameter.ELEM_PARTITION_PARAM);
Parameter wstypeparam = linktype.get_Parameter(BuiltInParameter.ELEM_PARTITION_PARAM);
try
{
wsparam.Set(wrks.Id.IntegerValue);
wstypeparam.Set(wrks.Id.IntegerValue);
}
catch { throw; }
/*
catch (Autodesk.Revit.Exceptions.OperationCanceledException e)
{
message = e.Message;
transaction2.RollBack();
}*/
}
transaction2.Commit();
}
else if (TaskDialogResult.CommandLink2 == t2Result)
{
transaction2.RollBack();
}
else
{
transaction2.RollBack();
}
//TaskDialog.Show("Worksets Created", ConcatList(unw));
}
else if (unw.Any() == false)
{
TaskDialog.Show("Link Worksets", "All links are in individual worksets");
}
}
else if (doc.IsWorkshared == false)
{
TaskDialog.Show("Not Workshared", "The documenet is not workshared!");
}
return Result.Succeeded;
}
public static List<object> CollectLinks(Document doc)
{
ICollection<Element> cl_links = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType().ToElements();
List<object> collected = new List<object>();
foreach (Element link in cl_links)
{
Element e = doc.GetElement(link.Id);
//RevitLinkType rvtType = e.GetType();
RevitLinkType revitLinkType = (RevitLinkType)doc.GetElement(e.GetTypeId());
if (cl_links.Contains(revitLinkType))
{
collected.Add(link);
}
}
foreach (RevitLinkInstance link in cl_links)
{
RevitLinkType revitLinkType = (RevitLinkType)doc.GetElement(link.GetTypeId());
if (revitLinkType.IsNestedLink == false)
{
collected.Add(link);
}
}
return collected;
}
public static List<object> GetUnpinned(ICollection<object> links)
{
List<object> unpinned = new List<object>();
foreach (Element link in links)
{
if (link.Pinned == false)
{
unpinned.Add(link);
}
}
return unpinned;
}
public static List<object> GetLinksWithoutWorkset(Document doc, ICollection<object> links)
{
List<object> unwrk = new List<object>();
//Get Workset and convers List to list of names
FilteredWorksetCollector wsfilter = new FilteredWorksetCollector(doc);
IList<Workset> wslist = wsfilter.ToWorksets();
List<object> wsnames = new List<object>();
foreach (Workset workset in wslist)
{
string wsname = workset.Name.ToString();
wsnames.Add(wsname);
}
foreach (Element link in links)
{
bool alreadyExist = wsnames.Contains(ConvertWSName((RevitLinkType)doc.GetElement(link.GetTypeId())));
if (alreadyExist == false)
{
unwrk.Add(link);
}
}
return unwrk;
}
public static string ConcatList(List<object> list1)
{
List<string> list2 = new List<string>();
foreach (Element obj in list1)
{
list2.Add(obj.Name.ToString());
}
string outputstring = string.Join(Environment.NewLine, list2.ToArray());
return outputstring;
}
public static string ConvertWSName(RevitLinkType linktype)
{
string workset_name = "##RVT_" + linktype.Name.ToString();
string newname = workset_name.Substring(0, workset_name.Length - 4);
return newname;
}
}
}