From c0185ad95c94e545185fc64e1c4ce382d4f2b2f8 Mon Sep 17 00:00:00 2001 From: Paddle Date: Mon, 4 Sep 2023 18:31:29 +0200 Subject: [PATCH] Move functions to UtilFunctions.py. --- src/Mod/Assembly/CMakeLists.txt | 1 + src/Mod/Assembly/CommandInsertLink.py | 30 ++------------ src/Mod/Assembly/UtilsAssembly.py | 56 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 26 deletions(-) create mode 100644 src/Mod/Assembly/UtilsAssembly.py diff --git a/src/Mod/Assembly/CMakeLists.txt b/src/Mod/Assembly/CMakeLists.txt index e948f8d502..cece8064ee 100644 --- a/src/Mod/Assembly/CMakeLists.txt +++ b/src/Mod/Assembly/CMakeLists.txt @@ -11,6 +11,7 @@ set(Assembly_Scripts TestAssemblyWorkbench.py Preferences.py AssemblyImport.py + UtilsAssembly.py ) if(BUILD_GUI) diff --git a/src/Mod/Assembly/CommandInsertLink.py b/src/Mod/Assembly/CommandInsertLink.py index c3932bf6c8..67cad076a3 100644 --- a/src/Mod/Assembly/CommandInsertLink.py +++ b/src/Mod/Assembly/CommandInsertLink.py @@ -30,6 +30,7 @@ if App.GuiUp: import FreeCADGui as Gui from PySide import QtCore, QtGui, QtWidgets +import UtilsAssembly # translate = App.Qt.translate __title__ = "Assembly Command Insert Link" @@ -37,29 +38,6 @@ __author__ = "Ondsel" __url__ = "https://www.freecad.org" -def activeAssembly(): - doc = Gui.ActiveDocument - - if doc is None or doc.ActiveView is None: - return None - - active_part = doc.ActiveView.getActiveObject("part") - - if active_part is not None and active_part.Type == "Assembly": - return active_part - - return None - - -def isDocTemporary(doc): - # Guard against older versions of FreeCad which don't have the Temporary attribute - try: - docTemporary = doc.Temporary - except AttributeError: - docTemporary = False - return docTemporary - - class CommandInsertLink: def __init__(self): pass @@ -80,10 +58,10 @@ class CommandInsertLink: } def IsActive(self): - return activeAssembly() is not None + return UtilsAssembly.activeAssembly() is not None def Activated(self): - assembly = activeAssembly() + assembly = UtilsAssembly.activeAssembly() if not assembly: return view = Gui.activeDocument().activeView() @@ -139,7 +117,7 @@ class TaskAssemblyInsertLink(QtCore.QObject): docList = App.listDocuments().values() for doc in docList: - if isDocTemporary(doc): + if UtilsAssembly.isDocTemporary(doc): continue for obj in doc.findObjects("App::Part"): diff --git a/src/Mod/Assembly/UtilsAssembly.py b/src/Mod/Assembly/UtilsAssembly.py new file mode 100644 index 0000000000..e956e7f8ac --- /dev/null +++ b/src/Mod/Assembly/UtilsAssembly.py @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# /**************************************************************************** +# * +# Copyright (c) 2023 Ondsel * +# * +# This file is part of FreeCAD. * +# * +# FreeCAD is free software: you can redistribute it and/or modify it * +# under the terms of the GNU Lesser General Public License as * +# published by the Free Software Foundation, either version 2.1 of the * +# License, or (at your option) any later version. * +# * +# FreeCAD is distributed in the hope that it will be useful, but * +# WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * +# Lesser General Public License for more details. * +# * +# You should have received a copy of the GNU Lesser General Public * +# License along with FreeCAD. If not, see * +# . * +# * +# ***************************************************************************/ + +import FreeCAD as App + +if App.GuiUp: + import FreeCADGui as Gui + +# translate = App.Qt.translate + +__title__ = "Assembly utilitary functions" +__author__ = "Ondsel" +__url__ = "https://www.freecad.org" + + +def activeAssembly(): + doc = Gui.ActiveDocument + + if doc is None or doc.ActiveView is None: + return None + + active_part = doc.ActiveView.getActiveObject("part") + + if active_part is not None and active_part.Type == "Assembly": + return active_part + + return None + + +def isDocTemporary(doc): + # Guard against older versions of FreeCad which don't have the Temporary attribute + try: + temp = doc.Temporary + except AttributeError: + temp = False + return temp