Skip to content

Commit

Permalink
Path workbench was renamed to CAM workbench; try to make BTL compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
knipknap committed Mar 19, 2024
1 parent 76f97ef commit 92fe1a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
11 changes: 7 additions & 4 deletions btl/ui/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
from .. import ToolDB, serializers, __version__
from ..const import icon_dir, translations_dir
from .library import LibraryUI
from .util import get_library_path, set_library_path

FreeCADGui.addLanguagePath(translations_dir)
ICON_FILE = os.path.join(icon_dir, 'tool-library.svg')
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")
HAS_CAM = 'CAMWorkbench' in FreeCADGui.listWorkbenches()

class OpenBTL:
"""Opens the Better Tool Library dialog."""

def GetResources(self):
wb = 'CAM' if HAS_CAM else 'Path'
return {
'Pixmap': 'Path_ToolTable',
'Pixmap': f'{wb}_ToolTable',
'Accel': "P, T",
"MenuText": QT_TRANSLATE_NOOP(
"btl", "ToolBit Library editor"
Expand All @@ -38,7 +40,7 @@ def open(self):

def on_library_open_clicked():
# Ensure that a library dir is defined in the preferences.
lib_dir = prefs.GetString("LastPathToolLibrary", "~/.btl/Library")
lib_dir = get_library_path()
lib_base_dir = os.path.dirname(lib_dir)
#FreeCAD.Console.PrintMessage("Library path is {}\n".format(lib_base_dir))

Expand All @@ -52,7 +54,7 @@ def on_library_open_clicked():
dialog.show()

def on_workbench_activated(workbench):
if workbench != 'PathWorkbench':
if workbench not in ('PathWorkbench', 'CAMWorkbench'):
return

# Create a toolbar.
Expand All @@ -75,4 +77,5 @@ def on_workbench_activated(workbench):
print(f'Better Tool Library {__version__} loaded successfully.')

FreeCADGui.addCommand("Path_ToolBitLibraryOpen", OpenBTL())
FreeCADGui.addCommand("CAM_ToolBitLibraryOpen", OpenBTL())
FreeCADGui.getMainWindow().workbenchActivated.connect(on_workbench_activated)
7 changes: 3 additions & 4 deletions btl/ui/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from pathlib import Path
from PySide import QtGui, QtCore
from ..i18n import translate
from .util import load_ui
from .util import load_ui, get_library_path, set_library_path

__dir__ = os.path.dirname(__file__)
ui_path = os.path.join(__dir__, "preferences.ui")
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")

def tool_dir_from_lib_dir(lib_dir):
base_dir = os.path.dirname(lib_dir)
Expand All @@ -22,7 +21,7 @@ def __init__(self, serializer, parent=None):
self.on_lib_path_select_clicked)
self.serializer = serializer

lib_path = prefs.GetString("LastPathToolLibrary", "~/.btl/tools")
lib_path = get_library_path()
self.form.lineEditToolPath.setText(lib_path)

def on_lib_path_select_clicked(self):
Expand Down Expand Up @@ -50,5 +49,5 @@ def exec(self):

tool_dir = tool_dir_from_lib_dir(lib_dir)
self.serializer.set_tool_dir(tool_dir)
prefs.SetString("LastPathToolLibrary", lib_dir)
set_library_path(lib_dir)
return True
17 changes: 17 additions & 0 deletions btl/ui/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import os
import FreeCAD
from PySide import QtCore, QtGui, QtUiTools, QtSvg

default_lib_path = os.path.join("~", ".btl", "Library")

def get_library_path():
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/CAM")
if not prefs.IsEmpty():
return prefs.GetString("LastPathToolLibrary", default_lib_path)
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")
return prefs.GetString("LastPathToolLibrary", default_lib_path)

def set_library_path(path):
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/CAM")
if not prefs.IsEmpty():
return prefs.SetString("LastPathToolLibrary", path)
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")
return prefs.SetString("LastPathToolLibrary", path)

def load_ui(ui_path, parent=None, custom_widgets=None):
loader = QtUiTools.QUiLoader(parent)
loader.setWorkingDirectory(os.path.dirname(__file__))
Expand Down

0 comments on commit 92fe1a0

Please sign in to comment.