Skip to content

Commit

Permalink
Merge pull request #967 from mcneel/1.16
Browse files Browse the repository at this point in the history
1.16
  • Loading branch information
kike-garbo authored Jul 27, 2023
2 parents 906c76d + 417f031 commit f57d161
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 70 deletions.
2 changes: 1 addition & 1 deletion docs/pages/_en/1.0/reference/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ group: Deployment & Configs
- Added 'Add Toposolid Sub-Division' component.
- Added 'Component References' component.
- Added 'Component Reference Plane' component.
- Added 'Element Annotations' component.
- Added 'Reference Annotations' component.
- Improved 'Host Shape' performance.

{% endcapture %}
Expand Down
84 changes: 53 additions & 31 deletions src/RhinoInside.Revit.GH/Components/Annotations/References.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Grasshopper.Kernel;
using ARDB = Autodesk.Revit.DB;

namespace RhinoInside.Revit.GH.Components.Annotations
{
using External.DB;
using External.DB.Extensions;

[ComponentVersion(introduced: "1.12")]
public class AnnotationReferences : ZuiComponent
Expand All @@ -17,7 +19,7 @@ public class AnnotationReferences : ZuiComponent
public AnnotationReferences() : base
(
name: "Annotation References",
nickname: "References",
nickname: "A-References",
description: string.Empty,
category: "Revit",
subCategory: "Annotate"
Expand Down Expand Up @@ -70,16 +72,16 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
}

[ComponentVersion(introduced: "1.16")]
public class ElementAnnotations : ZuiComponent
public class ReferenceAnnotations : ZuiComponent
{
public override Guid ComponentGuid => new Guid("2AB03AAF-98E4-4EF5-A84B-918B64E5908D");
public override GH_Exposure Exposure => GH_Exposure.quinary;
protected override string IconTag => string.Empty;

public ElementAnnotations() : base
public ReferenceAnnotations() : base
(
name: "Element Annotations",
nickname: "E-Annotations",
name: "Reference Annotations",
nickname: "R-Annotations",
description: string.Empty,
category: "Revit",
subCategory: "Annotate"
Expand All @@ -91,10 +93,10 @@ public ElementAnnotations() : base
{
new ParamDefinition
(
new Parameters.GraphicalElement()
new Parameters.GeometryObject()
{
Name = "Element",
NickName = "E",
Name = "Reference",
NickName = "R",
}
),
new ParamDefinition
Expand All @@ -111,14 +113,6 @@ public ElementAnnotations() : base
protected override ParamDefinition[] Outputs => outputs;
static readonly ParamDefinition[] outputs =
{
new ParamDefinition
(
new Parameters.GraphicalElement()
{
Name = "Element",
NickName = "E",
}, ParamRelevance.Secondary
),
new ParamDefinition
(
new Parameters.Dimension()
Expand All @@ -143,8 +137,9 @@ public ElementAnnotations() : base

protected override void TrySolveInstance(IGH_DataAccess DA)
{
if (!Params.GetData(DA, "Element", out Types.GraphicalElement element, x => x.IsValid)) return;
else Params.TrySetData(DA, "Element", () => element);
if (!Params.GetData(DA, "Reference", out Types.GeometryObject reference, x => x.IsValid)) return;
if (!reference.CastTo(out Types.GraphicalElement referenceElement)) return;

if (!Params.TryGetData(DA, "View", out Types.View view, x => x.IsValid)) return;

var _Dimensions_ = Params.IndexOfOutputParam("Dimensions");
Expand All @@ -164,24 +159,51 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
if (typesList.Count == 0) return;
var filter = CompoundElementFilter.ElementClassFilter(typesList);

if (view is object)
filter = filter.Intersect(new ARDB.ElementOwnerViewFilter(view.Id));

var dimensions = new List<ARDB.Dimension>();
var tags = new List<ARDB.Element>();
IEnumerable<ARDB.Element> annotationElements = null;

foreach (var id in element.Value.GetDependentElements(filter.ThatExcludes(element.Id)))
if (view is object)
{
switch (element.Document.GetElement(id))
if (view.Document.IsEquivalent(reference.Document))
{
filter = filter.Intersect(new ARDB.ElementOwnerViewFilter(view.Id, inverted: false));
annotationElements = referenceElement.Value.GetDependentElements(filter.ThatExcludes(reference.Id)).Select(reference.Document.GetElement);
}
else if (view.Document.IsEquivalent(reference.ReferenceDocument))
{
case ARDB.IndependentTag independentTag:
tags.Add(independentTag); break;
using (var collector = new ARDB.FilteredElementCollector(view.Document).WherePasses(filter))
annotationElements = collector.OwnedByView(view.Id).ToElements();
}
}
else
{
filter = filter.Intersect(new ARDB.ElementOwnerViewFilter(ElementIdExtension.Invalid, inverted: true));
annotationElements = referenceElement.Value.GetDependentElements(filter.ThatExcludes(reference.Id)).Select(reference.Document.GetElement);
}

case ARDB.SpatialElementTag spatialElementTag:
tags.Add(spatialElementTag); break;
var dimensions = new List<Types.Dimension>();
var tags = new List<Types.TagElement>();

case ARDB.Dimension dimension:
dimensions.Add(dimension); break;
foreach (var annotationElement in annotationElements)
{
var dependent = reference.GetElement<Types.GraphicalElement>(annotationElement);
if (dependent is Types.IAnnotationReferencesAccess annotation)
{
foreach (var annotationReference in annotation.References)
{
if (!annotationReference.IsEquivalent(reference))
continue;

switch (dependent)
{
case Types.Dimension dimension:
dimensions.Add(dimension); break;

case Types.TagElement tag:
tags.Add(tag); break;
}

break;
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/RhinoInside.Revit.GH/Components/Element/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ public ElementPropertyView()
Name = "View Specific",
NickName = "S",
Description = "Wheter element is view specific or not",
}
}, ParamRelevance.Primary
),
new ParamDefinition
(
new Parameters.View()
{
Name = "View",
NickName = "V",
Description = "Element owner view",
}
Description = "The view that owns the Element.",
}, ParamRelevance.Primary
),
};

Expand All @@ -73,8 +73,8 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
if (!Params.GetData(DA, "Element", out Types.GraphicalElement element, x => x.IsValid)) return;
else DA.SetData("Element", element);

DA.SetData("View Specific", element.ViewSpecific);
DA.SetData("View", element.OwnerView);
Params.TrySetData(DA, "View Specific", () => element.ViewSpecific);
Params.TrySetData(DA, "View", () => element.OwnerView);
}
}
}
23 changes: 13 additions & 10 deletions src/RhinoInside.Revit.GH/Parameters/GraphicalElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,9 @@ protected override void Menu_AppendBakeItem(ToolStripDropDown menu)

if (VolatileData.DataCount == 1)
{
var cplane = VolatileData.AllData(true).FirstOrDefault() is Types.GraphicalElement element &&
element.Location.IsValid;
// `Types.Group.Location` is too slow for this purpose.
//var cplane = VolatileData.AllData(true).FirstOrDefault() is Types.GraphicalElement element && element.Location.IsValid;
var cplane = VolatileData.AllData(true).FirstOrDefault() is Types.GraphicalElement element && element.IsValid;

Menu_AppendItem(menu, $"Set CPlane", Menu_SetCPlane, cplane, false);
}
Expand Down Expand Up @@ -515,20 +516,22 @@ private void Menu_HighlightElements(object sender, EventArgs e)

private void Menu_SetCPlane(object sender, EventArgs e)
{
if (VolatileData.AllData(true).FirstOrDefault() is Types.GraphicalElement element)
if
(
VolatileData.AllData(true).FirstOrDefault() is Types.GraphicalElement element &&
Rhino.RhinoDoc.ActiveDoc is Rhino.RhinoDoc doc &&
(doc.Views.ActiveView ?? doc.Views.FirstOrDefault()) is RhinoView view &&
view.ActiveViewport is RhinoViewport vport
)
{
if
(
Rhino.RhinoDoc.ActiveDoc is Rhino.RhinoDoc doc &&
(doc.Views.ActiveView ?? doc.Views.FirstOrDefault()) is RhinoView view &&
view.ActiveViewport is RhinoViewport vport
)
var location = element.Location;
if (location.IsValid)
{
view.BringToFront();
doc.Views.ActiveView = view;

var cplane = vport.GetConstructionPlane();
cplane.Plane = element.Location;
cplane.Plane = location;
vport.PushConstructionPlane(cplane);

view.Redraw();
Expand Down
2 changes: 1 addition & 1 deletion src/RhinoInside.Revit.GH/Types/Annotations/Annotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IGH_Annotation : IGH_GraphicalElement
interface IAnnotationReferencesAccess
{
/// <summary>
/// Returns an array of geometric references to which the dimension is attached.
/// Returns an array of geometric references to which the annotation is attached.
/// </summary>
GeometryObject[] References { get; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/RhinoInside.Revit.GH/Types/Annotations/Dimension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public override Curve Curve
public GeometryObject[] References =>
Value?.References.
Cast<ARDB.Reference>().
Select(x => GeometryObject.FromReference(ReferenceDocument, x)).
Select(GetGeometryObjectFromReference<GeometryObject>).
ToArray();
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public override GeometryObject[] References
if (Value is ARDB.AreaTag tag && tag.Area is ARDB.Area area)
{
using (var reference = ARDB.Reference.ParseFromStableRepresentation(area.Document, area.UniqueId))
return new GeometryObject[] { GeometryElement.FromReference(area.Document, reference) };
return new GeometryObject[] { GetGeometryObjectFromReference<GeometryElement>(reference) };
}

return default;
Expand Down Expand Up @@ -214,7 +214,7 @@ public override GeometryObject[] References
}
else
{
try { return new GeometryObject[] { GeometryObject.FromElementId(ReferenceDocument, tag.TaggedLocalRoomId) }; }
try { return new GeometryObject[] { GeometryObject.FromElementId(Document, tag.TaggedLocalRoomId) }; }
catch { }
}
}
Expand Down Expand Up @@ -242,7 +242,7 @@ public override GeometryObject[] References
if (Value is ARDB.Mechanical.SpaceTag tag && tag.Space is ARDB.Mechanical.Space space)
{
using (var reference = ARDB.Reference.ParseFromStableRepresentation(space.Document, space.UniqueId))
return new GeometryObject[] { GeometryElement.FromReference(space.Document, reference) };
return new GeometryObject[] { GetGeometryObjectFromReference<GeometryElement>(reference) };
}

return default;
Expand Down
2 changes: 1 addition & 1 deletion src/RhinoInside.Revit.GH/Types/Annotations/TagElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override Plane Location
public override GeometryObject[] References =>
Value?.GetTaggedReferences().
Cast<ARDB.Reference>().
Select(x => GeometryObject.FromReference(ReferenceDocument, x)).
Select(GetGeometryObjectFromReference<GeometryElement>).
ToArray();
#endregion

Expand Down
9 changes: 7 additions & 2 deletions src/RhinoInside.Revit.GH/Types/GeometryObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,13 @@ public static GeometryObject FromLinkElementId(ARDB.Document document, ARDB.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;
Expand All @@ -371,8 +378,6 @@ public static GeometryObject FromLinkElementId(ARDB.Document document, ARDB.Link
/// Accurate axis aligned <see cref="Rhino.Geometry.BoundingBox"/> for computation.
/// </summary>
public virtual BoundingBox BoundingBox => GetBoundingBox(Transform.Identity);

public virtual ARDB.Reference GetDefaultReference() => _Reference;
}

[Name("Element")]
Expand Down
25 changes: 10 additions & 15 deletions src/RhinoInside.Revit.GH/Types/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,12 @@ protected Reference(ARDB.Document doc, object value) : base(doc, value) { }

protected internal ARDB.Reference GetAbsoluteReference(ARDB.Reference reference)
{
if (reference.LinkedElementId == ElementIdExtension.Invalid)
if (IsLinked)
{
if (reference.ElementId != Id)
throw new ArgumentException("Invalid Reference", nameof(reference));

if (IsLinked)
if (reference.LinkedElementId == ElementIdExtension.Invalid)
return reference.CreateLinkReference(ReferenceDocument, ReferenceId, Document);
}
else
{
if (reference.ElementId != ReferenceId)

if (reference.LinkedElementId != ReferenceId)
throw new ArgumentException("Invalid Reference", nameof(reference));
}

Expand All @@ -205,13 +200,13 @@ protected internal T GetElement<T>(ARDB.Element element) where T : Element
{
if (element.IsValid())
{
if (!Document.IsEquivalent(element.Document))
throw new Exceptions.RuntimeArgumentException($"Invalid {typeof(T)} Document", nameof(element));
if (IsLinked && Document.IsEquivalent(element.Document))
return (T) Element.FromLinkElementId(ReferenceDocument, new ARDB.LinkElementId(ReferenceId, element.Id));

return (T)
(IsLinked ?
Element.FromLinkElementId(ReferenceDocument, new ARDB.LinkElementId(ReferenceId, element.Id)) :
Element.FromElement(element));
if (!ReferenceDocument.IsEquivalent(element.Document))
throw new Exceptions.RuntimeArgumentException(nameof(element), $"Invalid {typeof(T)} Document");

return (T) Element.FromElement(element);
}

return null;
Expand Down

0 comments on commit f57d161

Please sign in to comment.