Draft: Improve logic of Draft_Label texts

The proposed code fixes the following issues:
1. There is no check if the target object itself has a property that matches the label type.
2. If a subelement is selected that does not have the label type property, the value from the main object is displayed instead.
3. The default text is an empty string.

Example:

Currently If you select a vertex of a wire, and select "Length" as the label type, you will get the length of the wire. With the new code the text will then be "Length not available for (sub)object".

Forum topic (issue #1):
https://forum.freecad.org/viewtopic.php?t=86222
This commit is contained in:
Roy-043
2024-03-20 18:29:01 +01:00
parent 41de357e8f
commit fa49577838

View File

@@ -300,9 +300,6 @@ class Label(DraftAnnotation):
# will be overwritten # will be overwritten
pass pass
# Reset the text, only change it depending on the options
obj.Text = ""
if obj.LabelType == "Custom": if obj.LabelType == "Custom":
if obj.CustomText: if obj.CustomText:
obj.Text = obj.CustomText obj.Text = obj.CustomText
@@ -314,9 +311,10 @@ class Label(DraftAnnotation):
# The sublist may be empty so we test it first # The sublist may be empty so we test it first
subelement = sub_list[0] if sub_list else None subelement = sub_list[0] if sub_list else None
obj.Text = return_info(target, typ, subelement)
text_list = return_info(target, typ, subelement) else:
obj.Text = text_list obj.Text = [translate("draft", "No Target")]
# Alias for compatibility with v0.18 and earlier # Alias for compatibility with v0.18 and earlier
@@ -362,54 +360,43 @@ def return_info(target, typ, subelement=None):
if typ == "Name": if typ == "Name":
return _get_name(target) return _get_name(target)
elif typ == "Label": if typ == "Label":
return _get_label(target) return _get_label(target)
elif typ == "Tag" and hasattr(target, "Tag"): if typ == "Tag":
return _get_tag(target) return _get_tag(target)
elif (typ == "Material" if typ == "Material":
and hasattr(target, "Material")
and hasattr(target.Material, "Label")):
return _get_material(target) return _get_material(target)
elif (typ == "Label + Material" if typ == "Label + Material":
and hasattr(target, "Material")
and hasattr(target.Material, "Label")):
return _get_label(target) + _get_material(target) return _get_label(target) + _get_material(target)
elif typ == "Position": if typ == "Position":
return _get_position(target, subelement) return _get_position(target, subelement)
elif typ == "Label + Position": if typ == "Label + Position":
return _get_label(target) + _get_position(target, subelement) return _get_label(target) + _get_position(target, subelement)
elif typ == "Length" and hasattr(target, 'Shape'): if typ == "Length":
return _get_length(target, subelement) return _get_length(target, subelement)
elif typ == "Label + Length" and hasattr(target, 'Shape'): if typ == "Label + Length":
return _get_label(target) + _get_length(target, subelement) return _get_label(target) + _get_length(target, subelement)
elif typ == "Area" and hasattr(target, 'Shape'): if typ == "Area":
return _get_area(target, subelement) return _get_area(target, subelement)
elif typ == "Label + Area" and hasattr(target, 'Shape'): if typ == "Label + Area":
return _get_label(target) + _get_area(target, subelement) return _get_label(target) + _get_area(target, subelement)
elif (typ == "Volume" if typ == "Volume":
and hasattr(target, 'Shape') return _get_volume(target, subelement)
and hasattr(target.Shape, "Volume")):
return _get_volume(target)
elif (typ == "Label + Volume" if typ == "Label + Volume":
and hasattr(target, 'Shape') return _get_label(target) + _get_volume(target, subelement)
and hasattr(target.Shape, "Volume")):
return _get_label(target) + _get_volume(target)
# If the type is not the correct one, or the subelement doesn't have return [translate("draft", "Invalid label type")]
# the required `Shape` and information underneath, it will return
# an empty list
return [""]
def _get_name(target): def _get_name(target):
@@ -421,54 +408,72 @@ def _get_label(target):
def _get_tag(target): def _get_tag(target):
if hasattr(target, "Tag"):
return [target.Tag] return [target.Tag]
else:
return [translate("draft", "Tag not available for object")]
def _get_material(target): def _get_material(target):
if (hasattr(target, "Material") and hasattr(target.Material, "Label")):
return [target.Material.Label] return [target.Material.Label]
else:
return [translate("draft", "Material not available for object")]
def _get_position(target, subelement): def _get_position(target, subelement):
p = target.Placement.Base point = None
if subelement is not None:
# Position of the vertex if it is given as subelement if "Vertex" in subelement:
if subelement and "Vertex" in subelement: point = target.Shape.Vertexes[int(subelement[6:]) - 1].Point
p = target.Shape.Vertexes[int(subelement[6:]) - 1].Point else:
point = target.Placement.Base
text_list = [U.Quantity(x, U.Length).UserString for x in tuple(p)] if point is None:
return text_list return [translate("draft", "Position not available for (sub)object")]
return [U.Quantity(x, U.Length).UserString for x in tuple(point)]
def _get_length(target, subelement): def _get_length(target, subelement):
text_list = ["No length"] length = None
if hasattr(target.Shape, "Length"): if subelement is not None:
text_list = [U.Quantity(target.Shape.Length, U.Length).UserString] if "Edge" in subelement:
length = target.Shape.Edges[int(subelement[4:]) - 1].Length
# Length of the edge if it is given as subelement elif "Face" in subelement:
if subelement and "Edge" in subelement: length = target.Shape.Faces[int(subelement[4:]) - 1].Length
edge = target.Shape.Edges[int(subelement[4:]) - 1] elif hasattr(target, "Length"):
text_list = [U.Quantity(edge.Length, U.Length).UserString] length = target.Length
elif hasattr(target, "Shape") and hasattr(target.Shape, "Length"):
return text_list length = target.Shape.Length
if length is None:
return [translate("draft", "Length not available for (sub)object")]
return [U.Quantity(length, U.Length).UserString]
def _get_area(target, subelement): def _get_area(target, subelement):
text_list = ["No area"] area = None
if hasattr(target.Shape, "Area"): if subelement is not None:
area = U.Quantity(target.Shape.Area, U.Area).UserString if "Face" in subelement:
text_list = [area.replace("^2", "²")] area = target.Shape.Faces[int(subelement[4:]) - 1].Area
elif hasattr(target, "Area"):
# Area of the face if it is given as subelement area = target.Area
if subelement and "Face" in subelement: elif hasattr(target, "Shape") and hasattr(target.Shape, "Area"):
face = target.Shape.Faces[int(subelement[4:]) - 1] area = target.Shape.Area
text_list = [U.Quantity(face.Area, U.Area).UserString] if area is None:
return [translate("draft", "Area not available for (sub)object")]
return text_list return [U.Quantity(area, U.Area).UserString.replace("^2", "²")]
def _get_volume(target): def _get_volume(target, subelement):
volume = U.Quantity(target.Shape.Volume, U.Volume).UserString volume = None
return [volume.replace("^3", "³")] if subelement is not None:
pass
elif hasattr(target, "Volume"):
volume = target.Volume
elif hasattr(target, "Shape") and hasattr(target.Shape, "Volume"):
volume = target.Shape.Volume
if volume is None:
return [translate("draft", "Volume not available for (sub)object")]
return [U.Quantity(volume, U.Volume).UserString.replace("^3", "³")]
## @} ## @}