1 # Copyright (C) 2014-2021 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 email : webmaster.salome@opencascade.com
24 from GeomDataAPI import *
25 from ModelAPI import *
29 #=========================================================================
31 #=========================================================================
32 def distance(pointA, pointB):
34 subroutine to calculate distance between two points
35 result of calculated distance is has 10**-5 precision
37 xdiff = math.pow((pointA.x() - pointB.x()), 2)
38 ydiff = math.pow((pointA.y() - pointB.y()), 2)
39 return round(math.sqrt(xdiff + ydiff), 5)
42 def createNAngle(theSketch, theN, theRadius, theEdgeLength=0):
43 # Create an N-Angle at (0,0)
44 rad = 2. * math.pi / theN
47 x = round(math.cos(rad * a), 10) * theRadius
48 y = round(math.sin(rad * a), 10) * theRadius
51 points.append(points[0])
53 # Create lines iterating over (A,B), (B,C), ... (N,A)
57 for begin, end in zip(points[:-1], points[1:]):
58 aSketchLine = theSketch.addFeature("SketchLine")
59 aStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
60 anEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
61 aStartPoint.setValue(begin[0], begin[1])
62 anEndPoint.setValue(end[0], end[1])
64 allStartPoints.append(aStartPoint)
65 allEndPoints.append(anEndPoint)
66 allLines.append(aSketchLine)
68 # Shift a start point to the end of list:
69 # (Bb, Cb, Ab) zip (Ae, Be, Ce) --> ((Ae, Bb), (Cb, Be), (Ab, Ce))
70 allStartPoints.append(allStartPoints.pop(0))
71 # Make lines coincident:
72 for pointA, pointB in zip(allStartPoints, allEndPoints):
73 aCoincidence = theSketch.addFeature("SketchConstraintCoincidence")
74 aCoincidence.refattr("ConstraintEntityA").setAttr(pointA)
75 aCoincidence.refattr("ConstraintEntityB").setAttr(pointB)
79 def fixLineLength(theSketch, theLines, theEdgeLength=0):
80 aFirstLineLength = theEdgeLength
81 # Make give lines fixed length:
82 for aLine in theLines:
83 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
84 refattrA = aLengthConstraint.refattr("ConstraintEntityA")
85 refattrA.setObject(modelAPI_ResultConstruction(aLine.firstResult()))
86 if aFirstLineLength == 0:
87 pointA = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
88 pointB = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
89 aFirstLineLength = distance(pointA, pointB)
90 aLengthConstraint.real("ConstraintValue").setValue(aFirstLineLength)
93 def moveTo(theLines, theDeltaX, theDeltaY):
94 for aLine in theLines:
95 aStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
96 anEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
97 aStart.setValue(aStart.x() + theDeltaX, aStart.y() + theDeltaY)
98 anEnd.setValue(anEnd.x() + theDeltaX, anEnd.y() + theDeltaY)
100 #=========================================================================
101 # Initialization of the test
102 #=========================================================================
104 __updated__ = "2014-11-27"
106 aSession = ModelAPI_Session.get()
107 aDocument = aSession.moduleDocument()
108 #=========================================================================
109 # Creation of a sketch
110 #=========================================================================
111 aSession.startOperation()
112 aSketchCommonFeature = aDocument.addFeature("Sketch")
113 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
114 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
115 origin.setValue(0, 0, 0)
116 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
117 dirx.setValue(1, 0, 0)
118 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
119 norm.setValue(0, 0, 1)
120 aSession.finishOperation()
121 #=========================================================================
122 # Create 4x4 polygons N = {5, 21}
123 #=========================================================================
125 deltaX = deltaY = 50.
127 aSession.startOperation()
130 allNangleLines = createNAngle(aSketchFeature, n, 50)
131 fixLineLength(aSketchFeature, allNangleLines)
132 moveTo(allNangleLines, deltaX, deltaY)
138 aSession.finishOperation()
141 #=========================================================================
143 #=========================================================================
145 from salome.shaper import model
146 assert(model.dof(aSketchFeature) == aDOF)
147 assert(model.checkPythonDump())