xBIM base 14 IFC export Excel reports

  IFC Export Excel report file space

  Benpian will show you some of the concepts required to read data from the IFC file. It uses IFC4 interface for IFC2x3 and IFC4 model. To create an Excel file, we use NPOI . In this example, you only need to  xBIM Essentials  components. All code contains sample data can be obtained here .

The results of this example are as follows:

 

You will need the following usingstatement:

using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Xbim.Ifc;
using Xbim.Ifc4.Interfaces;

The main functions are as follows:

// from the template initialization NPOI workbook 
var Workbook = new new XSSFWorkbook ( " template.xlsx " );
 var Sheet = workbook.GetSheet ( " Spaces " ); // units to create beautiful digital format. In reality requires more concerned with the unit. 
// We only know that we now have a model space area and space volume in cubic units // Note that the original data exported from Revit is wrong, because the data should be greater than 1000-fold. // In this example, the data is restored using xBIM. var areaFormat = workbook.CreateDataFormat (); var areaFormatId = areaFormat.GetFormat ( " # ## 0.00 [$ m²] " ); var AreaStyle = workbook.CreateCellStyle (); areaStyle.DataFormat =areaFormatId; var volumeFormat = workbook.CreateDataFormat (); var volumeFormatId = volumeFormat.GetFormat ( " # ## 0.00 [m³ $] " ); var volumeStyle = workbook.CreateCellStyle (); volumeStyle.DataFormat = volumeFormatId; // open the IFC model. It does not change anything in the model, so we can put the message editor preserved. the using ( var Model = IfcStore.Open ( " SampleHouse.ifc " )) { // Get all the model space. // need ToList () easy to use the Foreach var Spaces = model.Instances.OfType <IIfcSpace> () .ToList (); // set the report title sheet.GetRow ( 0 ) .GetCell ( 0 ) .SetCellValue ($ " Space the Report ({} spaces.Count Spaces) " ); the foreach ( var Space in Spaces) { // Write Data Report WriteSpaceRow (space , Sheet, AreaStyle, volumeStyle); } } // save report the using ( var Stream File.Create = ( " spaces.xlsx " )) { workbook.Write(stream); stream.Close (); } // open EXCEL file saved the Process.Start ( " spaces.xlsx " );

 This code uses the following function to read data from and write row for each IFC space

private static void WriteSpaceRow(IIfcSpace space, ISheet sheet, ICellStyle areaStyle, ICellStyle volumeStyle)
{
    var row = sheet.CreateRow(sheet.LastRowNum + 1);

    var name = space.Name;
    row.CreateCell(0).SetCellValue(name);

    var floor = GetFloor(space);
    row.CreateCell(1).SetCellValue(floor?.Name);

    var area = GetArea(space);
    if (area != null)
    {
        var cell = row.CreateCell(2);
        cell.CellStyle=AreaStyle; 

        // if from the property rather than quantity, so stiff can not guarantee that it is a digital 
        IF (area.UnderlyingSystemType == typeof ( Double )) 
            cell.SetCellValue (( Double ) (area.Value));
         the else 
            cell.SetCellValue (Area .ToString ()); 
    } 

    var Volume = getVolume (Space);
     IF (Volume =! null ) 
    { 
        var Cell row.CreateCell = ( . 3 ); 
        cell.CellStyle = volumeStyle; 

        // if not from the number of attributes, then We can not guarantee that it is a digital 
        IF (volume.UnderlyingSystemType == typeof (double))
            cell.SetCellValue((double)(volume.Value));
        else
            cell.SetCellValue(volume.ToString());
    }
}

To get floor contains space, you need to do this:

Private  static IIfcBuildingStorey GetFloor (IIfcSpace Space) 
{ 
    return 
        // get all objects of the relationship, these relationships will break down space Click 
        space.Decomposes 

        // Select the object decomposition (these objects may be other space or building floor) 
        .Select ( = R & lt> r.RelatingObject) 

        // acquire only floor 
        .OfType <IIfcBuildingStorey> () 

        // Get first 
        .FirstOrDefault (); 
}

IFC comprises a data base structure for storing products and their data related to any type. This infrastructure is quite complex. Two main ways to store data or the number of attributes. Number is clear, the type of value they contain, which property can contain as many different types of data values. For the area and volume, if the defined number, it is preferable to obtain the value from the number of

Private  static IIfcValue GetArea (IIfcProduct Product) 
{ 
    // try to start acquiring the number 
    var Area =
         // get all the attributes and relationships can be defined number of sets of 
        product.IsDefinedBy
         // search across all the attributes and the number of sets.
        // You may also want to search for a specific number by name 
        .SelectMany (r => r.RelatingPropertyDefinition.PropertySetDefinitions)
         // number of collection 
        .OfType <IIfcElementQuantity> ()
         // get all the number from the number set 
        .SelectMany (qset => qset. Quantities)
         // we only area of interest 
        .OfType <IIfcQuantityArea> ()
         //We will take the first one. Apparently there is more than one area of the property
         // So, to check the name. However, we will keep it simple this example. 
        ? .FirstOrDefault () 
        .AreaValue; 
    IF (Area =! Null )
         return Area;
     // get the value from the attribute 
    return the GetProperty (Product, " Area " ); 
} 

Private  static IIfcValue getVolume (IIfcProduct Product) 
{ 
    var Volume = Product. isDefinedBy 
        .SelectMany (R & lt => r.RelatingPropertyDefinition.PropertySetDefinitions) 
        .OfType <IIfcElementQuantity> () 
        .SelectMany (qset=> qset.Quantities)
        .OfType<IIfcQuantityVolume>()
        .FirstOrDefault()?.VolumeValue;
    if (volume != null)
        return volume;
    return GetProperty(product, "Volume");
}

More common attributes in the attribute set search

Private  static IIfcValue the GetProperty (IIfcProduct Product, String name) 
{ 
    return 
        // Get the number of sets may be defined for all attributes and relationships 
        product.IsDefinedBy
         // search across all the attributes and the number of sets. You may also want to focus your search on specific attributes 
        .SelectMany (r => r.RelatingPropertyDefinition.PropertySetDefinitions)
         // In this case, considering only attribute set. 
        .OfType <IIfcPropertySet> ()
         // Get all attributes from all the attribute set 
        .SelectMany (psets => pset.HasProperties)
         // only consider a single attribute value. There are enumerated attribute,
         // Table Properties, quoted attributes, complex property and other 
        .OfType <IIfcPropertySingleValue> () 
        .Where (the p-=>
            string.Equals(p.Name, name, System.StringComparison.OrdinalIgnoreCase) ||
            p.Name.ToString().ToLower().Contains(name.ToLower()))
        .FirstOrDefault()?.NominalValue;
}

 

Reference: http://docs.xbim.net/examples/excel-space-report-from-ifc.html

2019-06-05

Guess you like

Origin www.cnblogs.com/SavionZhang/p/10981040.html