mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-28 15:53:00 +00:00
You can now customise the display of empty strings in IfcCSV.
This commit is contained in:
+19
-14
@@ -70,6 +70,7 @@ class IfcCsv:
|
||||
include_global_id=True,
|
||||
delimiter=",",
|
||||
null="-",
|
||||
empty="",
|
||||
bool_true="YES",
|
||||
bool_false="NO",
|
||||
sort=None,
|
||||
@@ -96,6 +97,8 @@ class IfcCsv:
|
||||
value = ifcopenshell.util.selector.get_element_value(element, attribute)
|
||||
if value is None:
|
||||
value = null
|
||||
elif value == "":
|
||||
value = empty
|
||||
elif value is True:
|
||||
value = bool_true
|
||||
elif value is False:
|
||||
@@ -368,17 +371,17 @@ class IfcCsv:
|
||||
results.update([p.Name for p in element.Quantities])
|
||||
return ["{}.{}".format(pset_qto_name, n) for n in results]
|
||||
|
||||
def Import(self, ifc_file, table, attributes=None, delimiter=",", null="-", bool_true="YES", bool_false="NO"):
|
||||
def Import(self, ifc_file, table, attributes=None, delimiter=",", null="-", empty="", bool_true="YES", bool_false="NO"):
|
||||
ext = table.split(".")[-1].lower()
|
||||
|
||||
if ext == "csv":
|
||||
self.import_csv(ifc_file, table, attributes, delimiter, null, bool_true, bool_false)
|
||||
self.import_csv(ifc_file, table, attributes, delimiter, null, empty, bool_true, bool_false)
|
||||
elif ext == "ods":
|
||||
self.import_ods(ifc_file, table, attributes, null, bool_true, bool_false)
|
||||
self.import_ods(ifc_file, table, attributes, null, empty, bool_true, bool_false)
|
||||
elif ext == "xlsx":
|
||||
self.import_xlsx(ifc_file, table, attributes, null, bool_true, bool_false)
|
||||
self.import_xlsx(ifc_file, table, attributes, null, empty, bool_true, bool_false)
|
||||
|
||||
def import_csv(self, ifc_file, table, attributes=None, delimiter=",", null="-", bool_true="YES", bool_false="NO"):
|
||||
def import_csv(self, ifc_file, table, attributes=None, delimiter=",", null="-", empty="", bool_true="YES", bool_false="NO"):
|
||||
with open(table, newline="", encoding="utf-8") as f:
|
||||
reader = csv.reader(f, delimiter=delimiter)
|
||||
headers = []
|
||||
@@ -390,17 +393,17 @@ class IfcCsv:
|
||||
elif len(attributes) == len(headers) - 1:
|
||||
attributes.insert(0, "") # The GlobalId column
|
||||
continue
|
||||
self.process_row(ifc_file, row, headers, attributes, null, bool_true, bool_false)
|
||||
self.process_row(ifc_file, row, headers, attributes, null, empty, bool_true, bool_false)
|
||||
|
||||
def import_xlsx(self, ifc_file, table, attributes, null, bool_true, bool_false):
|
||||
def import_xlsx(self, ifc_file, table, attributes, null, empty, bool_true, bool_false):
|
||||
df = pd.read_excel(table)
|
||||
self.import_pd(ifc_file, df, attributes, null, bool_true, bool_false)
|
||||
self.import_pd(ifc_file, df, attributes, null, empty, bool_true, bool_false)
|
||||
|
||||
def import_ods(self, ifc_file, table, attributes, null, bool_true, bool_false):
|
||||
def import_ods(self, ifc_file, table, attributes, null, empty, bool_true, bool_false):
|
||||
df = pd.read_excel(table, engine="odf")
|
||||
self.import_pd(ifc_file, df, attributes, null, bool_true, bool_false)
|
||||
self.import_pd(ifc_file, df, attributes, null, empty, bool_true, bool_false)
|
||||
|
||||
def import_pd(self, ifc_file, df, attributes=None, null="-", bool_true="YES", bool_false="NO"):
|
||||
def import_pd(self, ifc_file, df, attributes=None, null="-", empty="", bool_true="YES", bool_false="NO"):
|
||||
headers = df.columns.tolist()
|
||||
|
||||
if not attributes:
|
||||
@@ -409,9 +412,9 @@ class IfcCsv:
|
||||
attributes.insert(0, "") # The GlobalId column
|
||||
|
||||
for _, row in df.iterrows():
|
||||
self.process_row(ifc_file, row.tolist(), headers, attributes, null, bool_true, bool_false)
|
||||
self.process_row(ifc_file, row.tolist(), headers, attributes, null, empty, bool_true, bool_false)
|
||||
|
||||
def process_row(self, ifc_file, row, headers, attributes, null, bool_true, bool_false):
|
||||
def process_row(self, ifc_file, row, headers, attributes, null, empty, bool_true, bool_false):
|
||||
try:
|
||||
element = ifc_file.by_guid(row[0])
|
||||
except:
|
||||
@@ -422,6 +425,8 @@ class IfcCsv:
|
||||
continue # Skip GlobalId
|
||||
if value == null:
|
||||
value = None
|
||||
elif value == empty:
|
||||
value = ""
|
||||
elif value == bool_true:
|
||||
value = True
|
||||
elif value == bool_false:
|
||||
@@ -449,7 +454,7 @@ if __name__ == "__main__":
|
||||
help="Specify attributes that are part of the extract, using the IfcQuery syntax such as 'class', 'Name' or 'Pset_Foo.Bar'",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-h", "--headers", nargs="+", help="Specify human readable headers that correlate to each attribute."
|
||||
"--headers", nargs="+", help="Specify human readable headers that correlate to each attribute."
|
||||
)
|
||||
parser.add_argument("--sort", nargs="+", help="Specify one or more attributes to sort by.")
|
||||
parser.add_argument("--order", nargs="+", help="Choose the sort order from ASC or DESC for each sorted attribute.")
|
||||
|
||||
Reference in New Issue
Block a user