Salome HOME
updated copyright message
[modules/shaper.git] / src / PartSetPlugin / PartSetPlugin_Duplicate.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSetPlugin_Duplicate.h"
21 #include "PartSetPlugin_Part.h"
22 #include <ModelAPI_AttributeRefAttr.h>
23 #include <ModelAPI_AttributeDocRef.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_ResultPart.h>
27 #include <ModelAPI_Session.h>
28
29 void PartSetPlugin_Duplicate::execute()
30 {
31   std::shared_ptr<ModelAPI_Session> aPManager = ModelAPI_Session::get();
32   std::shared_ptr<ModelAPI_Document> aRoot = aPManager->moduleDocument();
33   std::shared_ptr<ModelAPI_ResultPart> aSource;  // searching for source document attribute
34   for (int a = aRoot->size(getGroup()) - 1; a >= 0; a--) {
35     aSource = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aRoot->object(getGroup(), a));
36     if (aSource && aSource->data()
37         && aSource->data()->document(ModelAPI_ResultPart::DOC_REF())->value()
38             == aPManager->activeDocument())
39       break;
40     aSource.reset();
41   }
42   if (aSource) {
43     // create a new Part feature
44     FeaturePtr aNewPart = aRoot->addFeature(PartSetPlugin_Part::ID(), false);
45     aNewPart->execute(); // to make result and generate a unique name
46
47     std::shared_ptr<ModelAPI_Document> aCopy = aPManager->copy(
48       aSource->data()->document(ModelAPI_ResultPart::DOC_REF())->value(),
49       std::dynamic_pointer_cast<ModelAPI_ResultPart>(aNewPart->firstResult())->partDoc()->id());
50   }
51 }