Salome HOME
Merge Dev_2.1.0 with PythonAPI branch
[modules/shaper.git] / src / SketchPlugin / Test / TestMultiTranslation.py
1 """
2     TestMultiTranslation.py
3     Unit test of SketchPlugin_MultiTranslation class
4         
5     SketchPlugin_MultiTranslation
6         static const std::string MY_CONSTRAINT_TRANSLATION_ID("SketchMultiTranslation");
7         data()->addAttribute(VALUE_TYPE(), ModelAPI_AttributeString::typeId());
8         data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
9         data()->addAttribute(START_FULL_POINT_ID(), GeomDataAPI_Point2D::typeId());
10         data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
11         data()->addAttribute(END_FULL_POINT_ID(), GeomDataAPI_Point2D::typeId());
12         data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
13         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
14         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
15         data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
16
17 """
18 from GeomDataAPI import *
19 from ModelAPI import *
20
21 #=========================================================================
22 # Auxiliary functions
23 #=========================================================================
24 def createSketch(theSketch):
25     # Initialize sketch by arc
26     allFeatures = []
27     # Create arc
28     aSketchArc = theSketch.addFeature("SketchArc")
29     aCenter     = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
30     aStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcStartPoint"))
31     aEndPoint   = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
32     aCenter.setValue(5., 5.)
33     aStartPoint.setValue(10., 5.)
34     aEndPoint.setValue(5., 10.)
35     allFeatures.append(aSketchArc)
36     theSketch.execute()
37     return allFeatures
38     
39 def checkTranslation(theObjects, theNbObjects, theDeltaX, theDeltaY):
40     # Verify distances of the objects and the number of copies
41     aFeatures = []
42     aSize = theObjects.size()
43     for i in range (0, aSize):
44         feat = ModelAPI_Feature.feature(theObjects.object(i))
45         assert(feat is not None)
46         aFeatures.append(feat)
47         
48     anInd = 0 
49     for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
50         anInd = anInd + 1
51         if (anInd > theNbObjects):
52             anInd = 0
53             continue
54         assert(feat.getKind() == next.getKind())
55         
56         anAttributes = []
57         if (feat.getKind() == "SketchLine"):
58             anAttributes.append('StartPoint')
59             anAttributes.append('EndPoint')
60         elif (feat.getKind() == "SketchArc"):
61             anAttributes.append('ArcCenter')
62             anAttributes.append('ArcStartPoint')
63             anAttributes.append('ArcEndPoint')
64             
65         for attr in anAttributes:
66             aPoint1 = geomDataAPI_Point2D(feat.attribute(attr))
67             aPoint2 = geomDataAPI_Point2D(next.attribute(attr))
68             aDiffX = aPoint2.x() - aPoint1.x() - theDeltaX
69             aDiffY = aPoint2.y() - aPoint1.y() - theDeltaY
70             assert(aDiffX**2 + aDiffY**2 < 1.e-15)
71     # Check the number of copies is as planed
72     assert(anInd == theNbObjects-1)
73
74
75 #=========================================================================
76 # Initialization of the test
77 #=========================================================================
78
79 __updated__ = "2015-09-18"
80
81 aSession = ModelAPI_Session.get()
82 aDocument = aSession.moduleDocument()
83 #=========================================================================
84 # Creation of a sketch
85 #=========================================================================
86 aSession.startOperation()
87 aSketchCommonFeature = aDocument.addFeature("Sketch")
88 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
89 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
90 origin.setValue(0, 0, 0)
91 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
92 dirx.setValue(1, 0, 0)
93 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
94 norm.setValue(0, 0, 1)
95 aSession.finishOperation()
96 #=========================================================================
97 # Initialize sketch
98 #=========================================================================
99 aSession.startOperation()
100 aFeaturesList = createSketch(aSketchFeature)
101 aSession.finishOperation()
102 #=========================================================================
103 # Global variables
104 #=========================================================================
105 START_X = 0.
106 START_Y = 15.
107 DIR_X = 20.
108 DIR_Y = 10.
109 DELTA_X = -5.
110 DELTA_Y = 3.
111 #=========================================================================
112 # Create the Translation constraint
113 #=========================================================================
114 aSession.startOperation()
115 aMultiTranslation = aSketchFeature.addFeature("SketchMultiTranslation")
116 aTransList = aMultiTranslation.reflist("MultiTranslationList")
117 for aFeature in aFeaturesList:
118     aResult = modelAPI_ResultConstruction(aFeature.lastResult())
119     assert(aResult is not None)
120     aTransList.append(aResult)
121
122 aValueType = aMultiTranslation.string("ValueType")
123 aValueType.setValue("SingleValue")
124 aStartPoint = geomDataAPI_Point2D(aMultiTranslation.attribute("MultiTranslationStartPoint"))
125 aEndPoint = geomDataAPI_Point2D(aMultiTranslation.attribute("MultiTranslationEndPoint"))
126 aStartPoint.setValue(START_X, START_Y)
127 aEndPoint.setValue(START_X + DIR_X, START_Y + DIR_Y)
128 aNbCopies = aMultiTranslation.integer("MultiTranslationObjects")
129 aNbCopies.setValue(2)
130 aMultiTranslation.execute()
131 aSession.finishOperation()
132 #=========================================================================
133 # Verify the objects are moved for the specified distance
134 #=========================================================================
135 aTranslated = aMultiTranslation.reflist("ConstraintEntityB")
136 checkTranslation(aTranslated, aNbCopies.value(), DIR_X, DIR_Y)
137 #=========================================================================
138 # Change number of copies and verify translation
139 #=========================================================================
140 aSession.startOperation()
141 aNbCopies.setValue(3)
142 aSession.finishOperation()
143 aTranslated = aMultiTranslation.reflist("ConstraintEntityB")
144 checkTranslation(aTranslated, aNbCopies.value(), DIR_X, DIR_Y)
145 #=========================================================================
146 # TODO: improve test
147 # 1. Add more features into translation
148 # 2. Move one of initial features and check the translated is moved too
149 #=========================================================================
150 #=========================================================================
151 # End of test
152 #=========================================================================