Skip to content

Commit

Permalink
add project on to line
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Mar 18, 2023
1 parent bd87ead commit 29e3e3d
Show file tree
Hide file tree
Showing 4 changed files with 562 additions and 12 deletions.
18 changes: 13 additions & 5 deletions OpenMEP/Element/MEPCurve.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System.Collections;
using System.Runtime.CompilerServices;
using Autodesk.DesignScript.Runtime;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Dynamo.Graph.Nodes;
using GShark.Geometry;
using OpenMEP.Helpers;
using Revit.GeometryConversion;
using RevitServices.Transactions;
using Curve = Autodesk.DesignScript.Geometry.Curve;
using Line = Autodesk.Revit.DB.Line;
using Point = OpenMEPSandbox.Geometry.Point;

namespace OpenMEP.Element;

Expand Down Expand Up @@ -42,6 +45,7 @@ private MEPCurve()
return mCurve.Document.GetElement(id).ToDynamoType();
}
}

TransactionManager.Instance.TransactionTaskDone();
return null;
}
Expand All @@ -54,12 +58,13 @@ private MEPCurve()
/// <returns></returns>
private static ElementId BreakCurve(Autodesk.Revit.DB.MEPCurve? mepCurve, XYZ? ptBreak)
{
if(mepCurve==null) throw new ArgumentNullException(nameof(mepCurve));
if(ptBreak==null) throw new ArgumentNullException(nameof(ptBreak));
if (mepCurve == null) throw new ArgumentNullException(nameof(mepCurve));
if (ptBreak == null) throw new ArgumentNullException(nameof(ptBreak));
if (mepCurve is Autodesk.Revit.DB.Plumbing.Pipe || mepCurve is Autodesk.Revit.DB.Plumbing.FlexPipe)
{
return PlumbingUtils.BreakCurve(mepCurve.Document, mepCurve.Id, ptBreak);
}

if (mepCurve is Autodesk.Revit.DB.Mechanical.Duct || mepCurve is Autodesk.Revit.DB.Mechanical.FlexDuct)
{
return MechanicalUtils.BreakCurve(mepCurve.Document, mepCurve.Id, ptBreak);
Expand All @@ -70,6 +75,7 @@ private static ElementId BreakCurve(Autodesk.Revit.DB.MEPCurve? mepCurve, XYZ? p
ElementId elementId = BreakConduitCableTray(mepCurve.Document, mepCurve.Id, ptBreak);
return elementId;
}

return ElementId.InvalidElementId;
}

Expand Down Expand Up @@ -223,6 +229,8 @@ private static void AdjustMepCurve(Autodesk.Revit.DB.Element mepCurve, XYZ p1, X
return newTeeFitting.ToDynamoType();
}



/// <summary>
/// Add a new family instance of an transition fitting into the Autodesk Revit document,
/// using two connectors.
Expand Down Expand Up @@ -431,11 +439,11 @@ private static void AdjustMepCurve(Autodesk.Revit.DB.Element mepCurve, XYZ p1, X
/// </example>
[MultiReturn("Percent", "Degrees", "Ratio")]
[NodeCategory("Query")]
public static Dictionary<string, object?> Slope(Revit.Elements.Element mepCurve,double digit =0)
public static Dictionary<string, object?> Slope(Revit.Elements.Element mepCurve, double digit = 0)
{
if(mepCurve==null) throw new ArgumentNullException(nameof(mepCurve));
if (mepCurve == null) throw new ArgumentNullException(nameof(mepCurve));
Autodesk.Revit.DB.MEPCurve? internalElement = mepCurve.InternalElement as Autodesk.Revit.DB.MEPCurve;
LocationCurve? locationCurve = internalElement!.Location as LocationCurve;
return OpenMEPSandbox.Geometry.Line.Slope(locationCurve?.Curve.ToDynamoType(), digit);
return OpenMEPSandbox.Geometry.Line.Slope(locationCurve?.Curve.ToDynamoType(), digit);
}
}
38 changes: 31 additions & 7 deletions OpenMEPSandbox/Geometry/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ public static Autodesk.DesignScript.Geometry.Point ProjectOntoPlane(
return Autodesk.DesignScript.Geometry.Point.ByCoordinates(projectedPoint.X, projectedPoint.Y, projectedPoint.Z);
}

/// <summary>
/// Project a point onto a line
/// </summary>
/// <param name="point">Point need to project</param>
/// <param name="line">Line to project the point</param>
/// <returns name="point">projected point</returns>
/// <example>
/// ![](../OpenMEPPage/geometry/dyn/pic/Point.ProjectOnToLine.gif)
/// </example>
public static Autodesk.DesignScript.Geometry.Point ProjectOnToLine(Autodesk.DesignScript.Geometry.Point point,
Autodesk.DesignScript.Geometry.Line line)
{
Autodesk.DesignScript.Geometry.Vector lineDirection = line.Direction.Normalized();
Autodesk.DesignScript.Geometry.Point start = line.StartPoint;
Autodesk.DesignScript.Geometry.Vector vector = point.AsVector().Subtract(start.AsVector());
double projectionLength = lineDirection.Dot(vector);
var ProjectedPoint = start.Add(lineDirection.Scale(projectionLength));
return ProjectedPoint;
}


/// <summary>
/// Get the centroid of a list of points
/// </summary>
Expand Down Expand Up @@ -99,9 +120,10 @@ public static bool IsOnPlane(Autodesk.DesignScript.Geometry.Point point, Autodes
/// ![](../OpenMEPPage/geometry/dyn/pic/Point.Deconstruct.png)
/// </example>
[MultiReturn("X", "Y", "Z")]
public static Dictionary<string, object?> Deconstruct ([DefaultArgument("Autodesk.DesignScript.Geometry.Point.ByCoordinates(0,0,0)")] Autodesk.DesignScript.Geometry.Point point)
public static Dictionary<string, object?> Deconstruct(
[DefaultArgument("Autodesk.DesignScript.Geometry.Point.ByCoordinates(0,0,0)")]
Autodesk.DesignScript.Geometry.Point point)
{

if (point == null) throw new ArgumentNullException(nameof(point));
return new Dictionary<string, object?>
{
Expand Down Expand Up @@ -240,8 +262,7 @@ public static double Euclidean(Autodesk.DesignScript.Geometry.Point p1, Autodesk
{
return Euclidean(p1.X, p2.X, p1.Y, p2.Y, p1.Z, p2.Z);
}




/// <summary>
/// return distance between two points by Euclidean distance
Expand Down Expand Up @@ -311,7 +332,7 @@ internal static double Euclidean(double x1, double x2, double y1, double y2, dou
/// <returns name="Permutation">Permutation of list index matching optimize</returns>
/// <returns name="mincost">minimum cost can optimize</returns>
/// <returns name="assignment">index optimize can assignment</returns>
[MultiReturn("assignment","mincost")]
[MultiReturn("assignment", "mincost")]
public static Dictionary<string, object?> AssignmentMatching(List<Autodesk.DesignScript.Geometry.Point> lcMachines,
List<Autodesk.DesignScript.Geometry.Point> lcDevices)
{
Expand All @@ -324,6 +345,7 @@ internal static double Euclidean(double x1, double x2, double y1, double y2, dou
cost[i, j] = manhattan;
}
}

// TODO : Array Matching Index
int[,]? originCost = cost.Clone() as int[,];
List<int> assignment = cost.FindAssignments().ToList();
Expand All @@ -333,6 +355,7 @@ internal static double Euclidean(double x1, double x2, double y1, double y2, dou
{
mincost += originCost[i, assignment[i]];
}

return new Dictionary<string, object?>()
{
{"assignment", assignment},
Expand All @@ -347,8 +370,8 @@ internal static double Euclidean(double x1, double x2, double y1, double y2, dou
/// <param name="lcDevices">list location of devices</param>
/// <returns name="mincost">minimum cost can optimize</returns>
/// <returns name="assignment">index optimize can assignment</returns>
[MultiReturn("assignment","mincost")]
public static Dictionary<string,object?> BruteForceMatching(List<Autodesk.DesignScript.Geometry.Point> lcMachines,
[MultiReturn("assignment", "mincost")]
public static Dictionary<string, object?> BruteForceMatching(List<Autodesk.DesignScript.Geometry.Point> lcMachines,
List<Autodesk.DesignScript.Geometry.Point> lcDevices)
{
int[,] cost = new int[lcMachines.Count, lcDevices.Count];
Expand All @@ -360,6 +383,7 @@ internal static double Euclidean(double x1, double x2, double y1, double y2, dou
cost[i, j] = manhattan;
}
}

BruteForceMethod bruteForceMethod = new BruteForceMethod();
(int minCost, IEnumerable<int> minAssignment) result = bruteForceMethod.BruteForce(cost);
IEnumerable<int> assignment = result.minAssignment;
Expand Down
Loading

0 comments on commit 29e3e3d

Please sign in to comment.