Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / PartSetPlugin / PartSetPlugin_Plugin.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
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<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "PartSetPlugin_Plugin.h"
21 #include "PartSetPlugin_Part.h"
22 #include "PartSetPlugin_Duplicate.h"
23 #include "PartSetPlugin_Remove.h"
24
25 #include <Events_Loop.h>
26
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Document.h>
29
30 #ifdef _DEBUG
31 #include <iostream>
32 #endif
33
34 // the only created instance of this plugin
35 static PartSetPlugin_Plugin* MY_PARTSET_INSTANCE = new PartSetPlugin_Plugin();
36
37 PartSetPlugin_Plugin::PartSetPlugin_Plugin()
38 {
39   // register this plugin
40   ModelAPI_Session::get()->registerPlugin(this);
41 }
42
43 FeaturePtr PartSetPlugin_Plugin::createFeature(std::string theFeatureID)
44 {
45   if (theFeatureID == PartSetPlugin_Part::ID()) {
46     return FeaturePtr(new PartSetPlugin_Part);
47   }
48   if (theFeatureID == PartSetPlugin_Duplicate::ID()) {
49     return FeaturePtr(new PartSetPlugin_Duplicate);
50   }
51   if (theFeatureID == PartSetPlugin_Remove::ID()) {
52     return FeaturePtr(new PartSetPlugin_Remove);
53   }
54   // feature of such kind is not found
55   return FeaturePtr();
56 }
57
58 void PartSetPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
59 {
60   const Events_ID kRequestEvent =
61       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
62   if (theMessage->eventID() == kRequestEvent) {
63     Events_Loop::loop()->send(getFeaturesState(), false);
64   } else if (theMessage.get()) {
65     #ifdef _DEBUG
66     std::cout << "PartSetPlugin_Plugin::processEvent: unhandled message caught: " << std::endl
67               << theMessage->eventID().eventText() << std::endl;
68     #endif
69   }
70 }
71
72 std::shared_ptr<ModelAPI_FeatureStateMessage> PartSetPlugin_Plugin::getFeaturesState()
73 {
74   const Events_ID kResponseEvent =
75         Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
76   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
77       new ModelAPI_FeatureStateMessage(kResponseEvent, this));
78   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
79   bool aDocIsPart = (aStdDocKind == PartSetPlugin_Part::ID());
80   aMsg->setState(PartSetPlugin_Part::ID(), true);
81   aMsg->setState(PartSetPlugin_Duplicate::ID(), aDocIsPart);
82   aMsg->setState(PartSetPlugin_Remove::ID(), aDocIsPart);
83   return aMsg;
84 }