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