-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathGeometryObject.cs
More file actions
1599 lines (1370 loc) · 48.3 KB
/
GeometryObject.cs
File metadata and controls
1599 lines (1370 loc) · 48.3 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
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Rhino;
using Rhino.Geometry;
using ARDB = Autodesk.Revit.DB;
namespace RhinoInside.Revit.GH.Types
{
using Convert.Display;
using Convert.Geometry;
using External.DB;
using External.DB.Extensions;
using GH.Kernel.Attributes;
[Name("Geometry")]
public interface IGH_GeometryObject : IGH_Reference { }
[Name("Geometry")]
public abstract class GeometryObject : Reference,
IGH_GeometryObject,
IGH_GeometricGoo,
IGH_PreviewData
{
#region System.Object
#if DEBUG
public override string ToString()
{
try { return GetReference()?.ConvertToStableRepresentation(ReferenceDocument) ?? base.ToString(); }
catch { return base.ToString(); }
}
#endif
#endregion
#region IGH_Goo
public override bool ConvertTo<Q>(out Q target)
{
if (base.ConvertTo<Q>(out target)) return true;
if (typeof(Q).IsAssignableFrom(typeof(ARDB.GeometryObject)))
{
target = (Q) (object) Value;
return true;
}
else if (typeof(Q).IsAssignableFrom(typeof(ARDB.Reference)))
{
target = (Q) (object) GetReference();
return true;
}
else if (typeof(IGH_Element).IsAssignableFrom(typeof(Q)))
{
if (GetReference() is ARDB.Reference reference)
target = (Q) (object) (Element.FromReference(ReferenceDocument, reference) is Q element ? element : default);
else if (Id == ElementIdExtension.Invalid)
target = (Q) (object) new Element();
else
target = default;
return true;
}
else if (typeof(Q).IsAssignableFrom(typeof(Category)))
{
target = (Q) (object) GraphicsStyle.Category;
return true;
}
else if (typeof(Q).IsAssignableFrom(typeof(GraphicsStyle)))
{
target = (Q) (object) GraphicsStyle;
return true;
}
return false;
}
#endregion
#region DocumentObject
public override string DisplayName => GetType().GetCustomAttribute<NameAttribute>().Name;
public override object ScriptVariable() => Value;
public new ARDB.GeometryObject Value => base.Value as ARDB.GeometryObject;
protected override object FetchValue()
{
LoadReferencedData();
ResetReferenceTransform();
if (_ReferenceDocument is object && _Reference is object)
{
try
{
if (_ReferenceDocument.GetElement(_Reference) is ARDB.Element element)
{
var geometryReference = _Reference;
if (element is ARDB.RevitLinkInstance link && _Reference.LinkedElementId.IsValid())
{
ReferenceTransform = link.GetTransform().ToTransform();
element = link.GetLinkDocument()?.GetElement(_Reference.LinkedElementId);
geometryReference = _Reference.CreateReferenceInLink(link);
}
if (element is ARDB.Instance instance)
{
if (_Reference.ElementReferenceType != ARDB.ElementReferenceType.REFERENCE_TYPE_NONE && _Reference.ElementReferenceType != ARDB.ElementReferenceType.REFERENCE_TYPE_SUBELEMENT)
ReferenceTransform = HasReferenceTransform ? ReferenceTransform * instance.GetTransform().ToTransform() : instance.GetTransform().ToTransform();
}
Document = element?.Document;
return element?.GetGeometryObjectFromReference(geometryReference);
}
}
catch (Autodesk.Revit.Exceptions.ArgumentException) { }
}
return default;
}
protected void SetValue(ARDB.Document document, ARDB.Reference reference)
{
ResetValue();
if (reference is null)
document = null;
if (document is object)
{
ReferenceUniqueId = reference.ConvertToPersistentRepresentation(document);
ReferenceDocumentId = document.GetPersistentGUID();
_ReferenceDocument = document;
Document = reference.LinkedElementId == ARDB.ElementId.InvalidElementId ? _ReferenceDocument :
_ReferenceDocument.GetElement<ARDB.RevitLinkInstance>(reference.ElementId)?.GetLinkDocument();
_Reference = reference;
}
}
protected override void SubInvalidateGraphics()
{
_Location = null;
_Wires = null;
_Meshes = null;
_LevelOfDetail = double.NaN;
_ClippingBox = default;
base.SubInvalidateGraphics();
}
#endregion
#region ReferenceObject
public override bool? IsEditable => Value?.IsReadOnly;
#endregion
#region Reference
public override ARDB.ElementId Id => ReferenceUniqueId == string.Empty ?
ElementIdExtension.Invalid :
_Reference is null ? null :
_Reference.LinkedElementId != ARDB.ElementId.InvalidElementId ?
_Reference.LinkedElementId :
_Reference.ElementId;
private ARDB.Document _ReferenceDocument;
public override ARDB.Document ReferenceDocument => _ReferenceDocument?.IsValidObject is true ? _ReferenceDocument : null;
private ARDB.Reference _Reference;
public override ARDB.Reference GetReference() => _Reference;
public override ARDB.ElementId ReferenceId => _Reference?.ElementId;
public override bool IsReferencedDataLoaded => _ReferenceDocument is object && _Reference is object;
public override bool LoadReferencedData()
{
if (IsReferencedData && !IsReferencedDataLoaded)
{
UnloadReferencedData();
if (Types.Document.TryGetDocument(ReferenceDocumentId, out _ReferenceDocument))
{
try
{
_Reference = ReferenceExtension.ParseFromPersistentRepresentation(_ReferenceDocument, ReferenceUniqueId);
if (_Reference.LinkedElementId == ARDB.ElementId.InvalidElementId)
{
Document = _ReferenceDocument;
return true;
}
if (_ReferenceDocument.GetElement(_Reference.ElementId) is ARDB.RevitLinkInstance link && link.GetLinkDocument() is ARDB.Document linkDocument)
{
ReferenceTransform = link.GetTransform().ToTransform();
Document = linkDocument;
return true;
}
}
catch { }
_ReferenceDocument = null;
_Reference = null;
Document = null;
}
}
return IsReferencedDataLoaded;
}
public override void UnloadReferencedData()
{
if (IsReferencedData)
{
_ReferenceDocument = default;
_Reference = default;
}
base.UnloadReferencedData();
}
#endregion
#region IGH_GeometricGoo
Guid IGH_GeometricGoo.ReferenceID
{
get => Guid.Empty;
set { if (value != Guid.Empty) throw new InvalidOperationException(); }
}
bool IGH_GeometricGoo.IsReferencedGeometry => IsReferencedData;
bool IGH_GeometricGoo.IsGeometryLoaded => IsReferencedDataLoaded;
void IGH_GeometricGoo.ClearCaches() => UnloadReferencedData();
IGH_GeometricGoo IGH_GeometricGoo.DuplicateGeometry() => (IGH_GeometricGoo) MemberwiseClone();
bool IGH_GeometricGoo.LoadGeometry() => IsReferencedDataLoaded || LoadReferencedData();
bool IGH_GeometricGoo.LoadGeometry(Rhino.RhinoDoc doc) => IsReferencedDataLoaded || LoadReferencedData();
IGH_GeometricGoo IGH_GeometricGoo.Transform(Transform xform) => null;
IGH_GeometricGoo IGH_GeometricGoo.Morph(SpaceMorph xmorph) => null;
BoundingBox IGH_GeometricGoo.Boundingbox => GetBoundingBox(ReferenceTransform);
BoundingBox IGH_GeometricGoo.GetBoundingBox(Transform xform) => GetBoundingBox(xform * ReferenceTransform);
public abstract BoundingBox GetBoundingBox(Transform xform);
#endregion
#region IGH_PreviewData
protected int _CurveEnd = -1;
protected Plane? _Location = null;
protected Curve[] _Wires = null;
protected Mesh[] _Meshes = null;
protected double _LevelOfDetail = double.NaN;
private BoundingBox? _ClippingBox;
BoundingBox IGH_PreviewData.ClippingBox => _ClippingBox ??= HasReferenceTransform ?
ReferenceTransform.TransformBoundingBox(ClippingBox) :
ClippingBox;
/// <summary>
/// Not necessarily accurate axis aligned <see cref="Rhino.Geometry.BoundingBox"/> used for display.
/// </summary>
/// <returns>
/// A finite axis aligned bounding box.
/// </returns>
protected virtual BoundingBox ClippingBox => BoundingBox;
void IGH_PreviewData.DrawViewportWires(GH_PreviewWireArgs args)
{
if (args.Thickness <= 0 || args.Color.A == 0)
return;
var hasTransform = HasReferenceTransform;
try
{
if (hasTransform)
args.Pipeline.PushModelTransform(args.Pipeline.ModelTransform * ReferenceTransform);
//if (!IsVisible(args.Pipeline))
// return;
DrawViewportWires(args);
}
catch { _ClippingBox = BoundingBox.Empty; }
finally { if (hasTransform) args.Pipeline.PopModelTransform(); }
}
protected virtual void DrawViewportWires(GH_PreviewWireArgs args) { }
void IGH_PreviewData.DrawViewportMeshes(GH_PreviewMeshArgs args)
{
if (args.MeshingParameters is null)
return;
var hasTransform = HasReferenceTransform;
try
{
if (hasTransform)
args.Pipeline.PushModelTransform(args.Pipeline.ModelTransform * ReferenceTransform);
//if (!IsVisible(args.Pipeline))
// return;
DrawViewportMeshes(args);
}
catch { _ClippingBox = BoundingBox.Empty; }
finally { if (hasTransform) args.Pipeline.PopModelTransform(); }
}
protected virtual void DrawViewportMeshes(GH_PreviewMeshArgs args) { }
#endregion
protected GeometryObject() { }
protected GeometryObject(Reference reference) : base(reference)
{
ReferenceDocumentId = reference.ReferenceDocumentId;
ReferenceUniqueId = reference.ReferenceUniqueId;
_ReferenceDocument = reference.ReferenceDocument;
_Reference = reference.GetReference();
Document = reference.Document;
}
protected GeometryObject(ARDB.Document document, ARDB.GeometryObject geometryObject) : base(document, geometryObject) { }
protected GeometryObject(ARDB.Document document, ARDB.Reference reference)
{
if (reference is null) document = null;
ReferenceDocumentId = document.GetPersistentGUID();
ReferenceUniqueId = reference?.ConvertToPersistentRepresentation(document) ?? string.Empty;
_ReferenceDocument = document;
_Reference = reference;
Document = reference is null ? document :
reference.LinkedElementId == ARDB.ElementId.InvalidElementId ?
_ReferenceDocument :
_ReferenceDocument.GetElement<ARDB.RevitLinkInstance>(reference.ElementId)?.GetLinkDocument();
}
public static GeometryObject FromReference(ARDB.Document document, ARDB.Reference reference)
{
switch (reference?.ElementReferenceType)
{
case ARDB.ElementReferenceType.REFERENCE_TYPE_NONE:
return new GeometryElement(document, reference);
case ARDB.ElementReferenceType.REFERENCE_TYPE_LINEAR:
{
var stable = reference.ConvertToStableRepresentation(document);
return (stable.EndsWith("/0") || stable.EndsWith("/1")) ?
(GeometryObject) new GeometryPoint(document, reference) :
(GeometryObject) new GeometryCurve(document, reference);
}
case ARDB.ElementReferenceType.REFERENCE_TYPE_SURFACE:
return new GeometryFace(document, reference);
#if REVIT_2018
case ARDB.ElementReferenceType.REFERENCE_TYPE_MESH:
return new GeometryMesh(document, reference);
case ARDB.ElementReferenceType.REFERENCE_TYPE_SUBELEMENT:
return new GeometrySubelement(document, reference);
#endif
}
return null;
}
public static GeometryObject FromElementId(ARDB.Document document, ARDB.ElementId id)
{
if (document.GetElement(id) is ARDB.Element element)
return new GeometryElement(document, ARDB.Reference.ParseFromStableRepresentation(document, element.UniqueId));
return null;
}
public static GeometryObject FromLinkElementId(ARDB.Document document, ARDB.LinkElementId id)
{
if (id.HostElementId != ARDB.ElementId.InvalidElementId)
return FromElementId(document, id.HostElementId);
if
(
document.GetElement(id.LinkInstanceId) is ARDB.RevitLinkInstance link &&
link.GetLinkDocument() is ARDB.Document linkedDocument &&
linkedDocument.GetElement(id.LinkedElementId) is ARDB.Element linkedElement
)
{
using (var linkedElementReference = ARDB.Reference.ParseFromStableRepresentation(linkedElement.Document, linkedElement.UniqueId))
return new GeometryElement(document, linkedElementReference.CreateLinkReference(link));
}
return default;
}
public virtual ARDB.Reference GetDefaultReference() => _Reference;
public bool IsEquivalent(GeometryObject other) => other is object &&
Id.Equals(other.Id) &&
Document.IsEquivalent(other.Document) &&
Equals(GetType(), other.GetType());
public GraphicsStyle GraphicsStyle => Value is ARDB.GeometryObject geometryObject ?
geometryObject.GraphicsStyleId.IsValid() ? GetElement<GraphicsStyle>(geometryObject.GraphicsStyleId) : new GraphicsStyle() :
null;
/// <summary>
/// Accurate axis aligned <see cref="Rhino.Geometry.BoundingBox"/> for computation.
/// </summary>
public virtual BoundingBox BoundingBox => GetBoundingBox(Transform.Identity);
}
[Name("Element")]
public class GeometryElement : GeometryObject,
IGH_PreviewData,
Bake.IGH_BakeAwareElement
{
public override object ScriptVariable() => base.Value;
public new ARDB.GeometryElement Value => base.Value as ARDB.GeometryElement;
public override ARDB.Reference GetDefaultReference()
{
return GetAbsoluteReference(Document?.GetElement(Id)?.GetDefaultReference());
}
public GeometryElement() { }
public GeometryElement(ARDB.Document doc, ARDB.Reference reference) : base(doc, reference) { }
public GeometryElement(GraphicalElement element) : base(element)
{
_Element = element;
}
public override BoundingBox GetBoundingBox(Transform xform)
{
bool identity = xform.IsIdentity;
var inverse = identity ? null : xform.ToTransform().Inverse;
var bbox = Value?.GetBoundingBox(inverse);
if (!bbox.IsFinite()) // Some elements like RevitLinkInstance don't give us a BoundingBoxXYZ
return (Element as GraphicalElement)?.GetBoundingBox(xform) ?? NaN.BoundingBox;
if (bbox.IsFinite() && bbox.ToBox() is Box box)
return identity ? box.BoundingBox : box.GetBoundingBox(xform);
return NaN.BoundingBox;
}
#region IGH_PreviewData
Element _Element;
private Element Element => _Element ??= Element.FromReference(ReferenceDocument, GetReference());
protected override void DrawViewportWires(GH_PreviewWireArgs args)
{
if (Element is IGH_PreviewData preview)
{
var hasTransform = HasReferenceTransform;
try
{
if (hasTransform)
args.Pipeline.PushModelTransform(args.Pipeline.ModelTransform * ElementTransform);
preview.DrawViewportWires(args);
}
catch { }
finally { if (hasTransform) args.Pipeline.PopModelTransform(); }
}
else if (IsValid)
{
var bbox = ClippingBox;
if (bbox.IsValid) args.Pipeline.DrawBoxCorners(bbox, args.Color);
}
}
protected override void DrawViewportMeshes(GH_PreviewMeshArgs args)
{
if (Element is IGH_PreviewData preview)
{
var hasTransform = HasReferenceTransform;
try
{
if (hasTransform)
args.Pipeline.PushModelTransform(args.Pipeline.ModelTransform * ElementTransform);
preview.DrawViewportMeshes(args);
}
catch { }
finally { if (hasTransform) args.Pipeline.PopModelTransform(); }
}
}
#endregion
#region IGH_BakeAwareElement
bool IGH_BakeAwareData.BakeGeometry(RhinoDoc doc, Rhino.DocObjects.ObjectAttributes att, out Guid guid)
{
guid = Guid.Empty;
return (Element as IGH_BakeAwareData)?.BakeGeometry(doc, att, out guid) ?? false;
}
bool Bake.IGH_BakeAwareElement.BakeElement(IDictionary<ARDB.ElementId, Guid> idMap, bool overwrite, RhinoDoc doc, Rhino.DocObjects.ObjectAttributes att, out Guid guid)
{
guid = Guid.Empty;
return (Element as Bake.IGH_BakeAwareElement)?.BakeElement(idMap, overwrite, doc, att, out guid) ?? false;
}
#endregion
#region Properties
public override string DisplayName
{
get
{
if (Id == ElementIdExtension.Invalid) return "<None>";
switch (Element.FromReference(ReferenceDocument, GetReference()))
{
case null: return $"Null {base.DisplayName}";
case Element element: return element.DisplayName;
}
}
}
#endregion
#region Casting
public override bool ConvertTo<Q>(out Q target)
{
if (base.ConvertTo(out target)) return true;
if (typeof(Q).IsAssignableFrom(typeof(ARDB.GeometryElement)))
{
target = (Q) (object) (IsValid ? Value : null);
return true;
}
return Element?.ConvertTo(out target) ?? false;
}
public override bool ConvertFrom(object source)
{
if (source is IGH_Goo goo)
source = goo.ScriptVariable();
switch (source)
{
case ARDB.Element element:
if (element.GetDefaultReference() is ARDB.Reference reference && reference.ElementReferenceType == ARDB.ElementReferenceType.REFERENCE_TYPE_NONE)
{
SetValue(element.Document, reference);
return true;
}
break;
}
return base.ConvertFrom(source);
}
#endregion
}
#if REVIT_2018
[Name("Subelement")]
public class GeometrySubelement : GeometryObject, IGH_PreviewData
{
public override object ScriptVariable() => base.Value;
public new ARDB.GeometryElement Value => base.Value as ARDB.GeometryElement;
public GeometrySubelement() { }
public GeometrySubelement(ARDB.Document doc, ARDB.Reference reference) : base(doc, reference) { }
public override BoundingBox GetBoundingBox(Transform xform)
{
//using (var subelement = Document.GetSubelement(GetReference()))
//{
// try
// {
// var box = subelement.GetBoundingBox(null);
// var bbox = box.ToBox();
// if (bbox.IsValid)
// {
// if (HasTransform) bbox.Transform(GeometryToWorldTransform);
// return bbox.GetBoundingBox(xform);
// }
// }
// catch (Exception e) { }
//}
return NaN.BoundingBox;
}
#region IGH_PreviewData
protected override void DrawViewportWires(GH_PreviewWireArgs args)
{
if (!IsValid) return;
var bbox = ClippingBox;
if (!bbox.IsValid)
return;
args.Pipeline.DrawBoxCorners(bbox, args.Color);
}
#endregion
#region Properties
public override string DisplayName
{
get
{
if (Id == ElementIdExtension.Invalid) return "<None>";
switch (Element.FromReference(ReferenceDocument, GetReference()))
{
case null: return $"Null {base.DisplayName} : Subelement";
case Element element: return $"{element.DisplayName} : Subelement";
}
}
}
#endregion
#region Casting
public override bool ConvertTo<Q>(out Q target)
{
if (base.ConvertTo(out target)) return true;
if (typeof(Q).IsAssignableFrom(typeof(ARDB.GeometryElement)))
{
target = (Q) (object) (IsValid ? Value : null);
return true;
}
return false;
}
public override bool ConvertFrom(object source)
{
if (source is IGH_Goo goo)
source = goo.ScriptVariable();
switch (source)
{
case ARDB.Subelement element:
if (element.GetReference() is ARDB.Reference reference && reference.ElementReferenceType == ARDB.ElementReferenceType.REFERENCE_TYPE_SUBELEMENT)
{
SetValue(element.Document, reference);
return true;
}
break;
}
return base.ConvertFrom(source);
}
#endregion
}
#endif
[Name("Point")]
public class GeometryPoint : GeometryObject, IGH_PreviewData
{
public override object ScriptVariable() => Value;
public new ARDB.Point Value
{
get
{
if (GetReference() is ARDB.Reference reference && reference.ElementReferenceType == ARDB.ElementReferenceType.REFERENCE_TYPE_LINEAR)
{
var uniqueId = reference.ConvertToStableRepresentation(ReferenceDocument);
int end = -1;
if (uniqueId.EndsWith("/0")) end = 0;
else if (uniqueId.EndsWith("/1")) end = 1;
if (end == 0 || end == 1)
{
var curve = default(ARDB.Curve);
switch (base.Value)
{
case ARDB.Edge e: curve = e.AsCurve(); break;
case ARDB.Curve c: curve = c; break;
}
if (curve is object && curve.IsBound)
return ARDB.Point.Create(curve.GetEndPoint(end));
}
}
return base.Value as ARDB.Point;
}
}
public GeometryPoint() { }
public GeometryPoint(ARDB.Document document, ARDB.XYZ xyz) : base(document, ARDB.Point.Create(xyz)) { }
public GeometryPoint(ARDB.Document doc, ARDB.Reference reference) : base(doc, reference) { }
public sealed override string ToString()
{
return IsReferencedData ? base.ToString() : GH_Format.FormatPoint(Position);
}
public static new GeometryPoint FromReference(ARDB.Document document, ARDB.Reference reference)
{
var stable = reference.ConvertToStableRepresentation(document);
if (reference.ElementReferenceType == ARDB.ElementReferenceType.REFERENCE_TYPE_LINEAR && stable.EndsWith("/0") || stable.EndsWith("/1"))
{
return new GeometryPoint(document, reference);
}
else if (document.GetGeometryObjectFromReference(reference, out var transform) is ARDB.GeometryObject geometry)
{
using (geometry)
{
if (reference.GlobalPoint is object)
{
switch (geometry)
{
case ARDB.Edge edge:
{
using (var worldCurve = edge.AsCurve().CreateTransformed(transform))
{
var result = worldCurve.Project(reference.GlobalPoint, out var end);
reference = ARDB.Reference.ParseFromStableRepresentation(document, $"{stable}/{end}");
return new GeometryPoint(document, reference);
}
}
case ARDB.Curve curve:
{
using (var worldCurve = curve.CreateTransformed(transform))
{
var result = worldCurve.Project(reference.GlobalPoint, out var end);
reference = ARDB.Reference.ParseFromStableRepresentation(document, $"{stable}/{end}");
return new GeometryPoint(document, reference);
}
}
}
return new GeometryPoint(document, reference.GlobalPoint);
}
else if (geometry.TryGetLocation(out var location, out var _, out var _))
{
return new GeometryPoint(document, location);
}
}
}
return null;
}
public override BoundingBox GetBoundingBox(Transform xform)
{
var point = Position;
point.Transform(xform);
return point.IsValid ? new BoundingBox(point, point) : NaN.BoundingBox;
}
public Point Point => new Point(Position);
public Point3d Position => Location.Origin;
public Plane Location
{
get
{
if (_Location is null)
{
if (Curve is Curve curve && (_CurveEnd == CurveEnd.Start || _CurveEnd == CurveEnd.End))
{
if (curve.PerpendicularFrameAt(_CurveEnd == CurveEnd.Start ? curve.Domain.T0 : curve.Domain.T1, out var plane))
_Location = plane;
}
if (_Location is null && Value is ARDB.Point point)
_Location = new Plane(point.Coord.ToPoint3d(), Vector3d.XAxis, Vector3d.YAxis);
_Location ??= NaN.Plane;
}
return _Location.Value;
}
}
public Curve Curve
{
get
{
if (_Wires is null)
{
if (Document is ARDB.Document document && GetReference() is ARDB.Reference reference)
{
var stable = reference.ConvertToStableRepresentation(document);
if (stable.EndsWith("/0")) _CurveEnd = CurveEnd.Start;
if (stable.EndsWith("/1")) _CurveEnd = CurveEnd.End;
if (_CurveEnd == CurveEnd.Start || _CurveEnd == CurveEnd.End)
{
stable.Substring(0, stable.Length - 2);
reference = ARDB.Reference.ParseFromStableRepresentation(document, stable);
if (GeometryCurve.FromReference(document, reference) is GeometryCurve geometry && geometry.Curve is Curve curve)
_Wires = new Curve[] { curve };
}
}
_Wires ??= Array.Empty<Curve>();
}
return _Wires.FirstOrDefault();
}
}
#region IGH_PreviewData
protected override void DrawViewportWires(GH_PreviewWireArgs args)
{
var point = Position;
if (point.IsValid) args.Pipeline.DrawPoint(point, CentralSettings.PreviewPointStyle, CentralSettings.PreviewPointRadius, args.Color);
}
#endregion
#region Casting
public override bool ConvertTo<Q>(out Q target)
{
if (base.ConvertTo(out target)) return true;
if (typeof(Q).IsAssignableFrom(typeof(ARDB.Point)))
{
target = (Q) (object) (base.IsValid ? base.Value : null);
return true;
}
else if (typeof(Q).IsAssignableFrom(typeof(GH_Point)))
{
target = (Q) (object) new GH_Point(Position);
return true;
}
else if (typeof(Q).IsAssignableFrom(typeof(GH_Plane)))
{
target = (Q) (object) new GH_Plane(Location);
return true;
}
else if (typeof(Q).IsAssignableFrom(typeof(GeometryCurve)))
{
if (Document is ARDB.Document document && GetReference() is ARDB.Reference reference)
{
var stable = reference.ConvertToStableRepresentation(document);
if (stable.EndsWith("/0") || stable.EndsWith("/1"))
{
stable = stable.Substring(0, stable.Length - 2);
reference = ARDB.Reference.ParseFromStableRepresentation(document, stable);
}
target = (Q) (object) GeometryCurve.FromReference(document, reference);
return true;
}
}
return false;
}
#endregion
}
[Name("Curve")]
public class GeometryCurve : GeometryObject, IGH_PreviewData, IGH_Goo
{
public override object ScriptVariable() => base.Value;
public new ARDB.Curve Value
{
get
{
switch (base.Value)
{
case ARDB.Curve c: return c;
case ARDB.Edge e: return e.AsCurve();
}
return default;
}
}
public GeometryCurve() { }
public GeometryCurve(ARDB.Document doc, ARDB.Reference reference) : base(doc, reference) { }
public static new GeometryCurve FromReference(ARDB.Document document, ARDB.Reference reference)
{
return reference?.ElementReferenceType == ARDB.ElementReferenceType.REFERENCE_TYPE_LINEAR ?
new GeometryCurve(document, reference) : null;
}
public override BoundingBox GetBoundingBox(Transform xform)
{
return Curve is Curve curve ?
(
xform == Transform.Identity ?
curve.GetBoundingBox(true) :
curve.GetBoundingBox(xform)
) : NaN.BoundingBox;
}
#region Properties
public override string DisplayName
{
get
{
var value = base.Value;
string visibility;
switch (value?.Visibility)
{
case null: visibility = string.Empty; break;
case ARDB.Visibility.Visible: visibility = string.Empty; break;
default: visibility = $"{value.Visibility} "; break;
}
var typeName = base.DisplayName;
if (value is ARDB.Edge edge)
{
typeName = "Edge";
value = edge.AsCurve();
}
switch (value)
{
case null: return $"Null {typeName}";
case ARDB.Arc _: return $"{visibility}Arc {typeName}";
case ARDB.CylindricalHelix _: return $"{visibility}Helix {typeName}";
case ARDB.Ellipse _: return $"{visibility}Ellipse {typeName}";
case ARDB.HermiteSpline _: return $"{visibility}Hermite {typeName}";
case ARDB.Line _: return $"{visibility}Line {typeName}";
case ARDB.NurbSpline _: return $"{visibility}NURBS {typeName}";
case ARDB.Curve _: return $"{visibility}Unknown {typeName}";
default: return "Curve";
}
}
}
public Curve Curve
{
get
{
if (_Wires is null)
{
if (Value is ARDB.Curve curve)
_Wires = new Curve[] { curve.ToCurve() };
else
_Wires = Array.Empty<Curve>();
}
return _Wires.FirstOrDefault();
}
}
public GeometryPoint StartPoint
{
get
{
if (GetReference() is ARDB.Reference reference)
{
var stableRepresentation = reference.ConvertToStableRepresentation(ReferenceDocument);
return new GeometryPoint(ReferenceDocument, ARDB.Reference.ParseFromStableRepresentation(ReferenceDocument, $"{stableRepresentation}/0"));
}
return default;
}
}
public GeometryPoint EndPoint
{
get
{
if (GetReference() is ARDB.Reference reference)
{
var stableRepresentation = reference.ConvertToStableRepresentation(ReferenceDocument);
return new GeometryPoint(ReferenceDocument, ARDB.Reference.ParseFromStableRepresentation(ReferenceDocument, $"{stableRepresentation}/1"));
}
return default;
}
}
//public GeometryPoint StartPoint
//{
// get
// {
// if (base.Value is ARDB.Edge edge && edge.GetEndPointReference(CurveEnd.Start) is ARDB.Reference edgeReference)
// return GeometryObject.FromReference(ReferenceDocument, GetAbsoluteReference(edgeReference)) as GeometryPoint;
// if (base.Value is ARDB.Curve curve && curve.GetEndPointReference(CurveEnd.Start) is ARDB.Reference curveReference)
// return GeometryObject.FromReference(ReferenceDocument, GetAbsoluteReference(curveReference)) as GeometryPoint;
// return default;
// }
//}
//public GeometryPoint EndPoint
//{
// get
// {
// if (base.Value is ARDB.Edge edge && edge.GetEndPointReference(CurveEnd.End) is ARDB.Reference edgeReference)
// return GeometryObject.FromReference(ReferenceDocument, GetAbsoluteReference(edgeReference)) as GeometryPoint;
// if (base.Value is ARDB.Curve curve && curve.GetEndPointReference(CurveEnd.End) is ARDB.Reference curveReference)
// return GeometryObject.FromReference(ReferenceDocument, GetAbsoluteReference(curveReference)) as GeometryPoint;
// return default;
// }
//}
public GeometryFace LeftFace
{
get
{
if (base.Value is ARDB.Edge edge && edge.GetFace(0) is ARDB.Face face)
return GeometryObject.FromReference(ReferenceDocument, GetAbsoluteReference(face.Reference)) as GeometryFace;