Salome HOME
[Code coverage GeomAPI]: Call methods related to GUI
[modules/shaper.git] / src / GeomAPI / Test / TestPolygon.py
1 ## Copyright (C) 2018-20xx  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 from GeomAPI import *
22 from SketchAPI import *
23
24 from salome.shaper import model
25
26 import math
27
28 TOLERANCE = 1.e-7
29
30 def assertRectangle(theWire):
31     assert(theWire is not None)
32     aCorners = [GeomAPI.GeomAPI_Pnt(0, 0, 0)]
33     assert(theWire.isRectangle(aCorners))
34
35 def checkRectangleFace(theDocument, theFaceName):
36     aFace = model.addFace(theDocument, [model.selection("FACE", theFaceName)])
37     aShape = aFace.result().resultSubShapePair()[0].shape()
38     assert(aShape.isFace())
39     aSubs = aShape.subShapes(GeomAPI.GeomAPI_Shape.WIRE)
40     assert(aSubs.size() == 1)
41     assertRectangle(aSubs[0].wire())
42     theDocument.removeFeature(aFace.feature())
43
44 def checkRectangleWire(theDocument, theEdgeNames):
45     aSelection = []
46     for name in theEdgeNames:
47         aSelection.append(model.selection("EDGE", name))
48     aWire = model.addWire(theDocument, aSelection)
49     aShape = aWire.result().resultSubShapePair()[0].shape()
50     assert(aShape.isWire())
51     assert(aShape.wire().isClosed())
52     assertRectangle(aShape.wire())
53     theDocument.removeFeature(aWire.feature())
54
55
56 def assertPolygon(theWire):
57     assert(theWire is not None)
58     aCorners = [GeomAPI.GeomAPI_Pnt(0, 0, 0)]
59     assert(theWire.isPolygon(aCorners))
60
61 def checkPolygonWire(theDocument, theEdgeNames):
62     aSelection = []
63     for name in theEdgeNames:
64         aSelection.append(model.selection("EDGE", name))
65     aWire = model.addWire(theDocument, aSelection)
66     aShape = aWire.result().resultSubShapePair()[0].shape()
67     assert(aShape.isWire())
68     assertPolygon(aShape.wire())
69     theDocument.removeFeature(aWire.feature())
70
71 def checkPolyline(theDocument, theVertexNames):
72     aSelection = []
73     for name in theVertexNames:
74         aSelection.append(model.selection("VERTEX", name))
75     aWire = model.addPolyline3D(Part_1_doc, aSelection, False)
76     aShape = aWire.result().resultSubShapePair()[0].shape()
77     assert(aShape.isWire())
78     assertPolygon(aShape.wire())
79     theDocument.removeFeature(aWire.feature())
80
81
82
83 model.begin()
84 partSet = model.moduleDocument()
85 Part_1 = model.addPart(partSet)
86 Part_1_doc = Part_1.document()
87 Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
88 model.do()
89
90 # Test 1. Check face/wire of box is a rectangle
91 checkRectangleFace(Part_1_doc, "Box_1_1/Top")
92 Wire_edges = ["[Box_1_1/Left][Box_1_1/Top]", "[Box_1_1/Front][Box_1_1/Top]", "[Box_1_1/Right][Box_1_1/Top]", "[Box_1_1/Back][Box_1_1/Top]"]
93 checkRectangleWire(Part_1_doc, Wire_edges)
94
95 # Test 2. Build a polygon from edges
96 Wire_edges = ["[Box_1_1/Left][Box_1_1/Bottom]", "[Box_1_1/Front][Box_1_1/Left]", "[Box_1_1/Left][Box_1_1/Top]", "[Box_1_1/Back][Box_1_1/Top]", "[Box_1_1/Right][Box_1_1/Top]"]
97 checkPolygonWire(Part_1_doc, Wire_edges)
98
99 # Test 3. Build a polygon from vertices
100 Poly_vertices = ["[Box_1_1/Back][Box_1_1/Left][Box_1_1/Bottom]", "[Box_1_1/Front][Box_1_1/Left][Box_1_1/Bottom]", "[Box_1_1/Front][Box_1_1/Right][Box_1_1/Bottom]", "[Box_1_1/Front][Box_1_1/Right][Box_1_1/Top]", "[Box_1_1/Front][Box_1_1/Left][Box_1_1/Top]", "[Box_1_1/Back][Box_1_1/Right][Box_1_1/Top]"]
101 checkPolyline(Part_1_doc, Poly_vertices)
102
103 model.end()