Skip to content

Commit

Permalink
Track last actions and friendlier error reporting when something breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Moult committed May 9, 2024
1 parent f075a61 commit a1e0b88
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 36 deletions.
43 changes: 31 additions & 12 deletions src/blenderbim/blenderbim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import subprocess
import webbrowser
import addon_utils
from collections import deque

bl_info = {
"name": "BlenderBIM",
Expand All @@ -38,6 +39,7 @@
}

last_error = None
last_actions: deque = deque(maxlen=10)


def get_debug_info():
Expand All @@ -60,10 +62,22 @@ def get_debug_info():
"processor": platform.processor(),
"blender_version": bpy.app.version_string,
"blenderbim_version": version,
"last_actions": last_actions,
"last_error": last_error,
}


def format_debug_info(info: dict):
last_actions = ""
for action in info["last_actions"]:
last_actions += f"\n# {action['type']}: {action['name']}"
if settings := action.get("settings"):
last_actions += f"\n>>> {settings}"
info["last_actions"] = last_actions
text = "\n".join(f"{k}: {v}" for k, v in info.items())
return text.strip()


if sys.modules.get("bpy", None):
# Process *.pth in /libs/site/packages to setup globally importable modules
# This is 3 levels deep as required by the static RPATH of ../../ from dependencies taken from Anaconda
Expand All @@ -72,6 +86,18 @@ def get_debug_info():

try:
import blenderbim.bim
import ifcopenshell.api

def log_api(usecase_path, ifc_file, settings):
last_actions.append(
{
"type": "ifcopenshell.api",
"name": usecase_path,
"settings": ifcopenshell.api.serialise_settings(settings),
}
)

ifcopenshell.api.add_pre_listener("*", "action_logger", log_api)

def register():
blenderbim.bim.register()
Expand All @@ -83,7 +109,7 @@ def unregister():
last_error = traceback.format_exc()

print(last_error)
print(get_debug_info())
print(format_debug_info(get_debug_info()))
print("\nFATAL ERROR: Unable to load the BlenderBIM Add-on")

class BIM_PT_fatal_error(bpy.types.Panel):
Expand Down Expand Up @@ -122,21 +148,14 @@ class CopyDebugInformation(bpy.types.Operator):
bl_description = "Copies debugging information to your clipboard for use in bugreports"

def execute(self, context):
info = get_debug_info()
# Format it in a readable way
text = "\n".join(f"{k}: {v}" for k, v in info.items())
print(text)
info = format_debug_info(get_debug_info())

if platform.system() == "Windows":
command = "echo | set /p nul=" + text.strip()
command = "echo | set /p nul=" + info
elif platform.system() == "Darwin": # for MacOS
command = 'printf "' + text.strip().replace("\n", "\\n").replace('"', "") + '" | pbcopy'
command = 'printf "' + info.replace("\n", "\\n").replace('"', "") + '" | pbcopy'
else: # Linux
command = (
'printf "'
+ text.strip().replace("\n", "\\n").replace('"', "")
+ '" | xclip -selection clipboard'
)
command = 'printf "' + info.replace("\n", "\\n").replace('"', "") + '" | xclip -selection clipboard'
subprocess.run(command, shell=True, check=True)
return {"FINISHED"}

Expand Down
29 changes: 18 additions & 11 deletions src/blenderbim/blenderbim/bim/ifc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import hashlib
import zipfile
import tempfile
import traceback
import ifcopenshell
import ifcopenshell.geom
import ifcopenshell.ifcopenshell_wrapper
import blenderbim
import blenderbim.bim.handler
import blenderbim.tool as tool
from pathlib import Path
Expand All @@ -37,19 +39,19 @@

