Hi all, cannot seem to get the centre point of my model with this code below. Does anyone have a solution of this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace REVITAPIFYP
{
public class CenterPointFinder : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
// Get the active document
Document doc = commandData.Application.ActiveUIDocument.Document;
// Get all elements in the model
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> allElements = collector.ToElements();
// Calculate the center point
XYZ centerPoint = GetCenterPoint(allElements);
// Display the center point (for demonstration purposes)
TaskDialog.Show("Center Point", $"Center Point: {centerPoint}");
return Result.Succeeded;
}
private XYZ GetCenterPoint(ICollection<Element> elements)
{
// Initialize variables to calculate the center point
double totalX = 0;
double totalY = 0;
double totalZ = 0;
int count = 0;
// Loop through all elements to calculate the total position
foreach (Element element in elements)
{
Location location = element.Location;
if (location != null)
{
LocationPoint locationPoint = location as LocationPoint;
if (locationPoint != null)
{
XYZ position = locationPoint.Point;
totalX += position.X;
totalY += position.Y;
totalZ += position.Z;
count++;
}
}
}
// Calculate the average position to get the center point
if (count > 0)
{
double centerX = totalX / count;
double centerY = totalY / count;
double centerZ = totalZ / count;
return new XYZ(centerX, centerY, centerZ);
}
return null; // Return null if no valid elements are found
}
}
}
Solved! Go to Solution.
Hi all, cannot seem to get the centre point of my model with this code below. Does anyone have a solution of this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace REVITAPIFYP
{
public class CenterPointFinder : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
// Get the active document
Document doc = commandData.Application.ActiveUIDocument.Document;
// Get all elements in the model
FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> allElements = collector.ToElements();
// Calculate the center point
XYZ centerPoint = GetCenterPoint(allElements);
// Display the center point (for demonstration purposes)
TaskDialog.Show("Center Point", $"Center Point: {centerPoint}");
return Result.Succeeded;
}
private XYZ GetCenterPoint(ICollection<Element> elements)
{
// Initialize variables to calculate the center point
double totalX = 0;
double totalY = 0;
double totalZ = 0;
int count = 0;
// Loop through all elements to calculate the total position
foreach (Element element in elements)
{
Location location = element.Location;
if (location != null)
{
LocationPoint locationPoint = location as LocationPoint;
if (locationPoint != null)
{
XYZ position = locationPoint.Point;
totalX += position.X;
totalY += position.Y;
totalZ += position.Z;
count++;
}
}
}
// Calculate the average position to get the center point
if (count > 0)
{
double centerX = totalX / count;
double centerY = totalY / count;
double centerZ = totalZ / count;
return new XYZ(centerX, centerY, centerZ);
}
return null; // Return null if no valid elements are found
}
}
}
Solved! Go to Solution.
Solved by Mohamed_Arshad. Go to Solution.
What you have did was not right method every element does not has Location Point I modified your code little bit, Kindly follow the below steps:
Reference Code
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// Get the active document
Document doc = commandData.Application.ActiveUIDocument.Document;
// Get all elements in the model
FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id); //Filter in Active View
ICollection<Element> allElements = collector.ToElements();
// Calculate the center point
XYZ centerPoint = GetCenterPoint(allElements, doc.ActiveView);
using (Transaction createPoint = new Transaction(doc, "Create Center Point"))
{
createPoint.Start();
//Create Point to Check the whether it is in center or not
DirectShape point = DirectShape.CreateElement(doc,new ElementId( BuiltInCategory.OST_GenericModel));
point.AppendShape(new List<GeometryObject>() { Point.Create(centerPoint) });
createPoint.Commit();
}
// Display the center point (for demonstration purposes)
TaskDialog.Show("Center Point", $"Center Point: {centerPoint}");
return Result.Succeeded;
}
private XYZ GetCenterPoint(ICollection<Element> elements, View activeView)
{
// Initialize variables to calculate the center point
List<double> minX = new List<double>();
List<double> minY = new List<double>();
List<double> minZ = new List<double>();
List<double> maxX = new List<double>();
List<double> maxY = new List<double>();
List<double> maxZ = new List<double>();
// Loop through all elements to calculate the total position
foreach (Element element in elements)
{
BoundingBoxXYZ bb = element.get_BoundingBox(activeView);
if (bb != null)
{
minX.Add(bb.Min.X);
minY.Add(bb.Min.Y);
minZ.Add(bb.Min.Z);
maxX.Add(bb.Max.X);
maxY.Add(bb.Max.Y);
maxZ.Add(bb.Max.Z);
}
}
//Get Mininum of the points
double min_X = minX.Min();
double min_Y = minY.Min();
double min_Z = minZ.Min();
//Get Maximum of the points
double max_X = maxX.Max();
double max_Y = maxY.Max();
double max_Z = maxZ.Max();
//Create 3D Point
XYZ min = new XYZ(min_X, min_Y, min_Z);
XYZ max = new XYZ(max_X, max_Y, max_Z);
//Create Center Point
XYZ centerPoint = new XYZ();
centerPoint = min.Add(max).Divide(2.0);
return centerPoint; // Return null if no valid elements are found
}
Hope this will Helps 🙂
What you have did was not right method every element does not has Location Point I modified your code little bit, Kindly follow the below steps:
Reference Code
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// Get the active document
Document doc = commandData.Application.ActiveUIDocument.Document;
// Get all elements in the model
FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id); //Filter in Active View
ICollection<Element> allElements = collector.ToElements();
// Calculate the center point
XYZ centerPoint = GetCenterPoint(allElements, doc.ActiveView);
using (Transaction createPoint = new Transaction(doc, "Create Center Point"))
{
createPoint.Start();
//Create Point to Check the whether it is in center or not
DirectShape point = DirectShape.CreateElement(doc,new ElementId( BuiltInCategory.OST_GenericModel));
point.AppendShape(new List<GeometryObject>() { Point.Create(centerPoint) });
createPoint.Commit();
}
// Display the center point (for demonstration purposes)
TaskDialog.Show("Center Point", $"Center Point: {centerPoint}");
return Result.Succeeded;
}
private XYZ GetCenterPoint(ICollection<Element> elements, View activeView)
{
// Initialize variables to calculate the center point
List<double> minX = new List<double>();
List<double> minY = new List<double>();
List<double> minZ = new List<double>();
List<double> maxX = new List<double>();
List<double> maxY = new List<double>();
List<double> maxZ = new List<double>();
// Loop through all elements to calculate the total position
foreach (Element element in elements)
{
BoundingBoxXYZ bb = element.get_BoundingBox(activeView);
if (bb != null)
{
minX.Add(bb.Min.X);
minY.Add(bb.Min.Y);
minZ.Add(bb.Min.Z);
maxX.Add(bb.Max.X);
maxY.Add(bb.Max.Y);
maxZ.Add(bb.Max.Z);
}
}
//Get Mininum of the points
double min_X = minX.Min();
double min_Y = minY.Min();
double min_Z = minZ.Min();
//Get Maximum of the points
double max_X = maxX.Max();
double max_Y = maxY.Max();
double max_Z = maxZ.Max();
//Create 3D Point
XYZ min = new XYZ(min_X, min_Y, min_Z);
XYZ max = new XYZ(max_X, max_Y, max_Z);
//Create Center Point
XYZ centerPoint = new XYZ();
centerPoint = min.Add(max).Divide(2.0);
return centerPoint; // Return null if no valid elements are found
}
Hope this will Helps 🙂
Thanks so much man.
Thanks so much man.
Can't find what you're looking for? Ask the community or share your knowledge.