Salome HOME
Issue #1366: Remove Sub-Shapes feature added.
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_SubShapes.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        BuildPlugin_SubShapes.cpp
4 // Created:     14 April 2016
5 // Author:      Dmitry Bobylev
6
7 #include "BuildPlugin_SubShapes.h"
8
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_Session.h>
13 #include <ModelAPI_Validator.h>
14
15 #include <GeomAPI_Edge.h>
16 #include <GeomAPI_ShapeIterator.h>
17 #include <GeomAPI_Wire.h>
18
19 #include <GeomAlgoAPI_ShapeBuilder.h>
20
21 //==================================================================================================
22 BuildPlugin_SubShapes::BuildPlugin_SubShapes()
23 {
24 }
25
26 //==================================================================================================
27 void BuildPlugin_SubShapes::initAttributes()
28 {
29   data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
30
31   data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
32   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SUBSHAPES_ID());
33 }
34
35 void BuildPlugin_SubShapes::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
50     GeomShapePtr aBaseShape = aShapeAttrSelection->value();
51     if(!aBaseShape.get()) {
52       return;
53     }
54     GeomAPI_Shape::ShapeType aBaseShapeType = aBaseShape->shapeType();
55     for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
56       GeomShapePtr aSubShape = anIt.current();
57       if(aBaseShapeType == GeomAPI_Shape::WIRE) {
58         for(GeomAPI_ShapeIterator aSubIt(aSubShape); aSubIt.more(); aSubIt.next()) {
59           GeomShapePtr aSubOfSubShape = aSubIt.current();
60           if(aSubOfSubShape->orientation() == GeomAPI_Shape::INTERNAL) {
61             aSubShapesAttrList->append(aContext, aSubOfSubShape);
62           }
63         }
64       } else if(aBaseShapeType == GeomAPI_Shape::FACE) {
65         if(aSubShape->shapeType() != GeomAPI_Shape::WIRE) {
66           aSubShapesAttrList->append(aContext, aSubShape);
67         } else if(aSubShape->orientation() == GeomAPI_Shape::INTERNAL) {
68           if(aSubShape->shapeType() == GeomAPI_Shape::WIRE) {
69             for(GeomAPI_ShapeIterator aWireIt(aSubShape); aWireIt.more(); aWireIt.next()) {
70               aSubShapesAttrList->append(aContext, aWireIt.current());
71             }
72           }
73         }
74       }
75     }
76   }
77 }
78
79 //==================================================================================================
80 void BuildPlugin_SubShapes::execute()
81 {
82   // Get base shape and sub-shapes list.
83   AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
84   AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
85   if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
86     return;
87   }
88
89   // Get base shape without internal shapes.
90   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
91   if(!aBaseShape.get()) {
92     return;
93   }
94   GeomAlgoAPI_ShapeBuilder aBuilder;
95   aBuilder.removeInternal(aBaseShape);
96   GeomShapePtr aResultShape = aBuilder.shape();
97
98   // Get list of shapes.
99   ListOfShape aShapesToAdd;
100   for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
101     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
102     aShapesToAdd.push_back(anAttrSelectionInList->value());
103   }
104
105   // Copy sub-shapes from list to new shape.
106   if(!aShapesToAdd.empty()) {
107     aBuilder.addInternal(aResultShape, aShapesToAdd);
108     aResultShape = aBuilder.shape();
109   }
110
111   // Store result.
112   const int aModVertexTag = 1;
113   const int aModEdgeTag = 2;
114   ResultBodyPtr aResultBody = document()->createBody(data());
115   aResultBody->storeModified(aBaseShape, aResultShape);
116   aResultBody->loadAndOrientModifiedShapes(&aBuilder, aBaseShape, GeomAPI_Shape::EDGE, aModEdgeTag,
117                                           "Modified_Edge", *aBuilder.mapOfSubShapes().get());
118   for(ListOfShape::const_iterator anIt = aShapesToAdd.cbegin(); anIt != aShapesToAdd.cend(); ++anIt) {
119     GeomAPI_Shape::ShapeType aShType = (*anIt)->shapeType();
120     aResultBody->loadAndOrientModifiedShapes(&aBuilder, *anIt, aShType,
121                                              aShType == GeomAPI_Shape::VERTEX ? aModVertexTag : aModEdgeTag,
122                                              aShType == GeomAPI_Shape::VERTEX ? "Modified_Vertex" : "Modified_Edge",
123                                              *aBuilder.mapOfSubShapes().get());
124   }
125   setResult(aResultBody);
126 }