2 TestMultiTranslation.py
3 Unit test of SketchPlugin_MultiTranslation class
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());
18 from GeomDataAPI import *
19 from ModelAPI import *
21 #=========================================================================
23 #=========================================================================
24 def createSketch(theSketch):
25 # Initialize sketch by 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)
39 def checkTranslation(theObjects, theNbObjects, theDeltaX, theDeltaY):
40 # Verify distances of the objects and the number of copies
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)
49 for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
51 if (anInd > theNbObjects):
54 assert(feat.getKind() == next.getKind())
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')
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)
75 #=========================================================================
76 # Initialization of the test
77 #=========================================================================
79 __updated__ = "2015-09-18"
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 #=========================================================================
98 #=========================================================================
99 aSession.startOperation()
100 aFeaturesList = createSketch(aSketchFeature)
101 aSession.finishOperation()
102 #=========================================================================
104 #=========================================================================
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)
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 #=========================================================================
147 # 1. Add more features into translation
148 # 2. Move one of initial features and check the translated is moved too
149 #=========================================================================
150 #=========================================================================
152 #=========================================================================