-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoordPlan.cs
More file actions
228 lines (207 loc) · 9.1 KB
/
CoordPlan.cs
File metadata and controls
228 lines (207 loc) · 9.1 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
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
//using Autodesk.Windows;
using Application = Autodesk.Revit.ApplicationServices.Application;
namespace RG_Tools
{
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class CoordPlan : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
// Get UIDocument
UIDocument uidoc = commandData.Application.ActiveUIDocument;
// Get Document
Document doc = uidoc.Document;
// Get UIApplication
UIApplication uiapp = commandData.Application;
// Get app
Application application = uiapp.Application;
// Get transaction
Transaction transaction = new Transaction(doc);
// View coordPlan = (View)doc.GetElement(el).Duplicate(ViewDuplicateOption.Duplicate);
Level lvl = new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_Levels)
.WhereElementIsNotElementType()
.ToElements().Cast<Level>().FirstOrDefault<Level>();
// Get Family View Types
FilteredElementCollector a = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType));
// Get View type
ViewFamilyType vf = get_ViewType(transaction, a);
// Find existed views
List<object> cp = FindCoordinationView(doc);
// Creating a view plan based on View Family Type and level in current document
if (cp.Count == 0)
{
transaction.Start("Create Coordination Plan");
ViewPlan coordPlan = createCoordPlan(doc, vf, lvl);
transaction.Commit();
// Make new plan active in viewer
uidoc.ActiveView = (View)doc.GetElement(coordPlan.Id);
return Result.Succeeded;
}
// Work on exceptions if plan existed
else if (cp.Any())
{
string taskName = "Existed Coordination Plan detected";
string taskDescription = "Delete existed Coordination Plan and create new";
TaskDialog mainDialog = new TaskDialog(taskName)
{
MainInstruction = "Coordination Plan Detected!",
MainContent =
"What would you like to do with existing Coordination Plan?"
};
// Add commmandLink options to task dialog
mainDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1,
"Keep existed and open it");
mainDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2,
taskDescription);
TaskDialogResult tResult = mainDialog.Show();
if (TaskDialogResult.CommandLink1 == tResult)
{
uidoc.ActiveView = (View)cp[0];
return Result.Succeeded;
}
else if (TaskDialogResult.CommandLink2 == tResult)
{
using TransactionGroup transGroup = new TransactionGroup(doc);
transGroup.Start("Delete all existed and create new Coordination plan");
ViewPlan _dummy = null;
using (Transaction trans0 = new Transaction(doc))
{
trans0.Start("Creating Dummy");
ViewPlan dummy = ViewPlan.Create(doc, vf.Id, lvl.Id);
trans0.Commit();
uidoc.ActiveView = dummy;
_dummy = dummy;
}
using (Transaction trans1 = new Transaction(doc))
{
trans1.Start("Delete existed Coordination Plan and Create new");
foreach (View c in cp.Cast<View>())
{
doc.Delete(c.Id);
}
ViewPlan vp = createCoordPlan(doc, vf, lvl);
trans1.Commit();
uidoc.ActiveView = vp;
}
using (Transaction trans2 = new Transaction(doc))
{
trans2.Start("Delete dummy");
doc.Delete(_dummy.Id);
trans2.Commit();
}
transGroup.Assimilate();
}
return Result.Succeeded;
}
return Result.Failed;
}
// Function that finds existed Navis View in a Document
public static List<object> FindCoordinationView(Document doc)
{
List<object> coord_plan = new List<object>();
FilteredElementCollector collector = new FilteredElementCollector(doc);
IList<Element> elems = collector.OfCategory(BuiltInCategory.OST_Views).WhereElementIsNotElementType().ToElements();
foreach (View elem in elems.Cast<View>())
{
if (elem.ViewType == ViewType.FloorPlan && elem.IsTemplate == false)
{
if (elem.Name is string name && name.Contains("Coordination Plan")) { coord_plan.Add(elem); }
else continue;
}
}
return coord_plan;
}
// Find an existed ViewFamilyType
// or Try to create new ViewFamilyType called Coordination
public static ViewFamilyType get_ViewType(Transaction transaction, FilteredElementCollector a)
{
foreach (ViewFamilyType vft in a.ToElements())
{
if (vft.Name.Equals("Coordination Plan"))
{
try
{
vft.DefaultTemplateId = null;
}
catch { };
return vft;
}
}
try
{
transaction.Start("Create View Family Type");
ViewFamilyType old_v = get_viewTypePlan(a.ToElements());
ViewFamilyType new_v = old_v.Duplicate("Coordination Plan") as ViewFamilyType;
try
{
new_v.DefaultTemplateId = null;
}
catch { };
transaction.Commit();
return new_v;
}
catch
{
transaction.RollBack();
}
return null;
}
// Gets the first available TypePlan
public static ViewFamilyType get_viewTypePlan(IEnumerable<Element> elems)
{
foreach (ViewFamilyType elem in elems)
{
if (elem.FamilyName == "Floor Plan")
{
return elem;
}
}
return null;
}
public static ViewPlan createCoordPlan(Document doc, ViewFamilyType vft, Level lvl)
{
//Create ViewPlan
ViewPlan floorPlan = ViewPlan.Create(doc, vft.Id, lvl.Id);
floorPlan.ViewTemplateId = ElementId.InvalidElementId;
if (floorPlan.ViewTemplateId != null)
{
floorPlan.ViewTemplateId = ElementId.InvalidElementId;
}
//Adjust coordination plan settings
floorPlan.Name = "Coordination Plan";
//rendering options
floorPlan.DetailLevel = ViewDetailLevel.Coarse;
floorPlan.DisplayStyle = DisplayStyle.Wireframe;
floorPlan.Discipline = ViewDiscipline.Coordination;
//show coordination points
floorPlan.SetCategoryHidden(Category.GetCategory(doc, BuiltInCategory.OST_ProjectBasePoint).Id, false);
floorPlan.SetCategoryHidden(Category.GetCategory(doc, BuiltInCategory.OST_Site).Id, false);
floorPlan.SetCategoryHidden(Category.GetCategory(doc, BuiltInCategory.OST_SharedBasePoint).Id, false);
//removing cropboxes
floorPlan.AreAnnotationCategoriesHidden = false;
floorPlan.CropBoxActive = false;
floorPlan.CropBoxVisible = false;
floorPlan.get_Parameter(BuiltInParameter.VIEWER_ANNOTATION_CROP_ACTIVE).Set(0);
//editing view range
PlanViewRange vr = floorPlan.GetViewRange();
vr.SetLevelId(PlanViewPlane.TopClipPlane, PlanViewRange.Unlimited);
vr.SetLevelId(PlanViewPlane.BottomClipPlane, PlanViewRange.Unlimited);
vr.SetLevelId(PlanViewPlane.ViewDepthPlane, PlanViewRange.Unlimited);
floorPlan.SetViewRange(vr);
return floorPlan;
}
}
}
// TODO: Add options for new plan (Base point visibility, etc...)