Salome HOME
Union of validator and filter functionalities.
[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::type());
8         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
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 = modelAPI_CompositeFeature(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 diry = geomDataAPI_Dir(aSketchFeature.attribute("DirY"))
33 diry.setValue(0, 1, 0)
34 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
35 norm.setValue(0, 0, 1)
36 aSession.finishOperation()
37 #=========================================================================
38 # Creation of an arc and a circle
39 #=========================================================================
40 aSession.startOperation()
41 aSketchArc = aSketchFeature.addFeature("SketchArc")
42 anArcCentr = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
43 anArcCentr.setValue(10., 10.)
44 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcStartPoint"))
45 anArcStartPoint.setValue(0., 50.)
46 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
47 anArcEndPoint.setValue(50., 0.)
48 aSession.finishOperation()
49 # Circle
50 aSession.startOperation()
51 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
52 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
53 aCircleRadius = aSketchCircle.real("CircleRadius")
54 anCircleCentr.setValue(-25., -25)
55 aCircleRadius.setValue(25.)
56 aSession.finishOperation()
57 #=========================================================================
58 # A constraint to make equal radii of arc and circle
59 #=========================================================================
60 aSession.startOperation()
61 aConstraintEqRad = aSketchFeature.addFeature("SketchConstraintEqual")
62 aRefObjectA = aConstraintEqRad.refattr("ConstraintEntityA")
63 aRefObjectB = aConstraintEqRad.refattr("ConstraintEntityB")
64 aResultA = modelAPI_ResultConstruction(aSketchArc.lastResult())
65 aResultB = modelAPI_ResultConstruction(aSketchCircle.lastResult())
66 assert (aResultA is not None)
67 assert (aResultB is not None)
68 aRefObjectA.setObject(aResultA)
69 aRefObjectB.setObject(aResultB)
70 aConstraintEqRad.execute()
71 aSession.finishOperation()
72 aCircRadius = aCircleRadius.value();
73 anArcVecX = anArcStartPoint.x() - anArcCentr.x();
74 anArcVecY = anArcStartPoint.y() - anArcCentr.y();
75 anArcRadius = math.sqrt(anArcVecX**2 + anArcVecY**2)
76 assert (math.fabs(aCircRadius - anArcRadius) <= 1.e-10)
77 #=========================================================================
78 # Creation of two different lines
79 #=========================================================================
80 # First Line
81 aSession.startOperation()
82 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
83 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
84 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
85 aLine1StartPoint.setValue(0., 15.)
86 aLine1EndPoint.setValue(20., 25.)
87 aSession.finishOperation()
88 # Second Line
89 aSession.startOperation()
90 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
91 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
92 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
93 aLine2StartPoint.setValue(0., 0.)
94 aLine2EndPoint.setValue(-1., 10.)
95 aSession.finishOperation()
96 #=========================================================================
97 # A constraint to make equal lengths of lines
98 #=========================================================================
99 aSession.startOperation()
100 aConstraintEqLen = aSketchFeature.addFeature("SketchConstraintEqual")
101 aRefObjectA = aConstraintEqLen.refattr("ConstraintEntityA")
102 aRefObjectB = aConstraintEqLen.refattr("ConstraintEntityB")
103 aResultA = modelAPI_ResultConstruction(aSketchLine1.firstResult())
104 aResultB = modelAPI_ResultConstruction(aSketchLine2.firstResult())
105 assert (aResultA is not None)
106 assert (aResultB is not None)
107 aRefObjectA.setObject(aResultA)
108 aRefObjectB.setObject(aResultB)
109 aConstraintEqLen.execute()
110 aSession.finishOperation()
111 aVecX = aLine1StartPoint.x() - aLine1EndPoint.x();
112 aVecY = aLine1StartPoint.y() - aLine1EndPoint.y();
113 aLine1Len = math.sqrt(anArcVecX * anArcVecX + anArcVecY * anArcVecY)
114 aVecX = aLine2StartPoint.x() - aLine2EndPoint.x();
115 aVecY = aLine2StartPoint.y() - aLine2EndPoint.y();
116 aLine2Len = math.sqrt(anArcVecX**2 + anArcVecY**2)
117 assert (aLine1Len == aLine2Len)
118 #=========================================================================
119 # End of test
120 #=========================================================================