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 = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
49 if(!aResultCompSolid.get()) {
53 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
54 if(!aBaseShape.get()) {
55 aBaseShape = aContext->shape();
57 if(!aBaseShape.get()) {
60 GeomAPI_Shape::ShapeType aShapeType = aBaseShape->shapeType();
61 if(aShapeType != GeomAPI_Shape::WIRE
62 && aShapeType != GeomAPI_Shape::SHELL
63 && aShapeType != GeomAPI_Shape::COMPSOLID
64 && aShapeType != GeomAPI_Shape::COMPOUND) {
67 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
68 GeomShapePtr aSubShape = anIt.current();
69 const int aNumOfSubs = aResultCompSolid->numberOfSubs();
71 aSubShapesAttrList->append(aContext, aSubShape);
73 for(int anIndex = 0; anIndex < aResultCompSolid->numberOfSubs(); ++anIndex) {
74 ResultBodyPtr aSubResult = aResultCompSolid->subResult(anIndex);
75 if(aSubResult->shape()->isEqual(aSubShape)) {
76 aSubShapesAttrList->append(aSubResult, aSubShape);
85 //==================================================================================================
86 void FeaturesPlugin_RemoveSubShapes::execute()
88 // Get base shape and sub-shapes list.
89 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
90 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
91 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
96 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
97 if(!aBaseShape.get()) {
100 GeomShapePtr aResultShape = aBaseShape->emptyCopied();
102 // Copy sub-shapes from list to new shape.
103 for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
104 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
105 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
106 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
110 ResultBodyPtr aResultBody = document()->createBody(data());
111 aResultBody->storeModified(aBaseShape, aResultShape);
112 setResult(aResultBody);