Salome HOME
33ea0b0ad67379be6c24e8e18e8b629fab935bad
[modules/shaper.git] / src / BuildPlugin / Test / TestSubShapes.py
1 # Copyright (C) 2014-2021  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 email : webmaster.salome@opencascade.com
18 #
19
20 # Initialization of the test
21 from ModelAPI import *
22 from GeomDataAPI import *
23 from GeomAlgoAPI import *
24 from GeomAPI import *
25
26 import random
27
28 def createPoint(theSketchFeature):
29     aSketchPointFeature = theSketchFeature.addFeature("SketchPoint")
30     aPointCoordinates = geomDataAPI_Point2D(aSketchPointFeature.attribute("PointCoordinates"))
31     aPointCoordinates.setValue(random.uniform(0, 50), random.uniform(0, 50))
32     return aSketchPointFeature
33
34 def createLine(theSketchFeature):
35     aSketchLineFeature = theSketchFeature.addFeature("SketchLine")
36     aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
37     aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
38     aSketchLineStartPoint.setValue(random.uniform(0, 50), random.uniform(0, 50))
39     aSketchLineEndPoint.setValue(random.uniform(0, 50), random.uniform(0, 50))
40     return aSketchLineFeature
41
42 # Get document
43 aSession = ModelAPI_Session.get()
44 aDocument = aSession.moduleDocument()
45
46 # Create a part
47 aSession.startOperation()
48 aPartFeature = aDocument.addFeature("Part")
49 aSession.finishOperation()
50 aPartResult = modelAPI_ResultPart(aPartFeature.firstResult())
51 aPart = aPartResult.partDoc()
52
53 # Create a sketch
54 aSession.startOperation()
55 aSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
56 anOrigin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
57 anOrigin.setValue(0, 0, 0)
58 aDirX = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
59 aDirX.setValue(1, 0, 0)
60 aNorm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
61 aNorm.setValue(0, 0, 1)
62
63 # Create lines
64 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
65 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
66 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
67 aSketchLineStartPoint.setValue(0, 0)
68 aSketchLineEndPoint.setValue(0, 50)
69 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
70 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
71 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
72 aSketchLineStartPoint.setValue(0, 50)
73 aSketchLineEndPoint.setValue(50, 50)
74 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
75 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
76 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
77 aSketchLineStartPoint.setValue(50, 50)
78 aSketchLineEndPoint.setValue(50, 0)
79 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
80 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
81 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
82 aSketchLineStartPoint.setValue(50, 0)
83 aSketchLineEndPoint.setValue(0, 0)
84
85 aSession.finishOperation()
86 aSketchResult = aSketchFeature.firstResult()
87 aSketchShape = aSketchResult.shape()
88
89 # Create face
90 aSession.startOperation()
91 aFaceFeature = aPart.addFeature("Face")
92 aBaseObjectsList = aFaceFeature.selectionList("base_objects")
93 aShapeExplorer = GeomAPI_ShapeExplorer(aSketchShape, GeomAPI_Shape.EDGE)
94 while aShapeExplorer.more():
95     aBaseObjectsList.append(aSketchResult, aShapeExplorer.current())
96     aShapeExplorer.next()
97 aSession.finishOperation()
98 aFaceResult = aFaceFeature.firstResult()
99
100 # Create a sketch with points and lines
101 aSession.startOperation()
102 aSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
103 anOrigin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
104 anOrigin.setValue(0, 0, 0)
105 aDirX = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
106 aDirX.setValue(1, 0, 0)
107 aNorm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
108 aNorm.setValue(0, 0, 1)
109
110 # Create points
111 aNumOfPoints = 10
112 aPoints = []
113 for i in range(aNumOfPoints):
114     aSession.startOperation()
115     aSketchPointFeature = createPoint(aSketchFeature)
116     aSession.finishOperation()
117     aPoints.append(aSketchPointFeature.firstResult().shape())
118
119 # Create lines
120 aNumOfLines = 10
121 aLines = []
122 for i in range(aNumOfLines):
123     aSession.startOperation()
124     aSketchLineFeature = createLine(aSketchFeature)
125     aSession.finishOperation()
126     aLines.append(aSketchLineFeature.firstResult().shape())
127
128 aSession.finishOperation()
129 aSketchResult = aSketchFeature.firstResult()
130 aSketchShape = aSketchResult.shape()
131
132 # Create sub-shapes
133 aSession.startOperation()
134 aSubShapesFeature = aPart.addFeature("SubShapes")
135 aBaseShapeSelection = aSubShapesFeature.selection("base_shape")
136 aBaseShapeSelection.setValue(aFaceResult, None)
137 aSubShapesList = aSubShapesFeature.selectionList("subshapes")
138 for aPoint in aPoints:
139     aSubShapesList.append(aSketchResult, aPoint)
140 aShapeExplorer = GeomAPI_ShapeExplorer(aSketchShape, GeomAPI_Shape.EDGE)
141 while aShapeExplorer.more():
142     aSubShapesList.append(aSketchResult, aShapeExplorer.current())
143     aShapeExplorer.next()
144 aSession.finishOperation()
145
146 # Test results
147 assert (len(aSubShapesFeature.results()) > 0)
148
149 from salome.shaper import model
150
151 model.begin()
152 partSet = model.moduleDocument()
153 Part_1 = model.addPart(partSet)
154 Part_1_doc = Part_1.document()
155 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
156 SketchCircle_1 = Sketch_1.addCircle(-454.545454545455, 50.600343053173, 137.95899463189)
157 SketchCircle_2 = Sketch_1.addCircle(-454.545454545455, 50.600343053173, 62.129572131303)
158 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchCircle_1.center(), SketchCircle_2.center())
159 SketchPoint_1 = Sketch_1.addPoint(-490.566037735849, 50.600343053173)
160 SketchPoint_2 = Sketch_1.addPoint(-423.670668953688, 50.600343053173)
161 model.do()
162 Face_1 = model.addFace(Part_1_doc, [model.selection("EDGE", "Sketch_1/SketchCircle_1_2")])
163 Face_2 = model.addFace(Part_1_doc, [model.selection("EDGE", "Sketch_1/SketchCircle_2_2")])
164 SubShapes_1 = model.addSubShapes(Part_1_doc, model.selection("FACE", "Face_1_1"), [model.selection("VERTEX", "Sketch_1/SketchPoint_1"), model.selection("VERTEX", "Sketch_1/SketchPoint_2")])
165 SubShapes_1.setBaseShape(model.selection("FACE", "Face_2_1"))
166 model.end()
167
168 assert(model.checkPythonDump())