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