Salome HOME
Fix for crash in Object Browser when object was deleted but message not sent
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintEqual.py
1 """
2     TestConstraintEqual.py
3     Unit test of SketchPlugin_ConstraintEqual class
4         
5     SketchPlugin_ConstraintEqual
6         static const std::string MY_CONSTRAINT_EQAUL_ID("SketchConstraintEqual");
7         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
8         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
9
10 """
11 from GeomDataAPI import *
12 from ModelAPI import *
13 import math
14 #=========================================================================
15 # Initialization of the test
16 #=========================================================================
17
18 __updated__ = "2015-03-16"
19
20 aSession = ModelAPI_Session.get()
21 aDocument = aSession.moduleDocument()
22 #=========================================================================
23 # Creation of a sketch
24 #=========================================================================
25 aSession.startOperation()
26 aSketchCommonFeature = aDocument.addFeature("Sketch")
27 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
28 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
29 origin.setValue(0, 0, 0)
30 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
31 dirx.setValue(1, 0, 0)
32 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
33 norm.setValue(0, 0, 1)
34 aSession.finishOperation()
35 #=========================================================================
36 # Creation of an arc and a circle
37 #=========================================================================
38 aSession.startOperation()
39 aSketchArc = aSketchFeature.addFeature("SketchArc")
40 anArcCentr = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
41 anArcCentr.setValue(10., 10.)
42 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcStartPoint"))
43 anArcStartPoint.setValue(0., 50.)
44 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
45 anArcEndPoint.setValue(50., 0.)
46 aSession.finishOperation()
47 # Circle
48 aSession.startOperation()
49 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
50 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
51 aCircleRadius = aSketchCircle.real("CircleRadius")
52 anCircleCentr.setValue(-25., -25)
53 aCircleRadius.setValue(25.)
54 aSession.finishOperation()
55 #=========================================================================
56 # A constraint to make equal radii of arc and circle
57 #=========================================================================
58 aSession.startOperation()
59 aConstraintEqRad = aSketchFeature.addFeature("SketchConstraintEqual")
60 aRefObjectA = aConstraintEqRad.refattr("ConstraintEntityA")
61 aRefObjectB = aConstraintEqRad.refattr("ConstraintEntityB")
62 aResultA = modelAPI_ResultConstruction(aSketchArc.lastResult())
63 aResultB = modelAPI_ResultConstruction(aSketchCircle.lastResult())
64 assert (aResultA is not None)
65 assert (aResultB is not None)
66 aRefObjectA.setObject(aResultA)
67 aRefObjectB.setObject(aResultB)
68 aConstraintEqRad.execute()
69 aSession.finishOperation()
70 aCircRadius = aCircleRadius.value();
71 anArcVecX = anArcStartPoint.x() - anArcCentr.x();
72 anArcVecY = anArcStartPoint.y() - anArcCentr.y();
73 anArcRadius = math.sqrt(anArcVecX**2 + anArcVecY**2)
74 assert (math.fabs(aCircRadius - anArcRadius) <= 1.e-10)
75 #=========================================================================
76 # Creation of two different lines
77 #=========================================================================
78 # First Line
79 aSession.startOperation()
80 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
81 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
82 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
83 aLine1StartPoint.setValue(0., 15.)
84 aLine1EndPoint.setValue(20., 25.)
85 aSession.finishOperation()
86 # Second Line
87 aSession.startOperation()
88 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
89 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
90 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
91 aLine2StartPoint.setValue(0., 0.)
92 aLine2EndPoint.setValue(-1., 10.)
93 aSession.finishOperation()
94 #=========================================================================
95 # A constraint to make equal lengths of lines
96 #=========================================================================
97 aSession.startOperation()
98 aConstraintEqLen = aSketchFeature.addFeature("SketchConstraintEqual")
99 aRefObjectA = aConstraintEqLen.refattr("ConstraintEntityA")
100 aRefObjectB = aConstraintEqLen.refattr("ConstraintEntityB")
101 aResultA = modelAPI_ResultConstruction(aSketchLine1.firstResult())
102 aResultB = modelAPI_ResultConstruction(aSketchLine2.firstResult())
103 assert (aResultA is not None)
104 assert (aResultB is not None)
105 aRefObjectA.setObject(aResultA)
106 aRefObjectB.setObject(aResultB)
107 aConstraintEqLen.execute()
108 aSession.finishOperation()
109 aVecX = aLine1StartPoint.x() - aLine1EndPoint.x();
110 aVecY = aLine1StartPoint.y() - aLine1EndPoint.y();
111 aLine1Len = math.sqrt(anArcVecX * anArcVecX + anArcVecY * anArcVecY)
112 aVecX = aLine2StartPoint.x() - aLine2EndPoint.x();
113 aVecY = aLine2StartPoint.y() - aLine2EndPoint.y();
114 aLine2Len = math.sqrt(anArcVecX**2 + anArcVecY**2)
115 assert (aLine1Len == aLine2Len)
116 #=========================================================================
117 # End of test
118 #=========================================================================