import math
from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCAD as App
import DraftVecUtils
import DraftGeomUtils
import WorkingPlane
from draftobjects.draft_annotation import DraftAnnotation
from draftutils import gui_utils
from draftutils import utils
from draftutils.messages import _log
class DimensionBase(DraftAnnotation):
def set_properties(self, obj):
properties = obj.PropertiesList
if "Normal" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property", "The normal direction of the text " "of the dimension"
)
obj.addProperty("App::PropertyVector", "Normal", "Dimension", _tip, locked=True)
obj.Normal = App.Vector(0, 0, 1)
if "Support" not in properties:
_tip = QT_TRANSLATE_NOOP("App::Property", "The object measured by this dimension")
obj.addProperty("App::PropertyLink", "Support", "Dimension", _tip, locked=True)
obj.Support = None
if "LinkedGeometry" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property",
"The object, and specific subelements "
"of it,\n"
"that this dimension "
"is measuring.\n"
"\n"
"There are various possibilities:\n"
"- An object, and one of its edges.\n"
"- An object, and two of its vertices.\n"
"- An arc object, and its edge.",
)
obj.addProperty(
"App::PropertyLinkSubList", "LinkedGeometry", "Dimension", _tip, locked=True
)
obj.LinkedGeometry = []
if "Dimline" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property",
"A point through which the dimension "
"line, or an extrapolation of it, "
"will pass.\n"
"\n"
"- For linear dimensions, this property "
"controls how close the dimension line\n"
"is to the measured object.\n"
"- For radial dimensions, this controls "
"the direction of the dimension line\n"
"that displays the measured radius or "
"diameter.\n"
"- For angular dimensions, "
"this controls the radius of the "
"dimension arc\n"
"that displays the measured angle.",
)
obj.addProperty(
"App::PropertyVectorDistance", "Dimline", "Dimension", _tip, locked=True
)
obj.Dimline = App.Vector(0, 1, 0)
def update_properties_0v21(self, obj, vobj):
vobj.Proxy.set_text_properties(vobj, vobj.PropertiesList)
vobj.TextColor = vobj.LineColor
_log("v0.21, " + obj.Name + ", added view property 'TextColor'")
_log("v0.21, " + obj.Name + ", renamed 'DisplayMode' options to 'World/Screen'")
class LinearDimension(DimensionBase):
def __init__(self, obj):
obj.Proxy = self
self.Type = "LinearDimension"
self.set_properties(obj)
def set_properties(self, obj):
super().set_properties(obj)
properties = obj.PropertiesList
if "Start" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property",
"Starting point of the dimension line.\n"
"\n"
"If it is a radius dimension it will be "
"the center of the arc.\n"
"If it is a diameter dimension "
"it will be a point that lies "
"on the arc.",
)
obj.addProperty(
"App::PropertyVectorDistance", "Start", "Linear/radial dimension", _tip, locked=True
)
obj.Start = App.Vector(0, 0, 0)
if "End" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property",
"Ending point of the dimension line.\n"
"\n"
"If it is a radius or diameter "
"dimension\n"
"it will be a point that lies "
"on the arc.",
)
obj.addProperty(
"App::PropertyVectorDistance", "End", "Linear/radial dimension", _tip, locked=True
)
obj.End = App.Vector(1, 0, 0)
if "Direction" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property",
"The direction of the dimension line.\n"
"If this remains '(0,0,0)', "
"the direction will be calculated "
"automatically.",
)
obj.addProperty(
"App::PropertyVector", "Direction", "Linear/radial dimension", _tip, locked=True
)
if "Distance" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property",
"The value of the measurement.\n"
"\n"
"This property is read-only because "
"the value is calculated\n"
"from the 'Start' and 'End' properties.\n"
"\n"
"If the 'Linked Geometry' "
"is an arc or circle, this 'Distance'\n"
"is the radius or diameter, depending "
"on the 'Diameter' property.",
)
obj.addProperty(
"App::PropertyLength", "Distance", "Linear/radial dimension", _tip, locked=True
)
obj.Distance = 0
if "Diameter" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property",
"When measuring circular arcs, "
"it determines whether to display\n"
"the radius or the diameter value",
)
obj.addProperty("App::PropertyBool", "Diameter", "Radial dimension", _tip, locked=True)
obj.Diameter = False
def onDocumentRestored(self, obj):
super().onDocumentRestored(obj)
gui_utils.restore_view_object(
obj, vp_module="view_dimension", vp_class="ViewProviderLinearDimension"
)
vobj = getattr(obj, "ViewObject", None)
if vobj is None:
return
if not hasattr(vobj, "TextColor"):
self.update_properties_0v21(obj, vobj)
def loads(self, state):
self.Type = "LinearDimension"
def onChanged(self, obj, prop):
if hasattr(obj, "Distance"):
obj.setPropertyStatus("Distance", "ReadOnly")
if hasattr(obj, "Support"):
obj.setPropertyStatus("Support", "Hidden")
def transform(self, obj, pla):
obj.Start = pla.multVec(obj.Start)
obj.End = pla.multVec(obj.End)
obj.Dimline = pla.multVec(obj.Dimline)
obj.Normal = pla.Rotation.multVec(obj.Normal)
obj.Direction = pla.Rotation.multVec(obj.Direction)
def execute(self, obj):
if obj.LinkedGeometry:
if len(obj.LinkedGeometry) == 1:
linked_obj = obj.LinkedGeometry[0][0]
sub_list = obj.LinkedGeometry[0][1]
if len(sub_list) == 1:
subelement = sub_list[0]
(obj.Start, obj.End) = measure_one_obj_edge(
linked_obj, subelement, obj.Dimline, obj.Diameter
)
elif len(sub_list) == 2:
(obj.Start, obj.End) = measure_one_obj_vertices(linked_obj, sub_list)
elif len(obj.LinkedGeometry) == 2:
(obj.Start, obj.End) = measure_two_objects(
obj.LinkedGeometry[0], obj.LinkedGeometry[1]
)
net_length = (obj.Start.sub(obj.End)).Length
rounded_1 = round(obj.Distance.Value, utils.precision())
rounded_2 = round(net_length, utils.precision())
if rounded_1 != rounded_2:
obj.Distance = net_length
if App.GuiUp and obj.ViewObject:
obj.ViewObject.update()
def measure_one_obj_edge(obj, subelement, dim_point, diameter=False):
start = App.Vector()
end = App.Vector()
if "Edge" in subelement:
n = int(subelement[4:]) - 1
edge = obj.Shape.Edges[n]
if DraftGeomUtils.geomType(edge) == "Line":
start = edge.Vertexes[0].Point
end = edge.Vertexes[-1].Point
elif DraftGeomUtils.geomType(edge) == "Circle":
center = edge.Curve.Center
radius = edge.Curve.Radius
axis = edge.Curve.Axis
dim_line = dim_point.sub(center)
ray = dim_line.projectToPlane(App.Vector(0, 0, 0), axis)
if ray.Length == 0:
ray = axis.cross(App.Vector(1, 0, 0))
if ray.Length == 0:
ray = axis.cross(App.Vector(0, 1, 0))
ray = DraftVecUtils.scaleTo(ray, radius)
if diameter:
start = center.add(ray.negative())
end = center.add(ray)
else:
start = center
end = center.add(ray)
return start, end
def measure_one_obj_vertices(obj, subelements):
start = App.Vector()
end = App.Vector()
subelement1 = subelements[0]
subelement2 = subelements[1]
if "Vertex" in subelement1 and "Vertex" in subelement2:
n1 = int(subelement1[6:]) - 1
n2 = int(subelement2[6:]) - 1
start = obj.Shape.Vertexes[n1].Point
end = obj.Shape.Vertexes[n2].Point
return start, end
def measure_two_objects(link_sub_1, link_sub_2):
start = App.Vector()
end = App.Vector()
obj1 = link_sub_1[0]
lsub1 = link_sub_1[1]
obj2 = link_sub_2[0]
lsub2 = link_sub_2[1]
if lsub1 and lsub2:
subelement1 = lsub1[0]
subelement2 = lsub2[0]
if "Vertex" in subelement1 and "Vertex" in subelement2:
n1 = int(subelement1[6:]) - 1
n2 = int(subelement2[6:]) - 1
start = obj1.Shape.Vertexes[n1].Point
end = obj2.Shape.Vertexes[n2].Point
return start, end
_Dimension = LinearDimension
class AngularDimension(DimensionBase):
def __init__(self, obj):
obj.Proxy = self
self.Type = "AngularDimension"
self.set_properties(obj)
def set_properties(self, obj):
super().set_properties(obj)
properties = obj.PropertiesList
if "FirstAngle" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property",
"Starting angle of the dimension line "
"(circular arc).\n"
"The arc is drawn counter-clockwise.",
)
obj.addProperty(
"App::PropertyAngle", "FirstAngle", "Angular dimension", _tip, locked=True
)
obj.FirstAngle = 0
if "LastAngle" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property",
"Ending angle of the dimension line "
"(circular arc).\n"
"The arc is drawn counter-clockwise.",
)
obj.addProperty(
"App::PropertyAngle", "LastAngle", "Angular dimension", _tip, locked=True
)
obj.LastAngle = 90
if "Center" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property",
"The center point of the dimension "
"line, which is a circular arc.\n"
"\n"
"This is normally the point where two "
"line segments, or their extensions\n"
"intersect, resulting in the "
"measured 'Angle' between them.",
)
obj.addProperty(
"App::PropertyVectorDistance", "Center", "Angular dimension", _tip, locked=True
)
obj.Center = App.Vector(0, 0, 0)
if "Angle" not in properties:
_tip = QT_TRANSLATE_NOOP(
"App::Property",
"The value of the measurement.\n"
"\n"
"This property is read-only because "
"the value is calculated from\n"
"the 'First Angle' and "
"'Last Angle' properties.",
)
obj.addProperty("App::PropertyAngle", "Angle", "Angular dimension", _tip, locked=True)
obj.Angle = 0
def onDocumentRestored(self, obj):
super().onDocumentRestored(obj)
gui_utils.restore_view_object(
obj, vp_module="view_dimension", vp_class="ViewProviderAngularDimension"
)
vobj = getattr(obj, "ViewObject", None)
if vobj is None:
return
if not hasattr(vobj, "TextColor"):
self.update_properties_0v21(obj, vobj)
def loads(self, state):
self.Type = "AngularDimension"
def transform(self, obj, pla):
import Part
new_normal = pla.Rotation.multVec(obj.Normal)
c_old = Part.makeCircle(1, App.Vector(), obj.Normal)
c_tra = c_old.transformShape((pla * c_old.Placement).Rotation.Matrix)
c_new = Part.makeCircle(1, App.Vector(), new_normal)
delta_angle = math.degrees(c_new.Curve.parameter(c_tra.firstVertex().Point))
first_angle = (obj.FirstAngle.Value + delta_angle) % 360
last_angle = (obj.LastAngle.Value + delta_angle) % 360
obj.Center = pla.multVec(obj.Center)
obj.Dimline = pla.multVec(obj.Dimline)
obj.Normal = new_normal
obj.FirstAngle = first_angle
obj.LastAngle = last_angle
def execute(self, obj):
if App.GuiUp and obj.ViewObject:
obj.ViewObject.update()
def onChanged(self, obj, prop):
if hasattr(obj, "Angle"):
obj.setPropertyStatus("Angle", "ReadOnly")
if hasattr(obj, "Normal"):
obj.setPropertyStatus("Normal", "Hidden")
if hasattr(obj, "Support"):
obj.setPropertyStatus("Support", "Hidden")
if hasattr(obj, "LinkedGeometry"):
obj.setPropertyStatus("LinkedGeometry", "Hidden")
def measure_two_obj_angles(link_sub_1, link_sub_2):
start = 0
end = 0
obj1 = link_sub_1[0]
lsub1 = link_sub_1[1]
obj2 = link_sub_2[0]
lsub2 = link_sub_2[1]
if lsub1 and lsub2:
subelement1 = lsub1[0]
subelement2 = lsub2[0]
if "Edge" in subelement1 and "Edge" in subelement2:
n1 = int(subelement1[4:]) - 1
n2 = int(subelement2[4:]) - 1
start = obj1.Shape.Edges[n1].Curve.Direction
end = obj2.Shape.Edges[n2].Curve.Direction
wp = WorkingPlane.get_working_plane(update=False)
start_r = DraftVecUtils.angle(start, wp.u)
end_r = DraftVecUtils.angle(end, wp.u)
start = math.degrees(start_r)
end = math.degrees(end_r)
if start < 0:
start = abs(start)
if end < 0:
end = abs(end)
return start, end
_AngularDimension = AngularDimension