xBIM Basic 02 Quick Start

xBIM Basic 02 Quick Start

Series catalog [The latest development articles have been updated, click to view details]

1. New project

  After creating a new project in Visual Studio and Nuget after the project is created, add Xbim.Essentials to the project, then if the project requires a geometry engine, you also need to integrate Xbim.Geometry. There are years of development behind xBIM, and both packages are mature and very stable.

Due to the choice of Xbim.Essentials 5.0.213 version, there are many dependencies

After installing the dll, many dlls are referenced

2. Create a credential and open the file

1. you should create credentials to keep the owner history of all entities in the IFC world.

 2///Create credentials
 5 public static XbimEditorCredentials CreateCredentials()
 6 {
 7 XbimEditorCredentials editor = new XbimEditorCredentials
 8 {
 9 ApplicationDevelopersName = "NJQY",//Application developer name
10 ApplicationFullName = "SparkDigitalReview",//application name
11 ApplicationIdentifier = "NJQYf43a-faa7-4a49-b06d-4cb21f81d220",//application identifier, represented by Guid
12 ApplicationVersion = "4.0",
14//Personal information
15 EditorsFamilyName = "Zhang",
16 EditorsGivenName = "ChuanNing",
17 EditorsOrganisationName = "bim"
18 };
20 return editor;
21 }

All implementations of the IModel interface in xBIM are IDisposable, so you should always use them in the using statement, as shown below:

1 using (var model = IfcStore.Open(fileName, editor, true))
2 {
3//...do something with the model

IfcStore.Open() is smart enough to recognize the file format (.ifc, .ifczip, *.xml) and IFC version (IFC2x3, IFC4). Using this static function, it also decides whether the memory model or Esent database should be used to store the data. You can use other parameters to clearly say what you want. You can also pass in a commission that will report progress.

3. create a file

If you want to create a new model from scratch, you can also use the following functions. In this case, you must specify which architecture and storage should be used, because we don't know what you need, and the model needs to know these two things from the beginning. Also make sure to use the correct schema namespace for the model you create, because you cannot mix data from multiple schemas in a single model.

IfcSchemaVersion.Ifc4 is an enumeration, it represents the version of IFC, the latest version is IFC 4

1 using (var model = IfcStore.Create(editor, IfcSchemaVersion.Ifc4, XbimStoreType.InMemoryModel))
2 {
3//...do something with the model

  If you want to create or modify anything in the model, you must use transactions. These should also be used in the using statement, so they have the appropriate scope for the final rollback operation when something happens. You must explicitly commit the transaction to preserve the changes. Transactions cannot be nested, so there is always only one transaction at that time.

using (var txn = model.BeginTransaction("Hello Wall"))
   //....do something in the scope of this transaction

All operations related to entities can be created through IModel.Instances. This is your access point for obtaining, changing and creating new entities in the model. To create any new objects, use this templated function.

[You must always specify the non-abstract type to be created. This is built in xBIM, if you don't, you will get a compile-time error. Each model is mode-specific, so it is IFC2x3 or IFC4 or other specific modes. IfcStore makes it easier because it can open two IFC versions and will tell you what it is, but when you want to create data, make sure you don't mess up your using statement. If you try to create an IFC4 entity using a model initialized to IFC2x3, a runtime exception will be thrown. 】

var newWall = mode.Instances.New<IfcWall>();

Apart from using this feature, new entities cannot be created in any other way. As you will see in the code above, this function uses an optional typed object initializer to set the value of the object. There is no need to use them, but I personally like it because I can see the structure of the resulting entity. To find the entity you need, you will use the following function: You cannot create a new entity in any other way than using this function. As you will see in the code above, this function uses an optional typed object initializer to set the value of the object. There is no need to use them, but I personally like it because I can see the structure of the resulting entity. To find the desired entity, you will use the following functions:

1 var firstWall = model.Instances.FirstOrDefault<IfcWall>();
2 var allWalls = model.Instances.OfType<IfcWall>();
3 var specificWall = model.Instances.Where<IfcWall>(w => w.Name == "Brick wall");

You can see that all these functions are templated, so they use the type of the object as the first-level filter. If you know the type you need, you should always specify it to improve performance. For all search queries, you can also use the interface to retrieve entities. We have implemented the IFC4 interface on the IFC2x3 entity, which means you can use a single code base to query IFC2x3 and IFC4.

Using all these basic things, your first simple code might look like the following. Because it uses Xbim.Ifc4.Interfaces, this code will apply to both IFC2x3 and IFC4.

 1 public class QuickStart
 2 {
 3 public static void Start()
 4 {
 5 const string fileName = "SampleHouse.ifc";//Can be IFC2x3 or IFC4 format file
 6 var credentials = XBIMUtility.CreateCredentials();
 8 using (var model = IfcStore.Open(fileName, credentials))
 9 {
10 using (var txn = model.BeginTransaction("Quick start transaction"))
11 {
12//Get all the walls in the model
13 var walls = model.Instances.OfType<IIfcWall>();
15//Go through all the walls and change their names
16 foreach (var wall in walls)
17 {
18 wall.Name = "Iterated wall: "+ wall.Name;
21//Commit the transaction 
22 txn.Commit();
23 }
25//Save the changed model. The extensions that IfcStore can use are *.ifc, *.ifczip or *.ifcxml.
26 model.SaveAs("SampleHouse_Modified.ifc");
Reference: https://cloud.tencent.com/developer/article/1507704 xBIM Basic 02 Quick Start-Cloud + Community-Tencent Cloud