mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-25 17:57:02 +00:00
ifccsv to web - fix Tabulator dropping columns with non-unique names
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
|
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
import os
|
import os
|
||||||
import bpy
|
import bpy
|
||||||
import json
|
import json
|
||||||
@@ -28,6 +29,11 @@ import bonsai.tool as tool
|
|||||||
import bonsai.bim.module.drawing.scheduler as scheduler
|
import bonsai.bim.module.drawing.scheduler as scheduler
|
||||||
from bonsai.bim.ifc import IfcStore
|
from bonsai.bim.ifc import IfcStore
|
||||||
from bonsai.bim.handler import refresh_ui_data
|
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):
|
class AddCsvAttribute(bpy.types.Operator):
|
||||||
@@ -264,11 +270,23 @@ class ExportIfcCsv(bpy.types.Operator):
|
|||||||
if props.format == "web":
|
if props.format == "web":
|
||||||
if not context.scene.WebProperties.is_connected:
|
if not context.scene.WebProperties.is_connected:
|
||||||
bpy.ops.bim.connect_websocket_server()
|
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()}.")
|
self.report({"INFO"}, f"Data is exported to {props.format.upper()}.")
|
||||||
return {"FINISHED"}
|
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):
|
class ImportIfcCsv(bpy.types.Operator):
|
||||||
bl_idname = "bim.import_ifccsv"
|
bl_idname = "bim.import_ifccsv"
|
||||||
|
|||||||
Reference in New Issue
Block a user