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