Draft: move ShapeString GuiCommand to gui_shapestrings module

This commit is contained in:
vocx-fc
2020-04-02 00:44:01 -06:00
committed by Yorik van Havre
parent 44be767583
commit afd941b66d
3 changed files with 236 additions and 135 deletions

View File

@@ -94,6 +94,7 @@ SET(Creator_tools
draftguitools/gui_ellipses.py
draftguitools/gui_texts.py
draftguitools/gui_dimensions.py
draftguitools/gui_shapestrings.py
)
SET(Draft_GUI_tools

View File

@@ -93,7 +93,6 @@ from draftguitools.gui_dimension_ops import Draft_FlipDimension
from draftguitools.gui_lineslope import Draft_Slope
import draftguitools.gui_arrays
# import DraftFillet
import drafttaskpanels.task_shapestring as task_shapestring
import drafttaskpanels.task_scale as task_scale
# ---------------------------------------------------------------------------
@@ -163,142 +162,12 @@ from draftguitools.gui_polygons import Polygon
from draftguitools.gui_ellipses import Ellipse
from draftguitools.gui_texts import Text
from draftguitools.gui_dimensions import Dimension
from draftguitools.gui_shapestrings import ShapeString
class ShapeString(Creator):
"""The Draft_ShapeString FreeCAD command definition."""
def GetResources(self):
"""Set icon, menu and tooltip."""
_menu = "Shape from text"
_tooltip = ("Creates a shape from a text string by choosing "
"a specific font and a placement.\n"
"The closed shapes can be used for extrusions "
"and boolean operations.")
d = {'Pixmap': 'Draft_ShapeString',
'Accel': "S, S",
'MenuText': QtCore.QT_TRANSLATE_NOOP("Draft_ShapeString", _menu),
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Draft_ShapeString", _tooltip)}
return d
def Activated(self):
name = translate("draft","ShapeString")
Creator.Activated(self,name)
self.creator = Creator
if self.ui:
self.ui.sourceCmd = self
self.taskmode = Draft.getParam("UiMode",1)
if self.taskmode:
try:
del self.task
except AttributeError:
pass
self.task = task_shapestring.ShapeStringTaskPanel()
self.task.sourceCmd = self
ToDo.delay(FreeCADGui.Control.showDialog, self.task)
else:
self.dialog = None
self.text = ''
self.ui.sourceCmd = self
self.ui.pointUi(name)
self.active = True
self.call = self.view.addEventCallback("SoEvent",self.action)
self.ssBase = None
self.ui.xValue.setFocus()
self.ui.xValue.selectAll()
FreeCAD.Console.PrintMessage(translate("draft", "Pick ShapeString location point")+"\n")
FreeCADGui.draftToolBar.show()
def createObject(self):
"""creates object in the current doc"""
#print("debug: D_T ShapeString.createObject type(self.SString): " str(type(self.SString)))
dquote = '"'
if sys.version_info.major < 3: # Python3: no more unicode
String = 'u' + dquote + self.SString.encode('unicode_escape') + dquote
else:
String = dquote + self.SString + dquote
Size = str(self.SSSize) # numbers are ascii so this should always work
Tracking = str(self.SSTrack) # numbers are ascii so this should always work
FFile = dquote + self.FFile + dquote
# print("debug: D_T ShapeString.createObject type(String): " str(type(String)))
# print("debug: D_T ShapeString.createObject type(FFile): " str(type(FFile)))
try:
qr,sup,points,fil = self.getStrings()
FreeCADGui.addModule("Draft")
self.commit(translate("draft","Create ShapeString"),
['ss=Draft.makeShapeString(String='+String+',FontFile='+FFile+',Size='+Size+',Tracking='+Tracking+')',
'plm=FreeCAD.Placement()',
'plm.Base='+DraftVecUtils.toString(self.ssBase),
'plm.Rotation.Q='+qr,
'ss.Placement=plm',
'ss.Support='+sup,
'Draft.autogroup(ss)',
'FreeCAD.ActiveDocument.recompute()'])
except Exception as e:
FreeCAD.Console.PrintError("Draft_ShapeString: error delaying commit\n")
self.finish()
def action(self,arg):
"""scene event handler"""
if arg["Type"] == "SoKeyboardEvent":
if arg["Key"] == "ESCAPE":
self.finish()
elif arg["Type"] == "SoLocation2Event": #mouse movement detection
if self.active:
self.point,ctrlPoint,info = getPoint(self,arg,noTracker=True)
redraw3DView()
elif arg["Type"] == "SoMouseButtonEvent":
if (arg["State"] == "DOWN") and (arg["Button"] == "BUTTON1"):
if not self.ssBase:
self.ssBase = self.point
self.active = False
FreeCADGui.Snapper.off()
self.ui.SSUi()
def numericInput(self,numx,numy,numz):
'''this function gets called by the toolbar when valid
x, y, and z have been entered there'''
self.ssBase = Vector(numx,numy,numz)
self.ui.SSUi() #move on to next step in parameter entry
def numericSSize(self,ssize):
'''this function is called by the toolbar when valid size parameter
has been entered. '''
self.SSSize = ssize
self.ui.STrackUi()
def numericSTrack(self,strack):
'''this function is called by the toolbar when valid size parameter
has been entered. ?'''
self.SSTrack = strack
self.ui.SFileUi()
def validSString(self,sstring):
'''this function is called by the toolbar when a ?valid? string parameter
has been entered. '''
self.SString = sstring
self.ui.SSizeUi()
def validFFile(self,FFile):
'''this function is called by the toolbar when a ?valid? font file parameter
has been entered. '''
self.FFile = FFile
# last step in ShapeString parm capture, create object
self.createObject()
def finish(self, finishbool=False):
"""terminates the operation"""
Creator.finish(self)
if self.ui:
# del self.dialog # what does this do??
if self.ui.continueMode:
self.Activated()
#---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Modifier functions
#---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
from draftguitools.gui_base_original import Modifier
@@ -3001,7 +2870,6 @@ from draftguitools.gui_snaps import ShowSnapBar
# drawing commands
FreeCADGui.addCommand('Draft_Point',Point())
FreeCADGui.addCommand('Draft_ShapeString',ShapeString())
FreeCADGui.addCommand('Draft_Facebinder',Draft_Facebinder())
FreeCADGui.addCommand('Draft_Label',Draft_Label())

View File

@@ -0,0 +1,232 @@
# ***************************************************************************
# * (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
# * (c) 2009, 2010 Ken Cline <cline@frii.com> *
# * (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
# * *
# * This file is part of the FreeCAD CAx development system. *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * 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 Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with FreeCAD; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
"""Provides tools for creating text shapes with the Draft Workbench.
These text shapes are made of various edges and closed faces, and therefore
can be extruded to create solid bodies that can be used in boolean
operations. That is, these text shapes can be used for engraving text
into solid bodies.
They are more complex that simple text annotations.
"""
## @package gui_shapestrings
# \ingroup DRAFT
# \brief Provides tools for creating text shapes with the Draft Workbench.
from PySide.QtCore import QT_TRANSLATE_NOOP
import sys
import FreeCAD as App
import FreeCADGui as Gui
import Draft_rc
import DraftVecUtils
import draftutils.utils as utils
import draftguitools.gui_base_original as gui_base_original
import draftguitools.gui_tool_utils as gui_tool_utils
import drafttaskpanels.task_shapestring as task_shapestring
import draftutils.todo as todo
from draftutils.translate import translate
from draftutils.messages import _msg, _err
# The module is used to prevent complaints from code checkers (flake8)
True if Draft_rc.__name__ else False
class ShapeString(gui_base_original.Creator):
"""Gui command for the ShapeString tool."""
def GetResources(self):
"""Set icon, menu and tooltip."""
_menu = "Shape from text"
_tip = ("Creates a shape from a text string by choosing "
"a specific font and a placement.\n"
"The closed shapes can be used for extrusions "
"and boolean operations.")
d = {'Pixmap': 'Draft_ShapeString',
'Accel': "S, S",
'MenuText': QT_TRANSLATE_NOOP("Draft_ShapeString", _menu),
'ToolTip': QT_TRANSLATE_NOOP("Draft_ShapeString", _tip)}
return d
def Activated(self):
"""Execute when the command is called."""
name = translate("draft", "ShapeString")
super().Activated(name)
self.creator = gui_base_original.Creator
if self.ui:
self.ui.sourceCmd = self
self.taskmode = utils.getParam("UiMode", 1)
if self.taskmode:
# This doesn't use the task panel defined in DraftGui
# so it is deleted and a new task panel is installed
try:
del self.task
except AttributeError:
pass
self.task = task_shapestring.ShapeStringTaskPanel()
self.task.sourceCmd = self
todo.ToDo.delay(Gui.Control.showDialog, self.task)
else:
self.dialog = None
self.text = ''
self.ui.sourceCmd = self
self.ui.pointUi(name)
self.active = True
self.call = self.view.addEventCallback("SoEvent", self.action)
self.ssBase = None
self.ui.xValue.setFocus()
self.ui.xValue.selectAll()
_msg(translate("draft", "Pick ShapeString location point"))
Gui.draftToolBar.show()
def createObject(self):
"""Create the actual object in the current document."""
# print("debug: D_T ShapeString.createObject type(self.SString):"
# + str(type(self.SString)))
dquote = '"'
if sys.version_info.major < 3:
# Python2, string needs to be converted to unicode
String = ('u' + dquote
+ self.SString.encode('unicode_escape') + dquote)
else:
# Python3, string is already unicode
String = dquote + self.SString + dquote
# Size and tracking are numbers;
# they are ASCII so this conversion should always work
Size = str(self.SSSize)
Tracking = str(self.SSTrack)
FFile = dquote + self.FFile + dquote
try:
qr, sup, points, fil = self.getStrings()
Gui.addModule("Draft")
_cmd = 'Draft.makeShapeString'
_cmd += '('
_cmd += 'String=' + String + ', '
_cmd += 'FontFile=' + FFile + ', '
_cmd += 'Size=' + Size + ', '
_cmd += 'Tracking=' + Tracking
_cmd += ')'
_cmd_list = ['ss = ' + _cmd,
'plm = FreeCAD.Placement()',
'plm.Base = ' + DraftVecUtils.toString(self.ssBase),
'plm.Rotation.Q = ' + qr,
'ss.Placement = plm',
'ss.Support = ' + sup,
'Draft.autogroup(ss)',
'FreeCAD.ActiveDocument.recompute()']
self.commit(translate("draft", "Create ShapeString"),
_cmd_list)
except Exception:
_err("Draft_ShapeString: error delaying commit")
self.finish()
def action(self, arg):
"""Handle the 3D scene events.
This is installed as an EventCallback in the Inventor view.
Parameters
----------
arg: dict
Dictionary with strings that indicates the type of event received
from the 3D view.
"""
if arg["Type"] == "SoKeyboardEvent":
if arg["Key"] == "ESCAPE":
self.finish()
elif arg["Type"] == "SoLocation2Event": # mouse movement detection
if self.active:
(self.point,
ctrlPoint, info) = gui_tool_utils.getPoint(self, arg,
noTracker=True)
gui_tool_utils.redraw3DView()
elif arg["Type"] == "SoMouseButtonEvent":
if (arg["State"] == "DOWN") and (arg["Button"] == "BUTTON1"):
if not self.ssBase:
self.ssBase = self.point
self.active = False
Gui.Snapper.off()
self.ui.SSUi()
def numericInput(self, numx, numy, numz):
"""Validate the entry fields in the user interface.
This function is called by the toolbar or taskpanel interface
when valid x, y, and z have been entered in the input fields.
"""
self.ssBase = App.Vector(numx, numy, numz)
self.ui.SSUi() # move on to next step in parameter entry
def numericSSize(self, ssize):
"""Validate the size in the user interface.
This function is called by the toolbar or taskpanel interface
when a valid size parameter has been entered in the input field.
"""
self.SSSize = ssize
self.ui.STrackUi()
def numericSTrack(self, strack):
"""Validate the tracking value in the user interface.
This function is called by the toolbar or taskpanel interface
when a valid tracking value has been entered in the input field.
"""
self.SSTrack = strack
self.ui.SFileUi()
def validSString(self, sstring):
"""Validate the string value in the user interface.
This function is called by the toolbar or taskpanel interface
when a valid string value has been entered in the input field.
"""
self.SString = sstring
self.ui.SSizeUi()
def validFFile(self, FFile):
"""Validate the font file value in the user interface.
This function is called by the toolbar or taskpanel interface
when a valid font file value has been entered in the input field.
"""
self.FFile = FFile
# last step in ShapeString parm capture, create object
self.createObject()
def finish(self, finishbool=False):
"""Terminate the operation."""
super().finish()
if self.ui:
# del self.dialog # what does this do??
if self.ui.continueMode:
self.Activated()
Gui.addCommand('Draft_ShapeString', ShapeString())