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_AttributeBoolean.h>
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_AttributeString.h>
10 #include <ModelAPI_AttributeSelection.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_Result.h>
14 #include <Events_Message.h>
15 #include <Events_InfoMessage.h>
19 // the only created instance of this plugin
20 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE =
21 new InitializationPlugin_Plugin();
23 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
25 Events_Loop* aLoop = Events_Loop::loop();
26 const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
27 aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
30 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
32 const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
33 if (theMessage->eventID() == kDocCreatedEvent) {
34 std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::dynamic_pointer_cast<
35 ModelAPI_DocumentCreatedMessage>(theMessage);
36 DocumentPtr aDoc = aMessage->document();
38 /// Issue 431: for the current moment create planes only in the module document,
39 /// Later if it is needed we may create special initial planes in Parts (may be different)
40 if (aDoc != ModelAPI_Session::get()->moduleDocument())
43 std::list<FeaturePtr> aFeatures;
45 // the viewer update should be blocked in order to avoid the features blinking before they are
47 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
48 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
49 Events_Loop::loop()->send(aMsg);
51 FeaturePtr aOrigin = createPoint(aDoc, "Origin", 0., 0., 0.);
52 aFeatures.push_back(aOrigin);
53 aFeatures.push_back(createAxis(aDoc, aOrigin, 100., 0., 0.));
54 aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 100., 0.));
55 aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 0., 100.));
56 aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
57 aFeatures.push_back(createPlane(aDoc, 0., -1., 0.));
58 aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
59 // for PartSet it is done outside of the transaction, so explicitly flush this creation
60 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
62 // hides the created features, the precondition is that the feature's results have been
63 // already built, so the createPlane/Points method calls the execute function for the planes
64 std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
65 for (; aFIter != aFeatures.cend(); aFIter++) {
66 FeaturePtr aPlane = *aFIter;
67 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
68 std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
69 for (; aRIter != aResults.cend(); aRIter++) {
70 (*aRIter)->setDisplayed(false);
73 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);
83 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
86 FeaturePtr aPlane = theDoc->addFeature("Plane");
87 aPlane->string("creation_method")->setValue("by_general_equation");
88 aPlane->real("A")->setValue(theX);
89 aPlane->real("B")->setValue(theY);
90 aPlane->real("C")->setValue(theZ);
91 aPlane->real("D")->setValue(0.);
94 aPlane->data()->setName("YOZ");
96 aPlane->data()->setName("XOZ");
98 aPlane->data()->setName("XOY");
100 aPlane->setInHistory(aPlane, false); // don't show automatically created feature in the features history
102 // the plane should be executed in order to build the feature result immediatelly
103 // the results are to be hidden in the plugin
105 // this flag is needed here to avoid setting it inside of the next transaction
106 // (may cause crash on redo of the first transaction in OCAF)
107 aPlane->data()->execState(ModelAPI_StateDone);
108 aPlane->firstResult()->data()->execState(ModelAPI_StateDone);
113 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc, const std::string& theName,
114 double theX, double theY, double theZ)
116 std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
117 //aPoint->string("creation_method")->setValue("by_xyz");
118 aPoint->real("x")->setValue(theX);
119 aPoint->real("y")->setValue(theY);
120 aPoint->real("z")->setValue(theZ);
121 aPoint->data()->setName(theName);
122 aPoint->setInHistory(aPoint, false); // don't show automatically created feature in the features history
124 // the point should be executed in order to build the feature result immediatelly
125 // the results are to be hidden in the plugin
127 aPoint->data()->execState(ModelAPI_StateDone);
128 aPoint->firstResult()->data()->execState(ModelAPI_StateDone);
133 FeaturePtr InitializationPlugin_Plugin::createAxis(DocumentPtr theDoc, FeaturePtr theOrigin,
134 double theX, double theY, double theZ)
136 std::shared_ptr<ModelAPI_Feature> aAxis = theDoc->addFeature("Axis");
137 aAxis->string("CreationMethod")->setValue("AxisByPointAndDirection");
139 ResultPtr aResult = theOrigin->firstResult();
140 aAxis->selection("FirstPoint")->setValue(aResult, aResult->shape());
142 aAxis->real("X_Direction")->setValue(theX);
143 aAxis->real("Y_Direction")->setValue(theY);
144 aAxis->real("Z_Direction")->setValue(theZ);
147 aAxis->data()->setName("OX");
148 } else if (theY != 0) {
149 aAxis->data()->setName("OY");
150 } else if (theZ != 0) {
151 aAxis->data()->setName("OZ");
153 aAxis->setInHistory(aAxis, false); // don't show automatically created feature in the features history
155 aAxis->data()->execState(ModelAPI_StateDone);
156 aAxis->firstResult()->data()->execState(ModelAPI_StateDone);