Salome HOME
af141ae28fbe5448185295937b1c7a0c899c223a
[modules/shaper.git] / src / SketchPlugin / Test / TestMirror.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 from salome.shaper import model
16
17 #=========================================================================
18 # Initialization of the test
19 #=========================================================================
20
21 __updated__ = "2015-03-17"
22
23 #=========================================================================
24 # Auxiliary functions
25 #=========================================================================
26 def normalize(theDir):
27     aLen = math.hypot(theDir[0], theDir[1])
28     if aLen < 1.e-10:
29         aLen = 1.0
30     return [theDir[0] / aLen, theDir[1] / aLen]
31
32 def checkMirror(theListInit, theListMirr, theMirrorLine):
33     TOL = 6.e-5
34     aListSize = theListInit.size()
35
36     aLineStartPoint = geomDataAPI_Point2D(theMirrorLine.attribute("StartPoint"))
37     aLineEndPoint = geomDataAPI_Point2D(theMirrorLine.attribute("EndPoint"))
38     aLineDir = [aLineEndPoint.x() - aLineStartPoint.x(), aLineEndPoint.y() - aLineStartPoint.y()]
39     aLineDir = normalize(aLineDir)
40
41     for ind in range(0, aListSize):
42         aFeatureB = ModelAPI_Feature.feature(theListInit.object(ind))
43         aFeatureC = ModelAPI_Feature.feature(theListMirr.object(ind))
44         assert(aFeatureB is not None)
45         assert(aFeatureC is not None)
46         assert(aFeatureB.getKind() == aFeatureC.getKind())
47
48         anAttributes = []
49         if (aFeatureB.getKind() == "SketchLine"):
50             anAttributes = ['StartPoint', 'EndPoint']
51         elif (aFeatureB.getKind() == "SketchArc"):
52             anAttributes = ['center_point', 'start_point', 'end_point']
53
54         for key in anAttributes:
55             aPointB = geomDataAPI_Point2D(aFeatureB.attribute(key))
56             aPointC = geomDataAPI_Point2D(aFeatureC.attribute(key))
57             aDir = [aPointC.x() - aPointB.x(), aPointC.y() - aPointB.y()]
58             aDir = normalize(aDir)
59             aDot = aLineDir[0] * aDir[0] + aLineDir[1] * aDir[1]
60             assert math.fabs(aDot) < TOL, "aDot = {0}".format(aDot)
61             aDir[0] = aLineEndPoint.x() - 0.5 * (aPointB.x() + aPointC.x())
62             aDir[1] = aLineEndPoint.y() - 0.5 * (aPointB.y() + aPointC.y())
63             aCross = aLineDir[0] * aDir[1] - aLineDir[1] * aDir[0]
64             assert math.fabs(aCross) < TOL, "aCross = {0}".format(aCross)
65
66
67 #=========================================================================
68 # Start of test
69 #=========================================================================
70 aSession = ModelAPI_Session.get()
71 aDocument = aSession.moduleDocument()
72 #=========================================================================
73 # Creation of a sketch
74 #=========================================================================
75 aSession.startOperation()
76 aSketchCommonFeature = aDocument.addFeature("Sketch")
77 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
78 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
79 origin.setValue(0, 0, 0)
80 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
81 dirx.setValue(1, 0, 0)
82 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
83 norm.setValue(0, 0, 1)
84 aSession.finishOperation()
85 #=========================================================================
86 # Creation of an arc and two lines
87 #=========================================================================
88 # Arc
89 aSession.startOperation()
90 aSketchArc1 = aSketchFeature.addFeature("SketchArc")
91 anArcCentr = geomDataAPI_Point2D(aSketchArc1.attribute("center_point"))
92 anArcCentr.setValue(10., 10.)
93 anArcStartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("start_point"))
94 anArcStartPoint.setValue(0., 50.)
95 anArcEndPoint = geomDataAPI_Point2D(aSketchArc1.attribute("end_point"))
96 anArcEndPoint.setValue(50., 0.)
97 aSession.finishOperation()
98 # Line 1
99 aSession.startOperation()
100 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
101 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
102 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
103 aLine1StartPoint.setValue(0., 50.)
104 aLine1EndPoint.setValue(0., 100.)
105 aSession.finishOperation()
106 # Line 2
107 aSession.startOperation()
108 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
109 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
110 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
111 aLine2StartPoint.setValue(50., 0.)
112 aLine2EndPoint.setValue(100., 0.)
113 aSession.finishOperation()
114 assert (model.dof(aSketchFeature) == 13)
115 #=========================================================================
116 # Link arc points and lines points by the coincidence constraint
117 #=========================================================================
118 aSession.startOperation()
119 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
120 reflistA = aConstraint.refattr("ConstraintEntityA")
121 reflistB = aConstraint.refattr("ConstraintEntityB")
122 reflistA.setAttr(anArcStartPoint)
123 reflistB.setAttr(aLine1StartPoint)
124 aConstraint.execute()
125 aSession.finishOperation()
126 aSession.startOperation()
127 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
128 reflistA = aConstraint.refattr("ConstraintEntityA")
129 reflistB = aConstraint.refattr("ConstraintEntityB")
130 reflistA.setAttr(anArcEndPoint)
131 reflistB.setAttr(aLine2StartPoint)
132 aConstraint.execute()
133 aSession.finishOperation()
134 assert (model.dof(aSketchFeature) == 9)
135 #=========================================================================
136 # Add tangency constraint and check correctness
137 #=========================================================================
138 aSession.startOperation()
139 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
140 aRefObjectA = aTangency.refattr("ConstraintEntityA")
141 aRefObjectB = aTangency.refattr("ConstraintEntityB")
142 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
143 anObjectB = modelAPI_ResultConstruction(aSketchLine1.lastResult())
144 assert (anObjectA is not None)
145 assert (anObjectB is not None)
146 aRefObjectA.setObject(anObjectA)
147 aRefObjectB.setObject(anObjectB)
148 aTangency.execute()
149 aSession.finishOperation()
150 assert (model.dof(aSketchFeature) == 8)
151 #=========================================================================
152 # Create mirror line
153 #=========================================================================
154 aSession.startOperation()
155 aMirrorLine = aSketchFeature.addFeature("SketchLine")
156 aLineStartPoint = geomDataAPI_Point2D(aMirrorLine.attribute("StartPoint"))
157 aLineEndPoint = geomDataAPI_Point2D(aMirrorLine.attribute("EndPoint"))
158 aLineStartPoint.setValue(100., 0.)
159 aLineEndPoint.setValue(100., 100.)
160 aSession.finishOperation()
161 assert (model.dof(aSketchFeature) == 12)
162 #=========================================================================
163 # Make mirror for objects created above
164 #=========================================================================
165 aSession.startOperation()
166 aMirror = aSketchFeature.addFeature("SketchConstraintMirror")
167 aRefObjectA = aMirror.refattr("ConstraintEntityA")
168 aRefObjectA.setObject(modelAPI_ResultConstruction(aMirrorLine.firstResult()))
169 aRefListInitial = aMirror.reflist("ConstraintMirrorList")
170 aRefListInitial.append(aSketchLine1.lastResult())
171 aRefListInitial.append(aSketchArc1.lastResult())
172 aRefListInitial.append(aSketchLine2.lastResult())
173 aMirror.execute()
174 aSession.finishOperation()
175 assert (model.dof(aSketchFeature) == 12)
176 #=========================================================================
177 # Verify the simmetricity of all mirrored objects
178 #=========================================================================
179 aRefListB = aMirror.reflist("ConstraintEntityB")
180 aRefListC = aMirror.reflist("ConstraintEntityC")
181 assert (aRefListB.size() == 3)
182 assert (aRefListC.size() == 3)
183 checkMirror(aRefListB, aRefListC, aMirrorLine)
184 assert (model.dof(aSketchFeature) == 12)
185
186 #=========================================================================
187 # Remove object from mirror
188 #=========================================================================
189 aSession.startOperation()
190 aRefListInitial.remove(aSketchLine2.lastResult())
191 aSession.finishOperation()
192 assert (aRefListB.size() == 2)
193 assert (aRefListC.size() == 2)
194 checkMirror(aRefListB, aRefListC, aMirrorLine)
195 assert (model.dof(aSketchFeature) == 12)
196
197 #=========================================================================
198 # Clear list of mirrored features
199 #=========================================================================
200 aSession.startOperation()
201 aRefListInitial.clear()
202 assert (aRefListB.size() == 0)
203 assert (aRefListC.size() == 0)
204 # add arc once again
205 aRefListInitial.append(aSketchArc1.lastResult())
206 aSession.finishOperation()
207 assert (aRefListB.size() == 1)
208 assert (aRefListC.size() == 1)
209 checkMirror(aRefListB, aRefListC, aMirrorLine)
210 assert (model.dof(aSketchFeature) == 12)
211
212 #=========================================================================
213 # Create distance between original and mirrored entities (check the error appears)
214 #=========================================================================
215 aSketchErrorAttr = aSketchFeature.string("SolverError")
216 assert len(aSketchErrorAttr.value()) == 0, "Sketch failed with error: {}".format(aSketchErrorAttr.value())
217 aMirroredArc = model.lastSubFeature(aSketchFeature, "SketchArc")
218 aSession.startOperation()
219 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
220 refAttrA = aConstraint.refattr("ConstraintEntityA")
221 refAttrB = aConstraint.refattr("ConstraintEntityB")
222 anArcStartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("start_point"))
223 aMirroredArcStartPoint = geomDataAPI_Point2D(aMirroredArc.attribute("start_point"))
224 refAttrA.setAttr(anArcStartPoint)
225 refAttrB.setAttr(aMirroredArcStartPoint)
226 aConstraint.real("ConstraintValue").setValue(200.)
227 aSession.finishOperation()
228 print "Sketch error : {}".format(aSketchErrorAttr.value())
229 assert len(aSketchErrorAttr.value()) != 0, "ERROR: Sketch has not been failed as expected"
230 aSession.startOperation()
231 aDocument.removeFeature(aConstraint)
232 aSession.finishOperation()
233
234 #=========================================================================
235 # End of test
236 #=========================================================================
237
238 assert(model.checkPythonDump())