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