1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: BuildPlugin_SubShapes.cpp
4 // Created: 14 April 2016
5 // Author: Dmitry Bobylev
7 #include "BuildPlugin_SubShapes.h"
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_Session.h>
13 #include <ModelAPI_Validator.h>
15 #include <GeomAPI_Edge.h>
16 #include <GeomAPI_ShapeIterator.h>
17 #include <GeomAPI_Wire.h>
19 #include <GeomAlgoAPI_ShapeBuilder.h>
21 //==================================================================================================
22 BuildPlugin_SubShapes::BuildPlugin_SubShapes()
26 //==================================================================================================
27 void BuildPlugin_SubShapes::initAttributes()
29 data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
31 data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
32 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SUBSHAPES_ID());
35 void BuildPlugin_SubShapes::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();
50 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
51 if(!aBaseShape.get()) {
54 GeomAPI_Shape::ShapeType aBaseShapeType = aBaseShape->shapeType();
55 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
56 GeomShapePtr aSubShape = anIt.current();
57 if(aBaseShapeType == GeomAPI_Shape::WIRE) {
58 for(GeomAPI_ShapeIterator aSubIt(aSubShape); aSubIt.more(); aSubIt.next()) {
59 GeomShapePtr aSubOfSubShape = aSubIt.current();
60 if(aSubOfSubShape->orientation() == GeomAPI_Shape::INTERNAL) {
61 aSubShapesAttrList->append(aContext, aSubOfSubShape);
64 } else if(aBaseShapeType == GeomAPI_Shape::FACE) {
65 if(aSubShape->shapeType() != GeomAPI_Shape::WIRE) {
66 aSubShapesAttrList->append(aContext, aSubShape);
67 } else if(aSubShape->orientation() == GeomAPI_Shape::INTERNAL) {
68 if(aSubShape->shapeType() == GeomAPI_Shape::WIRE) {
69 for(GeomAPI_ShapeIterator aWireIt(aSubShape); aWireIt.more(); aWireIt.next()) {
70 aSubShapesAttrList->append(aContext, aWireIt.current());
79 //==================================================================================================
80 void BuildPlugin_SubShapes::execute()
82 // Get base shape and sub-shapes list.
83 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
84 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
85 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
89 // Get base shape without internal shapes.
90 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
91 if(!aBaseShape.get()) {
94 GeomAlgoAPI_ShapeBuilder aBuilder;
95 aBuilder.removeInternal(aBaseShape);
96 GeomShapePtr aResultShape = aBuilder.shape();
98 // Get list of shapes.
99 ListOfShape aShapesToAdd;
100 for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) {
101 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
102 aShapesToAdd.push_back(anAttrSelectionInList->value());
105 // Copy sub-shapes from list to new shape.
106 if(!aShapesToAdd.empty()) {
107 aBuilder.addInternal(aResultShape, aShapesToAdd);
108 aResultShape = aBuilder.shape();
112 const int aModVertexTag = 1;
113 const int aModEdgeTag = 2;
114 ResultBodyPtr aResultBody = document()->createBody(data());
115 aResultBody->storeModified(aBaseShape, aResultShape);
116 aResultBody->loadAndOrientModifiedShapes(&aBuilder, aBaseShape, GeomAPI_Shape::EDGE, aModEdgeTag,
117 "Modified_Edge", *aBuilder.mapOfSubShapes().get());
118 for(ListOfShape::const_iterator anIt = aShapesToAdd.cbegin(); anIt != aShapesToAdd.cend(); ++anIt) {
119 GeomAPI_Shape::ShapeType aShType = (*anIt)->shapeType();
120 aResultBody->loadAndOrientModifiedShapes(&aBuilder, *anIt, aShType,
121 aShType == GeomAPI_Shape::VERTEX ? aModVertexTag : aModEdgeTag,
122 aShType == GeomAPI_Shape::VERTEX ? "Modified_Vertex" : "Modified_Edge",
123 *aBuilder.mapOfSubShapes().get());
125 setResult(aResultBody);