Salome HOME
Fix for the issue #2753 : error when dump/load script
[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     assertRectangle(aShape.wire())
52     theDocument.removeFeature(aWire.feature())
53
54
55 def assertPolygon(theWire):
56     assert(theWire is not None)
57     aCorners = [GeomAPI.GeomAPI_Pnt(0, 0, 0)]
58     assert(theWire.isPolygon(aCorners))
59
60 def checkPolygonWire(theDocument, theEdgeNames):
61     aSelection = []
62     for name in theEdgeNames:
63         aSelection.append(model.selection("EDGE", name))
64     aWire = model.addWire(theDocument, aSelection)
65     aShape = aWire.result().resultSubShapePair()[0].shape()
66     assert(aShape.isWire())
67     assertPolygon(aShape.wire())
68     theDocument.removeFeature(aWire.feature())
69
70 def checkPolyline(theDocument, theVertexNames):
71     aSelection = []
72     for name in theVertexNames:
73         aSelection.append(model.selection("VERTEX", name))
74     aWire = model.addPolyline3D(Part_1_doc, aSelection, False)
75     aShape = aWire.result().resultSubShapePair()[0].shape()
76     assert(aShape.isWire())
77     assertPolygon(aShape.wire())
78     theDocument.removeFeature(aWire.feature())
79
80
81
82 model.begin()
83 partSet = model.moduleDocument()
84 Part_1 = model.addPart(partSet)
85 Part_1_doc = Part_1.document()
86 Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
87 model.do()
88
89 # Test 1. Check face/wire of box is a rectangle
90 checkRectangleFace(Part_1_doc, "Box_1_1/Top")
91 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]"]
92 checkRectangleWire(Part_1_doc, Wire_edges)
93
94 # Test 2. Build a polygon from edges
95 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]"]
96 checkPolygonWire(Part_1_doc, Wire_edges)
97
98 # Test 3. Build a polygon from vertices
99 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]"]
100 checkPolyline(Part_1_doc, Poly_vertices)
101
102 model.end()