Salome HOME
75015cca8e061307c12984d46f184377172e21a9
[modules/shaper.git] / src / FeaturesPlugin / Test / TestBoolean.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 = eachSketchFeature.firstResult()
104     aSketchEdges = modelAPI_ResultConstruction(aSketchResult).shape()
105     origin = geomDataAPI_Point(eachSketchFeature.attribute("Origin")).pnt()
106     dirX = geomDataAPI_Dir(eachSketchFeature.attribute("DirX")).dir()
107     norm = geomDataAPI_Dir(eachSketchFeature.attribute("Norm")).dir()
108     aSketchFaces = ShapeList()
109     GeomAlgoAPI_SketchBuilder.createFaces(
110         origin, dirX, norm, aSketchEdges, aSketchFaces)
111     # Create extrusion on them
112     anExtrusionFt = aPart.addFeature("Extrusion")
113     anExtrusionFt.selectionList("base").append(
114         aSketchResult, aSketchFaces[0])
115     anExtrusionFt.string("CreationMethod").setValue("BySizes")
116     anExtrusionFt.real("from_size").setValue(0)
117     anExtrusionFt.real("to_size").setValue(50)
118     anExtrusionFt.real("to_offset").setValue(0) #TODO: remove
119     anExtrusionFt.real("from_offset").setValue(0) #TODO: remove
120     anExtrusionFt.execute()
121     extrudedObjects.append(modelAPI_ResultBody(anExtrusionFt.firstResult()))
122 aSession.finishOperation()
123 #=========================================================================
124 # Create a pacman as boolean cut of the prism from the cylinder
125 #=========================================================================
126 aSession.startOperation()
127 aBooleanFt = aPart.addFeature("Cut")
128 aBooleanFt.selectionList("main_objects").append(extrudedObjects[0], extrudedObjects[0].shape())
129 aBooleanFt.selectionList("tool_objects").append(extrudedObjects[1], extrudedObjects[1].shape())
130 aBooleanFt.execute()
131 aSession.finishOperation()
132
133 assert (len(aBooleanFt.results()) > 0)
134 aBooleanResult = modelAPI_ResultBody(aBooleanFt.firstResult())
135 assert (aBooleanResult is not None)
136 #=========================================================================
137 # End of test
138 #=========================================================================
139
140 from salome.shaper import model
141
142 model.begin()
143 partSet = model.moduleDocument()
144 Part_1 = model.addPart(partSet)
145 Part_1_doc = Part_1.document()
146 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
147 SketchCircle_1 = Sketch_1.addCircle(-353.344768439108, 0.857632933105, 175.14200032067)
148 model.do()
149 Sketch_2 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
150 SketchCircle_2 = Sketch_2.addCircle(-126.929674099485, -2.572898799314, 176.971885556791)
151 model.do()
152 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchCircle_1_2f"), model.selection("FACE", "Sketch_2/Face-SketchCircle_2_2f")], model.selection(), 10, 0)
153 Common_1 = model.addCommon(Part_1_doc, [model.selection("SOLID", "Extrusion_1_1")], [model.selection("SOLID", "Extrusion_1_2")])
154 model.end()
155 assert(len(Common_1.results()) > 0)
156
157 model.begin()
158 partSet = model.moduleDocument()
159 Part_1 = model.addPart(partSet)
160 Part_1_doc = Part_1.document()
161 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
162 SketchCircle_1 = Sketch_1.addCircle(-353.344768439108, 0.857632933105, 175.14200032067)
163 model.do()
164 Sketch_2 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
165 SketchCircle_2 = Sketch_2.addCircle(-126.929674099485, -2.572898799314, 176.971885556791)
166 model.do()
167 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchCircle_1_2f"), model.selection("FACE", "Sketch_2/Face-SketchCircle_2_2f")], model.selection(), 10, 0)
168 Smash_1 = model.addSmash(Part_1_doc, [model.selection("SOLID", "Extrusion_1_1")], [model.selection("SOLID", "Extrusion_1_2")])
169 model.end()
170 assert(len(Smash_1.results()) > 0)
171
172 assert(model.checkPythonDump())