Salome HOME
[Code coverage FeaturesPlugin]: Improve coverage for Boolean operations
[modules/shaper.git] / src / FeaturesPlugin / Test / TestBoolean1.py
1 ## Copyright (C) 2014-2017  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 """
22       TestBoolean.py
23       Unit test of FeaturesPlugin_Boolean class
24
25       class FeaturesPlugin_Boolean
26         static const std::string MY_ID("Boolean");
27         static const std::string MY_OBJECT_ID("main_object");
28         static const std::string MY_TOOL_ID("tool_object");
29         static const std::string MY_TYPE_ID("bool_type");
30
31         data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_ID(), ModelAPI_AttributeReference::typeId());
32         data()->addAttribute(FeaturesPlugin_Boolean::TOOL_ID(), ModelAPI_AttributeReference::typeId());
33         data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
34 """
35 #=========================================================================
36 # Initialization of the test
37 #=========================================================================
38 from ModelAPI import *
39 from GeomDataAPI import *
40 from GeomAlgoAPI import *
41 from GeomAPI import *
42
43 __updated__ = "2014-12-16"
44
45 aSession = ModelAPI_Session.get()
46 # Create a part for extrusions & boolean
47 aSession.startOperation()
48 aPartFeature = aSession.moduleDocument().addFeature("Part")
49 aSession.finishOperation()
50 aPart = aSession.activeDocument()
51 #=========================================================================
52 # Create a sketch with circle to extrude
53 #=========================================================================
54 aSession.startOperation()
55 aCircleSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
56 origin = geomDataAPI_Point(aCircleSketchFeature.attribute("Origin"))
57 origin.setValue(0, 0, 0)
58 dirx = geomDataAPI_Dir(aCircleSketchFeature.attribute("DirX"))
59 dirx.setValue(1, 0, 0)
60 norm = geomDataAPI_Dir(aCircleSketchFeature.attribute("Norm"))
61 norm.setValue(0, 0, 1)
62 aSketchCircle = aCircleSketchFeature.addFeature("SketchCircle")
63 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("circle_center"))
64 aCircleRadius = aSketchCircle.real("circle_radius")
65 anCircleCentr.setValue(10., 10.)
66 aCircleRadius.setValue(50.)
67 aSession.finishOperation()
68 #=========================================================================
69 # Create a sketch with triangle to extrude
70 #=========================================================================
71 aSession.startOperation()
72 aTriangleSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
73 origin = geomDataAPI_Point(aTriangleSketchFeature.attribute("Origin"))
74 origin.setValue(0, 0, 0)
75 dirx = geomDataAPI_Dir(aTriangleSketchFeature.attribute("DirX"))
76 dirx.setValue(1, 0, 0)
77 norm = geomDataAPI_Dir(aTriangleSketchFeature.attribute("Norm"))
78 norm.setValue(0, 0, 1)
79 aSketchLineA = aTriangleSketchFeature.addFeature("SketchLine")
80 aSketchLineB = aTriangleSketchFeature.addFeature("SketchLine")
81 aSketchLineC = aTriangleSketchFeature.addFeature("SketchLine")
82 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
83 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
84 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
85 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
86 aLineCStartPoint = geomDataAPI_Point2D(aSketchLineC.attribute("StartPoint"))
87 aLineCEndPoint = geomDataAPI_Point2D(aSketchLineC.attribute("EndPoint"))
88 aLineAStartPoint.setValue(25., 25.)
89 aLineAEndPoint.setValue(100., 25.)
90 aLineBStartPoint.setValue(100., 25.)
91 aLineBEndPoint.setValue(60., 75.)
92 aLineCStartPoint.setValue(60., 75.)
93 aLineCEndPoint.setValue(25., 25.)
94 aSession.finishOperation()
95 #=========================================================================
96 # Make extrusion on circle (cylinder) and triangle (prism)
97 #=========================================================================
98 # Build shape from sketcher results
99 aSession.startOperation()
100 extrudedObjects = []
101 for eachSketchFeature in [aCircleSketchFeature, aTriangleSketchFeature]:
102     # Build sketch faces
103     aSketchResult = modelAPI_ResultConstruction(eachSketchFeature.firstResult())
104     # Create extrusion on them
105     anExtrusionFt = aPart.addFeature("Extrusion")
106     anExtrusionFt.selectionList("base").append(
107         aSketchResult, aSketchResult.face(0))
108     anExtrusionFt.string("CreationMethod").setValue("BySizes")
109     anExtrusionFt.real("from_size").setValue(0)
110     anExtrusionFt.real("to_size").setValue(50)
111     anExtrusionFt.real("to_offset").setValue(0) #TODO: remove
112     anExtrusionFt.real("from_offset").setValue(0) #TODO: remove
113     anExtrusionFt.execute()
114     extrudedObjects.append(modelAPI_ResultBody(anExtrusionFt.firstResult()))
115 aSession.finishOperation()
116 #=========================================================================
117 # Create a pacman as boolean cut of the prism from the cylinder
118 #=========================================================================
119 aSession.startOperation()
120 aBooleanFt = aPart.addFeature("Cut")
121 aBooleanFt.selectionList("main_objects").append(extrudedObjects[0], extrudedObjects[0].shape())
122 aBooleanFt.selectionList("tool_objects").append(extrudedObjects[1], extrudedObjects[1].shape())
123 aBooleanFt.execute()
124 aSession.finishOperation()
125
126 assert (len(aBooleanFt.results()) > 0)
127 aBooleanResult = modelAPI_ResultBody(aBooleanFt.firstResult())
128 assert (aBooleanResult is not None)
129 #=========================================================================
130 # End of test
131 #=========================================================================
132
133 from salome.shaper import model
134 assert(model.checkPythonDump())