Skip to content

Commit

Permalink
Draft: clean up the utils module a bit, PEP8, style
Browse files Browse the repository at this point in the history
  • Loading branch information
vocx-fc authored and yorikvanhavre committed Jun 2, 2020
1 parent 6665799 commit fea8294
Showing 1 changed file with 62 additions and 45 deletions.
107 changes: 62 additions & 45 deletions src/Mod/Draft/draftutils/utils.py
Expand Up @@ -36,12 +36,17 @@
import os
from PySide import QtCore

import FreeCAD
import FreeCAD as App
import Draft_rc
from draftutils.messages import _msg, _log

from draftutils.messages import _msg, _wrn, _err, _log
from draftutils.translate import _tr

App = FreeCAD
# TODO: move the functions that require the graphical interface
# This module should not import any graphical commands; those should be
# in gui_utils
if App.GuiUp:
import FreeCADGui as Gui

# The module is used to prevent complaints from code checkers (flake8)
True if Draft_rc else False
Expand Down Expand Up @@ -113,9 +118,8 @@ def type_check(args_and_types, name="?"):
"""
for v, t in args_and_types:
if not isinstance(v, t):
w = "typecheck[" + str(name) + "]: "
w += str(v) + " is not " + str(t) + "\n"
FreeCAD.Console.PrintWarning(w)
w = "typecheck[{}]: '{}' is not {}".format(name, v, t)
_wrn(w)
raise TypeError("Draft." + str(name))


Expand Down Expand Up @@ -158,7 +162,7 @@ def get_param_type(param):
"hideSnapBar", "alwaysShowGrid", "renderPolylineWidth",
"showPlaneTracker", "UsePartPrimitives",
"DiscretizeEllipses", "showUnit",
"Draft_array_fuse", "Draft_array_Link","gridBorder"):
"Draft_array_fuse", "Draft_array_Link", "gridBorder"):
return "bool"
elif param in ("color", "constructioncolor",
"snapcolor", "gridColor"):
Expand Down Expand Up @@ -204,8 +208,8 @@ def get_param(param, default=None):
draft_params = "User parameter:BaseApp/Preferences/Mod/Draft"
view_params = "User parameter:BaseApp/Preferences/View"

p = FreeCAD.ParamGet(draft_params)
v = FreeCAD.ParamGet(view_params)
p = App.ParamGet(draft_params)
v = App.ParamGet(view_params)
t = getParamType(param)
# print("getting param ",param, " of type ",t, " default: ",str(default))
if t == "int":
Expand Down Expand Up @@ -267,8 +271,8 @@ def set_param(param, value):
draft_params = "User parameter:BaseApp/Preferences/Mod/Draft"
view_params = "User parameter:BaseApp/Preferences/View"

p = FreeCAD.ParamGet(draft_params)
v = FreeCAD.ParamGet(view_params)
p = App.ParamGet(draft_params)
v = App.ParamGet(view_params)
t = getParamType(param)

if t == "int":
Expand Down Expand Up @@ -525,21 +529,36 @@ def is_clone(obj, objtype, recursive=False):


def get_clone_base(obj, strict=False):
"""get_clone_base(obj, [strict])
Returns the object cloned by this object, if any, or this object if
it is no clone.
"""Return the object cloned by this object, if any.
Parameters
----------
obj :
TODO: describe
obj: App::DocumentObject
Any type of object.
strict: bool, optional
It defaults to `False`.
If it is `True`, and this object is not a clone,
this function will return `False`.
strict : bool (default = False)
If strict is True, if this object is not a clone,
this function returns False
Returns
-------
App::DocumentObject
It `obj` is a `Draft Clone`, it will return the first object
that is in its `Objects` property.
If `obj` has a `CloneOf` property, it will search iteratively
inside the object pointed to by this property.
obj
If `obj` is not a `Draft Clone`, nor it has a `CloneOf` property,
it will return the same `obj`, as long as `strict` is `False`.
False
It will return `False` if `obj` is not a clone,
and `strict` is `True`.
"""
if hasattr(obj,"CloneOf"):
if hasattr(obj, "CloneOf"):
if obj.CloneOf:
return get_clone_base(obj.CloneOf)
if get_type(obj) == "Clone":
Expand All @@ -566,7 +585,7 @@ def get_group_names():
Otherwise, return an empty list.
"""
glist = []
doc = FreeCAD.ActiveDocument
doc = App.ActiveDocument
for obj in doc.Objects:
if (obj.isDerivedFrom("App::DocumentObjectGroup")
or getType(obj) in ("Floor", "Building", "Site")):
Expand All @@ -588,7 +607,7 @@ def ungroup(obj):
Any type of scripted object.
"""
for name in getGroupNames():
group = FreeCAD.ActiveDocument.getObject(name)
group = App.ActiveDocument.getObject(name)
if obj in group.Group:
# The list of objects cannot be modified directly,
# so a new list is created, this new list is modified,
Expand Down Expand Up @@ -647,8 +666,8 @@ def shapify(obj):
else:
name = getRealName(obj.Name)

