save plugins Fusion 360
This commit is contained in:
64
tamplate/lib/fusion360utils/general_utils.py
Normal file
64
tamplate/lib/fusion360utils/general_utils.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# Copyright 2022 by Autodesk, Inc.
|
||||
# Permission to use, copy, modify, and distribute this software in object code form
|
||||
# for any purpose and without fee is hereby granted, provided that the above copyright
|
||||
# notice appears in all copies and that both that copyright notice and the limited
|
||||
# warranty and restricted rights notice below appear in all supporting documentation.
|
||||
#
|
||||
# AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. AUTODESK SPECIFICALLY
|
||||
# DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.
|
||||
# AUTODESK, INC. DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
|
||||
# UNINTERRUPTED OR ERROR FREE.
|
||||
|
||||
import os
|
||||
import traceback
|
||||
import adsk.core
|
||||
|
||||
app = adsk.core.Application.get()
|
||||
ui = app.userInterface
|
||||
|
||||
# Attempt to read DEBUG flag from parent config.
|
||||
try:
|
||||
from ... import config
|
||||
DEBUG = config.DEBUG
|
||||
except:
|
||||
DEBUG = False
|
||||
|
||||
|
||||
def log(message: str, level: adsk.core.LogLevels = adsk.core.LogLevels.InfoLogLevel, force_console: bool = False):
|
||||
"""Utility function to easily handle logging in your app.
|
||||
|
||||
Arguments:
|
||||
message -- The message to log.
|
||||
level -- The logging severity level.
|
||||
force_console -- Forces the message to be written to the Text Command window.
|
||||
"""
|
||||
# Always print to console, only seen through IDE.
|
||||
print(message)
|
||||
|
||||
# Log all errors to Fusion log file.
|
||||
if level == adsk.core.LogLevels.ErrorLogLevel:
|
||||
log_type = adsk.core.LogTypes.FileLogType
|
||||
app.log(message, level, log_type)
|
||||
|
||||
# If config.DEBUG is True write all log messages to the console.
|
||||
if DEBUG or force_console:
|
||||
log_type = adsk.core.LogTypes.ConsoleLogType
|
||||
app.log(message, level, log_type)
|
||||
|
||||
|
||||
def handle_error(name: str, show_message_box: bool = False):
|
||||
"""Utility function to simplify error handling.
|
||||
|
||||
Arguments:
|
||||
name -- A name used to label the error.
|
||||
show_message_box -- Indicates if the error should be shown in the message box.
|
||||
If False, it will only be shown in the Text Command window
|
||||
and logged to the log file.
|
||||
"""
|
||||
|
||||
log('===== Error =====', adsk.core.LogLevels.ErrorLogLevel)
|
||||
log(f'{name}\n{traceback.format_exc()}', adsk.core.LogLevels.ErrorLogLevel)
|
||||
|
||||
# If desired you could show an error as a message box.
|
||||
if show_message_box:
|
||||
ui.messageBox(f'{name}\n{traceback.format_exc()}')
|
||||
Reference in New Issue
Block a user