]> SALOME platform Git repositories - modules/shaper.git/blob - src/InitializationPlugin/InitializationPlugin_Plugin.cpp
Salome HOME
Default planes and origin initialization
[modules/shaper.git] / src / InitializationPlugin / InitializationPlugin_Plugin.cpp
1
2 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3
4 #include <InitializationPlugin_Plugin.h>
5
6 #include <ModelAPI_Session.h>
7 #include <ModelAPI_Document.h>
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_AttributeString.h>
10
11
12 #include <Events_Message.h>
13
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_Session.h>
16
17 #include <memory>
18
19 #ifdef _DEBUG
20 #include <iostream>
21 #endif
22
23 // the only created instance of this plugin
24 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE = new InitializationPlugin_Plugin();
25
26 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
27 {
28   // register this plugin
29   SessionPtr aSession = ModelAPI_Session::get();
30   aSession->registerPlugin(this);
31
32   Events_Loop* aLoop = Events_Loop::loop();
33   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
34   aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
35 }
36
37 FeaturePtr InitializationPlugin_Plugin::createFeature(std::string theFeatureID)
38 {
39   return FeaturePtr();
40 }
41
42 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
43 {
44   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
45   if (theMessage->eventID() == kDocCreatedEvent) {
46     std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage =
47         std::dynamic_pointer_cast<ModelAPI_DocumentCreatedMessage>(theMessage);
48     DocumentPtr aDoc = aMessage->document();
49     createPoint(aDoc);
50     createPlane(aDoc, 1., 0., 0.);
51     createPlane(aDoc, 0., 1., 0.);
52     createPlane(aDoc, 0., 0., 1.);
53   } else if (theMessage.get()) {
54     #ifdef _DEBUG
55     std::cout << "InitializationPlugin_Plugin::processEvent: unhandled message caught: " << std::endl
56               << theMessage->eventID().eventText() << std::endl;
57     #endif
58   }
59 }
60
61 void InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theA, double theB, double theC)
62 {
63   std::shared_ptr<ModelAPI_Feature> aPlane = theDoc->addFeature("Plane");
64   aPlane->string("CreationMethod")->setValue("PlaneByGeneralEquation");
65   aPlane->real("A")->setValue(theA);
66   aPlane->real("B")->setValue(theB);
67   aPlane->real("C")->setValue(theC);
68   aPlane->real("D")->setValue(0.);
69
70   if (theA) {
71     aPlane->data()->setName("Y0Z");
72   } else if (theB) {
73     aPlane->data()->setName("X0Z");
74   } else if (theC) {
75     aPlane->data()->setName("X0Y");
76   }
77 }
78
79 void InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
80 {
81   std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
82   aPoint->real("x")->setValue(0.);
83   aPoint->real("y")->setValue(0.);
84   aPoint->real("z")->setValue(0.);
85   aPoint->data()->setName("Origin");
86 }
87