Salome HOME
Issue #1834: Fix length of lines
[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 = 
49       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
50     if(!aResultCompSolid.get()) {
51       return;
52     }
53
54     GeomShapePtr aBaseShape = aShapeAttrSelection->value();
55     if(!aBaseShape.get()) {
56       aBaseShape = aContext->shape();
57     }
58     if(!aBaseShape.get()) {
59       return;
60     }
61     GeomAPI_Shape::ShapeType aShapeType = aBaseShape->shapeType();
62     if(aShapeType != GeomAPI_Shape::WIRE
63         && aShapeType != GeomAPI_Shape::SHELL
64         && aShapeType != GeomAPI_Shape::COMPSOLID
65         && aShapeType != GeomAPI_Shape::COMPOUND) {
66       return;
67     }
68     for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
69       GeomShapePtr aSubShape = anIt.current();
70       const int aNumOfSubs = aResultCompSolid->numberOfSubs();
71       if(aNumOfSubs == 0) {
72         aSubShapesAttrList->append(aContext, aSubShape);
73       } else {
74         for(int anIndex = 0; anIndex < aResultCompSolid->numberOfSubs(); ++anIndex) {
75           ResultBodyPtr aSubResult = aResultCompSolid->subResult(anIndex);
76           if(aSubResult->shape()->isEqual(aSubShape)) {
77             aSubShapesAttrList->append(aSubResult, aSubShape);
78             break;
79           }
80         }
81       }
82     }
83   }
84 }
85
86 //==================================================================================================
87 void FeaturesPlugin_RemoveSubShapes::execute()
88 {
89   // Get base shape and sub-shapes list.
90   AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
91   AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
92   if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
93     return;
94   }
95
96   // Copy base shape.
97   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
98   if(!aBaseShape.get()) {
99     return;
100   }
101   GeomShapePtr aResultShape = aBaseShape->emptyCopied();
102
103   // Copy sub-shapes from list to new shape.
104   for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
105     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
106     GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
107     GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
108   }
109
110   // Store result.
111   ResultBodyPtr aResultBody = document()->createBody(data());
112   aResultBody->storeModified(aBaseShape, aResultShape);
113   setResult(aResultBody);
114 }