Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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.firstResult())
65 aResultB = modelAPI_ResultConstruction(aSketchCircle.firstResult())
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 * anArcVecX + anArcVecY * anArcVecY)
76 print anArcStartPoint.x(), anArcStartPoint.y()
77 print anArcCentr.x(), anArcCentr.y()
78 print aCircRadius, anArcRadius
79 assert (aCircRadius == anArcRadius)
80 #=========================================================================
81 # Creation of two different lines
82 #=========================================================================
83 # First Line
84 aSession.startOperation()
85 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
86 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
87 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
88 aLine1StartPoint.setValue(0., 15.)
89 aLine1EndPoint.setValue(20., 25.)
90 aSession.finishOperation()
91 # Second Line
92 aSession.startOperation()
93 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
94 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
95 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
96 aLine2StartPoint.setValue(0., 0.)
97 aLine2EndPoint.setValue(-1., 10.)
98 aSession.finishOperation()
99 #=========================================================================
100 # A constraint to make equal lengths of lines
101 #=========================================================================
102 aSession.startOperation()
103 aConstraintEqLen = aSketchFeature.addFeature("SketchConstraintEqual")
104 aRefObjectA = aConstraintEqLen.refattr("ConstraintEntityA")
105 aRefObjectB = aConstraintEqLen.refattr("ConstraintEntityB")
106 aResultA = modelAPI_ResultConstruction(aSketchLine1.firstResult())
107 aResultB = modelAPI_ResultConstruction(aSketchLine2.firstResult())
108 assert (aResultA is not None)
109 assert (aResultB is not None)
110 aRefObjectA.setObject(aResultA)
111 aRefObjectB.setObject(aResultB)
112 aConstraintEqLen.execute()
113 aSession.finishOperation()
114 aVecX = aLine1StartPoint.x() - aLine1EndPoint.x();
115 aVecY = aLine1StartPoint.y() - aLine1EndPoint.y();
116 aLine1Len = math.sqrt(anArcVecX * anArcVecX + anArcVecY * anArcVecY)
117 aVecX = aLine2StartPoint.x() - aLine2EndPoint.x();
118 aVecY = aLine2StartPoint.y() - aLine2EndPoint.y();
119 aLine2Len = math.sqrt(anArcVecX * anArcVecX + anArcVecY * anArcVecY)
120 assert (aLine1Len == aLine2Len)
121 #=========================================================================
122 # End of test
123 #=========================================================================