Salome HOME
abef71dc8871ec6beb57e152bc9d144e15fb67d2
[modules/shaper.git] / src / FeaturesPlugin / Test / TestTranslation.py
1 # Copyright (C) 2014-2023  CEA, EDF
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21       TestMovement.py
22       Unit test of FeaturesPlugin_Movement class
23
24       class FeaturesPlugin_Movement : public ModelAPI_Feature
25         static const std::string MY_MOVEMENT_ID("Translation");
26         static const std::string MY_OBJECTS_LIST_ID("main_objects");
27         static const std::string MY_AXIS_OBJECT_ID("axis_object");
28         static const std::string MY_DISTANCE_ID("distance");
29
30         data()->addAttribute(OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
31         data()->addAttribute(AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
32         data()->addAttribute(DISTANCE_ID(), ModelAPI_AttributeDouble::typeId());
33 """
34 #=========================================================================
35 # Initialization of the test
36 #=========================================================================
37 from ModelAPI import *
38 from GeomDataAPI import *
39 from GeomAlgoAPI import *
40 from GeomAPI import *
41 import math
42
43 aSession = ModelAPI_Session.get()
44 aDocument = aSession.moduleDocument()
45 # Create a part for movement
46 aSession.startOperation()
47 aPartFeature = aDocument.addFeature("Part")
48 aSession.finishOperation()
49 assert (len(aPartFeature.results()) == 1)
50 # Another way is:
51 # aPart = aSession.activeDocument()
52 aPartResult = modelAPI_ResultPart(aPartFeature.firstResult())
53 aPart = aPartResult.partDoc()
54
55 #=========================================================================
56 # Create a sketch circle to extrude
57 #=========================================================================
58 aSession.startOperation()
59 aCircleSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
60 origin = geomDataAPI_Point(aCircleSketchFeature.attribute("Origin"))
61 origin.setValue(0, 0, 0)
62 dirx = geomDataAPI_Dir(aCircleSketchFeature.attribute("DirX"))
63 dirx.setValue(1, 0, 0)
64 norm = geomDataAPI_Dir(aCircleSketchFeature.attribute("Norm"))
65 norm.setValue(0, 0, 1)
66 # Create circle
67 aSketchCircle = aCircleSketchFeature.addFeature("SketchCircle")
68 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("circle_center"))
69 aCircleRadius = aSketchCircle.real("circle_radius")
70 anCircleCentr.setValue(50, 50)
71 aCircleRadius.setValue(20)
72 aSession.finishOperation()
73 #=========================================================================
74 # Make extrusion on circle
75 #=========================================================================
76 # Build shape from sketcher results
77 aCircleSketchResult = modelAPI_ResultConstruction(aCircleSketchFeature.firstResult())
78 assert (aCircleSketchResult.facesNum() > 0)
79 # Create extrusion
80 aSession.startOperation()
81 anExtrusionFt = aPart.addFeature("Extrusion")
82 assert (anExtrusionFt.getKind() == "Extrusion")
83 # selection type FACE=4
84 anExtrusionFt.selectionList("base").append(
85     aCircleSketchResult, aCircleSketchResult.face(0))
86 anExtrusionFt.string("CreationMethod").setValue("BySizes")
87 anExtrusionFt.real("to_size").setValue(50)
88 anExtrusionFt.real("from_size").setValue(0)
89 anExtrusionFt.real("to_offset").setValue(0) #TODO: remove
90 anExtrusionFt.real("from_offset").setValue(0) #TODO: remove
91 anExtrusionFt.execute()
92 aSession.finishOperation()
93 assert (anExtrusionFt.real("to_size").value() == 50)
94
95 # Check extrusion results
96 assert (len(anExtrusionFt.results()) > 0)
97 anExtrusionResult = modelAPI_ResultBody(anExtrusionFt.firstResult())
98 assert (anExtrusionResult is not None)
99
100 #=========================================================================
101 # Create a sketch line to movement
102 #=========================================================================
103 aSession.startOperation()
104 aLineSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
105 origin = geomDataAPI_Point(aLineSketchFeature.attribute("Origin"))
106 origin.setValue(0, 0, 0)
107 dirx = geomDataAPI_Dir(aLineSketchFeature.attribute("DirX"))
108 dirx.setValue(1, 0, 0)
109 norm = geomDataAPI_Dir(aLineSketchFeature.attribute("Norm"))
110 norm.setValue(0, 0, 1)
111
112 aSketchLine = aLineSketchFeature.addFeature("SketchLine")
113 aLineStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
114 aLineEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
115 aLineStartPoint.setValue(-100, -100)
116 aLineEndPoint.setValue(100, -100)
117 aSession.finishOperation()
118
119 # Build shape from sketcher results
120 aLineSketchResult = aLineSketchFeature.firstResult()
121 aLineSketchShape = modelAPI_ResultConstruction(aLineSketchResult).shape()
122 aShapeExplorer = GeomAPI_ShapeExplorer(aLineSketchShape, GeomAPI_Shape.EDGE)
123 aLineEdge = aShapeExplorer.current()
124
125 #=========================================================================
126 # Test movement
127 #=========================================================================
128 aSession.startOperation()
129 aMoveFt = aPart.addFeature("Translation")
130 assert (aMoveFt.getKind() == "Translation")
131 aMoveFt.selectionList("main_objects").append(
132     anExtrusionResult, anExtrusionResult.shape())
133 aMoveFt.string("CreationMethod").setValue("ByAxisAndDistance")
134 aMoveFt.selection("axis_object").setValue(aLineSketchResult, aLineEdge)
135 aMoveFt.real("distance").setValue(100)
136 aMoveFt.execute()
137 aSession.finishOperation()
138 assert (aMoveFt.real("distance").value() == 100)
139
140 # Check movement results
141 aFactory = ModelAPI_Session.get().validators()
142 assert (aFactory.validate(aMoveFt))
143 assert (len(aMoveFt.results()) > 0)
144 aMoveResult = modelAPI_ResultBody(aMoveFt.firstResult())
145 assert (aMoveResult is not None)
146
147 from salome.shaper import model
148 assert(model.checkPythonDump())