FreeCAD

Форк
0
223 строки · 9.3 Кб
1
# SPDX-License-Identifier: LGPL-2.1-or-later
2
# /****************************************************************************
3
#                                                                           *
4
#    Copyright (c) 2023 Ondsel <development@ondsel.com>                     *
5
#                                                                           *
6
#    This file is part of FreeCAD.                                          *
7
#                                                                           *
8
#    FreeCAD is free software: you can redistribute it and/or modify it     *
9
#    under the terms of the GNU Lesser General Public License as            *
10
#    published by the Free Software Foundation, either version 2.1 of the   *
11
#    License, or (at your option) any later version.                        *
12
#                                                                           *
13
#    FreeCAD is distributed in the hope that it will be useful, but         *
14
#    WITHOUT ANY WARRANTY; without even the implied warranty of             *
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU       *
16
#    Lesser General Public License for more details.                        *
17
#                                                                           *
18
#    You should have received a copy of the GNU Lesser General Public       *
19
#    License along with FreeCAD. If not, see                                *
20
#    <https://www.gnu.org/licenses/>.                                       *
21
#                                                                           *
22
# ***************************************************************************/
23

24
import FreeCAD as App
25
import Part
26
import unittest
27

28
import UtilsAssembly
29
import JointObject
30

31

32
def _msg(text, end="\n"):
33
    """Write messages to the console including the line ending."""
34
    App.Console.PrintMessage(text + end)
35

36

37
class TestCore(unittest.TestCase):
38
    @classmethod
39
    def setUpClass(cls):
40
        """setUpClass()...
41
        This method is called upon instantiation of this test class.  Add code and objects here
42
        that are needed for the duration of the test() methods in this class.  In other words,
43
        set up the 'global' test environment here; use the `setUp()` method to set up a 'local'
44
        test environment.
45
        This method does not have access to the class `self` reference, but it
46
        is able to call static methods within this same class.
47
        """
48
        pass
49

50
    @classmethod
51
    def tearDownClass(cls):
52
        """tearDownClass()...
53
        This method is called prior to destruction of this test class.  Add code and objects here
54
        that cleanup the test environment after the test() methods in this class have been executed.
55
        This method does not have access to the class `self` reference.  This method
56
        is able to call static methods within this same class.
57
        """
58
        pass
59

60
    # Setup and tear down methods called before and after each unit test
61
    def setUp(self):
62
        """setUp()...
63
        This method is called prior to each `test()` method.  Add code and objects here
64
        that are needed for multiple `test()` methods.
65
        """
66
        doc_name = self.__class__.__name__
67
        if App.ActiveDocument:
68
            if App.ActiveDocument.Name != doc_name:
69
                App.newDocument(doc_name)
70
        else:
71
            App.newDocument(doc_name)
72
        App.setActiveDocument(doc_name)
73
        self.doc = App.ActiveDocument
74

75
        self.assembly = App.ActiveDocument.addObject("Assembly::AssemblyObject", "Assembly")
76
        if self.assembly:
77
            self.jointgroup = self.assembly.newObject("Assembly::JointGroup", "Joints")
78

79
        _msg("  Temporary document '{}'".format(self.doc.Name))
80

81
    def tearDown(self):
82
        """tearDown()...
83
        This method is called after each test() method. Add cleanup instructions here.
84
        Such cleanup instructions will likely undo those in the setUp() method.
85
        """
86
        App.closeDocument(self.doc.Name)
87

88
    def test_create_assembly(self):
89
        """Create an assembly."""
90
        operation = "Create Assembly Object"
91
        _msg("  Test '{}'".format(operation))
92
        self.assertTrue(self.assembly, "'{}' failed".format(operation))
93

94
    def test_create_jointGroup(self):
95
        """Create a joint group in an assembly."""
96
        operation = "Create JointGroup Object"
97
        _msg("  Test '{}'".format(operation))
98
        self.assertTrue(self.jointgroup, "'{}' failed".format(operation))
99

100
    def test_create_joint(self):
101
        """Create a joint in an assembly."""
102
        operation = "Create Joint Object"
103
        _msg("  Test '{}'".format(operation))
104

105
        joint = self.jointgroup.newObject("App::FeaturePython", "testJoint")
106
        self.assertTrue(joint, "'{}' failed (FeaturePython creation failed)".format(operation))
107
        JointObject.Joint(joint, 0)
108

109
        self.assertTrue(hasattr(joint, "JointType"), "'{}' failed".format(operation))
110

111
    def test_create_grounded_joint(self):
112
        """Create a grounded joint in an assembly."""
113
        operation = "Create Grounded Joint Object"
114
        _msg("  Test '{}'".format(operation))
115

116
        groundedjoint = self.jointgroup.newObject("App::FeaturePython", "testJoint")
117
        self.assertTrue(
118
            groundedjoint, "'{}' failed (FeaturePython creation failed)".format(operation)
119
        )
120

