Salome HOME
Merge branch 'master' into cgt/devCEA
[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_Copy.h>
19 #include <GeomAlgoAPI_ShapeBuilder.h>
20 #include <GeomAlgoAPI_ShapeTools.h>
21
22 //==================================================================================================
23 FeaturesPlugin_RemoveSubShapes::FeaturesPlugin_RemoveSubShapes()
24 {
25 }
26
27 //==================================================================================================
28 void FeaturesPlugin_RemoveSubShapes::initAttributes()
29 {
30   data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
31
32   data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
33 }
34
35 void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
36 {
37   ModelAPI_Feature::attributeChanged(theID);
38
39   if(theID == BASE_SHAPE_ID()) {
40     AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
41     AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
42     if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
43       return;
44     }
45
46     aSubShapesAttrList->clear();
47
48     ResultPtr aContext = aShapeAttrSelection->context();
49     ResultCompSolidPtr aResultCompSolid =
50       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
51     if(!aResultCompSolid.get()) {
52       return;
53     }
54
55     GeomShapePtr aBaseShape = aShapeAttrSelection->value();
56     if(!aBaseShape.get()) {
57       aBaseShape = aContext->shape();
58     }
59     if(!aBaseShape.get()) {
60       return;
61     }
62     GeomAPI_Shape::ShapeType aShapeType = aBaseShape->shapeType();
63     if(aShapeType != GeomAPI_Shape::WIRE
64         && aShapeType != GeomAPI_Shape::SHELL
65         && aShapeType != GeomAPI_Shape::COMPSOLID
66         && aShapeType != GeomAPI_Shape::COMPOUND) {
67       return;
68     }
69     for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
70       GeomShapePtr aSubShape = anIt.current();
71       const int aNumOfSubs = aResultCompSolid->numberOfSubs();
72       if(aNumOfSubs == 0) {
73         aSubShapesAttrList->append(aContext, aSubShape);
74       } else {
75         for(int anIndex = 0; anIndex < aResultCompSolid->numberOfSubs(); ++anIndex) {
76           ResultBodyPtr aSubResult = aResultCompSolid->subResult(anIndex);
77           if(aSubResult->shape()->isEqual(aSubShape)) {
78             aSubShapesAttrList->append(aSubResult, aSubShape);
79             break;
80           }
81         }
82       }
83     }
84   }
85 }
86
87 //==================================================================================================
88 void FeaturesPlugin_RemoveSubShapes::execute()
89 {
90   // Get base shape and sub-shapes list.
91   AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
92   AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
93   if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
94     return;
95   }
96
97   // Get base shape.
98   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
99
100   GeomShapePtr aResultShape;
101   int aSubsNb = aSubShapesAttrList->size();
102   if(aSubsNb > 1) {
103     if(!aBaseShape.get()) {
104       return;
105     }
106     aResultShape = aBaseShape->emptyCopied();
107
108     // Copy sub-shapes from list to new shape.
109     for(int anIndex = 0; anIndex < aSubsNb; ++anIndex) {
110       AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
111       GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
112       GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
113     }
114   } else if(aSubsNb == 1) {
115     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(0);
116     aResultShape = anAttrSelectionInList->value();
117   }
118
119   GeomAlgoAPI_Copy aCopy(aResultShape);
120   aResultShape = aCopy.shape();
121
122   // Store result.
123   ResultBodyPtr aResultBody = document()->createBody(data());
124   aResultBody->storeModified(aBaseShape, aResultShape, 1);
125   aResultBody->loadAndOrientModifiedShapes(&aCopy,
126                                            aBaseShape,
127                                            GeomAPI_Shape::FACE,
128                                            10000,
129                                            "Modified_Face",
130                                            *aCopy.mapOfSubShapes().get(),
131                                            true);
132   setResult(aResultBody);
133 }