FreeCAD
126 строк · 5.4 Кб
1# ***************************************************************************
2# * Copyright (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
3# * Copyright (c) 2009, 2010 Ken Cline <cline@frii.com> *
4# * Copyright (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
5# * Copyright (c) 2023 FreeCAD Project Association *
6# * *
7# * This file is part of the FreeCAD CAx development system. *
8# * *
9# * This program is free software; you can redistribute it and/or modify *
10# * it under the terms of the GNU Lesser General Public License (LGPL) *
11# * as published by the Free Software Foundation; either version 2 of *
12# * the License, or (at your option) any later version. *
13# * for detail see the LICENCE text file. *
14# * *
15# * FreeCAD is distributed in the hope that it will be useful, *
16# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18# * GNU Library General Public License for more details. *
19# * *
20# * You should have received a copy of the GNU Library General Public *
21# * License along with FreeCAD; if not, write to the Free Software *
22# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
23# * USA *
24# * *
25# ***************************************************************************
26"""Provides GUI tools to create Clone objects.
27
28The clone is basically a simple copy of the `Shape` of another object,
29whether that is a Draft object or any other 3D object.
30
31The Clone's `Shape` can be scaled in size in any direction.
32
33This implementation was developed before the `App::Link` object was created.
34In many cases using `App::Link` makes more sense, as this object is
35more memory efficient as it reuses the same internal `Shape`
36instead of creating a copy of it.
37"""
38## @package gui_clone
39# \ingroup draftguitools
40# \brief Provides GUI tools to create Clone objects.
41
42## \addtogroup draftguitools
43# @{
44from PySide.QtCore import QT_TRANSLATE_NOOP45
46import FreeCAD as App47import FreeCADGui as Gui48import Draft_rc49import draftguitools.gui_base_original as gui_base_original50import draftguitools.gui_tool_utils as gui_tool_utils51import draftutils.todo as todo52from draftutils.messages import _msg, _wrn53from draftutils.translate import translate54
55# The module is used to prevent complaints from code checkers (flake8)
56True if Draft_rc.__name__ else False57
58
59class Clone(gui_base_original.Modifier):60"""Gui Command for the Clone tool."""61
62def __init__(self):63super().__init__()64self.moveAfterCloning = False65
66def GetResources(self):67"""Set icon, menu and tooltip."""68
69return {"Pixmap": "Draft_Clone",70"Accel": "C, L",71"MenuText": QT_TRANSLATE_NOOP("Draft_Clone", "Clone"),72"ToolTip": QT_TRANSLATE_NOOP("Draft_Clone", "Creates a clone of the selected objects.\nThe resulting clone can be scaled in each of its three directions.")}73
74def Activated(self):75"""Execute when the command is called."""76super().Activated(name="Clone")77if not self.ui:78return79if not Gui.Selection.getSelection():80self.ui.selectUi(on_close_call=self.finish)81_msg(translate("draft", "Select an object to clone"))82self.call = self.view.addEventCallback("SoEvent", gui_tool_utils.selectObject)83else:84self.proceed()85
86def proceed(self):87"""Proceed with the command if objects were selected."""88objs = Gui.Selection.getSelection()89if not objs:90self.finish()91return92objs_shape = [obj for obj in objs if hasattr(obj, "Shape")]93if not objs_shape:94_wrn(translate("draft", "Cannot clone object(s) without a Shape, aborting"))95self.finish()96return97elif len(objs_shape) < len(objs):98_wrn(translate("draft", "Cannot clone object(s) without a Shape, skipping them"))99
100Gui.addModule("Draft")101App.ActiveDocument.openTransaction(translate("Draft", "Clone"))102for idx, obj in enumerate(objs_shape):103cmd = "Draft.make_clone(FreeCAD.ActiveDocument." + obj.Name + ")"104Gui.doCommand("clone" + str(idx) + " = " + cmd)105App.ActiveDocument.commitTransaction()106App.ActiveDocument.recompute()107
108Gui.Selection.clearSelection()109objs = App.ActiveDocument.Objects110for i in range(len(objs_shape)):111Gui.Selection.addSelection(objs[-(1 + i)])112self.finish()113
114def finish(self):115"""Terminate the operation of the tool."""116if self.call is not None:117self.end_callbacks(self.call)118super().finish()119if self.moveAfterCloning:120todo.ToDo.delay(Gui.runCommand, "Draft_Move")121
122
123Draft_Clone = Clone124Gui.addCommand('Draft_Clone', Clone())125
126## @}
127