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