1 // Copyright (C) 2014-2020 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 <Model_ResultPart.h>
21 #include <ModelAPI_Data.h>
22 #include <Model_Data.h>
23 #include <ModelAPI_AttributeDocRef.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_AttributeIntArray.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_AttributeReference.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <Model_Document.h>
32 #include <Model_Application.h>
33 #include <Events_Loop.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_Tools.h>
37 #include <GeomAPI_Trsf.h>
39 #include <GeomAlgoAPI_Transform.h>
41 #include <Locale_Convert.h>
43 #include <TNaming_Tool.hxx>
44 #include <TNaming_NamedShape.hxx>
45 #include <TDataStd_Name.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <BRep_Builder.hxx>
48 #include <TopExp_Explorer.hxx>
51 std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
53 void Model_ResultPart::initAttributes()
55 // append the color attribute. It is empty, the attribute will be filled by a request
56 AttributeDocRefPtr aDocRef = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(
57 data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId()));
58 data()->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
59 data()->addAttribute(BASE_REF_ID(), ModelAPI_AttributeReference::typeId());
60 data()->addAttribute(DEFLECTION_ID(), ModelAPI_AttributeDouble::typeId());
61 data()->addAttribute(TRANSPARENCY_ID(), ModelAPI_AttributeDouble::typeId());
62 data()->addAttribute(ISO_LINES_ID(), ModelAPI_AttributeIntArray::typeId());
64 if (aDocRef->isInitialized() && // initialized immediately means already exist and will be loaded
65 !Model_Application::getApplication()->hasDocument(aDocRef->docId()))
66 Model_Application::getApplication()->setLoadByDemand(data()->name(), aDocRef->docId());
69 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
71 if (myTrsf.get() && baseRef().get()) { // the second condition is due to #2035
72 return baseRef()->partDoc();
74 if (!data()->isValid())
76 DocumentPtr aRes = data()->document(DOC_REF())->value();
80 Model_ResultPart::Model_ResultPart()
84 void Model_ResultPart::activate()
87 baseRef()->activate();
91 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
93 // activation may cause changes in current features in document, so it must be in transaction
94 bool isNewTransaction = false;
95 SessionPtr aMgr = ModelAPI_Session::get();
96 if (!aMgr->isOperation()) {
97 // open transaction even document is not created to set current docs in setActiveDocument
98 std::string aMsg = "Activation " + Locale::Convert::toString(data()->name());
99 aMgr->startOperation(aMsg);
100 isNewTransaction = true;
102 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
103 Handle(Model_Application) anApp = Model_Application::getApplication();
104 if (anApp->isLoadByDemand(data()->name(), aDocRef->docId())) {
105 anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just new part, load fails
107 anApp->createDocument(aDocRef->docId());
109 std::shared_ptr<ModelAPI_Document> aDoc = aDocRef->value();
111 aDoc->synchronizeTransactions();
112 aDocRef->setValue(aDoc);
115 if (aDocRef->value().get()) {
116 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
118 if (isNewTransaction) {
119 aMgr->finishOperation();
124 void Model_ResultPart::loadPart()
126 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
127 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
128 Handle(Model_Application) anApp = Model_Application::getApplication();
129 if (anApp->isLoadByDemand(data()->name(), aDocRef->docId())) {
130 anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just new part, load fails
133 anApp->createDocument(aDocRef->docId());
139 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
141 if (myTrsf.get() && baseRef().get()) { // the second condition is due to #2035
142 return baseRef()->original();
144 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
147 bool Model_ResultPart::isActivated()
150 if (!baseRef().get()) // may be on close
152 return baseRef()->isActivated();
155 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
156 return aDocRef->value().get() != NULL;
159 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
162 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
163 if (!myTrsf.get()) { // disable of base result part
164 DocumentPtr aDoc = Model_ResultPart::partDoc();
165 if (aDoc.get() && aDoc->isOpened()) {
166 // make the current feature the last in any case: to update shapes before deactivation too
167 int aSize = aDoc->size(ModelAPI_Feature::group());
168 FeaturePtr aLastFeature;
170 aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
171 ModelAPI_Feature::group(), aSize - 1));
173 aDoc->setCurrentFeature(aLastFeature, false);
174 if (theFlag) { // disable, so make all features disabled too
175 // update the shape just before the deactivation: it will be used outside of part
178 aDoc->setCurrentFeature(FeaturePtr(), false);
179 // in order to update OB sub-elements of document before the document closing
180 Events_Loop* aLoop = Events_Loop::loop();
181 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
182 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
183 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
192 static GeomShapePtr transformShape(const GeomShapePtr theShape, const gp_Trsf& theTrsf)
194 GeomShapePtr aResult(new GeomAPI_Shape);
195 if (theTrsf.ScaleFactor() > 0) {
196 // just update the location of the shape in case of affine transformation
197 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
198 if (!aShape.IsNull()) {
199 aShape.Move(theTrsf);
200 aResult->setImpl(new TopoDS_Shape(aShape));
204 // all other transformations will modify the shape
205 GeomTrsfPtr aTrsf = std::make_shared<GeomAPI_Trsf>(new gp_Trsf(theTrsf));
206 GeomAlgoAPI_Transform aTransform(theShape, aTrsf);
207 aResult = aTransform.shape();
212 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
214 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
215 if (myShape.IsNull()) { // shape is not produced yet, create it
216 SessionPtr aMgr = ModelAPI_Session::get();
217 bool aToSendUpdate = aMgr->isOperation(); // inside of operation may send an update event
218 if (myTrsf.get()) { // get shape of the base result and apply the transformation
219 ResultPtr anOrigResult = baseRef();
220 std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
221 if (anOrigShape.get()) {
222 aResult = transformShape(anOrigShape, *myTrsf);
223 myShape = aResult->impl<TopoDS_Shape>();
225 if (!myShape.IsNull() && aToSendUpdate) {
226 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
227 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
231 DocumentPtr aDoc = Model_ResultPart::partDoc();
232 if (aDoc.get() && aDoc->isOpened()) {
233 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
234 TopoDS_Compound aResultComp;
235 BRep_Builder aBuilder;
236 aBuilder.MakeCompound(aResultComp);
238 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
239 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
240 // "object" method filters out disabled and concealed anyway, so don't check
241 if (aBody.get() && aBody->data()->isValid() && aBody->shape().get()) {
242 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
243 if (!aShape.IsNull()) {
244 aBuilder.Add(aResultComp, aShape);
250 myShape = aResultComp;
254 if (!myShape.IsNull() && aToSendUpdate) {
255 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
256 ModelAPI_EventCreator::get()->sendUpdated(data()->owner(), anEvent);
259 if (!myShape.IsNull()) {
260 aResult->setImpl(new TopoDS_Shape(myShape));
265 // Returns true is transformation matrices are equal
266 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
267 for(int aRow = 1; aRow < 4; aRow++) {
268 for(int aCol = 1; aCol < 5; aCol++) {
269 double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
270 if (Abs(aDiff) > 1.e-9)
277 std::wstring Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
280 theIndex = 0; // not initialized
282 if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
283 ResultPartPtr anOrigResult = baseRef();
284 // searching in the origin the shape equal to the given but with myTrsf
285 TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
286 gp_Trsf aSelTrsf = aSelection.Location().Transformation();
287 TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
288 if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
289 TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
290 for(; anExp.More(); anExp.Next()) {
291 if (anExp.Current().IsPartner(aSelection)) {
292 TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
293 //if (anOrigMoved.IsSame(aSelection)) {
294 if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
295 std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
296 anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
297 return anOrigResult->nameInPart(anOrigSel, theIndex);
302 // something is not right
306 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
310 // getting an access to the document of part
311 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
312 if (!aDoc.get()) // the part document is not presented for the moment
314 MAYBE_UNUSED TDF_Label anAccessLabel = aDoc->generalLabel();
315 // make the selection attribute anyway:
316 // otherwise just by name it is not stable to search the result
318 // for this the context result is needed
320 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
321 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
322 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
323 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
324 TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
325 // check is body contain the selected sub-shape
326 for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
327 if (aShape.IsSame(anExp.Current())) {
334 if (aContext.get()) {
335 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
336 aSelAttr->append(aContext, theShape);
337 theIndex = aSelAttr->size();
338 AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
339 return aNewAttr->namingName();
344 bool Model_ResultPart::updateInPart(const int theIndex)
346 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
348 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
349 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
350 if (aThisAttr.get()) {
351 if (aThisAttr->update()) {
352 bool aRemove = false;
353 aThisAttr->updateInHistory(aRemove);
354 return true; // it was updated
358 return false; // something is wrong
361 gp_Trsf Model_ResultPart::sumTrsf() {
365 aResult = aResult * baseRef()->sumTrsf();
370 bool Model_ResultPart::combineGeometrical(const int theIndex, std::wstring& theNewName)
372 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
374 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
375 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
376 if (aThisAttr.get()) {
377 aThisAttr->combineGeometrical();
378 if (aThisAttr->value().get()) {
380 theNewName = nameInPart(aThisAttr->value(), anIndex);
385 return false; // something is wrong
388 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
389 const std::wstring& theName, const std::string& theType, int& theIndex)
391 theIndex = 0; // not found yet
392 std::shared_ptr<GeomAPI_Shape> aResult;
393 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
394 if (!aDoc.get()) // the part document is not presented for the moment
397 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
398 // check this selection is already there: reuse it
399 int aSize = aSelAttr->size();
400 for(int a = 0; a < aSize; a++) {
401 if (aSelAttr->value(a)->namingName() == theName) {
403 return aSelAttr->value(a)->value();
407 aSelAttr->append(theName, theType);
408 theIndex = aSelAttr->size();
409 aResult = aSelAttr->value(theIndex - 1)->value();
410 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
411 gp_Trsf aSumTrsf = sumTrsf();
412 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
413 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
418 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
420 std::shared_ptr<GeomAPI_Shape> aResult;
421 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
422 if (!aDoc.get()) // the part document is not presented for the moment
425 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
426 aResult = aSelAttr->value(theIndex - 1)->value();
427 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
428 gp_Trsf aSumTrsf = sumTrsf();
429 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
430 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
435 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
436 std::string& theDefault)
438 theSection = "Visualization";
439 theName = "result_part_color";
440 theDefault = DEFAULT_COLOR();
443 void Model_ResultPart::updateShape()
449 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
450 const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
453 if (theTransformation.get()) {
454 myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
456 // the result must be explicitly updated
457 static Events_Loop* aLoop = Events_Loop::loop();
458 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
459 ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update
462 std::shared_ptr<GeomAPI_Trsf> Model_ResultPart::summaryTrsf()
464 GeomTrsfPtr aResult(new GeomAPI_Trsf);
465 aResult->setImpl<gp_Trsf>(new gp_Trsf(sumTrsf()));