Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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::type());
8         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefListAttr::type());
9         data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefListAttr::type());
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 aSession = ModelAPI_Session.get()
22 aDocument = aSession.moduleDocument()
23 #=========================================================================
24 # Creation of a sketch
25 #=========================================================================
26 aSession.startOperation()
27 aSketchCommonFeature = aDocument.addFeature("Sketch")
28 aSketchFeature = modelAPI_CompositeFeature(aSketchCommonFeature)
29 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
30 origin.setValue(0, 0, 0)
31 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
32 dirx.setValue(1, 0, 0)
33 diry = geomDataAPI_Dir(aSketchFeature.attribute("DirY"))
34 diry.setValue(0, 1, 0)
35 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
36 norm.setValue(0, 0, 1)
37 aSession.finishOperation()
38 #=========================================================================
39 # Creation of an arc and two lines
40 #=========================================================================
41 # Arc
42 aSession.startOperation()
43 aSketchArc1 = aSketchFeature.addFeature("SketchArc")
44 anArcCentr = geomDataAPI_Point2D(aSketchArc1.attribute("ArcCenter"))
45 anArcCentr.setValue(10., 10.)
46 anArcStartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("ArcStartPoint"))
47 anArcStartPoint.setValue(0., 50.)
48 anArcEndPoint = geomDataAPI_Point2D(aSketchArc1.attribute("ArcEndPoint"))
49 anArcEndPoint.setValue(50., 0.)
50 aSession.finishOperation()
51 # Line 1
52 aSession.startOperation()
53 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
54 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
55 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
56 aLine1StartPoint.setValue(0., 50.)
57 aLine1EndPoint.setValue(0., 100.)
58 aSession.finishOperation()
59 # Line 2
60 aSession.startOperation()
61 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
62 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
63 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
64 aLine2StartPoint.setValue(50., 0.)
65 aLine2EndPoint.setValue(100., 0.)
66 aSession.finishOperation()
67 #=========================================================================
68 # Link arc points and lines points by the coincidence constraint
69 #=========================================================================
70 aSession.startOperation()
71 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
72 reflistA = aConstraint.refattr("ConstraintEntityA")
73 reflistB = aConstraint.refattr("ConstraintEntityB")
74 reflistA.setAttr(anArcStartPoint)
75 reflistB.setAttr(aLine1StartPoint)
76 aConstraint.execute()
77 aSession.finishOperation()
78 aSession.startOperation()
79 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
80 reflistA = aConstraint.refattr("ConstraintEntityA")
81 reflistB = aConstraint.refattr("ConstraintEntityB")
82 reflistA.setAttr(anArcEndPoint)
83 reflistB.setAttr(aLine2StartPoint)
84 aConstraint.execute()
85 aSession.finishOperation()
86 #=========================================================================
87 # Add tangency constraint and check correctness
88 #=========================================================================
89 aSession.startOperation()
90 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
91 aRefObjectA = aTangency.refattr("ConstraintEntityA")
92 aRefObjectB = aTangency.refattr("ConstraintEntityB")
93 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
94 anObjectB = modelAPI_ResultConstruction(aSketchLine1.firstResult())
95 assert (anObjectA is not None)
96 assert (anObjectB is not None)
97 aRefObjectA.setObject(anObjectA)
98 aRefObjectB.setObject(anObjectB)
99 aTangency.execute()
100 aSession.finishOperation()
101 #=========================================================================
102 # Create mirror line
103 #=========================================================================
104 aSession.startOperation()
105 aMirrorLine = aSketchFeature.addFeature("SketchLine")
106 aLineStartPoint = geomDataAPI_Point2D(aMirrorLine.attribute("StartPoint"))
107 aLineEndPoint = geomDataAPI_Point2D(aMirrorLine.attribute("EndPoint"))
108 aLineStartPoint.setValue(100., 0.)
109 aLineEndPoint.setValue(100., 100.)
110 aSession.finishOperation()
111 #=========================================================================
112 # Make mirror for objects created above
113 #=========================================================================
114 aSession.startOperation()
115 aMirror = aSketchFeature.addFeature("SketchConstraintMirror")
116 aRefObjectA = aMirror.refattr("ConstraintEntityA")
117 aRefObjectA.setObject(modelAPI_ResultConstruction(aMirrorLine.firstResult()))
118 aRefListB = aMirror.reflist("ConstraintEntityB")
119 aRefListB.append(aSketchArc1)
120 aRefListB.append(aSketchLine1)
121 aRefListB.append(aSketchLine2)
122 aMirror.execute()
123 aSession.finishOperation()
124 #=========================================================================
125 # Verify the simmetricity of all mirrored objects
126 #=========================================================================
127 aRefListC = aMirror.reflist("ConstraintEntityC")
128 aListSize = aRefListB.size()
129 aLineDirX = aLineEndPoint.x() - aLineStartPoint.x()
130 aLineDirY = aLineEndPoint.y() - aLineStartPoint.y()
131
132 for ind in range(0, aListSize):
133   aFeatureB = modelAPI_Feature(aRefListB.object(ind))
134   aFeatureC = modelAPI_Feature(aRefListC.object(ind))
135   assert(aFeatureB is not None)
136   assert(aFeatureC is not None)
137   assert(aFeatureB.getKind() == aFeatureC.getKind())
138   anAttributes = {}
139   print aFeatureB.getKind()
140   if (aFeatureB.getKind() == "SketchLine"):
141     anAttributes = {'StartPoint':'StartPoint', 'EndPoint':'EndPoint'}
142   elif (aFeatureB.getKind() == "SketchArc"):
143     anAttributes = {'ArcCenter':'ArcCenter', 'ArcStartPoint':'ArcEndPoint', 'ArcEndPoint':'ArcStartPoint'}
144
145   for key in anAttributes:
146     aPointB = geomDataAPI_Point2D(aFeatureB.attribute(key))
147     aPointC = geomDataAPI_Point2D(aFeatureC.attribute(anAttributes[key]))
148     aDirX = aPointC.x() - aPointB.x()
149     aDirY = aPointC.y() - aPointB.y()
150     aDot = aLineDirX * aDirX + aLineDirY * aDirY
151     assert(math.fabs(aDot) < 1.e-10)
152     aDirX = aLineEndPoint.x() - 0.5 * (aPointB.x() + aPointC.x())
153     aDirY = aLineEndPoint.y() - 0.5 * (aPointB.y() + aPointC.y())
154     aCross = aLineDirX * aDirY - aLineDirY * aDirX
155     assert(math.fabs(aCross) < 1.e-10)
156 #=========================================================================
157 # End of test
158 #=========================================================================