Salome HOME
Improve PythonAPI documentation
[modules/shaper.git] / src / SketchPlugin / Test / TestMultiRotation.py
1 """
2     TestMultiRotation.py
3     Unit test of SketchPlugin_MultiRotation class
4         
5     SketchPlugin_MultiRotation
6         static const std::string MY_CONSTRAINT_ROTATION_ID("SketchMultiRotation");
7         data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
8         data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
9         data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeInteger::typeId());
10         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
11         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
12         data()->addAttribute(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
13
14 """
15 from GeomDataAPI import *
16 from ModelAPI import *
17 import math
18
19 #=========================================================================
20 # Auxiliary functions
21 #=========================================================================
22 def createSketch(theSketch):
23     # Initialize sketch by arc
24     allFeatures = []
25     # Create arc
26     aSketchArc = theSketch.addFeature("SketchArc")
27     aCenter     = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
28     aStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcStartPoint"))
29     aEndPoint   = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
30     aCenter.setValue(5., 5.)
31     aStartPoint.setValue(10., 5.)
32     aEndPoint.setValue(5., 10.)
33     allFeatures.append(aSketchArc)
34     theSketch.execute()
35     return allFeatures
36     
37 def checkRotation(theObjects, theNbCopies, theCenterX, theCenterY, theAngle):
38     # Verify distances of the objects and the number of copies
39     aFeatures = []
40     aSize = theObjects.size()
41     for i in range (0, aSize):
42         feat = ModelAPI_Feature.feature(theObjects.object(i))
43         assert(feat is not None)
44         aFeatures.append(feat)
45         
46     cosA = math.cos(theAngle * math.pi / 180.)
47         
48     anInd = 0 
49     for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
50         anInd = anInd + 1
51         if (anInd > theNbCopies):
52             anInd = 0
53             continue
54         assert(feat.getKind() == next.getKind())
55         
56         anAttributes = []
57         if (feat.getKind() == "SketchLine"):
58             anAttributes.append('StartPoint')
59             anAttributes.append('EndPoint')
60         elif (feat.getKind() == "SketchArc"):
61             anAttributes.append('ArcCenter')
62             anAttributes.append('ArcStartPoint')
63             anAttributes.append('ArcEndPoint')
64             
65         for attr in anAttributes:
66             aPoint1 = geomDataAPI_Point2D(feat.attribute(attr))
67             aPoint2 = geomDataAPI_Point2D(next.attribute(attr))
68             aDirX1 = aPoint1.x() - theCenterX
69             aDirY1 = aPoint1.y() - theCenterY
70             aLen1 = math.hypot(aDirX1, aDirY1)
71             aDirX2 = aPoint2.x() - theCenterX
72             aDirY2 = aPoint2.y() - theCenterY
73             aLen2 = math.hypot(aDirX2, aDirY2)
74             aLocCosA = (aDirX1 * aDirX2 + aDirY1 * aDirY2) / aLen1 / aLen2
75             assert(math.fabs(aLocCosA - cosA) < 1.e-10)
76     # Check the number of copies is as planed
77     assert(anInd == theNbCopies)
78
79
80 #=========================================================================
81 # Initialization of the test
82 #=========================================================================
83
84 __updated__ = "2015-09-18"
85
86 aSession = ModelAPI_Session.get()
87 aDocument = aSession.moduleDocument()
88 #=========================================================================
89 # Creation of a sketch
90 #=========================================================================
91 aSession.startOperation()
92 aSketchCommonFeature = aDocument.addFeature("Sketch")
93 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
94 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
95 origin.setValue(0, 0, 0)
96 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
97 dirx.setValue(1, 0, 0)
98 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
99 norm.setValue(0, 0, 1)
100 aSession.finishOperation()
101 #=========================================================================
102 # Initialize sketch
103 #=========================================================================
104 aSession.startOperation()
105 aFeaturesList = createSketch(aSketchFeature)
106 aSession.finishOperation()
107 #=========================================================================
108 # Global variables
109 #=========================================================================
110 CENTER_X = 0.
111 CENTER_Y = 0.
112 ANGLE = 30.
113 #=========================================================================
114 # Create the Rotation constraint
115 #=========================================================================
116 aSession.startOperation()
117 aMultiRotation = aSketchFeature.addFeature("SketchMultiRotation")
118 aRotList = aMultiRotation.reflist("MultiRotationList")
119 for aFeature in aFeaturesList:
120     aResult = modelAPI_ResultConstruction(aFeature.lastResult())
121     assert(aResult is not None)
122     aRotList.append(aResult)
123 aCenter = geomDataAPI_Point2D(aMultiRotation.attribute("MultiRotationCenter"))
124 anAngle = aMultiRotation.real("MultiRotationAngle")
125 aCenter.setValue(CENTER_X, CENTER_Y)
126 anAngle.setValue(ANGLE)
127 aNbCopies = aMultiRotation.integer("MultiRotationCopies")
128 aNbCopies.setValue(1)
129 aMultiRotation.execute()
130 aSession.finishOperation()
131 #=========================================================================
132 # Verify the objects are moved for the specified distance
133 #=========================================================================
134 aRotated = aMultiRotation.reflist("ConstraintEntityB")
135 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
136 #=========================================================================
137 # Change number of copies and verify Rotation
138 #=========================================================================
139 aSession.startOperation()
140 aNbCopies.setValue(3)
141 aSession.finishOperation()
142 aRotated = aMultiRotation.reflist("ConstraintEntityB")
143 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
144 #=========================================================================
145 # TODO: improve test
146 # 1. Add more features into Rotation
147 # 2. Move one of initial features and check the Rotated is moved too
148 #=========================================================================
149 #=========================================================================
150 # End of test
151 #=========================================================================