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])
44 allStartPoints.append(aStartPoint)
45 allEndPoints.append(anEndPoint)
46 allLines.append(aSketchLine)
48 # Shift a start point to the end of list:
49 # (Bb, Cb, Ab) zip (Ae, Be, Ce) --> ((Ae, Bb), (Cb, Be), (Ab, Ce))
50 allStartPoints.append(allStartPoints.pop(0))
51 # Make lines coincident:
52 for pointA, pointB in zip(allStartPoints, allEndPoints):
53 aCoincidence = theSketch.addFeature("SketchConstraintCoincidence")
54 aCoincidence.refattr("ConstraintEntityA").setAttr(pointA)
55 aCoincidence.refattr("ConstraintEntityB").setAttr(pointB)
59 def fixLineLength(theSketch, theLines, theEdgeLength=0):
60 aFirstLineLength = theEdgeLength
61 # Make give lines fixed length:
62 for aLine in theLines:
63 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
64 refattrA = aLengthConstraint.refattr("ConstraintEntityA")
65 refattrA.setObject(modelAPI_ResultConstruction(aLine.firstResult()))
66 if aFirstLineLength == 0:
67 pointA = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
68 pointB = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
69 aFirstLineLength = distance(pointA, pointB)
70 aLengthConstraint.real("ConstraintValue").setValue(aFirstLineLength)
73 def moveTo(theLines, theDeltaX, theDeltaY):
74 for aLine in theLines:
75 aStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
76 anEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
77 aStart.setValue(aStart.x() + theDeltaX, aStart.y() + theDeltaY)
78 anEnd.setValue(anEnd.x() + theDeltaX, anEnd.y() + theDeltaY)
80 #=========================================================================
81 # Initialization of the test
82 #=========================================================================
84 __updated__ = "2014-11-27"
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 = modelAPI_CompositeFeature(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 diry = geomDataAPI_Dir(aSketchFeature.attribute("DirY"))
99 diry.setValue(0, 1, 0)
100 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
101 norm.setValue(0, 0, 1)
102 aSession.finishOperation()
103 #=========================================================================
104 # Create 4x4 polygons N = {5, 21}
105 #=========================================================================
106 deltaX = deltaY = 50.
108 aSession.startOperation()
111 allNangleLines = createNAngle(aSketchFeature, n, 50)
112 fixLineLength(aSketchFeature, allNangleLines)
113 moveTo(allNangleLines, deltaX, deltaY)
118 aSession.finishOperation()
121 #=========================================================================
123 #=========================================================================