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