3 Unit test of SketchPlugin_MultiRotation class
5 SketchPlugin_MultiRotation
6 static const std::string MY_CONSTRAINT_ROTATION_ID("SketchMultiRotation");
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(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
15 from GeomDataAPI import *
16 from ModelAPI import *
19 #=========================================================================
21 #=========================================================================
22 def createSketch(theSketch):
23 # Initialize sketch by 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)
37 def checkRotation(theObjects, theNbCopies, theCenterX, theCenterY, theAngle):
38 # Verify distances of the objects and the number of copies
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)
46 cosA = math.cos(theAngle * math.pi / 180.)
49 for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
51 if (anInd > theNbCopies):
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 aDirX1 = aPoint1.x() - theCenterX
69 aDirY1 = aPoint1.y() - theCenterY
70 aLen1 = math.hypot(aDirX1, aDirY1)
71 aDirX2 = aPoint2.x() - theCenterX
72 aDirY2 = aPoint2.y() - theCenterY
73 aLen2 = math.hypot(aDirX2, aDirY2)
74 aLocCosA = (aDirX1 * aDirX2 + aDirY1 * aDirY2) / aLen1 / aLen2
75 assert(math.fabs(aLocCosA - cosA) < 1.e-10)
76 # Check the number of copies is as planed
77 assert(anInd == theNbCopies)
80 #=========================================================================
81 # Initialization of the test
82 #=========================================================================
84 __updated__ = "2015-09-18"
86 aSession = ModelAPI_Session.get()
87 aDocument = aSession.moduleDocument()
88 #=========================================================================
89 # Creation of a sketch
90 #=========================================================================
91 aSession.startOperation()
92 aSketchCommonFeature = aDocument.addFeature("Sketch")
93 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
94 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
95 origin.setValue(0, 0, 0)
96 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
97 dirx.setValue(1, 0, 0)
98 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
99 norm.setValue(0, 0, 1)
100 aSession.finishOperation()
101 #=========================================================================
103 #=========================================================================
104 aSession.startOperation()
105 aFeaturesList = createSketch(aSketchFeature)
106 aSession.finishOperation()
107 #=========================================================================
109 #=========================================================================
113 #=========================================================================
114 # Create the Rotation constraint
115 #=========================================================================
116 aSession.startOperation()
117 aMultiRotation = aSketchFeature.addFeature("SketchMultiRotation")
118 aRotList = aMultiRotation.reflist("MultiRotationList")
119 for aFeature in aFeaturesList:
120 aResult = modelAPI_ResultConstruction(aFeature.lastResult())
121 assert(aResult is not None)
122 aRotList.append(aResult)
123 aCenter = geomDataAPI_Point2D(aMultiRotation.attribute("MultiRotationCenter"))
124 anAngle = aMultiRotation.real("MultiRotationAngle")
125 aCenter.setValue(CENTER_X, CENTER_Y)
126 anAngle.setValue(ANGLE)
127 aNbCopies = aMultiRotation.integer("MultiRotationCopies")
128 aNbCopies.setValue(1)
129 aMultiRotation.execute()
130 aSession.finishOperation()
131 #=========================================================================
132 # Verify the objects are moved for the specified distance
133 #=========================================================================
134 aRotated = aMultiRotation.reflist("ConstraintEntityB")
135 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
136 #=========================================================================
137 # Change number of copies and verify Rotation
138 #=========================================================================
139 aSession.startOperation()
140 aNbCopies.setValue(3)
141 aSession.finishOperation()
142 aRotated = aMultiRotation.reflist("ConstraintEntityB")
143 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
144 #=========================================================================
146 # 1. Add more features into Rotation
147 # 2. Move one of initial features and check the Rotated is moved too
148 #=========================================================================
149 #=========================================================================
151 #=========================================================================