由于xbim是基于.net framework的一款ifc开源库,且unity同样支持C#语言的编写,故使用xbim在unity中解析.ifc文件。本文主要讲解如何在unity中构建三维模型。
使用xbim提供方法
const string fileName = @"SampleHouse4.ifc";
var wexBimFilename = Path.ChangeExtension(fileName, "wexBIM");
var xbimDbFilename = Path.ChangeExtension(fileName, "xBIM");
// Make sure we are using an IModel implementation that supports saving of '.xbim' files
IfcStore.ModelProviderFactory.UseHeuristicModelProvider();
using (var model = IfcStore.Open(fileName))
{
// IFC file is already parsed and open. Now build the 3D
var context = new Xbim3DModelContext(model);
context.CreateContext(); // Creates the Geometry using native GeometryEngine
// Optional: Export to 'wexbim' format for use in WebUI's xViewer - geometry only
using (var wexBimFile = File.Create(wexBimFilename))
{
using (var wexBimBinaryWriter = new BinaryWriter(wexBimFile))
{
model.SaveAsWexBim(wexBimBinaryWriter);
wexBimBinaryWriter.Close();
}
wexBimFile.Close();
}
// Save IFC to the internal XBIM format, which includes geometry
model.SaveAs(xbimDbFilename, StorageType.Xbim);
}
然而此种方法在unity中会导致unity闪退,原因见此:因为xbim.Geomotry重写了模型构建的方法,与unity内置模型构建方法冲突,故context.CreateContext()方法与unity冲突。但由于xbim是基于.net framework的开源库,而此bug则是xbim和unity的冲突,故开发者不予以解决。
我的方法
因为context.CreateContext()在unity中会报错,故要在一个新的项目中调用此语句生成中间文件,而后把中间文件解析获得模型。
wexbim
为适配web端,xbim团队开发了