Files
IfcOpenShell/src/bonsai/scripts/dxf2ifc.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
4.4 KiB
Python
Raw Normal View History

2024-08-14 13:49:19 +05:00
# Bonsai - OpenBIM Blender Add-on
2021-08-17 08:55:29 +10:00
# Copyright (C) 2020, 2021 Dion Moult <dion@thinkmoult.com>
#
2024-08-14 13:49:19 +05:00
# This file is part of Bonsai.
2021-08-17 08:55:29 +10:00
#
2024-08-14 13:49:19 +05:00
# Bonsai is free software: you can redistribute it and/or modify
2021-08-17 08:55:29 +10:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
2024-08-14 13:49:19 +05:00
# Bonsai is distributed in the hope that it will be useful,
2021-08-17 08:55:29 +10:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2024-08-14 13:49:19 +05:00
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
2021-08-17 08:55:29 +10:00
2026-01-26 17:11:12 +05:00
import ezdxf
import ifcopenshell
2024-05-08 17:21:00 +05:00
import ifcopenshell.guid
class Dxf2Ifc:
def execute(self):
self.create_ifc_file()
2020-11-01 20:08:48 +07:00
doc = ezdxf.readfile("input.dxf")
model = doc.modelspace()
products = []
for entity in model:
print(entity)
2020-11-01 20:08:48 +07:00
if entity.get_mode() == "AcDbPolyFaceMesh":
ifc_faces = []
for face in entity.faces():
ifc_faces.append(
2020-11-01 20:08:48 +07:00
self.file.createIfcFace(
[
self.file.createIfcFaceOuterBound(
self.file.createIfcPolyLoop(
[
2025-05-28 17:09:05 +05:00
self.file.createIfcCartesianPoint(face[index].dxf.location)
for index in range(len(face) - 1)
]
2020-11-01 20:08:48 +07:00
),
True,
)
]
)
)
representation = self.file.createIfcProductDefinitionShape(
None,
None,
[
self.file.createIfcShapeRepresentation(
self.subcontext,
"Body",
"Brep",
[self.file.createIfcFacetedBrep(self.file.createIfcClosedShell(ifc_faces))],
)
],
)
products.append(
self.file.create_entity(
"IfcBuildingElementProxy",
**{
"GlobalId": ifcopenshell.guid.new(),
"Name": entity.dxf.layer,
"ObjectPlacement": self.placement,
"Representation": representation,
}
)
)
else:
2020-11-01 20:08:48 +07:00
print("Not yet implemented")
self.file.createIfcRelContainedInSpatialStructure(
ifcopenshell.guid.new(), None, None, None, products, self.site
)
self.file.write("test.ifc")
def create_ifc_file(self):
self.file = ifcopenshell.file()
2020-11-01 20:08:48 +07:00
units = self.file.createIfcUnitAssignment([self.file.createIfcSIUnit(None, "LENGTHUNIT", None, "METRE")])
self.origin = self.file.createIfcAxis2Placement3D(
2020-11-01 20:08:48 +07:00
self.file.createIfcCartesianPoint((0.0, 0.0, 0.0)),
self.file.createIfcDirection((0.0, 0.0, 1.0)),
self.file.createIfcDirection((1.0, 0.0, 0.0)),
)
self.placement = self.file.createIfcLocalPlacement(None, self.origin)
2020-11-01 20:08:48 +07:00
self.context = self.file.createIfcGeometricRepresentationContext(None, "Model", 3, 1.0e-05, self.origin)
self.subcontext = self.file.createIfcGeometricRepresentationSubcontext(
2020-11-01 20:08:48 +07:00
"Body", "Model", None, None, None, None, self.context, None, "MODEL_VIEW", None
)
self.project = self.file.create_entity(
"IfcProject",
**{
"GlobalId": ifcopenshell.guid.new(),
"Name": "DXF Conversion",
"RepresentationContexts": [self.context],
"UnitsInContext": units,
}
)
self.site = self.file.create_entity(
"IfcSite",
**{"GlobalId": ifcopenshell.guid.new(), "Name": "DXF Conversion Site", "ObjectPlacement": self.placement}
)
self.file.createIfcRelAggregates(ifcopenshell.guid.new(), None, None, None, self.project, [self.site])
2020-11-01 20:08:48 +07:00
dxf2ifc = Dxf2Ifc()
dxf2ifc.execute()