class IfcStore:
path: str = ""
file: ifcopenshell.file = None
schema: ifcopenshell.ifcopenshell_wrapper.schema_definition = None
cache: ifcopenshell.ifcopenshell_wrapper.HdfSerializer = None
cache_path: str = None
file: Optional[ifcopenshell.file] = None
schema: Optional[ifcopenshell.ifcopenshell_wrapper.schema_definition] = None
cache: Optional[ifcopenshell.ifcopenshell_wrapper.HdfSerializer] = None
cache_path: Optional[str] = None
id_map: dict[int, IFC_CONNECTED_TYPE] = {}
guid_map: dict[str, IFC_CONNECTED_TYPE] = {}
edited_objs: Set[bpy.types.Object] = set()
pset_template_path: str = ""
pset_template_file: ifcopenshell.file = None
pset_template_file: Optional[ifcopenshell.file] = None
classification_path: str = ""
classification_file: ifcopenshell.file = None
classification_file: Optional[ifcopenshell.file] = None
library_path: str = ""
library_file: ifcopenshell.file = None
library_file: Optional[ifcopenshell.file] = None
current_transaction = ""
last_transaction = ""
history = []
Expand Down Expand Up @@ -329,6 +331,7 @@ def unlink_element(

@staticmethod
def execute_ifc_operator(operator: bpy.types.Operator, context: bpy.types.Context, is_invoke=False):
blenderbim.last_actions.append({"type": "operator", "name": operator.bl_idname})
bpy.context.scene.BIMProperties.is_dirty = True
is_top_level_operator = not bool(IfcStore.current_transaction)

Expand All @@ -343,10 +346,14 @@ def execute_ifc_operator(operator: bpy.types.Operator, context: bpy.types.Contex
else:
operator.transaction_key = IfcStore.current_transaction

if is_invoke:
result = getattr(operator, "_invoke")(context, None)
else:
result = getattr(operator, "_execute")(context)
try:
if is_invoke:
result = getattr(operator, "_invoke")(context, None)
else:
result = getattr(operator, "_execute")(context)
except:
blenderbim.last_error = traceback.format_exc()
raise

if is_top_level_operator:
if tool.Ifc.get():
Expand Down
14 changes: 8 additions & 6 deletions src/blenderbim/blenderbim/bim/module/debug/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import blenderbim.core.debug as core
import blenderbim.bim.handler
import blenderbim.bim.import_ifc as import_ifc
from blenderbim import get_debug_info
from blenderbim import get_debug_info, format_debug_info
from blenderbim.bim.ifc import IfcStore


Expand All @@ -54,16 +54,18 @@ def execute(self, context):
}
)

# Format it in a readable way
text = "\n".join(f"{k}: {v}" for k, v in info.items())
text = format_debug_info(info)

print("-" * 80)
print(text)
print("-" * 80)

if platform.system() == "Windows":
command = "echo | set /p nul=" + text.strip()
command = "echo | set /p nul=" + text
elif platform.system() == "Darwin": # for MacOS
command = 'printf "' + text.strip().replace("\n", "\\n").replace('"', "") + '" | pbcopy'
command = 'printf "' + text.replace("\n", "\\n").replace('"', "") + '" | pbcopy'
else: # Linux
command = 'printf "' + text.strip().replace("\n", "\\n").replace('"', "") + '" | xclip -selection clipboard'
command = 'printf "' + text.replace("\n", "\\n").replace('"', "") + '" | xclip -selection clipboard'
subprocess.run(command, shell=True, check=True)
return {"FINISHED"}

Expand Down
9 changes: 5 additions & 4 deletions src/ifcopenshell-python/ifcopenshell/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,11 @@ def serialise_entity_instance(entity):
vcs_settings[key] = {"cast_type": "ndarray", "value": value.tolist()}
elif isinstance(value, list) and value and isinstance(value[0], ifcopenshell.entity_instance):
vcs_settings[key] = [serialise_entity_instance(i) for i in value]
if "add_representation" in usecase_path:
return ""
elif "owner." in usecase_path:
return ""
else:
try:
vcs_settings[key] = str(value)
except:
vcs_settings[key] = "n/a"
try:
return json.dumps(vcs_settings)
except:
Expand Down
7 changes: 6 additions & 1 deletion src/ifctester/ifctester/facet.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class Facet:
def __init__(self, *parameters):
self.status = None
self.failures: list[FacetFailure] = []
self.parameters = []
self.applicability_templates = []
for i, name in enumerate(self.parameters):
setattr(self, name.replace("@", ""), parameters[i])

Expand Down Expand Up @@ -101,8 +103,10 @@ def parse(self, xml):
return self

def filter(
self, ifc_file: ifcopenshell.file, elements: list[ifcopenshell.entity_instance]
self, ifc_file: ifcopenshell.file, elements: Optional[list[ifcopenshell.entity_instance]]
) -> list[ifcopenshell.entity_instance]:
if not elements:
return []
return [e for e in elements if self(e)]

def to_string(
Expand Down Expand Up @@ -133,6 +137,7 @@ def to_string(
total_replacements += 1
if total_replacements == total_variables:
return template
return "This facet cannot be interpreted"

def to_ids_value(self, parameter: Union[str, Restriction, list]) -> dict[str, Any]:
if isinstance(parameter, str):
Expand Down
5 changes: 3 additions & 2 deletions src/ifctester/ifctester/ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def __init__(
milestone=None,
):
# Not part of the IDS spec, but very useful in practice
self.filepath = None
self.filename = None
self.filepath: Optional[str] = None
self.filename: Optional[str] = None

self.specifications: List[Specification] = []
self.info = {}
Expand Down Expand Up @@ -300,6 +300,7 @@ def get_usage(self) -> Cardinality:
return "optional"
elif self.maxOccurs == 0:
return "prohibited"
return "required" # Fallback

def set_usage(self, usage: Cardinality) -> None:
if usage == "optional":
Expand Down

0 comments on commit a1e0b88

Please sign in to comment.