Ask Your Question

Revision history [back]

To identify the nearest space to a specified point in an IFC model using the XBIM library in C#, you can follow these steps:

  1. Load the IFC model using the XBIM library.
using Xbim.Ifc;
using Xbim.ModelGeometry.Scene;

// Load the IFC model
var model = IfcStore.Open("sample.ifc");
  1. Get the geometric representation for the IFC spaces in the model.
// Create a scene builder to get the geometric representation of the model
var builder = new XbimModelGeometry(model);

// Generate the geometry for the spaces in the model
var spacesGeom = builder.GenerateGeometry(model.Instances.OfType<IfcSpace>());
  1. Calculate the distance between the specified point and each space in the model.
using Xbim.Common.Geometry;

// Define the specified point coordinates
var point = new XbimPoint3D(10, 5, 3);

// Calculate the distance between the point and each space in the model
var spaceDistances = spacesGeom.Select(s => new {
    Space = s.Product,
    Distance = s.Geometry.Distance(point)
}).ToList();
  1. Sort the list of distances in ascending order and return the nearest space.
// Sort the list of distances in ascending order
var nearest = spaceDistances.OrderBy(sd => sd.Distance).FirstOrDefault();

// Get the name of the nearest space
var nearestSpaceName = nearest?.Space?.Name;

The nearestSpaceName variable will contain the name of the nearest space to the specified point in the IFC model.