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_ShapeBuilder.h>
19 #include <GeomAlgoAPI_ShapeTools.h>
21 //==================================================================================================
22 FeaturesPlugin_RemoveSubShapes::FeaturesPlugin_RemoveSubShapes()
26 //==================================================================================================
27 void FeaturesPlugin_RemoveSubShapes::initAttributes()
29 data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
31 data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
34 void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
36 ModelAPI_Feature::attributeChanged(theID);
38 if(theID == BASE_SHAPE_ID()) {
39 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
40 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
41 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
45 aSubShapesAttrList->clear();
47 ResultPtr aContext = aShapeAttrSelection->context();
48 ResultCompSolidPtr aResultCompSolid =
49 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
50 if(!aResultCompSolid.get()) {
54 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
55 if(!aBaseShape.get()) {
56 aBaseShape = aContext->shape();
58 if(!aBaseShape.get()) {
61 GeomAPI_Shape::ShapeType aShapeType = aBaseShape->shapeType();
62 if(aShapeType != GeomAPI_Shape::WIRE
63 && aShapeType != GeomAPI_Shape::SHELL
64 && aShapeType != GeomAPI_Shape::COMPSOLID
65 && aShapeType != GeomAPI_Shape::COMPOUND) {
68 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
69 GeomShapePtr aSubShape = anIt.current();
70 const int aNumOfSubs = aResultCompSolid->numberOfSubs();
72 aSubShapesAttrList->append(aContext, aSubShape);
74 for(int anIndex = 0; anIndex < aResultCompSolid->numberOfSubs(); ++anIndex) {
75 ResultBodyPtr aSubResult = aResultCompSolid->subResult(anIndex);
76 if(aSubResult->shape()->isEqual(aSubShape)) {
77 aSubShapesAttrList->append(aSubResult, aSubShape);
86 //==================================================================================================
87 void FeaturesPlugin_RemoveSubShapes::execute()
89 // Get base shape and sub-shapes list.
90 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
91 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
92 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
97 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
98 if(!aBaseShape.get()) {
101 GeomShapePtr aResultShape = aBaseShape->emptyCopied();
103 // Copy sub-shapes from list to new shape.
104 for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
105 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
106 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
107 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
111 ResultBodyPtr aResultBody = document()->createBody(data());
112 aResultBody->storeModified(aBaseShape, aResultShape);
113 setResult(aResultBody);