5 from GeomDataAPI import *
10 #=========================================================================
12 #=========================================================================
13 def distance(pointA, pointB):
15 subroutine to calculate distance between two points
16 result of calculated distance is has 10**-5 precision
18 xdiff = math.pow((pointA.x() - pointB.x()), 2)
19 ydiff = math.pow((pointA.y() - pointB.y()), 2)
20 return round(math.sqrt(xdiff + ydiff), 5)
23 def createNAngle(theSketch, theN, theRadius, theEdgeLength=0):
24 # Create an N-Angle at (0,0)
25 rad = 2. * math.pi / theN
27 for a in xrange(theN):
28 x = round(math.cos(rad * a), 10) * theRadius
29 y = round(math.sin(rad * a), 10) * theRadius
32 points.append(points[0])
34 # Create lines iterating over (A,B), (B,C), ... (N,A)
38 for begin, end in zip(points[:-1], points[1:]):
39 aSketchLine = theSketch.addFeature("SketchLine")
40 aStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
41 anEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
42 aStartPoint.setValue(begin[0], begin[1])
43 anEndPoint.setValue(end[0], end[1])
45 allStartPoints.append(aStartPoint)
46 allEndPoints.append(anEndPoint)
47 allLines.append(aSketchLine)
49 # Shift a start point to the end of list:
50 # (Bb, Cb, Ab) zip (Ae, Be, Ce) --> ((Ae, Bb), (Cb, Be), (Ab, Ce))
51 allStartPoints.append(allStartPoints.pop(0))
52 # Make lines coincident:
53 for pointA, pointB in zip(allStartPoints, allEndPoints):
54 aCoincidence = theSketch.addFeature("SketchConstraintCoincidence")
55 aCoincidence.refattr("ConstraintEntityA").setAttr(pointA)
56 aCoincidence.refattr("ConstraintEntityB").setAttr(pointB)
60 def fixLineLength(theSketch, theLines, theEdgeLength=0):
61 aFirstLineLength = theEdgeLength
62 # Make give lines fixed length:
63 for aLine in theLines:
64 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
65 refattrA = aLengthConstraint.refattr("ConstraintEntityA")
66 refattrA.setObject(modelAPI_ResultConstruction(aLine.firstResult()))
67 if aFirstLineLength == 0:
68 pointA = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
69 pointB = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
70 aFirstLineLength = distance(pointA, pointB)
71 aLengthConstraint.real("ConstraintValue").setValue(aFirstLineLength)
74 def moveTo(theLines, theDeltaX, theDeltaY):
75 for aLine in theLines:
76 aStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
77 anEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
78 aStart.setValue(aStart.x() + theDeltaX, aStart.y() + theDeltaY)
79 anEnd.setValue(anEnd.x() + theDeltaX, anEnd.y() + theDeltaY)
81 #=========================================================================
82 # Initialization of the test
83 #=========================================================================
85 __updated__ = "2014-11-27"
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 #=========================================================================
103 # Create 4x4 polygons N = {5, 21}
104 #=========================================================================
106 deltaX = deltaY = 50.
108 aSession.startOperation()
111 allNangleLines = createNAngle(aSketchFeature, n, 50)
112 fixLineLength(aSketchFeature, allNangleLines)
113 moveTo(allNangleLines, deltaX, deltaY)
119 aSession.finishOperation()
122 #=========================================================================
124 #=========================================================================
126 from salome.shaper import model
127 assert(model.dof(aSketchFeature) == aDOF)
128 assert(model.checkPythonDump())