File size: 5,945 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# SPDX-License-Identifier: LGPL-2.1-or-later

# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2022 Yorik van Havre <yorik@uncreated.net>              *
# *                                                                         *
# *   This file is part of FreeCAD.                                         *
# *                                                                         *
# *   FreeCAD is free software: you can redistribute it and/or modify it    *
# *   under the terms of the GNU Lesser General Public License as           *
# *   published by the Free Software Foundation, either version 2.1 of the  *
# *   License, or (at your option) any later version.                       *
# *                                                                         *
# *   FreeCAD is distributed in the hope that it will be useful, but        *
# *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
# *   Lesser General Public License for more details.                       *
# *                                                                         *
# *   You should have received a copy of the GNU Lesser General Public      *
# *   License along with FreeCAD. If not, see                               *
# *   <https://www.gnu.org/licenses/>.                                      *
# *                                                                         *
# ***************************************************************************

"""
This module recreates the whole structure of an IFC file in FreeCAD (ie.
one FreeCAD object per IFC entity). It serves mostly as a proof of concept
and test to see how doable it is with large files.

The geometry of objects is not imported, only attributes, which are mapped
to FreeCAD properties.

The IfcOpenHouse model, included with ifcopenshell, imports in 4 sec on
my ryzen9 machine, for 2700 objects. Larger files like the King Arch file
(20 Mb / 750 000 objects) would import in 18 minutes...
"""

import time

import ifcopenshell

from PySide import QtWidgets


class ViewProvider:
    """A simple view provider to gather children"""

    def __init__(self, vobj):
        vobj.Proxy = self

    def attach(self, vobj):
        self.Object = vobj.Object

    def claimChildren(self):
        children = []
        relprops = ["Item", "ForLayerSet"]  # properties that actually store parents
        for prop in self.Object.PropertiesList:
            if prop.startswith("Relating") or (prop in relprops):
                continue
            else:
                value = getattr(self.Object, prop)
                if hasattr(value, "ViewObject"):
                    children.append(value)
                elif isinstance(value, list):
                    for item in value:
                        if hasattr(item, "ViewObject"):
                            children.append(item)
        for parent in self.Object.InList:
            for prop in parent.PropertiesList:
                if prop.startswith("Relating") or (prop in relprops):
                    value = getattr(parent, prop)
                    if value == self.Object:
                        children.append(parent)
        return children


def create(ifcentity):
    """The main function that creates objects and fills properties"""

    name = "Entity" + str(ifcentity.id())
    obj = FreeCAD.ActiveDocument.getObject(name)
    if obj:
        return obj
    obj = FreeCAD.ActiveDocument.addObject("App::FeaturePython", name)
    if getattr(ifcentity, "Name", None):
        obj.Label = ifcentity.Name
    else:
        obj.Label = ifcentity.is_a()
    for attr, value in ifcentity.get_info().items():
        if attr not in obj.PropertiesList:
            if attr == "id":
                attr = "StepId"
            elif attr == "type":
                attr = "Type"
            elif attr == "Name":
                continue
            if hasattr(obj, attr):
                continue
            elif isinstance(value, int):
                obj.addProperty("App::PropertyInteger", attr, "IFC", locked=True)
                setattr(obj, attr, value)
            elif isinstance(value, float):
                obj.addProperty("App::PropertyFloat", attr, "IFC", locked=True)
                setattr(obj, attr, value)
            elif isinstance(value, ifcopenshell.entity_instance):
                value = create(value)
                obj.addProperty("App::PropertyLink", attr, "IFC", locked=True)
                setattr(obj, attr, value)
            elif isinstance(value, (list, tuple)) and value:
                if isinstance(value[0], ifcopenshell.entity_instance):
                    nvalue = []
                    for elt in value:
                        nvalue.append(create(elt))
                    obj.addProperty("App::PropertyLinkList", attr, "IFC", locked=True)
                    setattr(obj, attr, nvalue)
            else:
                obj.addProperty("App::PropertyString", attr, "IFC", locked=True)
                if value is not None:
                    setattr(obj, attr, str(value))
    for parent in ifcfile.get_inverse(ifcentity):
        create(parent)
    if FreeCAD.GuiUp:
        ViewProvider(obj.ViewObject)
    return obj


# main

filepath = QtWidgets.QFileDialog.getOpenFileName(
    None, "Select IFC File", None, "IFC Files (*.ifc)"
)[0]
stime = time.time()
ifcfile = ifcopenshell.open(filepath)
project = ifcfile.by_type("IfcProject")[0]
if not FreeCAD.ActiveDocument:
    FreeCAD.newDocument()
create(project)
FreeCAD.ActiveDocument.recompute()
endtime = "%02d:%02d" % (divmod(round(time.time() - stime, 1), 60))
lenobjects = str(len(FreeCAD.ActiveDocument.Objects)) + " objects"
print("Import done:", endtime, ",", lenobjects)