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