1 // Copyright (C) 2014-2021 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "BuildPlugin_SubShapes.h"
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>
28 #include <GeomAPI_Edge.h>
29 #include <GeomAPI_ShapeIterator.h>
30 #include <GeomAPI_Wire.h>
32 #include <GeomAlgoAPI_ShapeBuilder.h>
34 //==================================================================================================
35 BuildPlugin_SubShapes::BuildPlugin_SubShapes()
39 //==================================================================================================
40 void BuildPlugin_SubShapes::initAttributes()
42 data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
44 data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
45 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SUBSHAPES_ID());
48 void BuildPlugin_SubShapes::attributeChanged(const std::string& theID)
50 ModelAPI_Feature::attributeChanged(theID);
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()) {
59 aSubShapesAttrList->clear();
61 ResultPtr aContext = aShapeAttrSelection->context();
63 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
64 if(!aBaseShape.get()) {
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);
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());
92 //==================================================================================================
93 void BuildPlugin_SubShapes::execute()
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()) {
102 // Get base shape without internal shapes.
103 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
104 if(!aBaseShape.get()) {
107 std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder());
108 aBuilder->removeInternal(aBaseShape);
109 GeomShapePtr aResultShape = aBuilder->shape();
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());
118 // Copy sub-shapes from list to new shape.
119 if(!aShapesToAdd.empty()) {
120 aBuilder->addInternal(aResultShape, aShapesToAdd);
121 aResultShape = aBuilder->shape();
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();
132 GeomAPI_Shape::ShapeType aShType = (*anIt)->shapeType();
133 aResultBody->loadModifiedShapes(aBuilder,
137 setResult(aResultBody);