Salome HOME
updated copyright message
[modules/shaper.git] / src / PythonAPI / Test / TestShapeInfo.py
1 # Copyright (C) 2014-2023  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 from salome.shaper import model
21 from salome.shaper import geom
22 from GeomAPI import *
23
24 model.begin()
25 partSet = model.moduleDocument()
26 ### Create Part
27 Part_1 = model.addPart(partSet)
28 Part_1_doc = Part_1.document()
29 ### Create Parallelepiped
30 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
31 SketchLine_1 = Sketch_1.addLine(100, 30, -100, 30)
32 SketchLine_2 = Sketch_1.addLine(-100, 30, -100, -12)
33 SketchLine_3 = Sketch_1.addLine(-100, -12, 100, -12)
34 SketchLine_4 = Sketch_1.addLine(100, -12, 100, 30)
35 model.do()
36 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchLine_1r-SketchLine_2f-SketchLine_3f-SketchLine_4f")], model.selection(), 8, 0, "Faces|Wires")
37
38 ### Create tool-set: all the needed types of edges and surfaces
39 Sketch_2 = model.addSketch(Part_1_doc, model.selection("FACE", "Extrusion_1_1/To_Face"))
40 SketchCircle_1 = Sketch_2.addCircle(-80, 10, 5)
41 SketchEllipse_1 = Sketch_2.addEllipse(-51, 10, -39, 10, 5)
42 SketchEllipse_1.construction(center = "aux", firstFocus = "aux", secondFocus = "aux", majorAxisStart = "aux", majorAxisEnd = "aux", minorAxisStart = "aux", minorAxisEnd = "aux", majorAxis = "aux", minorAxis = "aux")
43 # moon
44 SketchArc_1 = Sketch_2.addArc(-22, 10, -22, 2, -22, 18, False)
45 SketchEllipticArc_1 = Sketch_2.addEllipticArc(-22, 10, -22, 17, -22, 2, -22, 18, False)
46 SketchEllipticArc_1.construction(center = "aux", firstFocus = "aux", secondFocus = "aux", majorAxisStart = "aux", majorAxisEnd = "aux", minorAxisStart = "aux", minorAxisEnd = "aux", majorAxis = "aux", minorAxis = "aux")
47 # cone
48 SketchLine_9 = Sketch_2.addLine(3, 14, 3, -2)
49 SketchLine_10 = Sketch_2.addLine(3, -2, 11, 14)
50 SketchLine_11 = Sketch_2.addLine(11, 14, 3, 14)
51 # tore
52 SketchLine_12 = Sketch_2.addLine(39, 16, 39, 1)
53 SketchLine_12.setAuxiliary(True)
54 SketchCircle_2 = Sketch_2.addCircle(48, 10, 5)
55 # circle
56 SketchLine_13 = Sketch_2.addLine(76, 14, 76, 0)
57 SketchArc_2 = Sketch_2.addArc(76, 7, 76, 14, 76, 0, True)
58 model.do()
59 ### Create extrusions and revolutions
60 Extrusion_2_objects = [model.selection("FACE", "Sketch_2/Face-SketchCircle_1_2r"),
61                        model.selection("FACE", "Sketch_2/Face-SketchEllipse_1f"),
62                        model.selection("FACE", "Sketch_2/Face-SketchArc_1_2f-SketchEllipticArc_1r")]
63 Extrusion_2 = model.addExtrusion(Part_1_doc, Extrusion_2_objects, model.selection(), 8, 0, "Faces|Wires")
64 Revolution_1 = model.addRevolution(Part_1_doc, [model.selection("FACE", "Sketch_2/Face-SketchLine_9f-SketchLine_10f-SketchLine_11f")], model.selection("EDGE", "Sketch_2/SketchLine_9"), 180, 0)
65 Revolution_2 = model.addRevolution(Part_1_doc, [model.selection("FACE", "Sketch_2/Face-SketchCircle_2_2f")], model.selection("EDGE", "Sketch_2/SketchLine_12"), 180, 0)
66 Revolution_3 = model.addRevolution(Part_1_doc, [model.selection("FACE", "Sketch_2/Face-SketchLine_13f-SketchArc_2_2f")], model.selection("EDGE", "Sketch_2/SketchLine_13"), 180, 0)
67 model.do()
68 ### Fuse all
69 Fuse_1_objects_1 = [model.selection("SOLID", "Extrusion_1_1"),
70                     model.selection("SOLID", "Extrusion_2_1"),
71                     model.selection("SOLID", "Extrusion_2_2"),
72                     model.selection("SOLID", "Extrusion_2_3"),
73                     model.selection("SOLID", "Revolution_1_1"),
74                     model.selection("SOLID", "Revolution_2_1"),
75                     model.selection("SOLID", "Revolution_3_1")]
76 Fuse_1 = model.addFuse(Part_1_doc, Fuse_1_objects_1, keepSubResults = True)
77 model.end()
78
79 # a method to compare tupes: element by element, double values are compared with precision 1.e-9
80 def compareTuples(theT1, theT2):
81   assert(len(theT1) == len(theT2))
82   for i in range(len(theT1)):
83     assert(type(theT1[i]) == type(theT2[i]))
84     if type(theT1[i]) == float:
85       #print("Compare " + "{:0.9f}".format(theT1[i]) + " and " + "{:0.9f}".format(theT2[i]));
86       assert("{:0.9f}".format(theT1[i]) == "{:0.9f}".format(theT2[i]))
87     else:
88       assert(theT1[i] == theT2[i])
89
90
91 # check the whole solid
92 aSolid = Fuse_1.feature().firstResult().shape()
93 anInfo = geom.shapeInfo(aSolid)
94 compareTuples(anInfo, ('Solid',))
95 # check the shell
96 aShell = aSolid.subShapes(GeomAPI_Shape.SHELL)[0]
97 anInfo = geom.shapeInfo(aShell)
98 compareTuples(anInfo, ('Shell',))
99 # check faces
100 aFaces = aShell.subShapes(GeomAPI_Shape.FACE)
101 compareTuples(geom.shapeInfo(aFaces[0]), ('Rectangle', 100.0, 30.0, 8.0, -0.0, 1.0, -0.0, 8.0, 200.0))
102 compareTuples(geom.shapeInfo(aFaces[6]), ('Cone', 3.0, -2.0, 8.0, -0.0, 1.0, -0.0, 0.0, 8.0, 16.))
103 compareTuples(geom.shapeInfo(aFaces[8]), ('Sphere', 76.0, 7.0, 8.0, 7.0))
104 compareTuples(geom.shapeInfo(aFaces[9]), ('Cylinder', -80.0, 10.0, 16.0, -0.0, -0.0, -1.0, 5.0, 8.0))
105 compareTuples(geom.shapeInfo(aFaces[10]), ('Face',)) # linear extrusion surface
106 compareTuples(geom.shapeInfo(aFaces[13]), ('Torus', 39.0, 10.0, 8.0, -0.0, 1.0, -0.0, 9.0, 5.0))
107 compareTuples(geom.shapeInfo(aFaces[14]), ('Disk', -80.0, 10.0, 16.0, 0.0, 0.0, 1.0, 5.0)) # circular disk
108 compareTuples(geom.shapeInfo(aFaces[15]), ('Disk', -51.0, 10.0, 16.0, 0.0, 0.0, 1.0, 13.0, 5.0)) # elliptical disk
109 compareTuples(geom.shapeInfo(aFaces[16]), ('Plane', 0.0, 0.0, 16.0, 0.0, 0.0, 1.0))
110 # check wires
111 aWires = aShell.subShapes(GeomAPI_Shape.WIRE)
112 compareTuples(geom.shapeInfo(aWires[0]), ('Polygon', True, 100.0, 30.0, 8.0, 100.0, 30.0, 0.0, -100.0, 30.0, 0.0, -100.0, 30.0, 8.0))
113 compareTuples(geom.shapeInfo(aWires[6]), ('Wire', True))
114 # check edges
115 anEdges = aFaces[0].subShapes(GeomAPI_Shape.EDGE)
116 compareTuples(geom.shapeInfo(anEdges[0]), ('Line segment', 100.0, 30.0, 0.0, 100.0, 30.0, 8.0))
117 anEdges = aFaces[6].subShapes(GeomAPI_Shape.EDGE) # cone
118 compareTuples(geom.shapeInfo(anEdges[0]), ('Edge', True)) # Degenerated edge
119 compareTuples(geom.shapeInfo(anEdges[1]), ('Arc of circle', 3.0, 14.0, 8.0, 0.0, -1.0, 0.0, 8.0, 11.0, 14.0, 8.0, -5.0, 14.0, 8.0))
120 anEdges = aFaces[9].subShapes(GeomAPI_Shape.EDGE) # cylinder
121 compareTuples(geom.shapeInfo(anEdges[2]), ('Circle', -80.0, 10.0, 8.0, 0.0, 0.0, 1.0, 5.0))
122 anEdges = aFaces[10].subShapes(GeomAPI_Shape.EDGE) # ellipse
123 compareTuples(geom.shapeInfo(anEdges[2]), ('Ellipse', -51.0, 10.0, 8.0, 0.0, 0.0, 1.0, 13.0, 5.0))
124 anEdges = aFaces[16].subShapes(GeomAPI_Shape.EDGE) # moon top
125 compareTuples(geom.shapeInfo(anEdges[1]), ('Arc of ellipse', -22.0, 10.0, 16.0, 0.0, -0.0, 1.0, 8.0, 3.872983346207417, -22.0, 2.0, 16.0, -22.0, 18.0, 16.0))
126 # check vertex
127 aVertex = aSolid.subShapes(GeomAPI_Shape.VERTEX)[0]
128 compareTuples(geom.shapeInfo(aVertex), ('Vertex', 100.0, 30.0, 0.0))