FreeCAD
177 строк · 7.7 Кб
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) 2013 WandererFan <wandererfan@gmail.com> *
5# * Copyright (c) 2019 Zheng, Lei (realthunder)<realthunder.dev@gmail.com>*
6# * Copyright (c) 2020 Carlo Pavan <carlopav@gmail.com> *
7# * Copyright (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
8# * *
9# * This file is part of the FreeCAD CAx development system. *
10# * *
11# * This program is free software; you can redistribute it and/or modify *
12# * it under the terms of the GNU Lesser General Public License (LGPL) *
13# * as published by the Free Software Foundation; either version 2 of *
14# * the License, or (at your option) any later version. *
15# * for detail see the LICENCE text file. *
16# * *
17# * This program is distributed in the hope that it will be useful, *
18# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20# * GNU Library General Public License for more details. *
21# * *
22# * You should have received a copy of the GNU Library General Public *
23# * License along with this program; if not, write to the Free Software *
24# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
25# * USA *
26# * *
27# ***************************************************************************
28"""Provides GUI tools to create PathArray objects.
29
30The copies will be created along a path, like a polyline, B-spline,
31or Bezier curve.
32"""
33## @package gui_patharray
34# \ingroup draftguitools
35# \brief Provides GUI tools to create PathArray objects.
36
37## \addtogroup draftguitools
38# @{
39from PySide.QtCore import QT_TRANSLATE_NOOP
40
41import FreeCAD as App
42import FreeCADGui as Gui
43import Draft_rc
44import DraftVecUtils
45import draftguitools.gui_base_original as gui_base_original
46
47from draftutils.messages import _err
48from draftutils.translate import translate
49
50# The module is used to prevent complaints from code checkers (flake8)
51True if Draft_rc.__name__ else False
52
53
54class PathArray(gui_base_original.Modifier):
55"""Gui Command for the Path array tool.
56
57Parameters
58----------
59use_link: bool, optional
60It defaults to `False`. If it is `True`, the created object
61will be a `Link array`.
62"""
63
64def __init__(self, use_link=False):
65super(PathArray, self).__init__()
66self.use_link = use_link
67self.call = None
68
69def GetResources(self):
70"""Set icon, menu and tooltip."""
71
72return {'Pixmap': 'Draft_PathArray',
73'MenuText': QT_TRANSLATE_NOOP("Draft_PathArray", "Path array"),
74'ToolTip': QT_TRANSLATE_NOOP("Draft_PathArray", "Creates copies of the selected object along a selected path.\nFirst select the object, and then select the path.\nThe path can be a polyline, B-spline, Bezier curve, or even edges from other objects.")}
75
76def Activated(self, name="Path array"):
77"""Execute when the command is called."""
78super(PathArray, self).Activated(name=name)
79self.name = name
80# This was deactivated because it doesn't work correctly;
81# the selection needs to be made on two objects, but currently
82# it only selects one.
83
84# if not Gui.Selection.getSelectionEx():
85# if self.ui:
86# self.ui.selectUi()
87# _msg(translate("draft",
88# "Please select exactly two objects, "
89# "the base object and the path object, "
90# "before calling this command."))
91# self.call = \
92# self.view.addEventCallback("SoEvent",
93# gui_tool_utils.selectObject)
94# else:
95# self.proceed()
96self.proceed()
97
98def proceed(self):
99"""Proceed with the command if one object was selected."""
100sel = Gui.Selection.getSelectionEx()
101if len(sel) != 2:
102_err(translate("draft","Please select exactly two objects, the base object and the path object, before calling this command."))
103else:
104base_object = sel[0].Object
105path_object = sel[1].Object
106
107count = 4
108extra = App.Vector(0, 0, 0)
109subelements = list(sel[1].SubElementNames)
110align = False
111align_mode = "Original"
112tan_vector = App.Vector(1, 0, 0)
113force_vertical = False
114vertical_vector = App.Vector(0, 0, 1)
115start_offset = 0.0
116end_offset = 0.0
117use_link = self.use_link
118
119_edge_list_str = list()
120_edge_list_str = ["'" + edge + "'" for edge in subelements]
121_sub_str = ", ".join(_edge_list_str)
122subelements_list_str = "[" + _sub_str + "]"
123
124vertical_vector_str = DraftVecUtils.toString(vertical_vector)
125
126Gui.addModule("Draft")
127_cmd = "Draft.make_path_array"
128_cmd += "("
129_cmd += "App.ActiveDocument." + base_object.Name + ", "
130_cmd += "App.ActiveDocument." + path_object.Name + ", "
131_cmd += "count=" + str(count) + ", "
132_cmd += "extra=" + DraftVecUtils.toString(extra) + ", "
133_cmd += "subelements=" + subelements_list_str + ", "
134_cmd += "align=" + str(align) + ", "
135_cmd += "align_mode=" + "'" + align_mode + "', "
136_cmd += "tan_vector=" + DraftVecUtils.toString(tan_vector) + ", "
137_cmd += "force_vertical=" + str(force_vertical) + ", "
138_cmd += "vertical_vector=" + vertical_vector_str + ", "
139_cmd += "start_offset=" + str(start_offset) + ", "
140_cmd += "end_offset=" + str(end_offset) + ", "
141_cmd += "use_link=" + str(use_link)
142_cmd += ")"
143
144_cmd_list = ["_obj_ = " + _cmd,
145"Draft.autogroup(_obj_)",
146"App.ActiveDocument.recompute()"]
147self.commit(translate("draft","Path array"), _cmd_list)
148
149# Commit the transaction and execute the commands
150# through the parent class
151self.finish()
152
153
154Gui.addCommand('Draft_PathArray', PathArray())
155
156
157class PathLinkArray(PathArray):
158"""Gui Command for the PathLinkArray tool based on the PathArray tool."""
159
160def __init__(self):
161super(PathLinkArray, self).__init__(use_link=True)
162
163def GetResources(self):
164"""Set icon, menu and tooltip."""
165
166return {'Pixmap': 'Draft_PathLinkArray',
167'MenuText': QT_TRANSLATE_NOOP("Draft_PathLinkArray", "Path link array"),
168'ToolTip': QT_TRANSLATE_NOOP("Draft_PathLinkArray", "Like the PathArray tool, but creates a 'Link array' instead.\nA 'Link array' is more efficient when handling many copies but the 'Fuse' option cannot be used.")}
169
170def Activated(self):
171"""Execute when the command is called."""
172super(PathLinkArray, self).Activated(name="Path link array")
173
174
175Gui.addCommand('Draft_PathLinkArray', PathLinkArray())
176
177## @}
178