FreeCAD

Форк
0
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

28
The clone is basically a simple copy of the `Shape` of another object,
29
whether that is a Draft object or any other 3D object.
30

31
The Clone's `Shape` can be scaled in size in any direction.
32

33
This implementation was developed before the `App::Link` object was created.
34
In many cases using `App::Link` makes more sense, as this object is
35
more memory efficient as it reuses the same internal `Shape`
36
instead 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
# @{
44
from PySide.QtCore import QT_TRANSLATE_NOOP
45

46
import FreeCAD as App
47
import FreeCADGui as Gui
48
import Draft_rc
49
import draftguitools.gui_base_original as gui_base_original
50
import draftguitools.gui_tool_utils as gui_tool_utils
51
import draftutils.todo as todo
52
from draftutils.messages import _msg, _wrn
53
from draftutils.translate import translate
54

55
# The module is used to prevent complaints from code checkers (flake8)
56
True if Draft_rc.__name__ else False
57

58

59
class Clone(gui_base_original.Modifier):
60
    """Gui Command for the Clone tool."""
61

62
    def __init__(self):
63
        super().__init__()
64
        self.moveAfterCloning = False
65

66
    def GetResources(self):
67
        """Set icon, menu and tooltip."""
68

69
        return {"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

74
    def Activated(self):
75
        """Execute when the command is called."""
76
        super().Activated(name="Clone")
77
        if not self.ui:
78
            return
79
        if not Gui.Selection.getSelection():
80
            self.ui.selectUi(on_close_call=self.finish)
81
            _msg(translate("draft", "Select an object to clone"))
82
            self.call = self.view.addEventCallback("SoEvent", gui_tool_utils.selectObject)
83
        else:
84
            self.proceed()
85

86
    def proceed(self):
87
        """Proceed with the command if objects were selected."""
88
        objs = Gui.Selection.getSelection()
89
        if not objs:
90
            self.finish()
91
            return
92
        objs_shape = [obj for obj in objs if hasattr(obj, "Shape")]
93
        if not objs_shape:
94
            _wrn(translate("draft", "Cannot clone object(s) without a Shape, aborting"))
95
            self.finish()
96
            return
97
        elif len(objs_shape) < len(objs):
98
            _wrn(translate("draft", "Cannot clone object(s) without a Shape, skipping them"))
99

100
        Gui.addModule("Draft")
101
        App.ActiveDocument.openTransaction(translate("Draft", "Clone"))
102
        for idx, obj in enumerate(objs_shape):
103
            cmd = "Draft.make_clone(FreeCAD.ActiveDocument." + obj.Name + ")"
104
            Gui.doCommand("clone" + str(idx) + " = " + cmd)
105
        App.ActiveDocument.commitTransaction()
106
        App.ActiveDocument.recompute()
107

108
        Gui.Selection.clearSelection()
109
        objs = App.ActiveDocument.Objects
110
        for i in range(len(objs_shape)):
111
            Gui.Selection.addSelection(objs[-(1 + i)])
112
        self.finish()
113

114
    def finish(self):
115
        """Terminate the operation of the tool."""
116
        if self.call is not None:
117
            self.end_callbacks(self.call)
118
        super().finish()
119
        if self.moveAfterCloning:
120
            todo.ToDo.delay(Gui.runCommand, "Draft_Move")
121

122

123
Draft_Clone = Clone
124
Gui.addCommand('Draft_Clone', Clone())
125

126
## @}
127

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.