/
CmdDimensionWallsIterateFaces.cs
559 lines (431 loc) · 17.2 KB
/
CmdDimensionWallsIterateFaces.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#region Header
//
// CmdDimensionWallsIterateFaces.cs - create dimensioning elements
// between opposing walls by iterating over their faces
//
// Copyright (C) 2011-2021 by Jeremy Tammik, Autodesk Inc. All rights reserved.
//
// Keywords: The Building Coder Revit API C# .NET add-in.
//
#endregion // Header
#region Namespaces
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
#endregion // Namespaces
namespace BuildingCoder
{
/// <summary>
/// Dimension two opposing parallel walls.
/// For simplicity, the dimension is defined from
/// wall midpoint to midpoint, so the walls have
/// to be exactly opposite each other for it to work.
/// Iterate the wall solid faces to find the two
/// closest opposing faces and use references to
/// them to define the dimension element.
/// First sample solution for case
/// 1263071 [Revit 2011 Dimension Wall].
/// </summary>
[Transaction(TransactionMode.Manual)]
internal class CmdDimensionWallsIterateFaces : IExternalCommand
{
private const string _prompt
= "Please select two parallel opposing straight walls.";
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
var uiapp = commandData.Application;
var uidoc = uiapp.ActiveUIDocument;
var app = uiapp.Application;
var doc = uidoc.Document;
// obtain the current selection and pick
// out all walls from it:
//Selection sel = uidoc.Selection; // 2014
var ids = uidoc.Selection
.GetElementIds(); // 2015
var walls = new List<Wall>(2);
//foreach( Element e in sel.Elements ) // 2014
foreach (var id in ids) // 2015
{
var e = doc.GetElement(id);
if (e is Wall wall) walls.Add(wall);
}
if (2 != walls.Count)
{
message = _prompt;
return Result.Failed;
}
// ensure the two selected walls are straight and
// parallel; determine their mutual normal vector
// and a point on each wall for distance
// calculations:
var lines = new List<Line>(2);
var midpoints = new List<XYZ>(2);
XYZ normal = null;
foreach (var wall in walls)
{
var lc = wall.Location as LocationCurve;
var curve = lc.Curve;
if (!(curve is Line line))
{
message = _prompt;
return Result.Failed;
}
lines.Add(line);
midpoints.Add(Util.Midpoint(line));
if (null == normal)
{
normal = Util.Normal(line);
}
else
{
if (!Util.IsParallel(normal, Util.Normal(line)))
{
message = _prompt;
return Result.Failed;
}
}
}
// find the two closest facing faces on the walls;
// they are vertical faces that are parallel to the
// wall curve and closest to the other wall.
var opt = app.Create.NewGeometryOptions();
opt.ComputeReferences = true;
var faces = new List<Face>(2);
faces.Add(GetClosestFace(walls[0], midpoints[1], normal, opt));
faces.Add(GetClosestFace(walls[1], midpoints[0], normal, opt));
// create the dimensioning:
CreateDimensionElement(doc.ActiveView,
midpoints[0], faces[0].Reference,
midpoints[1], faces[1].Reference);
return Result.Succeeded;
}
#region CreateDimensionElement
/// <summary>
/// Create a new dimension element using the given
/// references and dimension line end points.
/// This method opens and commits its own transaction,
/// assuming that no transaction is open yet and manual
/// transaction mode is being used.
/// Note that this has only been tested so far using
/// references to surfaces on planar walls in a plan
/// view.
/// </summary>
public static void CreateDimensionElement(
View view,
XYZ p1,
Reference r1,
XYZ p2,
Reference r2)
{
var doc = view.Document;
var ra = new ReferenceArray();
ra.Append(r1);
ra.Append(r2);
var line = Line.CreateBound(p1, p2);
using var t = new Transaction(doc);
t.Start("Create New Dimension");
var dim = doc.Create.NewDimension(
view, line, ra);
t.Commit();
}
#endregion // CreateDimensionElement
#region GetClosestFace
/// <summary>
/// Return the closest planar face to a given point
/// p on the element e with a given normal vector.
/// </summary>
private static Face GetClosestFace(
Element e,
XYZ p,
XYZ normal,
Options opt)
{
Face face = null;
var min_distance = double.MaxValue;
var geo = e.get_Geometry(opt);
//GeometryObjectArray objects = geo.Objects; // 2012
//foreach( GeometryObject obj in objects ) // 2012
foreach (var obj in geo) // 2013
{
var solid = obj as Solid;
if (solid != null)
{
var fa = solid.Faces;
foreach (Face f in fa)
{
var pf = f as PlanarFace;
Debug.Assert(null != pf,
"expected planar wall faces");
if (null != pf
//&& normal.IsAlmostEqualTo( pf.Normal )
&& Util.IsParallel(normal, pf.FaceNormal))
{
//XYZ q = pf.Project( p ).XYZPoint; // Project returned null once
//double d = q.DistanceTo( p );
var v = p - pf.Origin;
var d = v.DotProduct(-pf.FaceNormal);
if (d < min_distance)
{
face = f;
min_distance = d;
}
}
}
}
}
return face;
}
#endregion // GetClosestFace
#region Dimension Filled Region Alexander
[Transaction(TransactionMode.Manual)]
public class CreateFillledRegionDimensionsCommand : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
var uiapp = commandData.Application;
var uidoc = uiapp.ActiveUIDocument;
var doc = uidoc.Document;
var view = uidoc.ActiveGraphicalView;
var filledRegions = FindFilledRegions(doc, view.Id);
using var transaction = new Transaction(doc,
"filled regions dimensions");
transaction.Start();
foreach (var filledRegion in filledRegions)
{
CreateDimensions(filledRegion,
-1 * view.RightDirection);
CreateDimensions(filledRegion, view.UpDirection);
}
transaction.Commit();
return Result.Succeeded;
}
private static void CreateDimensions(
FilledRegion filledRegion,
XYZ dimensionDirection)
{
var document = filledRegion.Document;
var view = (View) document.GetElement(
filledRegion.OwnerViewId);
var edgesDirection = dimensionDirection.CrossProduct(
view.ViewDirection);
var edges = FindRegionEdges(filledRegion)
.Where(x => IsEdgeDirectionSatisfied(x, edgesDirection))
.ToList();
if (edges.Count < 2)
return;
//var shift = UnitUtils.ConvertToInternalUnits(
// -10 * view.Scale, DisplayUnitType.DUT_MILLIMETERS ) // 2020
// * edgesDirection;
var shift = UnitUtils.ConvertToInternalUnits(
-10 * view.Scale, UnitTypeId.Millimeters) // 2021
* edgesDirection;
var dimensionLine = Line.CreateUnbound(
filledRegion.get_BoundingBox(view).Min
+ shift, dimensionDirection);
var references = new ReferenceArray();
foreach (var edge in edges)
references.Append(edge.Reference);
document.Create.NewDimension(view, dimensionLine,
references);
}
private static bool IsEdgeDirectionSatisfied(
Edge edge,
XYZ edgeDirection)
{
var edgeCurve = edge.AsCurve() as Line;
if (edgeCurve == null)
return false;
return edgeCurve.Direction.CrossProduct(
edgeDirection).IsAlmostEqualTo(XYZ.Zero);
}
private static IEnumerable<Edge> FindRegionEdges(
FilledRegion filledRegion)
{
var view = (View) filledRegion.Document.GetElement(
filledRegion.OwnerViewId);
var options = new Options
{
View = view,
ComputeReferences = true
};
return filledRegion
.get_Geometry(options)
.OfType<Solid>()
.SelectMany(x => x.Edges.Cast<Edge>());
}
private static IEnumerable<FilledRegion>
FindFilledRegions(
Document document,
ElementId viewId)
{
var collector = new FilteredElementCollector(
document, viewId);
return collector
.OfClass(typeof(FilledRegion))
.Cast<FilledRegion>();
}
}
#endregion // Dimension Filled Region Alexander
#region Developer Guide Sample Code
public void DuplicateDimension(
Document doc,
Dimension dimension)
{
var line = dimension.Curve as Line;
if (null != line)
{
var view = dimension.View;
var references = dimension.References;
var newDimension = doc.Create.NewDimension(
view, line, references);
}
}
public Dimension CreateLinearDimension(
Document doc)
{
var app = doc.Application;
// first create two lines
var pt1 = new XYZ(5, 5, 0);
var pt2 = new XYZ(5, 10, 0);
var line = Line.CreateBound(pt1, pt2);
//Plane plane = app.Create.NewPlane( pt1.CrossProduct( pt2 ), pt2 ); // 2016
var plane = Plane.CreateByNormalAndOrigin(pt1.CrossProduct(pt2), pt2); // 2017
//SketchPlane skplane = doc.FamilyCreate.NewSketchPlane( plane ); // 2013
var skplane = SketchPlane.Create(doc, plane); // 2014
var modelcurve1 = doc.FamilyCreate
.NewModelCurve(line, skplane);
pt1 = new XYZ(10, 5, 0);
pt2 = new XYZ(10, 10, 0);
line = Line.CreateBound(pt1, pt2);
//plane = app.Create.NewPlane( pt1.CrossProduct( pt2 ), pt2 ); // 2016
plane = Plane.CreateByNormalAndOrigin(pt1.CrossProduct(pt2), pt2); // 2017
//skplane = doc.FamilyCreate.NewSketchPlane( plane ); // 2013
skplane = SketchPlane.Create(doc, plane); // 2014
var modelcurve2 = doc.FamilyCreate
.NewModelCurve(line, skplane);
// now create a linear dimension between them
var ra = new ReferenceArray();
ra.Append(modelcurve1.GeometryCurve.Reference);
ra.Append(modelcurve2.GeometryCurve.Reference);
pt1 = new XYZ(5, 10, 0);
pt2 = new XYZ(10, 10, 0);
line = Line.CreateBound(pt1, pt2);
var dim = doc.FamilyCreate
.NewLinearDimension(doc.ActiveView, line, ra);
// create a label for the dimension called "width"
var param = doc.FamilyManager
.AddParameter("width",
//BuiltInParameterGroup.PG_CONSTRAINTS, // 2021
GroupTypeId.Constraints, // 2022
//ParameterType.Length, // 2021
SpecTypeId.Length, // 2022
false);
//dim.Label = param; // 2013
dim.FamilyLabel = param; // 2014
return dim;
}
#endregion // Developer Guide Sample Code
#region Dimension Filled Region Jorge
private void CreateDimensions(
FilledRegion filledRegion,
XYZ dimensionDirection,
string typeName)
{
var document = filledRegion.Document;
var view = (View) document.GetElement(
filledRegion.OwnerViewId);
var edgesDirection = dimensionDirection.CrossProduct(
view.ViewDirection);
var edges = FindRegionEdges(filledRegion)
.Where(x => IsEdgeDirectionSatisfied(x, edgesDirection))
.ToList();
if (edges.Count < 2)
return;
// Se hace este ajuste para que la distancia no
// depende de la escala. <<<<<< evaluar para
// informaciуn de acotado y etiquetado!!!
//var shift = UnitUtils.ConvertToInternalUnits(
// 5 * view.Scale, DisplayUnitType.DUT_MILLIMETERS ) // 2020
// * edgesDirection;
var shift = UnitUtils.ConvertToInternalUnits(
5 * view.Scale, UnitTypeId.Millimeters) // 2021
* edgesDirection;
var dimensionLine = Line.CreateUnbound(
filledRegion.get_BoundingBox(view).Min + shift,
dimensionDirection);
var references = new ReferenceArray();
foreach (var edge in edges)
references.Append(edge.Reference);
var dim = document.Create.NewDimension(
view, dimensionLine, references);
var dr_id = DimensionTypeId(
document, typeName);
if (dr_id != null) dim.ChangeTypeId(dr_id);
}
private static bool IsEdgeDirectionSatisfied(
Edge edge,
XYZ edgeDirection)
{
var edgeCurve = edge.AsCurve() as Line;
if (edgeCurve == null)
return false;
return edgeCurve.Direction.CrossProduct(
edgeDirection).IsAlmostEqualTo(XYZ.Zero);
}
private static IEnumerable<FilledRegion>
FindFilledRegions(
Document document,
ElementId viewId)
{
var collector = new FilteredElementCollector(
document, viewId);
return collector
.OfClass(typeof(FilledRegion))
.Cast<FilledRegion>();
}
private static IEnumerable<Edge>
FindRegionEdges(
FilledRegion filledRegion)
{
var view = (View) filledRegion.Document.GetElement(
filledRegion.OwnerViewId);
var options = new Options
{
View = view,
ComputeReferences = true
};
return filledRegion
.get_Geometry(options)
.OfType<Solid>()
.SelectMany(x => x.Edges.Cast<Edge>());
}
private static ElementId DimensionTypeId(
Document doc,
string typeName)
{
var mt_coll
= new FilteredElementCollector(doc)
.OfClass(typeof(DimensionType))
.WhereElementIsElementType();
DimensionType dimType = null;
foreach (var type in mt_coll)
if (type is DimensionType dimensionType)
if (dimensionType.Name == typeName)
{
dimType = dimensionType;
break;
}
return dimType.Id;
}
#endregion // Dimension Filled Region Jorge
}
}