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(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
8 data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
9 data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeInteger::typeId());
10 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
11 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
12 data()->addAttribute(TRANSLATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
15 from GeomDataAPI import *
16 from ModelAPI import *
18 #=========================================================================
20 #=========================================================================
21 def createSketch(theSketch):
22 # Initialize sketch by arc
25 aSketchArc = theSketch.addFeature("SketchArc")
26 aCenter = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
27 aStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcStartPoint"))
28 aEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
29 aCenter.setValue(5., 5.)
30 aStartPoint.setValue(10., 5.)
31 aEndPoint.setValue(5., 10.)
32 allFeatures.append(aSketchArc)
36 def checkTranslation(theObjects, theNbCopies, theDeltaX, theDeltaY):
37 # Verify distances of the objects and the number of copies
39 aSize = theObjects.size()
40 for i in range (0, aSize):
41 feat = ModelAPI_Feature.feature(theObjects.object(i))
42 assert(feat is not None)
43 aFeatures.append(feat)
46 for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
48 if (anInd > theNbCopies):
51 assert(feat.getKind() == next.getKind())
54 if (feat.getKind() == "SketchLine"):
55 anAttributes.append('StartPoint')
56 anAttributes.append('EndPoint')
57 elif (feat.getKind() == "SketchArc"):
58 anAttributes.append('ArcCenter')
59 anAttributes.append('ArcStartPoint')
60 anAttributes.append('ArcEndPoint')
62 for attr in anAttributes:
63 aPoint1 = geomDataAPI_Point2D(feat.attribute(attr))
64 aPoint2 = geomDataAPI_Point2D(next.attribute(attr))
65 aDiffX = aPoint2.x() - aPoint1.x() - theDeltaX
66 aDiffY = aPoint2.y() - aPoint1.y() - theDeltaY
67 assert(aDiffX**2 + aDiffY**2 < 1.e-15)
68 # Check the number of copies is as planed
69 assert(anInd == theNbCopies)
72 #=========================================================================
73 # Initialization of the test
74 #=========================================================================
76 __updated__ = "2015-09-18"
78 aSession = ModelAPI_Session.get()
79 aDocument = aSession.moduleDocument()
80 #=========================================================================
81 # Creation of a sketch
82 #=========================================================================
83 aSession.startOperation()
84 aSketchCommonFeature = aDocument.addFeature("Sketch")
85 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
86 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
87 origin.setValue(0, 0, 0)
88 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
89 dirx.setValue(1, 0, 0)
90 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
91 norm.setValue(0, 0, 1)
92 aSession.finishOperation()
93 #=========================================================================
95 #=========================================================================
96 aSession.startOperation()
97 aFeaturesList = createSketch(aSketchFeature)
98 aSession.finishOperation()
99 #=========================================================================
101 #=========================================================================
108 #=========================================================================
109 # Create the Translation constraint
110 #=========================================================================
111 aSession.startOperation()
112 aMultiTranslation = aSketchFeature.addFeature("SketchMultiTranslation")
113 aTransList = aMultiTranslation.reflist("MultiTranslationList")
114 for aFeature in aFeaturesList:
115 aResult = modelAPI_ResultConstruction(aFeature.lastResult())
116 assert(aResult is not None)
117 aTransList.append(aResult)
118 aStartPoint = geomDataAPI_Point2D(aMultiTranslation.attribute("MultiTranslationStartPoint"))
119 aEndPoint = geomDataAPI_Point2D(aMultiTranslation.attribute("MultiTranslationEndPoint"))
120 aStartPoint.setValue(START_X, START_Y)
121 aEndPoint.setValue(START_X + DIR_X, START_Y + DIR_Y)
122 aNbCopies = aMultiTranslation.integer("MultiTranslationCopies")
123 aNbCopies.setValue(1)
124 aMultiTranslation.execute()
125 aSession.finishOperation()
126 #=========================================================================
127 # Verify the objects are moved for the specified distance
128 #=========================================================================
129 aTranslated = aMultiTranslation.reflist("ConstraintEntityB")
130 checkTranslation(aTranslated, aNbCopies.value(), DIR_X, DIR_Y)
131 #=========================================================================
132 # Change number of copies and verify translation
133 #=========================================================================
134 aSession.startOperation()
135 aNbCopies.setValue(3)
136 aSession.finishOperation()
137 aTranslated = aMultiTranslation.reflist("ConstraintEntityB")
138 checkTranslation(aTranslated, aNbCopies.value(), DIR_X, DIR_Y)
139 #=========================================================================
141 # 1. Add more features into translation
142 # 2. Move one of initial features and check the translated is moved too
143 #=========================================================================
144 #=========================================================================
146 #=========================================================================