FreeCAD.ActiveDocument.removeObject(obj.Name)
newobj = FreeCAD.ActiveDocument.addObject("Part::Feature", name)
App.ActiveDocument.removeObject(obj.Name)
newobj = App.ActiveDocument.addObject("Part::Feature", name)
newobj.Shape = shape

return newobj
Expand Down Expand Up @@ -869,11 +888,11 @@ def compare_objects(obj1, obj2):
def load_svg_patterns():
"""Load the default Draft SVG patterns and user defined patterns.
The SVG patterns are added as a dictionary to the `FreeCAD.svgpatterns`
The SVG patterns are added as a dictionary to the `App.svgpatterns`
attribute.
"""
import importSVG
FreeCAD.svgpatterns = {}
App.svgpatterns = {}

# Getting default patterns in the resource file
patfiles = QtCore.QDir(":/patterns").entryList()
Expand All @@ -885,7 +904,7 @@ def load_svg_patterns():
if p:
for k in p:
p[k] = [p[k], file]
FreeCAD.svgpatterns.update(p)
App.svgpatterns.update(p)

# Get patterns in a user defined file
altpat = getParam("patternFile", "")
Expand All @@ -897,7 +916,7 @@ def load_svg_patterns():
if p:
for k in p:
p[k] = [p[k], file]
FreeCAD.svgpatterns.update(p)
App.svgpatterns.update(p)


loadSvgPatterns = load_svg_patterns
Expand All @@ -909,27 +928,25 @@ def svg_patterns():
Returns
-------
dict
Returns `FreeCAD.svgpatterns` if it exists.
Returns `App.svgpatterns` if it exists.
Otherwise it calls `load_svg_patterns` to create it
before returning it.
"""
if hasattr(FreeCAD, "svgpatterns"):
return FreeCAD.svgpatterns
if hasattr(App, "svgpatterns"):
return App.svgpatterns
else:
loadSvgPatterns()
if hasattr(FreeCAD, "svgpatterns"):
return FreeCAD.svgpatterns
if hasattr(App, "svgpatterns"):
return App.svgpatterns
return {}


svgpatterns = svg_patterns


def get_rgb(color, testbw=True):
"""getRGB(color,[testbw])
Return a rgb value #000000 from a freecad color
"""Return an RRGGBB value #000000 from a FreeCAD color.
Parameters
----------
testwb : bool (default = True)
Expand All @@ -941,8 +958,8 @@ def get_rgb(color, testbw=True):
col = "#"+r+g+b
if testbw:
if col == "#ffffff":
#print(getParam('SvgLinesBlack'))
if getParam('SvgLinesBlack',True):
# print(getParam('SvgLinesBlack'))
if getParam('SvgLinesBlack', True):
col = "#000000"
return col

Expand Down Expand Up @@ -1000,9 +1017,9 @@ def getProj(vec):
import Drawing
import DraftVecUtils
if not direction:
direction = FreeCAD.Vector(0,0,-1)
direction = App.Vector(0,0,-1)
if DraftVecUtils.isNull(direction):
direction = FreeCAD.Vector(0,0,-1)
direction = App.Vector(0,0,-1)
try:
d = Drawing.projectToDXF(obj.Shape,direction)
except:
Expand Down Expand Up @@ -1089,7 +1106,7 @@ def filter_objects_for_modifiers(objects, isCopied=False):
warningMessage = _tr("%s shares a base with %d other objects. Please check if you want to modify this.") % (obj.Name,len(parents) - 1)
App.Console.PrintError(warningMessage)
if App.GuiUp:
FreeCADGui.getMainWindow().showMessage(warningMessage, 0)
Gui.getMainWindow().showMessage(warningMessage, 0)
filteredObjects.append(obj.Base)
elif hasattr(obj,"Placement") and obj.getEditorMode("Placement") == ["ReadOnly"] and not isCopied:
App.Console.PrintError(_tr("%s cannot be modified because its placement is readonly.") % obj.Name)
Expand Down Expand Up @@ -1118,7 +1135,7 @@ def convert_draft_texts(textslist=[]):
if not isinstance(textslist,list):
textslist = [textslist]
if not textslist:
for o in FreeCAD.ActiveDocument.Objects:
for o in App.ActiveDocument.Objects:
if o.TypeId == "App::Annotation":
textslist.append(o)
todelete = []
Expand All @@ -1135,7 +1152,7 @@ def convert_draft_texts(textslist=[]):
g.append(obj)
p.Group = g
for n in todelete:
FreeCAD.ActiveDocument.removeObject(n)
App.ActiveDocument.removeObject(n)


convertDraftTexts = convert_draft_texts
Expand Down

0 comments on commit fea8294

Please sign in to comment.