From 0c0c3d0e86971b414fee8b0ccaa5aec7f4484f8c Mon Sep 17 00:00:00 2001 From: edi271 Date: Thu, 7 Dec 2023 11:56:12 +0100 Subject: [PATCH] [TD] Improve AxoLengthDimension tool --- .../CommandAxoLengthDimension.py | 145 +++++++++++------- 1 file changed, 87 insertions(+), 58 deletions(-) diff --git a/src/Mod/TechDraw/TechDrawTools/CommandAxoLengthDimension.py b/src/Mod/TechDraw/TechDrawTools/CommandAxoLengthDimension.py index da40e4d603..c9345d2eb6 100644 --- a/src/Mod/TechDraw/TechDrawTools/CommandAxoLengthDimension.py +++ b/src/Mod/TechDraw/TechDrawTools/CommandAxoLengthDimension.py @@ -18,20 +18,24 @@ # * USA * # * * # *************************************************************************** -"""Provides the TechDraw AxoLengthDimension GuiCommand.""" +""" +Provides the TechDraw AxoLengthDimension GuiCommand. +00.01 2023/02/01 Basic version +00.02 2023/12/07 Calculate real 3D values if parallel to coordinate axis +""" __title__ = "TechDrawTools.CommandAxoLengthDimension" __author__ = "edi" __url__ = "https://www.freecad.org" -__version__ = "00.01" -__date__ = "2023/02/01" +__version__ = "00.02" +__date__ = "2023/12/07" from PySide.QtCore import QT_TRANSLATE_NOOP import FreeCAD as App import FreeCADGui as Gui -import TechDrawTools +import TechDrawTools.TDToolsUtil as Utils import TechDraw from math import degrees @@ -56,69 +60,94 @@ class CommandAxoLengthDimension: def Activated(self): """Run the following code when the command is activated (button press).""" - if self.selectionTest(): - (edges,vertexes) = self.selectionTest() - view = Gui.Selection.getSelection()[0] - StartPt, EndPt = edges[1].Vertexes[0].Point, edges[1].Vertexes[1].Point - extLineVec = EndPt.sub(StartPt) - StartPt, EndPt = edges[0].Vertexes[0].Point, edges[0].Vertexes[1].Point - dimLineVec = EndPt.sub(StartPt) - xAxis = App.Vector(1,0,0) - extAngle = degrees(extLineVec.getAngle(xAxis)) - lineAngle = degrees(dimLineVec.getAngle(xAxis)) - if extLineVec.y < 0.0: - extAngle = 180-extAngle - if dimLineVec.y < 0.0: - lineAngle = 180-lineAngle - if abs(extAngle-lineAngle)>0.1: - distanceDim=TechDraw.makeDistanceDim(view,'Distance',vertexes[0].Point,vertexes[1].Point) - distanceDim.AngleOverride = True - distanceDim.LineAngle = lineAngle - distanceDim.ExtensionAngle = extAngle - distanceDim.X = (vertexes[0].Point.x+vertexes[1].Point.x)*view.Scale/2 - distanceDim.Y = (vertexes[0].Point.y+vertexes[1].Point.y)*view.Scale/2 - distanceDim.recompute() - view.requestPaint() - Gui.Selection.clearSelection() + vertexes = [] + edges = [] + if Utils.getSelEdges(2): + edges = Utils.getSelEdges(2) + vertexes = Utils.getSelVertexes(0) + + if len(vertexes)<2: + vertexes.append(edges[0].Vertexes[0]) + vertexes.append(edges[0].Vertexes[1]) + + view = Utils.getSelView() + + StartPt, EndPt = edges[1].Vertexes[0].Point, edges[1].Vertexes[1].Point + extLineVec = EndPt.sub(StartPt) + StartPt, EndPt = edges[0].Vertexes[0].Point, edges[0].Vertexes[1].Point + dimLineVec = EndPt.sub(StartPt) + xAxis = App.Vector(1,0,0) + extAngle = degrees(extLineVec.getAngle(xAxis)) + lineAngle = degrees(dimLineVec.getAngle(xAxis)) + + if extLineVec.y < 0.0: + extAngle = 180-extAngle + if dimLineVec.y < 0.0: + lineAngle = 180-lineAngle + if abs(extAngle-lineAngle)>0.1: + distanceDim=TechDraw.makeDistanceDim(view,'Distance',vertexes[0].Point,vertexes[1].Point) + distanceDim.AngleOverride = True + distanceDim.LineAngle = lineAngle + distanceDim.ExtensionAngle = extAngle + distanceDim.X = (vertexes[0].Point.x+vertexes[1].Point.x)/2 + distanceDim.Y = (vertexes[0].Point.y+vertexes[1].Point.y)/2 + distanceDim.recompute() + + (px,py,pz) = Utils.getCoordinateVectors(view) + arrowTips = distanceDim.getArrowPositions() + value2D = (arrowTips[1].sub(arrowTips[0])).Length + value3D = 1.0 + if self._checkParallel(px,dimLineVec): + value3D = value2D/px.Length + elif self._checkParallel(py,dimLineVec): + value3D = value2D/py.Length + elif self._checkParallel(pz,dimLineVec): + value3D = value2D/pz.Length + if value3D != 1.0: + fomatted3DValue = self._formatValueToSpec(value3D,distanceDim.FormatSpec) + distanceDim.Arbitrary = True + distanceDim.Label = distanceDim.Label.replace('Dimension','Dimension3D') + distanceDim.FormatSpec = fomatted3DValue + + distanceDim.recompute() + view.requestPaint() + Gui.Selection.clearSelection() def IsActive(self): """Return True when the command should be active or False when it should be disabled (greyed).""" if App.ActiveDocument: - return TechDrawTools.TDToolsUtil.havePage() and TechDrawTools.TDToolsUtil.haveView() + return Utils.havePage() and Utils.haveView() else: return False - def selectionTest(self): - '''test correct selection''' - if not Gui.Selection.getSelection(): - return False - view = Gui.Selection.getSelection()[0] - - if not Gui.Selection.getSelectionEx(): - return False - objectList = Gui.Selection.getSelectionEx()[0].SubElementNames - - if not len(objectList)>=2: - return False - - edges = [] - vertexes = [] - for objectString in objectList: - if objectString[0:4] == 'Edge': - edges.append(view.getEdgeBySelection(objectString)) - elif objectString[0:6] == 'Vertex': - vertexes.append(view.getVertexBySelection(objectString)) - if not len(edges) >= 2: - return False - - if len(vertexes)<2: - vertexes = [] - vertexes.append(edges[0].Vertexes[0]) - vertexes.append(edges[0].Vertexes[1]) - return(edges,vertexes) + def _checkParallel(self,v1,v2): + '''Check if two vectors are parallel''' + dot = abs(v1.dot(v2)) + mag = v1.Length*v2.Length + return (abs(dot-mag)<0.1) + def _formatValueToSpec(self, value, formatSpec): + '''Calculate value using "%.nf" or "%.nw" formatSpec''' + formatSpec = '{'+formatSpec+'}' + formatSpec = formatSpec.replace('%',':') + if formatSpec.find('w') > 0: + formatSpec = formatSpec.replace('w','f') + numDig = formatSpec.find(":.") + if numDig != -1: + numDig = numDig+2 + charList = list(formatSpec) + digits = int(charList[numDig]) + value = round(value,digits) + strValue = formatSpec.format(value) + strValueList = list(strValue) + while strValueList[-1] == '0': + strValueList.pop() + if strValueList[-1] == '.': + strValueList.pop() + return ''.join(strValueList) + else: + return formatSpec.format(value) # # The command must be "registered" with a unique name by calling its class. Gui.addCommand('TechDraw_AxoLengthDimension', CommandAxoLengthDimension()) -