Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_SubShapes.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "BuildPlugin_SubShapes.h"
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_ResultConstruction.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Validator.h>
28
29 #include <GeomAPI_Edge.h>
30 #include <GeomAPI_ShapeIterator.h>
31 #include <GeomAPI_Wire.h>
32
33 #include <GeomAlgoAPI_ShapeBuilder.h>
34
35 //==================================================================================================
36 BuildPlugin_SubShapes::BuildPlugin_SubShapes()
37 {
38 }
39
40 //==================================================================================================
41 void BuildPlugin_SubShapes::initAttributes()
42 {
43   data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
44
45   data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
46   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SUBSHAPES_ID());
47 }
48
49 void BuildPlugin_SubShapes::attributeChanged(const std::string& theID)
50 {
51   ModelAPI_Feature::attributeChanged(theID);
52
53   if(theID == BASE_SHAPE_ID()) {
54     AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
55     AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
56     if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
57       return;
58     }
59
60     aSubShapesAttrList->clear();
61
62     ResultPtr aContext = aShapeAttrSelection->context();
63
64     GeomShapePtr aBaseShape = aShapeAttrSelection->value();
65     if(!aBaseShape.get()) {
66       return;
67     }
68     GeomAPI_Shape::ShapeType aBaseShapeType = aBaseShape->shapeType();
69     for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
70       GeomShapePtr aSubShape = anIt.current();
71       if(aBaseShapeType == GeomAPI_Shape::WIRE) {
72         for(GeomAPI_ShapeIterator aSubIt(aSubShape); aSubIt.more(); aSubIt.next()) {
73           GeomShapePtr aSubOfSubShape = aSubIt.current();
74           if(aSubOfSubShape->orientation() == GeomAPI_Shape::INTERNAL) {
75             aSubShapesAttrList->append(aContext, aSubOfSubShape);
76           }
77         }
78       } else if(aBaseShapeType == GeomAPI_Shape::FACE) {
79         if(aSubShape->shapeType() != GeomAPI_Shape::WIRE) {
80           aSubShapesAttrList->append(aContext, aSubShape);
81         } else if(aSubShape->orientation() == GeomAPI_Shape::INTERNAL) {
82           if(aSubShape->shapeType() == GeomAPI_Shape::WIRE) {
83             for(GeomAPI_ShapeIterator aWireIt(aSubShape); aWireIt.more(); aWireIt.next()) {
84               aSubShapesAttrList->append(aContext, aWireIt.current());
85             }
86           }
87         }
88       }
89     }
90   }
91 }
92
93 //==================================================================================================
94 void BuildPlugin_SubShapes::execute()
95 {
96   // Get base shape and sub-shapes list.
97   AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
98   AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
99   if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
100     return;
101   }
102
103   // Get base shape without internal shapes.
104   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
105   if(!aBaseShape.get()) {
106     return;
107   }
108   GeomAlgoAPI_ShapeBuilder aBuilder;
109   aBuilder.removeInternal(aBaseShape);
110   GeomShapePtr aResultShape = aBuilder.shape();
111
112   // Get list of shapes.
113   ListOfShape aShapesToAdd;
114   for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
115     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
116     aShapesToAdd.push_back(anAttrSelectionInList->value());
117   }
118
119   // Copy sub-shapes from list to new shape.
120   if(!aShapesToAdd.empty()) {
121     aBuilder.addInternal(aResultShape, aShapesToAdd);
122     aResultShape = aBuilder.shape();
123   }
124
125   // Store result.
126   const int aModVertexTag = 1;
127   const int aModEdgeTag = 2;
128   ResultBodyPtr aResultBody = document()->createBody(data());
129   aResultBody->storeModified(aBaseShape, aResultShape);
130   aResultBody->loadAndOrientModifiedShapes(&aBuilder, aBaseShape, GeomAPI_Shape::EDGE, aModEdgeTag,
131                                           "Modified_Edge", *aBuilder.mapOfSubShapes().get());
132   for(ListOfShape::const_iterator
133       anIt = aShapesToAdd.cbegin(); anIt != aShapesToAdd.cend(); ++anIt) {
134     GeomAPI_Shape::ShapeType aShType = (*anIt)->shapeType();
135     aResultBody->loadAndOrientModifiedShapes(&aBuilder, *anIt, aShType,
136                         aShType == GeomAPI_Shape::VERTEX ? aModVertexTag : aModEdgeTag,
137                         aShType == GeomAPI_Shape::VERTEX ? "Modified_Vertex" : "Modified_Edge",
138                         *aBuilder.mapOfSubShapes().get());
139   }
140   setResult(aResultBody);
141 }