AUTOCAD .NET & IFC
Khi làm việc với AutoCAD và .NET, ta có thể kết hợp IFC để:
- Xuất đối tượng AutoCAD sang IFC (Polyline, Solid, Block...).
- Chuyển đổi mô hình từ AutoCAD sang BIM (Chuyển đổi 3D Solid thành IfcWall, IfcSlab...).
- Đọc dữ liệu từ tệp IFC vào AutoCAD để hiển thị mô hình BIM.
Để xuất dữ liệu từ AutoCAD sang IFC trong .NET, ta cần kết hợp AutoCAD .NET API và xBIM Toolkit để:
- Trích xuất dữ liệu từ AutoCAD (Polyline, Solid, Block...)
- Chuyển đổi sang đối tượng IFC tương ứng (IfcWall, IfcSlab...)
- Xuất mô hình thành tệp IFC
CÀI ĐẶT CÁC THƯ VIỆN CẦN THIẾT
1️⃣ Cài đặt AutoCAD .NET API
- Import các thư viện:
AcDbMgd.dll
,AcMgd.dll
,AcCoreMgd.dll
- Thêm vào project C# (.NET Framework)
2️⃣ Cài đặt xBIM Toolkit
Cài đặt thư viện qua NuGet:
CODE MẪU: CHUYỂN POLYLINE THÀNH IFCSLAB
Dưới đây là ví dụ lấy Polyline từ AutoCAD và xuất thành IfcSlab trong tệp IFC:
Code:
using System; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using Xbim.Common; using Xbim.Ifc; using Xbim.Ifc4.Interfaces; using Xbim.Ifc4.Kernel; using Xbim.Ifc4.ProductExtension; using Xbim.Ifc4.GeometryResource; using Xbim.IO; using System.Linq; using Xbim.Ifc4.SharedBldgElements; using Xbim.Ifc4.GeometricConstraintResource; using Xbim.Ifc4.ProfileResource; using Xbim.Ifc4.GeometricModelResource; using Xbim.Ifc4.RepresentationResource; // This line is not mandatory, but improves loading performances [assembly: CommandClass(typeof(AutoCAD_To_IFC.MyCommands))] namespace AutoCAD_To_IFC { // This class is instantiated by AutoCAD for each document when // a command is called by the user the first time in the context // of a given document. In other words, non static data in this class // is implicitly per-document! public class MyCommands { [CommandMethod("ExportPolylineToIFC")] public void ExportPolylineToIFC() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptEntityOptions peo = new PromptEntityOptions("\nChọn Polyline để xuất IFC:"); peo.SetRejectMessage("\nChỉ chọn Polyline!"); peo.AddAllowedClass(typeof(Polyline), true); PromptEntityResult per = ed.GetEntity(peo); if (per.Status != PromptStatus.OK) return; using (Transaction tr = db.TransactionManager.StartTransaction()) { Polyline pl = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Polyline; if (pl != null) { string filePath = "output.ifc"; CreateIFCFile(pl, filePath); ed.WriteMessage($"\nTệp IFC đã lưu: {filePath}"); } tr.Commit(); } } private void CreateIFCFile(Polyline pl, string filePath) { using (var model = IfcStore.Create(Xbim.Common.Step21.XbimSchemaVersion.Ifc4, XbimStoreType.InMemoryModel)) { using (var txn = model.BeginTransaction("Create IFC")) { var project = model.Instances.New<IfcProject>(p => p.Initialize(ProjectUnits.SIUnitsUK)); model.Instances.Append(project); var slab = model.Instances.New<IfcSlab>(s => { s.Name = "Slab from Polyline"; s.ObjectPlacement = model.Instances.New<IfcLocalPlacement>(); }); // Create IfcPolyline from AutoCAD Polyline var ifcPolyline = model.Instances.New<IfcPolyline>(); for (int i = 0; i < pl.NumberOfVertices; i++) { var vertex = pl.GetPoint3dAt(i); var ifcPoint = model.Instances.New<IfcCartesianPoint>(p => { p.SetXYZ(vertex.X, vertex.Y, vertex.Z); }); ifcPolyline.Points.Add(ifcPoint); } // Create IfcArbitraryClosedProfileDef using IfcPolyline var profileDef = model.Instances.New<IfcArbitraryClosedProfileDef>(p => { p.OuterCurve = ifcPolyline; }); // Create IfcExtrudedAreaSolid using IfcArbitraryClosedProfileDef var body = model.Instances.New<IfcExtrudedAreaSolid>(e => { e.SweptArea = profileDef; e.ExtrudedDirection = model.Instances.New<IfcDirection>(d => d.SetXYZ(0, 0, 1)); e.Depth = 200; // Set the thickness of the slab }); // Assign the geometry to the slab var shapeRep = model.Instances.New<IfcShapeRepresentation>(r => { r.ContextOfItems = project.RepresentationContexts.First(); r.RepresentationIdentifier = "Body"; r.RepresentationType = "SweptSolid"; r.Items.Add(body); }); slab.Representation = model.Instances.New<IfcProductDefinitionShape>(pds => { pds.Representations.Add(shapeRep); }); model.Instances.Append(slab); txn.Commit(); } model.SaveAs(filePath, StorageType.Ifc); } } } }
Dưới đây là ví dụ lấy Solid3d từ AutoCAD và xuất thành IfcWall/IfcColumn trong tệp IFC:
Code:
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using System; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using Xbim.Common; using Xbim.Ifc; using Xbim.Ifc4.Interfaces; using Xbim.Ifc4.Kernel; using Xbim.Ifc4.ProductExtension; using Xbim.Ifc4.GeometryResource; using Xbim.IO; using System.Linq; using Xbim.Ifc4.SharedBldgElements; using Xbim.Ifc4.GeometricConstraintResource; using Xbim.Ifc4.ProfileResource; using Xbim.Ifc4.GeometricModelResource; using Xbim.Ifc4.RepresentationResource; namespace AutoCAD_To_IFC { // This class is instantiated by AutoCAD for each document when // a command is called by the user the first time in the context // of a given document. In other words, non static data in this class // is implicitly per-document! public partial class MyCommands { [CommandMethod("ExportSolidToIFC")] public void ExportSolidToIFC() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptEntityOptions peo = new PromptEntityOptions("\nChọn 3D Solid để xuất IFC:"); peo.SetRejectMessage("\nChỉ chọn 3D Solid!"); peo.AddAllowedClass(typeof(Solid3d), true); PromptEntityResult per = ed.GetEntity(peo); if (per.Status != PromptStatus.OK) return; using (Transaction tr = db.TransactionManager.StartTransaction()) { Solid3d solid = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Solid3d; if (solid != null) { string filePath = "soutput.ifc"; CreateIFCFile(solid, filePath); ed.WriteMessage($"\nTệp IFC đã lưu: {filePath}"); } tr.Commit(); } } private void CreateIFCFile(Solid3d solid, string filePath) { using (var model = IfcStore.Create(Xbim.Common.Step21.XbimSchemaVersion.Ifc4, XbimStoreType.InMemoryModel)) { using (var txn = model.BeginTransaction("Create IFC")) { var project = model.Instances.New<IfcProject>(p => p.Initialize(ProjectUnits.SIUnitsUK)); model.Instances.Append(project); // Lấy kích thước từ Solid3D Extents3d extents = solid.GeometricExtents; double width = extents.MaxPoint.X - extents.MinPoint.X; double height = extents.MaxPoint.Z - extents.MinPoint.Z; double depth = extents.MaxPoint.Y - extents.MinPoint.Y; // Tạo IfcColumn từ Solid3D var column = model.Instances.New<IfcColumn>(c => { c.Name = "Column from Solid3D"; c.ObjectPlacement = model.Instances.New<IfcLocalPlacement>(); }); // Create IfcExtrudedAreaSolid using the dimensions of the Solid3D var profileDef = model.Instances.New<IfcRectangleProfileDef>(p => { p.ProfileType = IfcProfileTypeEnum.AREA; p.XDim = width; p.YDim = depth; }); var body = model.Instances.New<IfcExtrudedAreaSolid>(e => { e.SweptArea = profileDef; e.ExtrudedDirection = model.Instances.New<IfcDirection>(d => d.SetXYZ(0, 0, 1)); e.Depth = height; e.Position = model.Instances.New<IfcAxis2Placement3D>(p => p.Location = model.Instances.New<IfcCartesianPoint>(c => c.SetXYZ(0, 0, 0))); }); // Assign the geometry to the column var shapeRep = model.Instances.New<IfcShapeRepresentation>(r => { r.ContextOfItems = project.RepresentationContexts.First(); r.RepresentationIdentifier = "Body"; r.RepresentationType = "SweptSolid"; r.Items.Add(body); }); column.Representation = model.Instances.New<IfcProductDefinitionShape>(pds => { pds.Representations.Add(shapeRep); }); model.Instances.Append(column); txn.Commit(); } model.SaveAs(filePath, StorageType.Ifc); } } } }
Link tải (MediaFire)
📥 https://www.mediafire.com/
---------------------------------------------------------------------------------------------
Mọi thông tin xin liên hệ Fanpage AutoLISP Thật là đơn giản!
Cảm ơn bạn đã theo dõi!
Không có nhận xét nào:
Đăng nhận xét