xBIM learning and create a project application 02 VS

First, the new project

  Visual Studio New Project, created after the completion of the project Nuget, add items Xbim.Essentials , then if the project requires geometry engine also need to be integrated Xbim.Geometry . Behind xBIM has many years of development, these two packages are very mature, very stable.

By choosing Xbim.Essentials 5.0.213 version, which is more dependent

After you install the dll, dll many references

 

Second, create documents, open the file

First, you should create a credential for the owner to retain the history of all entities in the IFC world.

 1         /// <summary>
 2         ///  创建凭证
 3         /// </summary>
 4         /// <returns></returns>
 5         public static XbimEditorCredentials CreateCredentials()
 6         {
 7             XbimEditorCredentials editor = new XbimEditorCredentials
 8             {
 9                 ApplicationDevelopersName = "NJQY",                             //应用开发商名称
10                 ApplicationFullName = "SparkDigitalReview",                     //应用程序名称
11                 ApplicationIdentifier = "NJQYf43a-faa7-4a49-b06d-4cb21f81d220", //应用程序标示符,通过Guid来表示
12                 ApplicationVersion = "4.0",
13 
14                 //个人信息
15                 EditorsFamilyName = "Zhang",
16                 EditorsGivenName = "ChuanNing",
17                 EditorsOrganisationName = "bim"
18             };
19 
20             return editor;
21         }

xBIM中所有对IModel接口的所有实现都是可释放的(IDisposable),所以总是应该在using语句中使用它们,如下所示:

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

IfcStore.Open()足够智能识别文件格式( .ifc,.ifczip,*。xml)和IFC版本(IFC2x3,IFC4)。使用此静态函数,它还决定是否应使用内存模型或Esent数据库来存储数据。您可以使用其他参数来明确说出您想要的内容。您还可以传入将报告进度的委托。

 

三、创建文件

   如果要从头开始创建新模型,也可以使用以下功能。在这种情况下,您必须指定应该使用哪个架构和存储,因为我们不知道您需要什么,并且模型需要从一开始就知道这两件事。还要确保为您创建的模型使用正确的模式名称空间,因为您无法在单个模型中混合来自多个模式的数据。

IfcSchemaVersion.Ifc4 是枚举,代表IFC的版本,当前最新的版本是IFC 4

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

  如果要在模型中创建或修改任何内容,则必须使用事务。这些也应该在using语句中使用,因此它们具有适当的范围,以便在发生某些情况时进行最终回滚操作。您必须明确提交事务以保留更改。事务不能嵌套,因此当时总是只有一个事务。

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

所有与实体相关的操作都可通过IModel.Instances创建。这是您在模型中获取,更改和创建新实体的访问入口。要创建任何新对象,请使用此模板化函数。

【您始终必须指定要创建的非抽象类型。这是在xBIM中构建的,如果不这样,就会出现编译时错误。每个模型都是特定于模式的,因此它是IFC2x3或IFC4或其他特定模式。IfcStore使它更容易,因为它可以打开两个IFC版本,并会告诉你它是什么,但是当你想要创建数据时,请确保你不要搞砸你的using陈述。如果您尝试使用初始化为IFC2x3的模型创建IFC4实体,则会抛出运行时异常。】

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

  除了使用此功能之外,无法以任何其他方式创建新实体。您将在上面的代码中看到,此函数使用可选的类型化对象初始值设定项来设置对象的值。没有必要使用它们,但我个人喜欢它,因为我可以看到结果实体的结构。要查找所需的实体,您将使用以下功能:除了使用此功能之外,无法以任何其他方式创建新实体。您将在上面的代码中看到,此函数使用可选的类型化对象初始值设定项来设置对象的值。没有必要使用它们,但我个人喜欢它,因为我可以看到结果实体的结构。要查找所需的实体,您将使用以下功能:

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");

您可以看到所有这些函数都是模板化的,因此它们使用对象的类型作为第一级过滤器。如果您知道所需的类型,则应始终指定它以提高性能。对于所有搜索查询,您还可以使用接口来检索实体。我们在IFC2x3实体上实现了IFC4接口,这意味着您可以使用单个代码库查询IFC2x3和IFC4 。

使用所有这些基本的东西,您的第一个简单代码可能如下所示。因为它使用Xbim.Ifc4.Interfaces,此代码将同时适用于IFC2x3和IFC4。

 1 public class QuickStart
 2     {
 3         public static void Start()
 4         {
 5             const string fileName = "SampleHouse.ifc"; //可以是 IFC2x3 或者 IFC4 格式的文件
 6             var credentials = XBIMUtility.CreateCredentials();
 7 
 8             using (var model = IfcStore.Open(fileName, credentials))
 9             {
10                 using (var txn = model.BeginTransaction("Quick start transaction"))
11                 {
12                     // 获取模型中所有的墙
13                     var walls = model.Instances.OfType<IIfcWall>();
14 
15                     // 遍历所有的墙 并且改变他们的名称
16                     foreach (var wall in walls)
17                     {
18                         wall.Name = "Iterated wall: " + wall.Name;
19                     }
20 
21                     // 提交事务 
22                     txn.Commit();
23                 }
24 
25                 // 保存更改后的模型。 IfcStore 可以使用的扩展名为 *.ifc, *.ifczip or *.ifcxml.
26                 model.SaveAs("SampleHouse_Modified.ifc");
27             }
28         }
29     }

 

2019-05-31

 

Guess you like

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