1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <InitializationPlugin_Plugin.h>
5 #include <ModelAPI_Session.h>
6 #include <ModelAPI_Document.h>
7 #include <ModelAPI_AttributeDouble.h>
8 #include <ModelAPI_AttributeString.h>
9 #include <ModelAPI_AttributeSelection.h>
10 #include <ModelAPI_Events.h>
11 #include <ModelAPI_Result.h>
13 #include <Events_Message.h>
14 #include <Events_Error.h>
18 // the only created instance of this plugin
19 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE =
20 new InitializationPlugin_Plugin();
22 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
24 Events_Loop* aLoop = Events_Loop::loop();
25 const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
26 aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
29 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
31 const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
32 if (theMessage->eventID() == kDocCreatedEvent) {
33 std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::dynamic_pointer_cast<
34 ModelAPI_DocumentCreatedMessage>(theMessage);
35 DocumentPtr aDoc = aMessage->document();
37 /// Issue 431: for the current moment create planes only in the module document,
38 /// Later if it is needed we may create special initial planes in Parts (may be different)
39 if (aDoc != ModelAPI_Session::get()->moduleDocument())
42 std::list<FeaturePtr> aFeatures;
44 // the viewer update should be blocked in order to avoid the features blinking before they are
46 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
47 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
48 Events_Loop::loop()->send(aMsg);
50 FeaturePtr aOrigin = createPoint(aDoc, "Origin", 0., 0., 0.);
51 aFeatures.push_back(aOrigin);
52 aFeatures.push_back(createAxis(aDoc, aOrigin, 100., 0., 0.));
53 aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 100., 0.));
54 aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 0., 100.));
55 aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
56 aFeatures.push_back(createPlane(aDoc, 0., -1., 0.));
57 aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
58 // for PartSet it is done outside of the transaction, so explicitly flush this creation
59 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
61 // hides the created features, the precondition is that the feature's results have been
62 // already built, so the createPlane/Points method calls the execute function for the planes
63 std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
64 for (; aFIter != aFeatures.cend(); aFIter++) {
65 FeaturePtr aPlane = *aFIter;
66 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
67 std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
68 for (; aRIter != aResults.cend(); aRIter++) {
69 (*aRIter)->setDisplayed(false);
72 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
74 // the viewer update should be unblocked in order to avoid the features blinking before they are
76 aMsg = std::shared_ptr<Events_Message>(
77 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
79 Events_Loop::loop()->send(aMsg);
81 } else if (theMessage.get()) {
83 std::string("InitializationPlugin_Plugin::processEvent: unhandled message caught: ")
84 + theMessage->eventID().eventText());
88 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
91 FeaturePtr aPlane = theDoc->addFeature("Plane");
92 aPlane->string("CreationMethod")->setValue("PlaneByGeneralEquation");
93 aPlane->real("A")->setValue(theX);
94 aPlane->real("B")->setValue(theY);
95 aPlane->real("C")->setValue(theZ);
96 aPlane->real("D")->setValue(0.);
99 aPlane->data()->setName("YOZ");
101 aPlane->data()->setName("XOZ");
103 aPlane->data()->setName("XOY");
105 aPlane->setInHistory(aPlane, false); // don't show automatically created feature in the features history
107 // the plane should be executed in order to build the feature result immediatelly
108 // the results are to be hidden in the plugin
110 // this flag is needed here to avoid setting it inside of the next transaction
111 // (may cause crash on redo of the first transaction in OCAF)
112 aPlane->data()->execState(ModelAPI_StateDone);
113 aPlane->firstResult()->data()->execState(ModelAPI_StateDone);
118 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc, const std::string& theName,
119 double theX, double theY, double theZ)
121 std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
122 aPoint->real("x")->setValue(theX);
123 aPoint->real("y")->setValue(theY);
124 aPoint->real("z")->setValue(theZ);
125 aPoint->data()->setName(theName);
126 aPoint->setInHistory(aPoint, false); // don't show automatically created feature in the features history
128 // the point should be executed in order to build the feature result immediatelly
129 // the results are to be hidden in the plugin
131 aPoint->data()->execState(ModelAPI_StateDone);
132 aPoint->firstResult()->data()->execState(ModelAPI_StateDone);
137 FeaturePtr InitializationPlugin_Plugin::createAxis(DocumentPtr theDoc, FeaturePtr theOrigin,
138 double theX, double theY, double theZ)
140 std::shared_ptr<ModelAPI_Feature> aAxis = theDoc->addFeature("Axis");
141 aAxis->string("CreationMethod")->setValue("AxisByPointAndDirection");
143 ResultPtr aResult = theOrigin->firstResult();
144 aAxis->selection("FirstPoint")->setValue(aResult, aResult->shape());
146 aAxis->real("X_Direction")->setValue(theX);
147 aAxis->real("Y_Direction")->setValue(theY);
148 aAxis->real("Z_Direction")->setValue(theZ);
151 aAxis->data()->setName("OX");
152 } else if (theY != 0) {
153 aAxis->data()->setName("OY");
154 } else if (theZ != 0) {
155 aAxis->data()->setName("OZ");
157 aAxis->setInHistory(aAxis, false); // don't show automatically created feature in the features history
159 aAxis->data()->execState(ModelAPI_StateDone);
160 aAxis->firstResult()->data()->execState(ModelAPI_StateDone);