Skip to content

Instantly share code, notes, and snippets.

@chuongmep
Last active November 22, 2022 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuongmep/e10b8f78319dad3b2d4f2250e8a460e5 to your computer and use it in GitHub Desktop.
Save chuongmep/e10b8f78319dad3b2d4f2250e8a460e5 to your computer and use it in GitHub Desktop.
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Line = Autodesk.Revit.DB.Line;
using Point = Autodesk.Revit.DB.Point;
var Doc = commandData.Application.ActiveUIDocument.Document;
using TransactionGroup trang = new TransactionGroup(Doc, "test");
trang.Start();
XYZ a = UIDoc.Selection.PickPoint(ObjectSnapTypes.None);
SetPoint(a);
XYZ b = UIDoc.Selection.PickPoint(ObjectSnapTypes.None);
SetPoint(b);
SetLine(a,b);
XYZ p1 = UIDoc.Selection.PickPoint(ObjectSnapTypes.None);
SetPoint(p1);
XYZ p2 = UIDoc.Selection.PickPoint(ObjectSnapTypes.None);
SetPoint(p2);
bool isSamSide = IsSamSide(p1, p2, a, b);
MessageBox.Show(isSamSide.ToString());
trang.Assimilate();
// visualize a point
void SetPoint(XYZ xyz)
{
using (Transaction tran = new Transaction(Doc, "Add point"))
{
tran.Start();
Point point1 = Point.Create(xyz);
DirectShape ds =
DirectShape.CreateElement(Doc, new ElementId(BuiltInCategory.OST_GenericModel));
ds.SetShape(new List<GeometryObject>() {point1});
tran.Commit();
}
}
// visualize a line
void SetLine(XYZ x1,XYZ x2)
{
using (Transaction tran = new Transaction(Doc, "Add line"))
{
tran.Start();
Line line = Line.CreateBound(x1, x2);
DirectShape ds =
DirectShape.CreateElement(Doc, new ElementId(BuiltInCategory.OST_GenericModel));
ds.SetShape(new List<GeometryObject>() {line});
tran.Commit();
}
}
using TransactionGroup trang = new TransactionGroup(Doc, "test");
trang.Start();
XYZ a = UIDoc.Selection.PickPoint(ObjectSnapTypes.None);
SetPoint(a);
XYZ b = UIDoc.Selection.PickPoint(ObjectSnapTypes.None);
SetPoint(b);
SetLine(a,b);
XYZ c = UIDoc.Selection.PickPoint(ObjectSnapTypes.None);
SetPoint(c);
SetLine(b,c);
SetLine(a,c);
XYZ p = UIDoc.Selection.PickPoint(ObjectSnapTypes.None);
SetPoint(p);
bool pointInTriangle = PointInTriangle(p, a, b, c);
MessageBox.Show(pointInTriangle.ToString());
trang.Assimilate();
// visualize a point
void SetPoint(XYZ xyz)
{
using (Transaction tran = new Transaction(Doc, "Add point"))
{
tran.Start();
Point point1 = Point.Create(xyz);
DirectShape ds =
DirectShape.CreateElement(Doc, new ElementId(BuiltInCategory.OST_GenericModel));
ds.SetShape(new List<GeometryObject>() {point1});
tran.Commit();
}
}
// visualize a line
void SetLine(XYZ x1,XYZ x2)
{
using (Transaction tran = new Transaction(Doc, "Add line"))
{
tran.Start();
Line line = Line.CreateBound(x1, x2);
DirectShape ds =
DirectShape.CreateElement(Doc, new ElementId(BuiltInCategory.OST_GenericModel));
ds.SetShape(new List<GeometryObject>() {line});
tran.Commit();
}
}
using Objects;
using Objects.BuiltElements.Revit;
using Objects.Geometry;
using Speckle.Core.Api;
using Speckle.Core.Credentials;
using Speckle.Core.Models;
using Speckle.Core.Transports;
using Point = Objects.Geometry.Point;
using Stream = Speckle.Core.Api.Stream;
Account account = AccountManager.GetDefaultAccount();
Stream stream = new Stream() {id = "ef2f634638", name = "Test"};
ServerTransportV2 serverTransportV2 = new ServerTransportV2(account, stream.id);
string chairId = "b7a1a3349fde87a6f699656daef20e7a";
Base chair = Operations.Receive(chairId,serverTransportV2).Result;
FamilyInstance familyInstance = chair as FamilyInstance;
Point basePoint = familyInstance.basePoint;
Plane plane = new Plane();
plane.origin = basePoint;
plane.normal = new Vector(0,0,1);
Vector vector = new Vector(basePoint);
string floorId = "a9913516192b6e67858af66c5a6862f9";
Base floor = Operations.Receive(floorId,serverTransportV2).Result;
RevitFloor? revitFloor = floor as RevitFloor;
Polycurve polycurve = (Polycurve) revitFloor.outline;
List<ICurve> segments = polycurve.segments;
List<Vector> points = new List<Vector>();
foreach (ICurve segment in segments)
{
if (segment is Line)
{
Line line = (Line) segment;
Vector pointOnPlane = projectPointOnPlane(new Vector(line.start), plane);
points.Add(pointOnPlane);
}
}
Vector famVec = projectPointOnPlane(vector, plane);
bool pointInPoly = IsPointInPoly(points, famVec);
Console.WriteLine(pointInPoly);
bool IsPointInPoly(List<Vector> coordinates, Vector testPoint)
{
bool result = false;
int j = coordinates.Count() - 1;
for (int i = 0; i < coordinates.Count(); i++)
{
if (coordinates[i].y < testPoint.y && coordinates[j].y >= testPoint.y ||
coordinates[j].y < testPoint.y && coordinates[i].y >= testPoint.y)
{
if (coordinates[i].x + (testPoint.x - coordinates[i].y) / (coordinates[j].y - coordinates[i].y) *
(coordinates[j].x - coordinates[i].x) < testPoint.x)
{
result = !result;
}
}
j = i;
}
return result;
}
// check point in polyline
bool IsPointInPolygon(PolyLine poly, RoomPoint testPoint)
{
bool result = false;
IList<XYZ> coordinates = poly.GetCoordinates();
int j = coordinates.Count() - 1;
for (int i = 0; i < coordinates.Count(); i++)
{
if (coordinates[i].Y < testPoint.Y && coordinates[j].Y >= testPoint.Y ||
coordinates[j].Y < testPoint.Y && coordinates[i].Y >= testPoint.Y)
{
if (coordinates[i].X + (testPoint.Y - coordinates[i].Y) / (coordinates[j].Y - coordinates[i].Y) *
(coordinates[j].X - coordinates[i].X) < testPoint.X)
{
result = !result;
}
}
j = i;
}
return result;
}
// define again a point.
public class RoomPoint
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public RoomPoint(double x, double y, double z)
{
this.X = x;
this.Y = y;
this.Z = Z;
}
}
bool IsPointInPoly(List<Vector> coordinates, Vector testPoint)
{
bool result = false;
int j = coordinates.Count() - 1;
for (int i = 0; i < coordinates.Count(); i++)
{
if (coordinates[i].y < testPoint.y && coordinates[j].y >= testPoint.y ||
coordinates[j].y < testPoint.y && coordinates[i].y >= testPoint.y)
{
if (coordinates[i].x + (testPoint.x - coordinates[i].y) / (coordinates[j].y - coordinates[i].y) *
(coordinates[j].x - coordinates[i].x) < testPoint.x)
{
result = !result;
}
}
j = i;
}
return result;
}
var Id1 = 299866;
var Id = 289790;
var roomid = new Autodesk.Revit.DB.ElementId(Id1);
var chairId = new Autodesk.Revit.DB.ElementId(Id);
Room room = (Room)Doc.GetElement(roomid);
var chair = (FamilyInstance)Doc.GetElement(chairId);
LocationPoint locationPoint = (LocationPoint) chair.Location;
XYZ chairlc = locationPoint.Point;
bool isPointInRoom = room.IsPointInRoom(chairlc);
Trace.WriteLine(isPointInRoom);
return;
bool IsSamSide(XYZ p1, XYZ p2, XYZ a, XYZ b)
{
XYZ cp1 = (p1 - a).CrossProduct(b - a);
XYZ cp2 = (p2 - a).CrossProduct(b - a);
return cp1.DotProduct(cp2) >= 0;
}
bool PointInTriangle(XYZ p, XYZ a, XYZ b, XYZ c)
{
if(IsSamSide(p,a,b,c) && IsSamSide(p,b,a,c) && IsSamSide(p,c,a,b))
return true;
return false;
}
Vector projectPointOnPlane(Vector rVector, Plane plane)
{
Vector vector = rVector - new Vector(plane.origin);
Vector vector1 = vector - plane.normal * Vector.DotProduct(vector,plane.normal);
Vector result = new Vector(plane.origin) + vector1;
return result;
}
using System.Text;
using System.Windows;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Line = Autodesk.Revit.DB.Line;
using Point = Autodesk.Revit.DB.Point;
var Doc = commandData.Application.ActiveUIDocument.Document;
using (TransactionGroup transactionGroup = new TransactionGroup(Doc, "add"))
{
transactionGroup.Start();
List<XYZ> points = new List<XYZ>();
int i = 0;
StringBuilder sb = new StringBuilder();
while (true)
{
try
{
XYZ point = UIDoc.Selection.PickPoint(ObjectSnapTypes.None);
using (Transaction tran = new Transaction(Doc, "Add point"))
{
tran.Start();
Point point1 = Point.Create(point);
sb.AppendLine(point1.ToString());
DirectShape ds =
DirectShape.CreateElement(Doc, new ElementId(BuiltInCategory.OST_GenericModel));
ds.SetShape(new List<GeometryObject>() {point1});
points.Add(point);
tran.Commit();
}
}
catch (Exception)
{
break;
}
}
if(points.Count<3) throw new ArgumentException("Need at least 3 points");
using Autodesk.Revit.DB.Transaction trans = new Transaction(Doc,"Add PolyLine");
trans.Start();
if(points[0].DistanceTo(points[points.Count-1])>0.001)
points.Add(points[0]);
PolyLine polyLine = Autodesk.Revit.DB.PolyLine.Create(points);
DirectShape dspoly =
DirectShape.CreateElement(Doc, new ElementId(BuiltInCategory.OST_GenericModel));
dspoly.SetShape(new List<GeometryObject>() {polyLine});
trans.Commit();
trans.Start();
XYZ checkPoint = UIDoc.Selection.PickPoint(ObjectSnapTypes.None,"Pick Point To Check");
RoomPoint roomPoint = new RoomPoint(checkPoint.X, checkPoint.Y, 0);
bool flag = IsPointInPolygon(polyLine, roomPoint);
dspoly.AppendShape(new List<GeometryObject>(){Point.Create(checkPoint)});
trans.Commit();
MessageBox.Show(flag.ToString());
transactionGroup.Assimilate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment