Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Centre point of model

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
josenlee34_21
333 Views, 2 Replies

Centre point of model

josenlee34_21
Participant
Participant

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
}
}
}

0 Likes

Centre point of model

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
}
}
}

2 REPLIES 2
Message 2 of 3

Mohamed_Arshad
Advisor
Advisor
Accepted solution

HI @josenlee34_21 
  

    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:

 

  1. Get BoundingBox for each element
  2. Get Min and Max of the bounding box
  3. Calculate most Min and most Max of the BoundingBox Min and Max.
  4. And by using general formula (Min+Max/2) you can get the center point of the complete model.

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 & Regards,
Mohamed Arshad K

HI @josenlee34_21 
  

    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:

 

  1. Get BoundingBox for each element
  2. Get Min and Max of the bounding box
  3. Calculate most Min and most Max of the BoundingBox Min and Max.
  4. And by using general formula (Min+Max/2) you can get the center point of the complete model.

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 & Regards,
Mohamed Arshad K
Message 3 of 3

josenlee34_21
Participant
Participant

Thanks so much man. 

0 Likes

Thanks so much man. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report