3 Unit test of SketchPlugin_MultiRotation class
5 SketchPlugin_MultiRotation
6 static const std::string MY_CONSTRAINT_ROTATION_ID("SketchMultiRotation");
7 data()->addAttribute(ANGLE_TYPE(), ModelAPI_AttributeString::typeId());
8 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
9 data()->addAttribute(ANGLE_FULL_ID(), ModelAPI_AttributeDouble::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(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
16 from GeomDataAPI import *
17 from ModelAPI import *
20 #=========================================================================
22 #=========================================================================
23 def createSketch(theSketch):
24 # Initialize sketch by arc
27 aSketchArc = theSketch.addFeature("SketchArc")
28 aCenter = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
29 aStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcStartPoint"))
30 aEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
31 aCenter.setValue(5., 5.)
32 aStartPoint.setValue(10., 5.)
33 aEndPoint.setValue(5., 10.)
34 allFeatures.append(aSketchArc)
38 def checkRotation(theObjects, theNbObjects, theCenterX, theCenterY, theAngle):
39 # Verify distances of the objects and the number of copies
41 aSize = theObjects.size()
42 for i in range (0, aSize):
43 feat = ModelAPI_Feature.feature(theObjects.object(i))
44 assert(feat is not None)
45 aFeatures.append(feat)
47 cosA = math.cos(theAngle * math.pi / 180.)
50 for feat, next in zip(aFeatures[:-1], aFeatures[1:]):
52 if (anInd > theNbObjects-1):
55 assert(feat.getKind() == next.getKind())
58 if (feat.getKind() == "SketchLine"):
59 anAttributes.append('StartPoint')
60 anAttributes.append('EndPoint')
61 elif (feat.getKind() == "SketchArc"):
62 anAttributes.append('ArcCenter')
63 anAttributes.append('ArcStartPoint')
64 anAttributes.append('ArcEndPoint')
66 for attr in anAttributes:
67 aPoint1 = geomDataAPI_Point2D(feat.attribute(attr))
68 aPoint2 = geomDataAPI_Point2D(next.attribute(attr))
69 aDirX1 = aPoint1.x() - theCenterX
70 aDirY1 = aPoint1.y() - theCenterY
71 aLen1 = math.hypot(aDirX1, aDirY1)
72 aDirX2 = aPoint2.x() - theCenterX
73 aDirY2 = aPoint2.y() - theCenterY
74 aLen2 = math.hypot(aDirX2, aDirY2)
75 aLocCosA = (aDirX1 * aDirX2 + aDirY1 * aDirY2) / aLen1 / aLen2
76 assert(math.fabs(aLocCosA - cosA) < 1.e-10)
77 # Check the number of copies is as planed
78 assert(anInd == theNbObjects-1)
81 #=========================================================================
82 # Initialization of the test
83 #=========================================================================
85 __updated__ = "2015-09-18"
87 aSession = ModelAPI_Session.get()
88 aDocument = aSession.moduleDocument()
89 #=========================================================================
90 # Creation of a sketch
91 #=========================================================================
92 aSession.startOperation()
93 aSketchCommonFeature = aDocument.addFeature("Sketch")
94 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
95 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
96 origin.setValue(0, 0, 0)
97 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
98 dirx.setValue(1, 0, 0)
99 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
100 norm.setValue(0, 0, 1)
101 aSession.finishOperation()
102 #=========================================================================
104 #=========================================================================
105 aSession.startOperation()
106 aFeaturesList = createSketch(aSketchFeature)
107 aSession.finishOperation()
108 #=========================================================================
110 #=========================================================================
114 #=========================================================================
115 # Create rotation point
116 #=========================================================================
117 aSession.startOperation()
118 aRotationPoint = aSketchFeature.addFeature("SketchPoint")
119 aRotationPointPoint = geomDataAPI_Point2D(aRotationPoint.attribute("PointCoordindates"))
120 aRotationPointPoint.setValue(CENTER_X, CENTER_Y)
121 aSession.finishOperation()
122 #=========================================================================
123 # Create the Rotation constraint
124 #=========================================================================
125 aSession.startOperation()
126 aMultiRotation = aSketchFeature.addFeature("SketchMultiRotation")
127 aRotList = aMultiRotation.reflist("MultiRotationList")
128 for aFeature in aFeaturesList:
129 aResult = modelAPI_ResultConstruction(aFeature.lastResult())
130 assert(aResult is not None)
131 aRotList.append(aResult)
133 aValueType = aMultiRotation.string("AngleType")
134 aValueType.setValue("SingleValue")
136 aCenter = aMultiRotation.refattr("MultiRotationCenter")
137 aCenter.setAttr(aRotationPointPoint)
139 anAngle = aMultiRotation.real("MultiRotationAngle")
140 anAngle.setValue(ANGLE)
142 anAngle = aMultiRotation.string("AngleType")
143 anAngle.setValue("SingleAngle")
145 aNbCopies = aMultiRotation.integer("MultiRotationObjects")
146 aNbCopies.setValue(2)
147 aMultiRotation.execute()
148 aSession.finishOperation()
149 #=========================================================================
150 # Verify the objects are moved for the specified distance
151 #=========================================================================
152 aRotated = aMultiRotation.reflist("ConstraintEntityB")
153 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
154 #=========================================================================
155 # Change number of copies and verify Rotation
156 #=========================================================================
157 aSession.startOperation()
158 aNbCopies.setValue(3)
159 aSession.finishOperation()
160 aRotated = aMultiRotation.reflist("ConstraintEntityB")
161 checkRotation(aRotated, aNbCopies.value(), CENTER_X, CENTER_Y, ANGLE)
162 #=========================================================================
164 # 1. Add more features into Rotation
165 # 2. Move one of initial features and check the Rotated is moved too
166 #=========================================================================
167 #=========================================================================
169 #=========================================================================