Home > database >  Calculating room length issue
Calculating room length issue

Time:04-07

I want to get the length and width values of a room within Revit but it appears that Revit does not give access to these values like it does with room perimeter, area and volume.

I decided to start by finding the length value for a room through code as I can calculate the width using the length but I am getting results that do not seem correct, below is the relevant part of my code:

Room room = oneRoom as Room;
double area = room.Area;
double perimeter = room.Perimeter;

int roomArea = (int)area;
int roomPerimeter = (int)perimeter;

int length = (int)(((roomPerimeter / 2) - Math.Sqrt((roomPerimeter / 2) ^ 2 - 4 * roomArea)) / 2);

//int width = roomArea / length;
string valueMessage = "perimeter of room is "   length;
TaskDialog.Show("Message", valueMessage);

I am using an equation to work out the length given the area and the perimeter. The value i get back however is -21,474,836,648 which even if I treat the value as cm and ignore that it is a minus value, is insanely high and can't be correct.

For context on the room, it is a rectangle, the perimeter is 116.141732283465 and the area is 755.626511253023

Any help on fixing this issue is appreciated

CodePudding user response:

First, the internal Revit database unit or length is imperial feet. If you are working in metric units, you will have to convert them.

Secondly, a room can have any shape, so it can be very hard to define what exactly might be meant by 'width' and 'length'.

If you wish to restrict your calculations to rectangular rooms only, your question makes perfect sense.

I would suggest querying the room for its boundary using the GetBoundarySegments method. If you get four boundary segments with right angles between them, two of them are equally long and the other two equally short, your problem is solved.

In most cases, you will have to think a bit more.

CodePudding user response:

Given the values for area and perimeter, your length and width can't be integers. Calulating them,

var length = (perimeter - Math.Sqrt(Math.Pow(perimeter, 2) - 16 * area)) / 4;
var width = area / length;

My derivation:

A = l*w
P = 2l   2w = 2(l w)

l = A/w

P = 2(A/w w)
P = 2A/w   2w
wP = 2A   2w^2
-2w^2 wP = 2A
-2W^2   wP - 2A = 0

a = -2
b = P
c = -2A

x = (-b  /- Sqrt(b^2 - 4ac)) / 2a
w = (-P  /- Sqrt(P^2 - 4*(-2)*(-2A)) / 2(-2)
w = (-P  /- Sqrt(P^2 - 16A)) / -4
  = (-P - Sqrt(P^2 - 16A)) / -4
  = (P   Sqrt(P^2 - 16A)) / 4
w = (-P   Sqrt(P^2 - 16A)) / -4
  = (P - Sqrt(P^2 - 16A)) / 4
  • Related