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