Skip to content

Commit

Permalink
add GetClosestGridLine
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Aug 23, 2024
1 parent bfaa27c commit 6a2770a
Show file tree
Hide file tree
Showing 4 changed files with 767 additions and 5 deletions.
49 changes: 45 additions & 4 deletions OpenMEPRevit/ClassModel/GridItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ namespace OpenMEPRevit.ClassModel;
[IsVisibleInDynamoLibrary(false)]
public class GridItem
{
private readonly double Tolerance = 1.0e-9;
public Grid? Grid { get; set; }

public bool IsHorizontal => Direction(Grid.Curve)!.IsAlmostEqualTo(XYZ.BasisX);

public bool IsHorizontal => IsGridHorizontal();
public bool IsVertical => IsGridVertical();
public GridItem(Grid? grid)
{
Grid = grid;
}

public double DistanceTo(XYZ? point)
{
Curve curve = this.Grid.Curve;
Curve curve = this.Grid!.Curve;
var plane = Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), XYZ.Zero);
Curve newcurve = ProjectLineOnPlane(plane, curve);
XYZ p = ProjectPointOnPlane(plane, point);
Expand Down Expand Up @@ -47,6 +47,47 @@ Autodesk.Revit.DB.Curve ProjectLineOnPlane(Autodesk.Revit.DB.Plane plane, Autode
var p1 = line.GetEndPoint(1) - d * normal;
return Autodesk.Revit.DB.Line.CreateBound(p0, p1);
}
private bool IsGridHorizontal()
{
Outline? extents = this.Grid?.GetExtents();
if (extents == null)
{
return false;
}
XYZ maximumPoint = extents.MaximumPoint;
XYZ minimumPoint = extents.MinimumPoint;
Line line = GetMiddleLine(minimumPoint, maximumPoint);
return line.Direction.IsAlmostEqualTo(XYZ.BasisX,Tolerance);
}
private bool IsGridVertical()
{
Outline? extents = this.Grid?.GetExtents();
if (extents == null)
{
return false;
}
XYZ maximumPoint = extents.MaximumPoint;
XYZ minimumPoint = extents.MinimumPoint;
Line line = GetMiddleLine(minimumPoint, maximumPoint);
return line.Direction.IsAlmostEqualTo(XYZ.BasisY, Tolerance);
}
private Line GetMiddleLine(XYZ? minPoint, XYZ? maxPoint)
{
double x1 = minPoint.X;
double y1 = minPoint.Y;
double x2 = maxPoint.X;
double y2 = maxPoint.Y;
double width = x2 - x1;
double height = y2 - y1;
if (width > height)
{
double midY = (y1 + y2) / 2;
return Line.CreateBound(new XYZ(x1, midY, 0), new XYZ(x2, midY, 0));
}
// Vertical middle line
double midX = (x1 + x2) / 2;
return Line.CreateBound(new XYZ(midX, y1, 0), new XYZ(midX, y2, 0));
}
XYZ? Direction(Autodesk.Revit.DB.Curve curve)
{
Line? line = curve as Line;
Expand Down
48 changes: 48 additions & 0 deletions OpenMEPRevit/Element/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,54 @@ private Element()
};
}

/// <summary>
/// Get the closest grid line to the element, return the list closest grid line with 4 direction:
/// e.g: top, bottom, left, right
/// </summary>
/// <param name="element"></param>
/// <param name="grids"></param>
/// <returns></returns>
[NodeCategory("Query")]
[MultiReturn("TopGrids", "BottomGrids", "LeftGrids", "RightGrids")]
public static Dictionary<string, object?> GetClosestGridLine(Revit.Elements.Element element,
List<Revit.Elements.Element> grids)
{
List<Grid?> topGrids = null;
List<Grid?> bottomGrids = null;
List<Grid?> leftGrids = null;
List<Grid?> rightGrids = null;
// group top : from element to top view plan
XYZ location = GetLocation(element).ToXyz();
if (location == null)
{
return new Dictionary<string, object?>()
{
{ "TopGrids", null },
{ "BottomGrids", null },
{ "LeftGrids", null },
{ "RightGrids", null }
};
}
List<GridItem> gridItems = GetGrids(DocumentManager.Instance.CurrentDBDocument);
var xGrids = gridItems.Where(x => x.IsHorizontal).ToList();
var yGrids = gridItems.Where(x => x.IsVertical).ToList();
topGrids = xGrids.Where(x => x?.Grid?.Curve.GetEndPoint(0).Y >= location.Y)
.OrderBy(x => x.DistanceTo(location)).Select(x => x.Grid).ToList();
bottomGrids = xGrids.Where(x => x?.Grid?.Curve.GetEndPoint(0).Y <= location.Y)
.OrderBy(x => x.DistanceTo(location)).Select(x => x.Grid).ToList();
leftGrids = yGrids.Where(x => x?.Grid?.Curve.GetEndPoint(0).X <= location.X)
.OrderBy(x => x.DistanceTo(location)).Select(x => x.Grid).ToList();
rightGrids = yGrids.Where(x => x?.Grid?.Curve.GetEndPoint(0).X >= location.X)
.OrderBy(x => x.DistanceTo(location)).Select(x => x.Grid).ToList();
return new Dictionary<string, object?>()
{
{ "TopGrids", topGrids.Select(x => x?.ToDynamoType()).ToList() },
{ "BottomGrids", bottomGrids.Select(x => x?.ToDynamoType()).ToList() },
{ "LeftGrids", leftGrids.Select(x => x?.ToDynamoType()).ToList() },
{ "RightGrids", rightGrids.Select(x => x?.ToDynamoType()).ToList() }
};
}

/// <summary>
/// Get the location of the element belong to the grid line with the closest distance
/// the location include the top, bottom, left, right grid line
Expand Down
5 changes: 4 additions & 1 deletion build/_build.csproj.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_SIMPLE_ANONYMOUSMETHOD_ON_SINGLE_LINE/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=4a98fdf6_002D7d98_002D4f5a_002Dafeb_002Dea44ad98c70c/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=f9fce829_002De6f4_002D4cb2_002D80f1_002D5497c44f51df/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
Expand All @@ -24,4 +26,5 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EPredefinedNamingRulesToUserRulesUpgrade/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Loading

0 comments on commit 6a2770a

Please sign in to comment.