1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <InitializationPlugin_Plugin.h>
22 #include <InitializationPlugin_EvalListener.h>
23 #include <ModelAPI_Session.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_AttributeBoolean.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeSelection.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_Result.h>
32 #include <GeomDataAPI_Point.h>
34 #include <Events_Message.h>
35 #include <Events_InfoMessage.h>
39 // the only created instance of this plugin
40 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE =
41 new InitializationPlugin_Plugin();
43 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
45 char* isUnitTest = getenv("SHAPER_UNIT_TEST_IN_PROGRESS");
46 myInitDataModel = (!isUnitTest || isUnitTest[0] != '1');
48 Events_Loop* aLoop = Events_Loop::loop();
49 const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
50 aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
53 std::shared_ptr<InitializationPlugin_EvalListener>(new InitializationPlugin_EvalListener());
56 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
58 const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
59 if (theMessage->eventID() == kDocCreatedEvent) {
60 std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::dynamic_pointer_cast<
61 ModelAPI_DocumentCreatedMessage>(theMessage);
62 DocumentPtr aDoc = aMessage->document();
64 /// Issue 431: for the current moment create planes only in the module document,
65 /// Later if it is needed we may create special initial planes in Parts (may be different)
66 if (aDoc != ModelAPI_Session::get()->moduleDocument())
70 myEvalListener->initDataModel();
72 std::list<FeaturePtr> aFeatures;
74 // the viewer update should be blocked in order to avoid the features blinking before they are
76 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
77 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
78 Events_Loop::loop()->send(aMsg);
80 FeaturePtr aOrigin = createPoint(aDoc, L"Origin", 0., 0., 0.);
81 aFeatures.push_back(aOrigin);
82 aFeatures.push_back(createAxis(aDoc, aOrigin, 100., 0., 0.));
83 aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 100., 0.));
84 aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 0., 100.));
85 aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
86 aFeatures.push_back(createPlane(aDoc, 0., -1., 0.));
87 aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
88 // for PartSet it is done outside of the transaction, so explicitly flush this creation
89 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
91 // hides the created features, the precondition is that the feature's results have been
92 // already built, so the createPlane/Points method calls the execute function for the planes
93 std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
94 for (; aFIter != aFeatures.cend(); aFIter++) {
95 FeaturePtr aPlane = *aFIter;
96 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
97 std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
98 for (; aRIter != aResults.cend(); aRIter++) {
99 (*aRIter)->setDisplayed(false);
102 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
103 // the viewer update should be unblocked in order to avoid the features blinking before they are
105 aMsg = std::shared_ptr<Events_Message>(
106 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
108 Events_Loop::loop()->send(aMsg);
112 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
115 FeaturePtr aPlane = theDoc->addFeature("Plane");
116 aPlane->string("creation_method")->setValue("by_general_equation");
117 aPlane->real("A")->setValue(theX);
118 aPlane->real("B")->setValue(theY);
119 aPlane->real("C")->setValue(theZ);
120 aPlane->real("D")->setValue(0.);
123 aPlane->data()->setName(L"YOZ");
125 aPlane->data()->setName(L"XOZ");
127 aPlane->data()->setName(L"XOY");
129 // don't show automatically created feature in the features history
130 aPlane->setInHistory(aPlane, false);
132 // the plane should be executed in order to build the feature result immediatelly
133 // the results are to be hidden in the plugin
135 // this flag is needed here to avoid setting it inside of the next transaction
136 // (may cause crash on redo of the first transaction in OCAF)
137 aPlane->data()->execState(ModelAPI_StateDone);
138 aPlane->firstResult()->data()->execState(ModelAPI_StateDone);
143 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc, const std::wstring& theName,
144 double theX, double theY, double theZ)
146 std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
147 AttributePointPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>
148 (aPoint->data()->attribute("point3d"));
149 aPointAttr->setValue(theX, theY, theZ);
150 aPoint->string("creation_method")->setValue("by_xyz");
151 aPoint->data()->setName(theName);
152 // don't show automatically created feature in the features history
153 aPoint->setInHistory(aPoint, false);
155 // the point should be executed in order to build the feature result immediatelly
156 // the results are to be hidden in the plugin
158 aPoint->data()->execState(ModelAPI_StateDone);
159 aPoint->firstResult()->data()->execState(ModelAPI_StateDone);
164 FeaturePtr InitializationPlugin_Plugin::createAxis(DocumentPtr theDoc, FeaturePtr theOrigin,
165 double theX, double theY, double theZ)
167 std::shared_ptr<ModelAPI_Feature> aAxis = theDoc->addFeature("Axis");
168 aAxis->string("CreationMethod")->setValue("AxisByPointAndDirection");
170 ResultPtr aResult = theOrigin->firstResult();
171 aAxis->selection("FirstPoint")->setValue(aResult, aResult->shape());
173 aAxis->real("X_Direction")->setValue(theX);
174 aAxis->real("Y_Direction")->setValue(theY);
175 aAxis->real("Z_Direction")->setValue(theZ);
178 aAxis->data()->setName(L"OX");
179 } else if (theY != 0) {
180 aAxis->data()->setName(L"OY");
181 } else if (theZ != 0) {
182 aAxis->data()->setName(L"OZ");
184 // don't show automatically created feature in the features history
185 aAxis->setInHistory(aAxis, false);
187 aAxis->data()->execState(ModelAPI_StateDone);
188 aAxis->firstResult()->data()->execState(ModelAPI_StateDone);