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