ifccsv to web - fix Tabulator dropping columns with non-unique names

This commit is contained in:
Andrej730
2024-09-06 12:11:06 +05:00
parent 456ad2af8e
commit a3ff0a61a6
+19 -1
View File
@@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
import os
import bpy
import json
@@ -28,6 +29,11 @@ import bonsai.tool as tool
import bonsai.bim.module.drawing.scheduler as scheduler
from bonsai.bim.ifc import IfcStore
from bonsai.bim.handler import refresh_ui_data
from typing import TYPE_CHECKING
from collections import Counter
if TYPE_CHECKING:
import pandas as pd
class AddCsvAttribute(bpy.types.Operator):
@@ -264,11 +270,23 @@ class ExportIfcCsv(bpy.types.Operator):
if props.format == "web":
if not context.scene.WebProperties.is_connected:
bpy.ops.bim.connect_websocket_server()
tool.Web.send_webui_data(data=ifc_csv.dataframe.to_csv(index=False), data_key="csv_data", event="csv_data")
df = ifc_csv.dataframe
assert df is not None
# Tabulator seems to be ignoring columns non-unique columns,
# so we ensure they are unique at input.
df.columns = self.get_unique_column_names(df)
tool.Web.send_webui_data(data=df.to_csv(index=False), data_key="csv_data", event="csv_data")
self.report({"INFO"}, f"Data is exported to {props.format.upper()}.")
return {"FINISHED"}
def get_unique_column_names(self, dataframe: pd.DataFrame) -> list[str]:
count = Counter()
return [
f"{col}.{i:03d}" if duped and not count.update([col]) and (i := count[col]) else col
for col, duped in zip(dataframe.columns, dataframe.columns.duplicated())
]
class ImportIfcCsv(bpy.types.Operator):
bl_idname = "bim.import_ifccsv"