1 // Copyright (C) 2014-2020 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 <FeaturesPlugin_CompositeSketch.h>
21 #include <FeaturesPlugin_Tools.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_BodyBuilder.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
30 #include <GeomAlgoAPI_Revolution.h>
32 #include <GeomAPI_ShapeExplorer.h>
35 static void storeSubShape(const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
36 ResultBodyPtr theResultBody,
37 const GeomShapePtr theShape,
38 const GeomAPI_Shape::ShapeType theType,
39 const std::string& theName);
41 //=================================================================================================
42 void FeaturesPlugin_CompositeSketch::initCompositeSketchAttribtues(const int theInitFlags)
44 // Initialize sketch launcher.
45 if(theInitFlags & InitSketchLauncher) {
46 data()->addAttribute(SKETCH_ID(), ModelAPI_AttributeReference::typeId());
47 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_ID());
50 // Initialize selection list.
51 if(theInitFlags & InitBaseObjectsList) {
52 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
53 myCurrentSelectionType = selectionList(BASE_OBJECTS_ID())->selectionType();
54 selectionList(BASE_OBJECTS_ID())->setWholeResultAllowed(true);
58 //=================================================================================================
59 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::addFeature(std::string theID)
61 FeaturePtr aNew = document()->addFeature(theID, false);
63 data()->reference(SKETCH_ID())->setValue(aNew);
66 // Set as current also after it becomes sub to set correctly enabled for other sketch subs.
67 document()->setCurrentFeature(aNew, false);
71 //=================================================================================================
72 int FeaturesPlugin_CompositeSketch::numberOfSubs(bool /*forTree*/) const
74 ObjectPtr aObj = data()->reference(SKETCH_ID())->value();
75 return aObj.get() ? 1 : 0;
78 //=================================================================================================
79 std::shared_ptr<ModelAPI_Feature> FeaturesPlugin_CompositeSketch::subFeature(const int theIndex,
82 FeaturePtr aSubFeature;
85 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
90 //=================================================================================================
91 int FeaturesPlugin_CompositeSketch::subFeatureId(const int theIndex) const
95 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->reference(SKETCH_ID())->value());
97 return aFeature->data()->featureId();
104 //=================================================================================================
105 bool FeaturesPlugin_CompositeSketch::isSub(ObjectPtr theObject) const
107 bool isSubFeature = false;
108 // Check is this feature of result
109 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
110 if (aFeature.get()) {
111 ObjectPtr aSub = data()->reference(SKETCH_ID())->value();
112 isSubFeature = aSub == theObject;
117 //=================================================================================================
118 void FeaturesPlugin_CompositeSketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
120 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
121 if(aBaseObjectsSelectionList.get() && aBaseObjectsSelectionList->size() > 0) {
122 aBaseObjectsSelectionList->clear();
125 reference(SKETCH_ID())->setValue(ObjectPtr());
128 //=================================================================================================
129 void FeaturesPlugin_CompositeSketch::getBaseShapes(ListOfShape& theBaseShapesList,
130 const bool theIsMakeShells)
132 AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
134 bool isOk = FeaturesPlugin_Tools::getShape(
135 aBaseObjectsSelectionList, theIsMakeShells, theBaseShapesList, anError);
140 //=================================================================================================
141 void FeaturesPlugin_CompositeSketch::storeResult(const GeomShapePtr theBaseShape,
142 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
145 // Create result body.
146 ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
148 // Store generated shape.
149 aResultBody->storeGenerated(theBaseShape, theMakeShape->shape());
151 // Store generated edges/faces.
152 storeGenerationHistory(aResultBody, theBaseShape, theMakeShape);
154 setResult(aResultBody, theIndex);
157 //=================================================================================================
158 void FeaturesPlugin_CompositeSketch::storeGenerationHistory(ResultBodyPtr theResultBody,
159 const GeomShapePtr theBaseShape,
160 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
162 GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
163 GeomAPI_Shape::ShapeType aShapeTypeToExplode = GeomAPI_Shape::SHAPE;
165 switch(aBaseShapeType) {
166 case GeomAPI_Shape::EDGE: {
167 aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
170 case GeomAPI_Shape::WIRE: {
171 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
174 case GeomAPI_Shape::FACE:
175 case GeomAPI_Shape::SHELL: {
176 aShapeTypeToExplode = GeomAPI_Shape::EDGE;
179 case GeomAPI_Shape::COMPOUND: {
180 aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
182 default: // [to avoid compilation warnings]
186 if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
187 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
188 theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX);
190 if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
191 aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
192 theResultBody->loadGeneratedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
194 std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> > aSweeps; // all sweeps collected
195 std::shared_ptr<GeomAlgoAPI_MakeSweep> aMakeSweep =
196 std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(theMakeShape);
197 if(aMakeSweep.get()) {
198 aSweeps.push_back(aMakeSweep);
200 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeList =
201 std::dynamic_pointer_cast<GeomAlgoAPI_MakeShapeList>(theMakeShape);
202 if (aMakeList.get()) {
203 ListOfMakeShape::const_iterator anIter = aMakeList->list().cbegin();
204 for(; anIter != aMakeList->list().cend(); anIter++) {
205 std::shared_ptr<GeomAlgoAPI_MakeSweep> aSweep =
206 std::dynamic_pointer_cast<GeomAlgoAPI_MakeSweep>(*anIter);
208 aSweeps.push_back(aSweep);
212 std::list<std::shared_ptr<GeomAlgoAPI_MakeSweep> >::iterator aSweep = aSweeps.begin();
213 for(; aSweep != aSweeps.end(); aSweep++) {
214 // Store from shapes.
215 storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->fromShapes(), "From_");
218 storeShapes(theMakeShape, theResultBody, aBaseShapeType, (*aSweep)->toShapes(), "To_");
222 //=================================================================================================
223 void FeaturesPlugin_CompositeSketch::storeShapes(
224 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
225 ResultBodyPtr theResultBody,
226 const GeomAPI_Shape::ShapeType theBaseShapeType,
227 const ListOfShape& theShapes,
228 const std::string theName)
230 GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
231 std::string aShapeTypeStr = "Face";
232 switch(theBaseShapeType) {
233 case GeomAPI_Shape::VERTEX: {
234 aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
235 aShapeTypeStr = "Vertex";
238 case GeomAPI_Shape::EDGE:
239 case GeomAPI_Shape::WIRE: {
240 aShapeTypeToExplore = GeomAPI_Shape::EDGE;
241 aShapeTypeStr = "Edge";
244 case GeomAPI_Shape::FACE:
245 case GeomAPI_Shape::SHELL: {
246 aShapeTypeToExplore = GeomAPI_Shape::FACE;
247 aShapeTypeStr = "Face";
250 case GeomAPI_Shape::COMPOUND: {
251 aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
254 default: // [to avoid compilation warnings]
259 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
260 GeomShapePtr aShape = *anIt;
262 if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
263 std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
264 storeSubShape(theMakeShape, theResultBody, aShape, aShape->shapeType(), aName);
266 std::string aName = theName + aShapeTypeStr;
267 storeSubShape(theMakeShape, theResultBody, aShape, aShapeTypeToExplore, aName);
268 if (theBaseShapeType == GeomAPI_Shape::WIRE) { // issue 2289: special names also for vertices
269 aName = theName + "Vertex";
270 storeSubShape(theMakeShape, theResultBody, aShape, GeomAPI_Shape::VERTEX, aName);
277 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
278 ResultBodyPtr theResultBody,
279 const GeomShapePtr theShape,
280 const GeomAPI_Shape::ShapeType theType,
281 const std::string& theName)
283 for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
284 GeomShapePtr aSubShape = anExp.current();
285 if (!theResultBody->generated(aSubShape, theName)) {
286 int aNbSubs = theResultBody->numberOfSubs();
288 // check the modified shape is in the result body, don't store it if not
289 ListOfShape aNewShapes;
290 theMakeShape->modified(aSubShape, aNewShapes);
291 for (int i = 0; i < aNbSubs; ++i) {
292 ResultBodyPtr aSubRes = theResultBody->subResult(i);
293 GeomShapePtr aShape = aSubRes->shape();
294 ListOfShape::iterator aNewIt = aNewShapes.begin();
295 for (; aNewIt != aNewShapes.end(); ++aNewIt)
296 if (aShape->isSubShape(*aNewIt, false))
298 if (aNewIt != aNewShapes.end()) {
299 // store from/to shapes as primitives and then store modification of them by the boolean
300 aSubRes->generated(aSubShape, theName, false);
301 aSubRes->loadModifiedShapes(theMakeShape, aSubShape, theType);
306 // store from/to shapes as primitives and then store modification of them by the boolean
307 theResultBody->generated(aSubShape, theName, false);
308 theResultBody->loadModifiedShapes(theMakeShape, aSubShape, theType);