1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <FeaturesPlugin_CompositeSketch.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_BodyBuilder.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_Revolution.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_SketchBuilder.h>
35 #include <GeomAPI_PlanarEdges.h>
36 #include <GeomAPI_ShapeExplorer.h>
41 static void storeSubShape(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
42 ResultBodyPtr theResultBody,
43 const GeomShapePtr theShape,
44 const GeomAPI_Shape::ShapeType theType,
45 const std::string& theName);
47 //=================================================================================================
48 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
50 // Initialize sketch launcher.
51 if(theInitFlags & InitSketchLauncher) {
52 data()->addAttribute(SKETCH_ID(), ModelAPI_AttributeReference::typeId());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_ID());
56 // Initialize selection list.
57 if(theInitFlags & InitBaseObjectsList) {
58 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
62 //=================================================================================================
63 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
65 FeaturePtr aNew = document()->addFeature(theID, false);
67 data()->reference(SKETCH_ID())->setValue(aNew);
70 // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
71 document()->setCurrentFeature(aNew, false);
75 //=================================================================================================
76 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool forTree) const
78 ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
79 return aObj.get() ? 1 : 0;
82 //=================================================================================================
83 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex,
86 FeaturePtr aSubFeature;
89 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
94 //=================================================================================================
95 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
99 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
101 return aFeature->data()->featureId();
108 //=================================================================================================
109 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
111 bool isSubFeature = false;
112 // Check is this feature of result
113 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
114 if (aFeature.get()) {
115 ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
116 isSubFeature = aSub == theObject;
121 //=================================================================================================
122 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
124 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
125 if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
126 aBaseObjectsSelectionList->clear();
129 reference(SKETCH_ID())->setValue(ObjectPtr());
132 //=================================================================================================
133 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
134 const bool theIsMakeShells)
136 theBaseShapesList.clear();
138 ListOfShape aBaseFacesList;
139 std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
140 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
141 if(!aBaseObjectsSelectionList.get()) {
142 setError("Error: Could not get base objects selection list.");
145 if(aBaseObjectsSelectionList->size() == 0) {
146 setError("Error: Base objects list is empty.");
149 for(int anIndex = 0; anIndex < aBaseObjectsSelectionList->size(); anIndex++) {
150 AttributeSelectionPtr aBaseObjectSelection = aBaseObjectsSelectionList->value(anIndex);
151 if(!aBaseObjectSelection.get()) {
152 setError("Error: Selected base object is empty.");
155 GeomShapePtr aBaseShape = aBaseObjectSelection->value();
156 if(aBaseShape.get() && !aBaseShape->isNull()) {
157 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
158 if(aST == GeomAPI_Shape::SOLID || aST == GeomAPI_Shape::COMPSOLID) {
159 setError("Error: Selected shapes has unsupported type.");
162 ResultConstructionPtr aConstruction =
163 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
164 if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) &&
165 aST == GeomAPI_Shape::WIRE) {
166 // It is a wire on the sketch, store it to make face later.
167 aSketchWiresMap[aConstruction].push_back(aBaseShape);
170 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
171 theBaseShapesList.push_back(aBaseShape);
174 // This may be the whole sketch result selected, check and get faces.
175 ResultConstructionPtr aConstruction =
176 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
177 if(!aConstruction.get()) {
178 setError("Error: Selected sketches does not have results.");
181 int aFacesNum = aConstruction->facesNum();
183 // Probably it can be construction.
184 aBaseShape = aConstruction->shape();
185 if(aBaseShape.get() && !aBaseShape->isNull()) {
186 GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
187 if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE &&
188 aST != GeomAPI_Shape::WIRE &&
189 aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
190 setError("Error: Selected shapes has unsupported type.");
193 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
194 theBaseShapesList.push_back(aBaseShape);
197 for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
198 GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
199 if(!aBaseFace.get() || aBaseFace->isNull()) {
200 setError("Error: One of the faces on selected sketch is null.");
203 aBaseFacesList.push_back(aBaseFace);
209 // Make faces from sketch wires.
210 for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
211 anIt != aSketchWiresMap.cend(); ++anIt) {
212 const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
213 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
214 const ListOfShape& aWiresList = (*anIt).second;
216 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
217 aSketchPlanarEdges->norm(),
220 aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
223 // Searching faces with common edges.
224 if(theIsMakeShells && aBaseFacesList.size() > 1) {
226 ListOfShape aFreeFaces;
227 GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
228 GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL,
229 aShells, aFreeFaces);
230 theBaseShapesList.insert(theBaseShapesList.end(), aFreeFaces.begin(), aFreeFaces.end());
231 theBaseShapesList.insert(theBaseShapesList.end(), aShells.begin(), aShells.end());
233 theBaseShapesList.insert(theBaseShapesList.end(), aBaseFacesList.begin(),
234 aBaseFacesList.end());
238 //=================================================================================================
239 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
240 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
243 // Create result body.
244 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
246 // Store generated shape.
247 aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
249 // Store generated edges/faces.
250 storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
252 setResult(aResultBody, theIndex);
255 //=================================================================================================
256 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
257 const GeomShapePtr theBaseShape,
258 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
260 GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
261 GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
263 switch(aBaseShapeType) {
264 case GeomAPI_Shape::EDGE: {
265 aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
268 case GeomAPI_Shape::WIRE: {
269 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
272 case GeomAPI_Shape::FACE:
273 case GeomAPI_Shape::SHELL: {
274 aShapeTypeToExplode = GeomAPI_Shape::EDGE;
277 case GeomAPI_Shape::COMPOUND: {
278 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
282 int aLateralIndex = 1;
283 int aBaseEdgeIndex = 1;
284 int aVertexIndex = 1;
285 int aBaseVertexIndex = 1;
287 if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
288 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
289 theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX);
291 if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
292 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
293 theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
295 std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> > aSweeps; // all sweeps collected
296 std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep =
297 std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
298 if(aMakeSweep.get()) {
299 aSweeps.push_back(aMakeSweep);
301 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeList =
302 std::dynamic_pointer_cast<GeomAlgoAPI_MakeShapeList>(theMakeShape);
303 if (aMakeList.get()) {
304 ListOfMakeShape::const_iterator anIter = aMakeList->list().cbegin();
305 for(; anIter != aMakeList->list().cend(); anIter++) {
306 std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweep =
307 std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(*anIter);
309 aSweeps.push_back(aSweep);
313 std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> >::iterator aSweep = aSweeps.begin();
314 for(; aSweep != aSweeps.end(); aSweep++) {
315 // Store from shapes.
316 storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->fromShapes(), "From_");
319 storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->toShapes(), "To_");
323 //=================================================================================================
324 void FeaturesPlugin_CompositeSketch::storeShapes(
325 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
326 ResultBodyPtr theResultBody,
327 const GeomAPI_Shape::ShapeType theBaseShapeType,
328 const ListOfShape& theShapes,
329 const std::string theName)
331 GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
332 std::string aShapeTypeStr = "Face";
333 switch(theBaseShapeType) {
334 case GeomAPI_Shape::VERTEX: {
335 aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
336 aShapeTypeStr = "Vertex";
339 case GeomAPI_Shape::EDGE:
340 case GeomAPI_Shape::WIRE: {
341 aShapeTypeToExplore = GeomAPI_Shape::EDGE;
342 aShapeTypeStr = "Edge";
345 case GeomAPI_Shape::FACE:
346 case GeomAPI_Shape::SHELL: {
347 aShapeTypeToExplore = GeomAPI_Shape::FACE;
348 aShapeTypeStr = "Face";
351 case GeomAPI_Shape::COMPOUND: {
352 aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
358 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
359 GeomShapePtr aShape = *anIt;
361 if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
362 std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
363 storeSubShape(theMakeShape, theResultBody, aShape, aShape->shapeType(), aName);
365 std::string aName = theName + aShapeTypeStr;
366 storeSubShape(theMakeShape, theResultBody, aShape, aShapeTypeToExplore, aName);
367 if (theBaseShapeType == GeomAPI_Shape::WIRE) { // issue 2289: special names also for vertices
368 aName = theName + "Vertex";
369 storeSubShape(theMakeShape, theResultBody, aShape, GeomAPI_Shape::VERTEX, aName);
376 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
377 ResultBodyPtr theResultBody,
378 const GeomShapePtr theShape,
379 const GeomAPI_Shape::ShapeType theType,
380 const std::string& theName)
382 for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
383 GeomShapePtr aSubShape = anExp.current();
384 if (!theResultBody->generated(aSubShape, theName)) {
385 // store from/to shapes as primitives and then store modification of them by the boolean
386 theResultBody->generated(aSubShape, theName, false);
387 theResultBody->loadModifiedShapes(theMakeShape, aSubShape, theType);