File size: 6,022 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# ***************************************************************************
# *   Copyright (c) 2023 edi <edi271@a1.net>                                *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************
"""
Provides the TechDraw PositionSectionView GuiCommand.
00.01 2021/03/17 C++ Basic version
00.02 2023/12/21 Option to select an edge and its corresponding vertex
"""

__title__ = "TechDrawTools.CommandPositionSectionView"
__author__ = "edi"
__url__ = "https://www.freecad.org"
__version__ = "00.02"
__date__ = "2023/12/21"

from PySide.QtCore import QT_TRANSLATE_NOOP

import FreeCAD as App
import FreeCADGui as Gui

import TechDrawTools.TDToolsUtil as Utils

class CommandPositionSectionView:
    """Orthogonally align a section view with its source view."""

    def __init__(self):
        """Initialize variables for the command that must exist at all times."""
        pass

    def GetResources(self):
        """Return a dictionary with data that will be used by the button or menu item."""
        return {'Pixmap': 'TechDraw_ExtensionPositionSectionView.svg',
                'Accel': "",
                'MenuText': QT_TRANSLATE_NOOP("TechDraw_PositionSectionView", "Position Section View"),
                'ToolTip': QT_TRANSLATE_NOOP("TechDraw_PositionSectionView",
                  "Aligns the selected section view with its source view orthogonally or the selected edge in the section view to the selected vertex in the base view")}

    def Activated(self):
        """Run the following code when the command is activated (button pressed)."""
        selection = Gui.Selection.getSelectionEx()
        if not selection:
            return
        if len(selection) == 1:
            if Utils.getSelView():
                sectionView = Utils.getSelView()
                if sectionView.TypeId == 'TechDraw::DrawViewSection':
                    baseView = sectionView.BaseView
                    if baseView.TypeId == "TechDraw::DrawProjGroupItem":
                        baseView = baseView.InList[0]
                    basePoint = App.Vector(baseView.X,baseView.Y,0.0)
                    sectionPoint = App.Vector(sectionView.X,sectionView.Y,0.0)
                    moveVector = sectionPoint.sub(basePoint)
                    if abs(moveVector.x) > abs(moveVector.y):
                        moveVector.x = 0.0
                    else:
                        moveVector.y = 0.0
                else:
                    return

        elif len(selection) == 2:
            if Utils.getSelEdges() and Utils.getSelVertexes(1,1):
                sectionEdge = Utils.getSelEdges()
                sectionDir = sectionEdge[0].Curve.Direction
                sectionSel = sectionEdge[0].Vertexes
                baseSel = Utils.getSelVertexes(1,1)
                sectionView = Utils.getSelView(0)
                baseView = Utils.getSelView(1)
                if baseView.TypeId == "TechDraw::DrawProjGroupItem":
                    baseView = baseView.InList[0]

                basePoint = baseSel[0].Point
                sectionPoint = sectionSel[0].Point
                Scale = baseView.Scale
                centerBase = App.Vector(baseView.X,baseView.Y,0)
                basePoint = centerBase+basePoint*Scale
                centerSection = App.Vector(sectionView.X,sectionView.Y,0)
                sectionPoint = centerSection+sectionPoint*Scale
                sectionPoint = self.getTrianglePoint(sectionPoint,sectionDir,basePoint)
                moveVector = sectionPoint.sub(basePoint)
            else:
                return

        sectionView.X = sectionView.X.Value-moveVector.x
        sectionView.Y = sectionView.Y.Value-moveVector.y

    def IsActive(self):
        """Return True when the command should be active or False when it should be disabled (greyed)."""
        if App.ActiveDocument:
            return Utils.havePage() and Utils.haveView()
        else:
            return False

    def getTrianglePoint(self,p1,dir,p2):
        '''
        Calculate the third vertex of a right triangle.

        Parameters:
        p1, p2 : vertices of the hypotenuse
        dir    : direction vector of one leg (kathete)

        Returns:
        p3 : the third vertex completing the right triangle
        '''
        a = -dir.y
        b = dir.x
        c1 = p1.x * a + p1.y * b
        c2 = -p2.x * b + p2.y * a
        ab = a * a + b * b
        x = (c1 * a - c2 * b) / ab
        y = (c2 * a + c1 * b) / ab
        return App.Vector(x,y,0.0)

# The command must be "registered" with a unique name by calling its class.
Gui.addCommand('TechDraw_ExtensionPositionSectionView', CommandPositionSectionView())