121
        box = self.assembly.newObject("Part::Box", "Box")
122

123
        JointObject.GroundedJoint(groundedjoint, box)
124

125
        self.assertTrue(
126
            hasattr(groundedjoint, "ObjectToGround"),
127
            "'{}' failed: No attribute 'ObjectToGround'".format(operation),
128
        )
129
        self.assertTrue(
130
            groundedjoint.ObjectToGround == box,
131
            "'{}' failed: ObjectToGround not set correctly.".format(operation),
132
        )
133

134
    def test_find_placement(self):
135
        """Test find placement of joint."""
136
        operation = "Find placement"
137
        _msg("  Test '{}'".format(operation))
138

139
        joint = self.jointgroup.newObject("App::FeaturePython", "testJoint")
140
        JointObject.Joint(joint, 0)
141

142
        L = 2
143
        W = 3
144
        H = 7
145
        box = self.assembly.newObject("Part::Box", "Box")
146
        box.Length = L
147
        box.Width = W
148
        box.Height = H
149
        box.Placement = App.Placement(App.Vector(10, 20, 30), App.Rotation(15, 25, 35))
150

151
        # Step 0 : box with placement. No element selected
152
        plc = joint.Proxy.findPlacement(joint, box.Name, box, "", "")
153
        targetPlc = App.Placement(App.Vector(), App.Rotation())
154
        self.assertTrue(plc.isSame(targetPlc, 1e-6), "'{}' failed - Step 0".format(operation))
155

156
        # Step 1 : box with placement. Face + Vertex
157
        plc = joint.Proxy.findPlacement(joint, box.Name, box, "Face6", "Vertex7")
158
        targetPlc = App.Placement(App.Vector(L, W, H), App.Rotation())
159
        self.assertTrue(plc.isSame(targetPlc, 1e-6), "'{}' failed - Step 1".format(operation))
160

161
        # Step 2 : box with placement. Edge + Vertex
162
        plc = joint.Proxy.findPlacement(joint, box.Name, box, "Edge8", "Vertex8")
163
        targetPlc = App.Placement(App.Vector(L, W, 0), App.Rotation(0, 0, -90))
164
        self.assertTrue(plc.isSame(targetPlc, 1e-6), "'{}' failed - Step 2".format(operation))
165

166
        # Step 3 : box with placement. Vertex
167
        plc = joint.Proxy.findPlacement(joint, box.Name, box, "Vertex3", "Vertex3")
168
        targetPlc = App.Placement(App.Vector(0, W, H), App.Rotation())
169
        _msg("  plc '{}'".format(plc))
170
        _msg("  targetPlc '{}'".format(targetPlc))
171
        self.assertTrue(plc.isSame(targetPlc, 1e-6), "'{}' failed - Step 3".format(operation))
172

173
        # Step 4 : box with placement. Face
174
        plc = joint.Proxy.findPlacement(joint, box.Name, box, "Face2", "Face2")
175
        targetPlc = App.Placement(App.Vector(L, W / 2, H / 2), App.Rotation(0, -90, 180))
176
        _msg("  plc '{}'".format(plc))
177
        _msg("  targetPlc '{}'".format(targetPlc))
178
        self.assertTrue(plc.isSame(targetPlc, 1e-6), "'{}' failed - Step 4".format(operation))
179

180
    def test_solve_assembly(self):
181
        """Test solving an assembly."""
182
        operation = "Solve assembly"
183
        _msg("  Test '{}'".format(operation))
184

185
        box = self.assembly.newObject("Part::Box", "Box")
186
        box.Length = 10
187
        box.Width = 10
188
        box.Height = 10
189
        box.Placement = App.Placement(App.Vector(10, 20, 30), App.Rotation(15, 25, 35))
190

191
        box2 = self.assembly.newObject("Part::Box", "Box")
192
        box2.Length = 10
193
        box2.Width = 10
194
        box2.Height = 10
195
        box2.Placement = App.Placement(App.Vector(40, 50, 60), App.Rotation(45, 55, 65))
196

197
        ground = self.jointgroup.newObject("App::FeaturePython", "GroundedJoint")
198
        JointObject.GroundedJoint(ground, box2)
199

200
        joint = self.jointgroup.newObject("App::FeaturePython", "testJoint")
201
        JointObject.Joint(joint, 0)
202

203
        current_selection = []
204
        current_selection.append(
205
            {
206
                "object": box2,
207
                "part": box2,
208
                "element_name": "Face6",
209
                "vertex_name": "Vertex7",
210
            }
211
        )
212
        current_selection.append(
213
            {
214
                "object": box,
215
                "part": box,
216
                "element_name": "Face6",
217
                "vertex_name": "Vertex7",
218
            }
219
        )
220

221
        joint.Proxy.setJointConnectors(joint, current_selection)
222

223
        self.assertTrue(box.Placement.isSame(box2.Placement, 1e-6), "'{}'".format(operation))
224

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.