1 ## Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 ## This library is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU Lesser General Public
5 ## License as published by the Free Software Foundation; either
6 ## version 2.1 of the License, or (at your option) any later version.
8 ## This library is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ## Lesser General Public License for more details.
13 ## You should have received a copy of the GNU Lesser General Public
14 ## License along with this library; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
25 from GeomDataAPI import *
26 from ModelAPI import *
30 #=========================================================================
32 #=========================================================================
33 def distance(pointA, pointB):
35 subroutine to calculate distance between two points
36 result of calculated distance is has 10**-5 precision
38 xdiff = math.pow((pointA.x() - pointB.x()), 2)
39 ydiff = math.pow((pointA.y() - pointB.y()), 2)
40 return round(math.sqrt(xdiff + ydiff), 5)
43 def createNAngle(theSketch, theN, theRadius, theEdgeLength=0):
44 # Create an N-Angle at (0,0)
45 rad = 2. * math.pi / theN
48 x = round(math.cos(rad * a), 10) * theRadius
49 y = round(math.sin(rad * a), 10) * theRadius
52 points.append(points[0])
54 # Create lines iterating over (A,B), (B,C), ... (N,A)
58 for begin, end in zip(points[:-1], points[1:]):
59 aSketchLine = theSketch.addFeature("SketchLine")
60 aStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
61 anEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
62 aStartPoint.setValue(begin[0], begin[1])
63 anEndPoint.setValue(end[0], end[1])
65 allStartPoints.append(aStartPoint)
66 allEndPoints.append(anEndPoint)
67 allLines.append(aSketchLine)
69 # Shift a start point to the end of list:
70 # (Bb, Cb, Ab) zip (Ae, Be, Ce) --> ((Ae, Bb), (Cb, Be), (Ab, Ce))
71 allStartPoints.append(allStartPoints.pop(0))
72 # Make lines coincident:
73 for pointA, pointB in zip(allStartPoints, allEndPoints):
74 aCoincidence = theSketch.addFeature("SketchConstraintCoincidence")
75 aCoincidence.refattr("ConstraintEntityA").setAttr(pointA)
76 aCoincidence.refattr("ConstraintEntityB").setAttr(pointB)
80 def fixLineLength(theSketch, theLines, theEdgeLength=0):
81 aFirstLineLength = theEdgeLength
82 # Make give lines fixed length:
83 for aLine in theLines:
84 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
85 refattrA = aLengthConstraint.refattr("ConstraintEntityA")
86 refattrA.setObject(modelAPI_ResultConstruction(aLine.firstResult()))
87 if aFirstLineLength == 0:
88 pointA = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
89 pointB = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
90 aFirstLineLength = distance(pointA, pointB)
91 aLengthConstraint.real("ConstraintValue").setValue(aFirstLineLength)
94 def moveTo(theLines, theDeltaX, theDeltaY):
95 for aLine in theLines:
96 aStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
97 anEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
98 aStart.setValue(aStart.x() + theDeltaX, aStart.y() + theDeltaY)
99 anEnd.setValue(anEnd.x() + theDeltaX, anEnd.y() + theDeltaY)
101 #=========================================================================
102 # Initialization of the test
103 #=========================================================================
105 __updated__ = "2014-11-27"
107 aSession = ModelAPI_Session.get()
108 aDocument = aSession.moduleDocument()
109 #=========================================================================
110 # Creation of a sketch
111 #=========================================================================
112 aSession.startOperation()
113 aSketchCommonFeature = aDocument.addFeature("Sketch")
114 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
115 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
116 origin.setValue(0, 0, 0)
117 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
118 dirx.setValue(1, 0, 0)
119 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
120 norm.setValue(0, 0, 1)
121 aSession.finishOperation()
122 #=========================================================================
123 # Create 4x4 polygons N = {5, 21}
124 #=========================================================================
126 deltaX = deltaY = 50.
128 aSession.startOperation()
131 allNangleLines = createNAngle(aSketchFeature, n, 50)
132 fixLineLength(aSketchFeature, allNangleLines)
133 moveTo(allNangleLines, deltaX, deltaY)
139 aSession.finishOperation()
142 #=========================================================================
144 #=========================================================================
146 from salome.shaper import model
147 assert(model.dof(aSketchFeature) == aDOF)
148 assert(model.checkPythonDump())