Salome HOME
Updated copyright comment
[modules/shaper.git] / src / SketchPlugin / Test / TestSnowflake.py
1 # Copyright (C) 2014-2024  CEA, EDF
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21     TestSnowflake.py
22 """
23 from GeomAPI import *
24 from GeomDataAPI import *
25 from ModelAPI import *
26 import collections
27 import math
28 import random
29
30
31 #=========================================================================
32 # Useful subroutines
33 #=========================================================================
34 def mirrorDiagonal(theSketch, allLines):
35     result = []
36     for aLine in allLines:
37         aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
38         anEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
39
40         aNewLine = aSketchFeature.addFeature("SketchLine")
41         aNewStartPoint = geomDataAPI_Point2D(aNewLine.attribute("StartPoint"))
42         aNewEndPoint = geomDataAPI_Point2D(aNewLine.attribute("EndPoint"))
43         aNewStartPoint.setValue(aStartPoint.y(), aStartPoint.x())
44         aNewEndPoint.setValue(anEndPoint.y(), anEndPoint.x())
45         result.append(aNewLine)
46     allLines.extend(result)
47
48
49 def mirrorX(theSketch, allLines):
50     result = []
51     for aLine in allLines:
52         aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
53         anEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
54
55         aNewLine = aSketchFeature.addFeature("SketchLine")
56         aNewStartPoint = geomDataAPI_Point2D(aNewLine.attribute("StartPoint"))
57         aNewEndPoint = geomDataAPI_Point2D(aNewLine.attribute("EndPoint"))
58         aNewStartPoint.setValue(aStartPoint.x() * -1., aStartPoint.y())
59         aNewEndPoint.setValue(anEndPoint.x() * -1., anEndPoint.y())
60         result.append(aNewLine)
61     allLines.extend(result)
62
63
64 def mirrorY(theSketch, allLines):
65     result = []
66     for aLine in allLines:
67         aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
68         anEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
69
70         aNewLine = aSketchFeature.addFeature("SketchLine")
71         aNewStartPoint = geomDataAPI_Point2D(aNewLine.attribute("StartPoint"))
72         aNewEndPoint = geomDataAPI_Point2D(aNewLine.attribute("EndPoint"))
73         aNewStartPoint.setValue(aStartPoint.x(), aStartPoint.y() * -1.)
74         aNewEndPoint.setValue(anEndPoint.x(), anEndPoint.y() * -1.)
75         result.append(aNewLine)
76     allLines.extend(result)
77
78
79 def initContour(theNumLines):
80     prevPoint = (35, 0)
81     result = []
82     deltaY = random.uniform(20, 25)
83     for i in range(1, theNumLines):
84         rangeXMax = (prevPoint[1] + deltaY) * (30. / i if i % 2 != 0 else 2)
85         newX = random.uniform(prevPoint[1] + deltaY,
86                               max(rangeXMax, prevPoint[1] + deltaY + 25.))
87         newPoint = (round(newX, 4), round(prevPoint[1] + deltaY, 4))
88         result.append((prevPoint, newPoint))
89         prevPoint = newPoint
90     # Close the contour
91     result.append((prevPoint, (prevPoint[1], prevPoint[1])))
92     return result
93
94
95 def makeLinesCoincident(theSketch, allLines):
96     allPoints = collections.defaultdict(list)
97     for aLine in allLines:
98         aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
99         anEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
100         aStartPointValue = (aStartPoint.x(), aStartPoint.y())
101         anEndPointValue = (anEndPoint.x(), anEndPoint.y())
102         allPoints[aStartPointValue].append(aStartPoint)
103         allPoints[anEndPointValue].append(anEndPoint)
104     for keyPoint, valuePoints in allPoints.items():
105         if len(valuePoints) != 2:
106             print("Strange point: ", keyPoint, "has in coincidence: ", len(valuePoints))
107             continue
108         aConstraint = theSketch.addFeature("SketchConstraintCoincidence")
109         aConstraint.refattr("ConstraintEntityA").setAttr(valuePoints[0])
110         aConstraint.refattr("ConstraintEntityB").setAttr(valuePoints[1])
111
112
113 #=========================================================================
114 # Initialization of the test
115 #=========================================================================
116
117 __updated__ = "2014-11-27"
118
119 aSession = ModelAPI_Session.get()
120 aDocument = aSession.moduleDocument()
121 #=========================================================================
122 # Creation of a sketch
123 #=========================================================================
124 aSession.startOperation()
125 aSketchCommonFeature = aDocument.addFeature("Sketch")
126 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
127 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
128 origin.setValue(0, 0, 0)
129 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
130 dirx.setValue(1, 0, 0)
131 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
132 norm.setValue(0, 0, 1)
133 #=========================================================================
134 # 1 With the predefined contour or a new random generated in X0Y
135 # 2 Make complete contour using the diagonal, X and Y mirror symmetry
136 # 3 Link all lines in contour with coincidence
137 #=========================================================================
138 initialContour = [((35, 0), (134.8224, 22.0494)), ((134.8224, 22.0494), (71.987, 44.0988)),
139                   ((71.987, 44.0988), (205.1448, 66.1482)), ((205.1448, 66.1482), (127.862, 88.1976)),
140                   ((127.862, 88.1976), (416.861, 110.247)), ((416.861, 110.247), (233.0475, 132.2964)),
141                   ((233.0475, 132.2964), (251.1518, 154.3458)), ((251.1518, 154.3458), (351.0993, 176.3952)),
142                   ((351.0993, 176.3952), (432.3397, 198.4446)), ((432.3397, 198.4446), (223.3246, 220.494)),
143                   ((223.3246, 220.494), (617.4069, 242.5434)), ((617.4069, 242.5434), (479.6524, 264.5928)),
144                   ((479.6524, 264.5928), (453.5778, 286.6422)), ((453.5778, 286.6422), (541.3463, 308.6916)),
145                   ((541.3463, 308.6916), (564.5509, 330.741)), ((564.5509, 330.741), (636.9284, 352.7904)),
146                   ((636.9284, 352.7904), (383.5946, 374.8398)), ((383.5946, 374.8398), (403.3764, 396.8892)),
147                   ((403.3764, 396.8892), (463.9447, 418.9386)), ((463.9447, 418.9386), (669.1751, 440.988)),
148                   ((669.1751, 440.988), (602.2044, 463.0374)), ((602.2044, 463.0374), (942.0456, 485.0868)),
149                   ((942.0456, 485.0868), (526.574, 507.1362)), ((526.574, 507.1362), (826.3619, 529.1856)),
150                   ((826.3619, 529.1856), (576.9488, 551.235)), ((576.9488, 551.235), (624.5595, 573.2844)),
151                   ((624.5595, 573.2844), (648.7146, 595.3338)), ((648.7146, 595.3338), (1194.6944, 617.3832)),
152                   ((1194.6944, 617.3832), (646.6597, 639.4326)), ((646.6597, 639.4326), (839.8201, 661.482)),
153                   ((839.8201, 661.482), (690.7487, 683.5314)), ((690.7487, 683.5314), (1350.2538, 705.5808)),
154                   ((1350.2538, 705.5808), (731.0722, 727.6302)), ((731.0722, 727.6302), (1324.0992, 749.6796)),
155                   ((1324.0992, 749.6796), (790.4873, 771.729)), ((790.4873, 771.729), (813.9883, 793.7784)),
156                   ((813.9883, 793.7784), (828.9997, 815.8278)), ((828.9997, 815.8278), (1321.9798, 837.8772)),
157                   ((1321.9798, 837.8772), (872.1503, 859.9266)), ((872.1503, 859.9266), (859.9266, 859.9266))]
158
159 # Regenerate contour
160 # initialContour = initContour(40)
161 allLines = []
162 for begin, end in initialContour:
163     aNewLine = aSketchFeature.addFeature("SketchLine")
164     aStartPoint = geomDataAPI_Point2D(aNewLine.attribute("StartPoint"))
165     anEndPoint = geomDataAPI_Point2D(aNewLine.attribute("EndPoint"))
166     aStartPoint.setValue(begin[0], begin[1])
167     anEndPoint.setValue(end[0], end[1])
168     allLines.append(aNewLine)
169 mirrorDiagonal(aSketchFeature, allLines)
170 mirrorX(aSketchFeature, allLines)
171 mirrorY(aSketchFeature, allLines)
172 makeLinesCoincident(aSketchFeature, allLines)
173 aSession.finishOperation()
174 #=========================================================================
175 # End of test
176 #=========================================================================
177
178 from salome.shaper import model
179 assert(model.checkPythonDump())