Draft: update Draft_ApplyStyle

The command now applies all style properties from `utils.get_default_annotation_style()` and `utils.get_default_shape_style()`.

Additionally: minor improvements to gui_setstyle.py.
This commit is contained in:
Roy-043
2023-12-02 13:11:08 +01:00
parent 2010acc142
commit 6db658255f
5 changed files with 122 additions and 96 deletions

View File

@@ -435,15 +435,46 @@ def get_diffuse_color(objs):
return colors
def apply_current_style(objs):
"""Apply the current style to one or more objects.
Parameters
----------
objs: a single object or an iterable with objects.
"""
if not isinstance(objs, list):
objs = [objs]
anno_style = utils.get_default_annotation_style()
shape_style = utils.get_default_shape_style()
for obj in objs:
if not hasattr(obj, 'ViewObject'):
continue
vobj = obj.ViewObject
props = vobj.PropertiesList
style = anno_style if ("FontName" in props) else shape_style
for prop in props:
if prop in style:
if style[prop][0] == "index":
if style[prop][2] in vobj.getEnumerationsOfProperty(prop):
setattr(vobj, prop, style[prop][2])
elif style[prop][0] == "color":
setattr(vobj, prop, style[prop][1] & 0xFFFFFF00)
else:
setattr(vobj, prop, style[prop][1])
def format_object(target, origin=None):
"""Apply visual properties to an object.
This function only works if the graphical interface is available.
If construction mode is active the `origin` argument is ignored.
The `target` is then placed in the construction group and the `constr`
color is applied to its applicable color properties:
`TextColor`, `PointColor`, `LineColor`, and `ShapeColor`.
If origin is `None` and target is not an annotation, the DefaultDrawStyle
and DefaultDisplayMode preferences are applied. Else, the properties of
origin are applied to target.
If construction mode is active target is then placed in the construction
group and the `constr` color is applied to its applicable color properties:
TextColor, PointColor, LineColor, and ShapeColor.
Parameters
----------
@@ -465,34 +496,7 @@ def format_object(target, origin=None):
return
obrep = target.ViewObject
obprops = obrep.PropertiesList
if "FontName" not in obprops:
if "DrawStyle" in obprops:
dstyles = ["Solid", "Dashed", "Dotted", "Dashdot"]
obrep.DrawStyle = dstyles[utils.getParam("DefaultDrawStyle", 0)]
if "DisplayMode" in obprops:
dmodes = ["Flat Lines", "Shaded", "Wireframe", "Points"]
dm = dmodes[utils.getParam("DefaultDisplayMode", 0)]
if dm in obrep.getEnumerationsOfProperty("DisplayMode"):
obrep.DisplayMode = dm
if Gui.draftToolBar.isConstructionMode():
doc = App.ActiveDocument
col = Gui.draftToolBar.getDefaultColor("constr") + (0.0,)
grp = doc.getObject("Draft_Construction")
if not grp:
grp = doc.addObject("App::DocumentObjectGroup", "Draft_Construction")
grp.Label = utils.get_param("constructiongroupname", "Construction")
grp.addObject(target)
if "TextColor" in obprops:
obrep.TextColor = col
if "PointColor" in obprops:
obrep.PointColor = col
if "LineColor" in obprops:
obrep.LineColor = col
if "ShapeColor" in obprops:
obrep.ShapeColor = col
if hasattr(obrep, "Transparency"):
obrep.Transparency = 80
elif origin and hasattr(origin, 'ViewObject'):
if origin and hasattr(origin, 'ViewObject'):
matchrep = origin.ViewObject
for p in matchrep.PropertiesList:
if p not in ("DisplayMode", "BoundingBox",
@@ -513,6 +517,32 @@ def format_object(target, origin=None):
difcol = get_diffuse_color(origin)
if difcol:
obrep.DiffuseColor = difcol
elif "FontName" not in obprops:
# Apply 2 Draft style preferences, other style preferences are applied by Core.
if "DrawStyle" in obprops:
obrep.DrawStyle = utils.DRAW_STYLES[utils.getParam("DefaultDrawStyle", 0)]
if "DisplayMode" in obprops:
dm = utils.DISPLAY_MODES[utils.getParam("DefaultDisplayMode", 0)]
if dm in obrep.listDisplayModes():
obrep.DisplayMode = dm
if Gui.draftToolBar.isConstructionMode():
doc = App.ActiveDocument
col = Gui.draftToolBar.getDefaultColor("constr") + (0.0,)
grp = doc.getObject("Draft_Construction")
if not grp:
grp = doc.addObject("App::DocumentObjectGroup", "Draft_Construction")
grp.Label = utils.get_param("constructiongroupname", "Construction")
grp.addObject(target)
if "TextColor" in obprops:
obrep.TextColor = col
if "PointColor" in obprops:
obrep.PointColor = col
if "LineColor" in obprops:
obrep.LineColor = col
if "ShapeColor" in obprops:
obrep.ShapeColor = col
if hasattr(obrep, "Transparency"):
obrep.Transparency = 80
formatObject = format_object

View File

@@ -53,7 +53,10 @@ if App.GuiUp:
# The module is used to prevent complaints from code checkers (flake8)
True if Draft_rc else False
ARROW_TYPES = ["Dot", "Circle", "Arrow", "Tick", "Tick-2"]
DISPLAY_MODES = ["Flat Lines", "Shaded", "Wireframe", "Points"]
DRAW_STYLES = ["Solid", "Dashed", "Dotted", "Dashdot"]
arrowtypes = ARROW_TYPES
@@ -61,24 +64,43 @@ def get_default_annotation_style():
param = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft")
anno_scale = param.GetFloat("DraftAnnotationScale", 1)
scale_mult = 1 / anno_scale if anno_scale > 0 else 1
arrow_type_index = param.GetInt("dimsymbol", 0)
return {
"ArrowSize": ("float", param.GetFloat("arrowsize", 20)),
"ArrowType": ("index", param.GetInt("dimsymbol", 0)),
"ArrowSize": ("float", param.GetFloat("arrowsize", 1)),
"ArrowType": ("index", arrow_type_index, ARROW_TYPES[arrow_type_index]),
"Decimals": ("int", param.GetInt("dimPrecision", 2)),
"DimOvershoot": ("float", param.GetFloat("dimovershoot", 20)),
"ExtLines": ("float", param.GetFloat("extlines", 300)),
"ExtOvershoot": ("float", param.GetFloat("extovershoot", 20)),
"DimOvershoot": ("float", param.GetFloat("dimovershoot", 0)),
"ExtLines": ("float", param.GetFloat("extlines", -0.5)),
"ExtOvershoot": ("float", param.GetFloat("extovershoot", 2)),
"FontName": ("font", param.GetString("textfont", "Sans")),
"FontSize": ("float", param.GetFloat("textheight", 100)),
"FontSize": ("float", param.GetFloat("textheight", 3.5)),
"LineColor": ("color", param.GetUnsigned("DefaultAnnoLineColor", 255)),
"LineSpacing": ("float", param.GetFloat("LineSpacing", 1)),
"LineWidth": ("int", param.GetInt("DefaultAnnoLineWidth", 1)),
"LineWidth": ("int", param.GetInt("DefaultAnnoLineWidth", 2)),
"ScaleMultiplier": ("float", scale_mult),
"ShowLine": ("bool", param.GetBool("DimShowLine", True)),
"ShowUnit": ("bool", param.GetBool("showUnit", True)),
"TextColor": ("color", param.GetUnsigned("DefaultTextColor", 255)),
"TextSpacing": ("float", param.GetFloat("dimspacing", 20)),
"UnitOverride": ("str", param.GetString("overrideUnit", "")),
"TextSpacing": ("float", param.GetFloat("dimspacing", 1)),
"UnitOverride": ("str", param.GetString("overrideUnit", ""))
}
def get_default_shape_style():
# Uses the same format as get_default_annotation_style().
param_draft = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft")
param_view = App.ParamGet("User parameter:BaseApp/Preferences/View")
display_mode_index = param_draft.GetInt("DefaultDisplayMode", 0)
draw_style_index = param_draft.GetInt("DefaultDrawStyle", 0)
return {
"DisplayMode": ("index", display_mode_index, DISPLAY_MODES[display_mode_index]),
"DrawStyle": ("index", draw_style_index, DRAW_STYLES[draw_style_index]),
"LineColor": ("color", param_view.GetUnsigned("DefaultShapeLineColor", 255)),
"LineWidth": ("int", param_view.GetInt("DefaultShapeLineWidth", 2)),
"PointColor": ("color", param_view.GetUnsigned("DefaultShapeVertexColor", 255)),
"PointSize": ("int", param_view.GetInt("DefaultShapePointSize", 2)),
"ShapeColor": ("color", param_view.GetUnsigned("DefaultShapeColor", 3435973887)),
"Transparency": ("int", param_view.GetInt("DefaultShapeTransparency", 0))
}