Salome HOME
Update sketcher unit tests for code coverage
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintMirror.py
1 """
2     TestConstraintMirror.py
3     Unit test of SketchPlugin_ConstraintMirror class
4         
5     SketchPlugin_ConstraintMirror
6         static const std::string MY_CONSTRAINT_MIRROR_ID("SketchConstraintMirror");
7         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
8         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefListAttr::typeId());
9         data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefListAttr::typeId());
10
11 """
12 from GeomDataAPI import *
13 from ModelAPI import *
14 import math
15 #=========================================================================
16 # Initialization of the test
17 #=========================================================================
18
19 __updated__ = "2015-03-17"
20
21 #=========================================================================
22 # Auxiliary functions
23 #=========================================================================
24 def checkMirror(theListInit, theListMirr, theMirrorLine):
25     aListSize = theListInit.size()
26     
27     aLineStartPoint = geomDataAPI_Point2D(theMirrorLine.attribute("StartPoint"))
28     aLineEndPoint = geomDataAPI_Point2D(theMirrorLine.attribute("EndPoint"))
29     aLineDirX = aLineEndPoint.x() - aLineStartPoint.x()
30     aLineDirY = aLineEndPoint.y() - aLineStartPoint.y()
31
32     for ind in range(0, aListSize):
33         aFeatureB = ModelAPI_Feature.feature(theListInit.object(ind))
34         aFeatureC = ModelAPI_Feature.feature(theListMirr.object(ind))
35         assert(aFeatureB is not None)
36         assert(aFeatureC is not None)
37         assert(aFeatureB.getKind() == aFeatureC.getKind())
38         
39         anAttributes = {}
40         if (aFeatureB.getKind() == "SketchLine"):
41             anAttributes = {'StartPoint':'StartPoint', 'EndPoint':'EndPoint'}
42         elif (aFeatureB.getKind() == "SketchArc"):
43             anAttributes = {'ArcCenter':'ArcCenter', 'ArcStartPoint':'ArcEndPoint', 'ArcEndPoint':'ArcStartPoint'}
44         
45         for key in anAttributes:
46             aPointB = geomDataAPI_Point2D(aFeatureB.attribute(key))
47             aPointC = geomDataAPI_Point2D(aFeatureC.attribute(anAttributes[key]))
48             aDirX = aPointC.x() - aPointB.x()
49             aDirY = aPointC.y() - aPointB.y()
50             aDot = aLineDirX * aDirX + aLineDirY * aDirY
51             assert(math.fabs(aDot) < 1.e-10)
52             aDirX = aLineEndPoint.x() - 0.5 * (aPointB.x() + aPointC.x())
53             aDirY = aLineEndPoint.y() - 0.5 * (aPointB.y() + aPointC.y())
54             aCross = aLineDirX * aDirY - aLineDirY * aDirX
55             assert(math.fabs(aCross) < 1.e-10)
56
57
58 #=========================================================================
59 # Start of test
60 #=========================================================================
61 aSession = ModelAPI_Session.get()
62 aDocument = aSession.moduleDocument()
63 #=========================================================================
64 # Creation of a sketch
65 #=========================================================================
66 aSession.startOperation()
67 aSketchCommonFeature = aDocument.addFeature("Sketch")
68 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
69 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
70 origin.setValue(0, 0, 0)
71 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
72 dirx.setValue(1, 0, 0)
73 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
74 norm.setValue(0, 0, 1)
75 aSession.finishOperation()
76 #=========================================================================
77 # Creation of an arc and two lines
78 #=========================================================================
79 # Arc
80 aSession.startOperation()
81 aSketchArc1 = aSketchFeature.addFeature("SketchArc")
82 anArcCentr = geomDataAPI_Point2D(aSketchArc1.attribute("ArcCenter"))
83 anArcCentr.setValue(10., 10.)
84 anArcStartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("ArcStartPoint"))
85 anArcStartPoint.setValue(0., 50.)
86 anArcEndPoint = geomDataAPI_Point2D(aSketchArc1.attribute("ArcEndPoint"))
87 anArcEndPoint.setValue(50., 0.)
88 aSession.finishOperation()
89 # Line 1
90 aSession.startOperation()
91 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
92 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
93 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
94 aLine1StartPoint.setValue(0., 50.)
95 aLine1EndPoint.setValue(0., 100.)
96 aSession.finishOperation()
97 # Line 2
98 aSession.startOperation()
99 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
100 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
101 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
102 aLine2StartPoint.setValue(50., 0.)
103 aLine2EndPoint.setValue(100., 0.)
104 aSession.finishOperation()
105 #=========================================================================
106 # Link arc points and lines points by the coincidence constraint
107 #=========================================================================
108 aSession.startOperation()
109 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
110 reflistA = aConstraint.refattr("ConstraintEntityA")
111 reflistB = aConstraint.refattr("ConstraintEntityB")
112 reflistA.setAttr(anArcStartPoint)
113 reflistB.setAttr(aLine1StartPoint)
114 aConstraint.execute()
115 aSession.finishOperation()
116 aSession.startOperation()
117 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
118 reflistA = aConstraint.refattr("ConstraintEntityA")
119 reflistB = aConstraint.refattr("ConstraintEntityB")
120 reflistA.setAttr(anArcEndPoint)
121 reflistB.setAttr(aLine2StartPoint)
122 aConstraint.execute()
123 aSession.finishOperation()
124 #=========================================================================
125 # Add tangency constraint and check correctness
126 #=========================================================================
127 aSession.startOperation()
128 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
129 aRefObjectA = aTangency.refattr("ConstraintEntityA")
130 aRefObjectB = aTangency.refattr("ConstraintEntityB")
131 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
132 anObjectB = modelAPI_ResultConstruction(aSketchLine1.firstResult())
133 assert (anObjectA is not None)
134 assert (anObjectB is not None)
135 aRefObjectA.setObject(anObjectA)
136 aRefObjectB.setObject(anObjectB)
137 aTangency.execute()
138 aSession.finishOperation()
139 #=========================================================================
140 # Create mirror line
141 #=========================================================================
142 aSession.startOperation()
143 aMirrorLine = aSketchFeature.addFeature("SketchLine")
144 aLineStartPoint = geomDataAPI_Point2D(aMirrorLine.attribute("StartPoint"))
145 aLineEndPoint = geomDataAPI_Point2D(aMirrorLine.attribute("EndPoint"))
146 aLineStartPoint.setValue(100., 0.)
147 aLineEndPoint.setValue(100., 100.)
148 aSession.finishOperation()
149 #=========================================================================
150 # Make mirror for objects created above
151 #=========================================================================
152 aSession.startOperation()
153 aMirror = aSketchFeature.addFeature("SketchConstraintMirror")
154 aRefObjectA = aMirror.refattr("ConstraintEntityA")
155 aRefObjectA.setObject(modelAPI_ResultConstruction(aMirrorLine.firstResult()))
156 aRefListInitial = aMirror.reflist("ConstraintMirrorList")
157 aRefListInitial.append(aSketchArc1.lastResult())
158 aRefListInitial.append(aSketchLine1.lastResult())
159 aRefListInitial.append(aSketchLine2.lastResult())
160 aMirror.execute()
161 aSession.finishOperation()
162 #=========================================================================
163 # Verify the simmetricity of all mirrored objects
164 #=========================================================================
165 aRefListB = aMirror.reflist("ConstraintEntityB")
166 aRefListC = aMirror.reflist("ConstraintEntityC")
167 assert (aRefListB.size() == 3)
168 assert (aRefListC.size() == 3)
169 checkMirror(aRefListB, aRefListC, aMirrorLine)
170
171 #=========================================================================
172 # Remove object from mirror
173 #=========================================================================
174 aSession.startOperation()
175 aRefListInitial.remove(aSketchLine2.lastResult())
176 aSession.finishOperation()
177 assert (aRefListB.size() == 2)
178 assert (aRefListC.size() == 2)
179 checkMirror(aRefListB, aRefListC, aMirrorLine)
180
181 #=========================================================================
182 # Clear list of mirrored features
183 #=========================================================================
184 aSession.startOperation()
185 aRefListInitial.clear()
186 assert (aRefListB.size() == 0)
187 assert (aRefListC.size() == 0)
188 # add arc once again
189 aRefListInitial.append(aSketchArc1.lastResult())
190 aSession.finishOperation()
191 assert (aRefListB.size() == 1)
192 assert (aRefListC.size() == 1)
193 checkMirror(aRefListB, aRefListC, aMirrorLine)
194 #=========================================================================
195 # End of test
196 #=========================================================================