Skip to content

Commit

Permalink
[Draft] Dimension Style code cleanup
Browse files Browse the repository at this point in the history
thx @vocx-fc for reviewing
  • Loading branch information
carlopav committed Mar 13, 2020
1 parent 9c7819d commit 5e02819
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 203 deletions.
22 changes: 11 additions & 11 deletions src/Mod/Draft/draftguitools/gui_style_dimension.py
@@ -1,9 +1,3 @@
"""This module provides the Draft Dimension Style tool.
"""
## @package gui_style_dimension
# \ingroup DRAFT
# \brief This module provides the Draft Dimension Style tool.

# ***************************************************************************
# * (c) 2020 Carlo Pavan *
# * *
Expand All @@ -27,12 +21,18 @@
# * *
# ***************************************************************************

"""This module provides the Draft Dimension Style tool.
"""
## @package gui_style_dimension
# \ingroup DRAFT
# \brief This module provides the Draft Dimension Style tool.

import FreeCAD as App
import FreeCADGui as Gui
from PySide import QtCore
from . import gui_base
from draftutils import utils
from draftobjects import style_dimension
from draftobjects.style_dimension import make_dimension_style

class GuiCommandDimensionStyle(gui_base.GuiCommandBase):
"""
Expand All @@ -58,12 +58,12 @@ def Activated(self):

if len(sel) == 1:
if utils.get_type(sel[0]) == 'Dimension':
style_dimension.makeDimensionStyle(sel[0])
make_dimension_style(sel[0])
return self.finish()

style_dimension.makeDimensionStyle()
make_dimension_style()
self.finish()
return self.finish()

This comment has been minimized.

Copy link
@vocx-fc

vocx-fc Mar 13, 2020

By the way, I didn't mention this, but this is surely prototype code, right? Why would you call finish twice?


if App.GuiUp:
Gui.addCommand('Draft_DimensionStyle', GuiCommandDimensionStyle())

Gui.addCommand('Draft_DimensionStyle', GuiCommandDimensionStyle())
211 changes: 19 additions & 192 deletions src/Mod/Draft/draftobjects/style_dimension.py
@@ -1,9 +1,3 @@
"""This module provides the object code for Draft Dimension Style.
"""
## @package style_dimension
# \ingroup DRAFT
# \brief This module provides the object code for Draft DimensionStyle.

# ***************************************************************************
# * (c) 2020 Carlo Pavan *
# * *
Expand All @@ -27,201 +21,34 @@
# * *
# ***************************************************************************

"""This module provides the object code for Draft DimensionStyle.
"""
## @package style_dimension
# \ingroup DRAFT
# \brief This module provides the object code for Draft DimensionStyle.

import FreeCAD as App
import FreeCADGui as Gui
import Draft
from Draft import _ViewProviderDraft
from Draft import _DraftObject
from PySide.QtCore import QT_TRANSLATE_NOOP
from draftutils.utils import get_param, ARROW_TYPES

def makeDimensionStyle(existing_dimension = None):
if App.GuiUp:
import FreeCADGui as Gui
from draftviewproviders.view_style_dimension import ViewProviderDraftDimensionStyle

def make_dimension_style(existing_dimension = None):
"""
Make dimension style
"""
if not App.ActiveDocument:
FreeCAD.Console.PrintError("No active document. Aborting\n")
App.Console.PrintError("No active document. Aborting\n")
return
obj = App.ActiveDocument.addObject("App::FeaturePython","DimensionStyle")
ViewProviderDraftDimensionStyle(obj.ViewObject, existing_dimension)
#if Gui:
# formatObject(obj)
# select(obj)
DimensionStyle(obj)
if App.GuiUp:
ViewProviderDraftDimensionStyle(obj.ViewObject, existing_dimension)
return obj


class ViewProviderDraftDimensionStyle(_ViewProviderDraft):
"""
Dimension style dont have a proper object but just a viewprovider.
It stores inside a document object dimension settings and restore them on demand.
"""
def __init__(self, obj, existing_dimension = None):
"""
vobj properties type parameter type
----------------------------------------------------------------------------------
vobj.ScaleMultiplier" App::PropertyFloat "DraftAnnotationScale" Float
vobj.FontName App::PropertyFont "textfont" Text
vobj.FontSize App::PropertyLength "textheight" Float
vobj.TextSpacing App::PropertyLength "dimspacing" Float
vobj.Decimals App::PropertyInteger "dimPrecision" Integer
vobj.ShowUnit App::PropertyBool
vobj.UnitOverride App::PropertyString
vobj.LineWidth App::PropertyFloat
vobj.LineColor App::PropertyColor
vobj.ArrowSize App::PropertyLength "arrowsize" Float
vobj.ArrowType App::PropertyEnumeration "dimsymbol" Integer
vobj.FlipArrows App::PropertyBool
vobj.DimOvershoot App::PropertyDistance "dimovershoot" Float
vobj.ExtLines App::PropertyDistance "extlines" Float
vobj.ExtOvershoot App::PropertyDistance "extovershoot" Float
vobj.ShowLine App::PropertyBool
"""
param = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft")
annotation_scale = param.GetFloat("DraftAnnotationScale", 1.0)

# annotation properties
obj.addProperty("App::PropertyFloat","ScaleMultiplier",
"Annotation",QT_TRANSLATE_NOOP("App::Property",
"Dimension size overall multiplier")
).ScaleMultiplier = 1 / annotation_scale

# text properties
obj.addProperty("App::PropertyFont","FontName",
"Text",QT_TRANSLATE_NOOP("App::Property","Font name")
).FontName = get_param("textfont","")

obj.addProperty("App::PropertyLength","FontSize",
"Text",QT_TRANSLATE_NOOP("App::Property","Font size")
).FontSize = get_param("textheight",0.20)

obj.addProperty("App::PropertyLength","TextSpacing",
"Text",QT_TRANSLATE_NOOP("App::Property",
"The spacing between the text and the dimension line")
).TextSpacing = get_param("dimspacing",0.05)

# units properties
obj.addProperty("App::PropertyInteger","Decimals",
"Units",QT_TRANSLATE_NOOP("App::Property",
"The number of decimals to show")
).Decimals = get_param("dimPrecision",2)

obj.addProperty("App::PropertyBool","ShowUnit",
"Units",QT_TRANSLATE_NOOP("App::Property",
"Show the unit suffix")
).ShowUnit = get_param("showUnit",True)

obj.addProperty("App::PropertyString","UnitOverride",
"Units",QT_TRANSLATE_NOOP("App::Property",
"A unit to express the measurement. Leave blank for system default")
)

# graphics properties
obj.addProperty("App::PropertyFloat","LineWidth",
"Graphics",QT_TRANSLATE_NOOP("App::Property","Line width")
)

obj.addProperty("App::PropertyColor","LineColor",
"Graphics",QT_TRANSLATE_NOOP("App::Property","Line color")
)

obj.addProperty("App::PropertyLength","ArrowSize",
"Graphics",QT_TRANSLATE_NOOP("App::Property","Arrow size")
).ArrowSize = get_param("arrowsize",0.1)

obj.addProperty("App::PropertyEnumeration","ArrowType",
"Graphics",QT_TRANSLATE_NOOP("App::Property","Arrow type")
).ArrowType = ARROW_TYPES
obj.ArrowType = ARROW_TYPES[get_param("dimsymbol",0)]

obj.addProperty("App::PropertyBool","FlipArrows",
"Graphics",QT_TRANSLATE_NOOP("App::Property",
"Rotate the dimension arrows 180 degrees")
)

obj.addProperty("App::PropertyDistance","DimOvershoot",
"Graphics",QT_TRANSLATE_NOOP("App::Property",
"The distance the dimension line is extended past the extension lines")
).DimOvershoot = get_param("dimovershoot",0)

obj.addProperty("App::PropertyDistance","ExtLines",
"Graphics",QT_TRANSLATE_NOOP("App::Property",
"Length of the extension lines")
).ExtLines = get_param("extlines",0.3)

obj.addProperty("App::PropertyDistance","ExtOvershoot",
"Graphics",QT_TRANSLATE_NOOP("App::Property",
"Length of the extension line above the dimension line")
).ExtOvershoot = get_param("extovershoot",0)

obj.addProperty("App::PropertyBool","ShowLine",
"Graphics",QT_TRANSLATE_NOOP("App::Property",
"Shows the dimension line and arrows")
).ShowLine = True

if existing_dimension:
vobj = existing_dimension.ViewObject

obj.ScaleMultiplier = vobj.ScaleMultiplier

obj.FontName = vobj.FontName
obj.FontSize = vobj.FontSize
obj.TextSpacing = vobj.TextSpacing

obj.Decimals = vobj.Decimals
obj.ShowUnit = vobj.ShowUnit
obj.UnitOverride = vobj.UnitOverride

obj.ArrowSize = vobj.ArrowSize
obj.ArrowType = vobj.ArrowType
obj.DimOvershoot = vobj.DimOvershoot
obj.ExtLines = vobj.ExtLines
obj.ExtOvershoot = vobj.ExtOvershoot
obj.ShowLine = vobj.ShowLine

_ViewProviderDraft.__init__(self,obj)

def doubleClicked(self,vobj):
self.set_current(vobj)

def setupContextMenu(self,vobj,menu):
action1 = menu.addAction("Set current")
action1.triggered.connect(lambda f=self.set_current, arg=vobj:f(arg))
action2 = menu.addAction("Apply")
action2.triggered.connect(lambda f=self.apply_to_related_dimensions, arg=vobj:f(arg))
action3 = menu.addAction("Apply and override")
action3.triggered.connect(lambda f=self.override_related_dimensions, arg=vobj:f(arg))

def set_current(self, vobj):
"""
Sets the current dimension style as default for new created dimensions
"""
param = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft")
param.SetFloat("DraftAnnotationScale", 1 / vobj.ScaleMultiplier)

param.SetString("textfont", vobj.FontName)
param.SetFloat("textheight", vobj.FontSize)
param.SetFloat("dimspacing", vobj.TextSpacing)

param.SetInt("dimPrecision", vobj.Decimals)

param.SetFloat("arrowsize", vobj.ArrowSize)
param.SetInt("dimsymbol", ARROW_TYPES.index(vobj.ArrowType))
param.SetFloat("dimovershoot", vobj.DimOvershoot)
param.SetFloat("extlines", vobj.ExtLines)
param.SetFloat("extovershoot", vobj.ExtOvershoot)

App.Console.PrintMessage("Current dimension style set to " + str(vobj.Object.Label) + "\n")

def apply_to_related_dimensions(self, vobj):
"""
Apply the style to the related dimensions
"""
App.Console.PrintMessage("Apply is not implemented yet\n")

def override_related_dimensions(self, vobj):
"""
Apply the style to the related dimensions and override eventually changed settings
"""
App.Console.PrintMessage("Override is not implemented yet\n")
class DimensionStyle(_DraftObject):
def __init__(self, obj):
_DraftObject.__init__(self, obj, "DimensionStyle")

0 comments on commit 5e02819

Please sign in to comment.