Salome HOME
Issue #1366: Remove Sub-Shapes feature added.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_RemoveSubShapes.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        FeaturesPlugin_RemoveSubShapes.cpp
4 // Created:     14 April 2016
5 // Author:      Dmitry Bobylev
6
7 #include "FeaturesPlugin_RemoveSubShapes.h"
8
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_ResultCompSolid.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_Session.h>
14 #include <ModelAPI_Validator.h>
15
16 #include <GeomAPI_ShapeIterator.h>
17
18 #include <GeomAlgoAPI_ShapeBuilder.h>
19 #include <GeomAlgoAPI_ShapeTools.h>
20
21 //==================================================================================================
22 FeaturesPlugin_RemoveSubShapes::FeaturesPlugin_RemoveSubShapes()
23 {
24 }
25
26 //==================================================================================================
27 void FeaturesPlugin_RemoveSubShapes::initAttributes()
28 {
29   data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
30
31   data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
32 }
33
34 void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
35 {
36   ModelAPI_Feature::attributeChanged(theID);
37
38   if(theID == BASE_SHAPE_ID()) {
39     AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
40     AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
41     if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
42       return;
43     }
44
45     aSubShapesAttrList->clear();
46
47     ResultPtr aContext = aShapeAttrSelection->context();
48     ResultCompSolidPtr aResultCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
49     if(!aResultCompSolid.get()) {
50       return;
51     }
52
53     GeomShapePtr aBaseShape = aShapeAttrSelection->value();
54     if(!aBaseShape.get()) {
55       aBaseShape = aContext->shape();
56     }
57     if(!aBaseShape.get()) {
58       return;
59     }
60     GeomAPI_Shape::ShapeType aShapeType = aBaseShape->shapeType();
61     if(aShapeType != GeomAPI_Shape::WIRE
62         && aShapeType != GeomAPI_Shape::SHELL
63         && aShapeType != GeomAPI_Shape::COMPSOLID
64         && aShapeType != GeomAPI_Shape::COMPOUND) {
65       return;
66     }
67     for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
68       GeomShapePtr aSubShape = anIt.current();
69       const int aNumOfSubs = aResultCompSolid->numberOfSubs();
70       if(aNumOfSubs == 0) {
71         aSubShapesAttrList->append(aContext, aSubShape);
72       } else {
73         for(int anIndex = 0; anIndex < aResultCompSolid->numberOfSubs(); ++anIndex) {
74           ResultBodyPtr aSubResult = aResultCompSolid->subResult(anIndex);
75           if(aSubResult->shape()->isEqual(aSubShape)) {
76             aSubShapesAttrList->append(aSubResult, aSubShape);
77             break;
78           }
79         }
80       }
81     }
82   }
83 }
84
85 //==================================================================================================
86 void FeaturesPlugin_RemoveSubShapes::execute()
87 {
88   // Get base shape and sub-shapes list.
89   AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
90   AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
91   if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
92     return;
93   }
94
95   // Copy base shape.
96   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
97   if(!aBaseShape.get()) {
98     return;
99   }
100   GeomShapePtr aResultShape = aBaseShape->emptyCopied();
101
102   // Copy sub-shapes from list to new shape.
103   for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
104     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
105     GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
106     GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
107   }
108
109   // Store result.
110   ResultBodyPtr aResultBody = document()->createBody(data());
111   aResultBody->storeModified(aBaseShape, aResultShape);
112   setResult(aResultBody);
113 }