FreeCAD
162 строки · 7.0 Кб
1# ***************************************************************************
2# * (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
3# * (c) 2009, 2010 Ken Cline <cline@frii.com> *
4# * (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
5# * *
6# * This file is part of the FreeCAD CAx development system. *
7# * *
8# * This program is free software; you can redistribute it and/or modify *
9# * it under the terms of the GNU Lesser General Public License (LGPL) *
10# * as published by the Free Software Foundation; either version 2 of *
11# * the License, or (at your option) any later version. *
12# * for detail see the LICENCE text file. *
13# * *
14# * FreeCAD is distributed in the hope that it will be useful, *
15# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17# * GNU Library General Public License for more details. *
18# * *
19# * You should have received a copy of the GNU Library General Public *
20# * License along with FreeCAD; if not, write to the Free Software *
21# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22# * USA *
23# * *
24# ***************************************************************************
25"""Provides GUI tools to create simple Point objects.
26
27A point is just a simple vertex with a position in 3D space.
28
29Its visual properties can be changed, like display size on screen
30and color.
31"""
32## @package gui_points
33# \ingroup draftguitools
34# \brief Provides GUI tools to create simple Point objects.
35
36## \addtogroup draftguitools
37# @{
38import pivy.coin as coin39from PySide.QtCore import QT_TRANSLATE_NOOP40
41import FreeCAD as App42import FreeCADGui as Gui43import Draft_rc44from draftguitools import gui_base_original45from draftutils import gui_utils46from draftutils import params47from draftutils import todo48from draftutils import utils49from draftutils.translate import translate50
51# The module is used to prevent complaints from code checkers (flake8)
52True if Draft_rc.__name__ else False53
54
55class Point(gui_base_original.Creator):56"""Gui Command for the Point tool."""57
58def GetResources(self):59"""Set icon, menu and tooltip."""60
61return {'Pixmap': 'Draft_Point',62'MenuText': QT_TRANSLATE_NOOP("Draft_Point", "Point"),63'ToolTip': QT_TRANSLATE_NOOP("Draft_Point", "Creates a point object. Click anywhere on the 3D view.")}64
65def Activated(self):66"""Execute when the command is called."""67super().Activated(name="Point")68if self.ui:69self.ui.pointUi(title=translate("draft", self.featureName), icon="Draft_Point")70self.ui.isRelative.hide()71self.ui.continueCmd.show()72# adding 2 callback functions73self.callbackClick = self.view.addEventCallbackPivy(coin.SoMouseButtonEvent.getClassTypeId(), self.click)74self.callbackMove = self.view.addEventCallbackPivy(coin.SoLocation2Event.getClassTypeId(), self.move)75
76def move(self, event_cb):77"""Execute as a callback when the pointer moves in the 3D view.78
79It should automatically update the coordinates in the widgets
80of the task panel.
81"""
82event = event_cb.getEvent()83mousepos = event.getPosition().getValue()84ctrl = event.wasCtrlDown()85self.point = Gui.Snapper.snap(mousepos, active=ctrl)86if self.ui:87self.ui.displayPoint(self.point)88
89def numericInput(self, numx, numy, numz):90"""Validate the entry fields in the user interface.91
92This function is called by the toolbar or taskpanel interface
93when valid x, y, and z have been entered in the input fields.
94"""
95self.point = App.Vector(numx, numy, numz)96self.click()97
98def click(self, event_cb=None):99"""Execute as a callback when the pointer clicks on the 3D view.100
101It should act as if the Enter key was pressed, or the OK button
102was pressed in the task panel.
103"""
104if event_cb:105event = event_cb.getEvent()106if (event.getState() != coin.SoMouseButtonEvent.DOWN or107event.getButton() != event.BUTTON1):108return109if self.point:110Gui.addModule("Draft")111if params.get_param("UsePartPrimitives"):112# Insert a Part::Primitive object113_cmd = 'FreeCAD.ActiveDocument.'114_cmd += 'addObject("Part::Vertex", "Point")'115_cmd_list = ['point = ' + _cmd,116'point.X = ' + str(self.point[0]),117'point.Y = ' + str(self.point[1]),118'point.Z = ' + str(self.point[2]),119'Draft.autogroup(point)',120'Draft.select(point)',121'FreeCAD.ActiveDocument.recompute()']122self.commit(translate("draft", "Create Point"), _cmd_list)123else:124# Insert a Draft point125_cmd = 'Draft.make_point'126_cmd += '('127_cmd += str(self.point[0]) + ', '128_cmd += str(self.point[1]) + ', '129_cmd += str(self.point[2])130_cmd += ')'131_cmd_list = ['point = ' + _cmd,132'Draft.autogroup(point)',133'FreeCAD.ActiveDocument.recompute()']134self.commit(translate("draft", "Create Point"), _cmd_list)135self.finish(cont=None)136
137def finish(self, cont=False):138"""Terminate the operation.139
140Parameters
141----------
142cont: bool or None, optional
143Restart (continue) the command if `True`, or if `None` and
144`ui.continueMode` is `True`.
145"""
146if self.callbackClick:147self.view.removeEventCallbackPivy(coin.SoMouseButtonEvent.getClassTypeId(), self.callbackClick)148if self.callbackMove:149self.view.removeEventCallbackPivy(coin.SoLocation2Event.getClassTypeId(), self.callbackMove)150if self.callbackClick or self.callbackMove:151# Next line fixes https://github.com/FreeCAD/FreeCAD/issues/10469:152gui_utils.end_all_events()153self.callbackClick = None154self.callbackMove = None155super().finish()156if cont or (cont is None and self.ui and self.ui.continueMode):157self.Activated()158
159
160Gui.addCommand('Draft_Point', Point())161
162## @}
163