1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_RemoveSubShapes.cpp
4 // Created: 14 April 2016
5 // Author: Dmitry Bobylev
7 #include "FeaturesPlugin_RemoveSubShapes.h"
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>
16 #include <GeomAPI_ShapeIterator.h>
18 #include <GeomAlgoAPI_Copy.h>
19 #include <GeomAlgoAPI_ShapeBuilder.h>
20 #include <GeomAlgoAPI_ShapeTools.h>
22 //==================================================================================================
23 FeaturesPlugin_RemoveSubShapes::FeaturesPlugin_RemoveSubShapes()
27 //==================================================================================================
28 void FeaturesPlugin_RemoveSubShapes::initAttributes()
30 data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
32 data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
35 void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
37 ModelAPI_Feature::attributeChanged(theID);
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()) {
46 aSubShapesAttrList->clear();
48 ResultPtr aContext = aShapeAttrSelection->context();
49 ResultCompSolidPtr aResultCompSolid =
50 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
51 if(!aResultCompSolid.get()) {
55 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
56 if(!aBaseShape.get()) {
57 aBaseShape = aContext->shape();
59 if(!aBaseShape.get()) {
62 GeomAPI_Shape::ShapeType aShapeType = aBaseShape->shapeType();
63 if(aShapeType != GeomAPI_Shape::WIRE
64 && aShapeType != GeomAPI_Shape::SHELL
65 && aShapeType != GeomAPI_Shape::COMPSOLID
66 && aShapeType != GeomAPI_Shape::COMPOUND) {
69 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
70 GeomShapePtr aSubShape = anIt.current();
71 const int aNumOfSubs = aResultCompSolid->numberOfSubs();
73 aSubShapesAttrList->append(aContext, aSubShape);
75 for(int anIndex = 0; anIndex < aResultCompSolid->numberOfSubs(); ++anIndex) {
76 ResultBodyPtr aSubResult = aResultCompSolid->subResult(anIndex);
77 if(aSubResult->shape()->isEqual(aSubShape)) {
78 aSubShapesAttrList->append(aSubResult, aSubShape);
87 //==================================================================================================
88 void FeaturesPlugin_RemoveSubShapes::execute()
90 // Get base shape and sub-shapes list.
91 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
92 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
93 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
98 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
100 GeomShapePtr aResultShape;
101 int aSubsNb = aSubShapesAttrList->size();
103 if(!aBaseShape.get()) {
106 aResultShape = aBaseShape->emptyCopied();
108 // Copy sub-shapes from list to new shape.
109 for(int anIndex = 0; anIndex < aSubsNb; ++anIndex) {
110 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
111 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
112 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
114 } else if(aSubsNb == 1) {
115 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(0);
116 aResultShape = anAttrSelectionInList->value();
119 GeomAlgoAPI_Copy aCopy(aResultShape);
120 aResultShape = aCopy.shape();
123 ResultBodyPtr aResultBody = document()->createBody(data());
124 aResultBody->storeModified(aBaseShape, aResultShape, 1);
125 aResultBody->loadAndOrientModifiedShapes(&aCopy,
130 *aCopy.mapOfSubShapes().get(),
132 setResult(aResultBody);