1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModelAPI_ResultPart.cpp
4 // Created: 07 Jul 2014
5 // Author: Mikhail PONIKAROV
7 #include <Model_ResultPart.h>
8 #include <ModelAPI_Data.h>
9 #include <Model_Data.h>
10 #include <ModelAPI_AttributeDocRef.h>
11 #include <ModelAPI_Session.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_ResultBody.h>
14 #include <ModelAPI_AttributeIntArray.h>
15 #include <ModelAPI_AttributeSelectionList.h>
16 #include <ModelAPI_AttributeReference.h>
17 #include <Model_Document.h>
18 #include <Events_Loop.h>
19 #include <ModelAPI_Events.h>
21 #include <TNaming_Tool.hxx>
22 #include <TNaming_NamedShape.hxx>
23 #include <TNaming_Iterator.hxx>
24 #include <TDataStd_Name.hxx>
25 #include <TopoDS_Compound.hxx>
26 #include <BRep_Builder.hxx>
27 #include <TopExp_Explorer.hxx>
29 #define baseRef() std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
31 void Model_ResultPart::initAttributes()
33 // append the color attribute. It is empty, the attribute will be filled by a request
34 data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId());
35 data()->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
36 data()->addAttribute(BASE_REF_ID(), ModelAPI_AttributeReference::typeId());
39 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
42 return baseRef()->partDoc();
44 DocumentPtr aRes = data()->document(DOC_REF())->value();
45 if (!aRes.get() && myIsInLoad) { // trying to get this document from the session
46 aRes = document()->subDocument(data()->name());
51 Model_ResultPart::Model_ResultPart()
56 void Model_ResultPart::activate()
59 baseRef()->activate();
63 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
65 // activation may cause changes in current features in document, so it must be in transaction
66 bool isNewTransaction = false;
67 SessionPtr aMgr = ModelAPI_Session::get();
68 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
70 if (!aMgr->isOperation()) {
71 aMgr->startOperation("Activation");
72 isNewTransaction = true;
74 std::shared_ptr<ModelAPI_Document> aDoc = document()->subDocument(data()->name());
77 aDoc->synchronizeTransactions();
78 aDocRef->setValue(aDoc);
81 if (aDocRef->value().get()) {
82 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
84 if (isNewTransaction) {
85 aMgr->finishOperation();
89 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
92 return baseRef()->original();
94 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
97 bool Model_ResultPart::isActivated()
100 return baseRef()->isActivated();
103 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
104 return aDocRef->value().get() != NULL;
107 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
110 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
111 if (!myTrsf.get()) { // disable of base result part
112 DocumentPtr aDoc = Model_ResultPart::partDoc();
113 if (aDoc.get() && aDoc->isOpened()) {
114 std::shared_ptr<Model_Document> anIntDoc = std::dynamic_pointer_cast<Model_Document>(aDoc);
115 // make the current feature the last in any case: to update shapes defore deactivation too
116 FeaturePtr aLastFeature = anIntDoc->lastFeature();
117 aDoc->setCurrentFeature(aLastFeature, false);
118 if (theFlag) { // disable, so make all features disabled too
119 // update the shape just before the deactivation: it will be used outside of part
122 aDoc->setCurrentFeature(FeaturePtr(), false);
123 // in order to update OB sub-elements of document before the document closing
124 Events_Loop* aLoop = Events_Loop::loop();
125 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
126 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
127 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
136 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
138 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
139 if (myTrsf.get()) { // get shape of the base result and apply the transformation
140 ResultPtr anOrigResult = baseRef();
141 std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
142 if (anOrigShape.get()) {
143 TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
144 if (!aShape.IsNull()) {
145 aShape.Move(*(myTrsf.get()));
146 aResult->setImpl(new TopoDS_Shape(aShape));
151 if (myShape.IsNull()) { // shape is not produced yet, create it
152 DocumentPtr aDoc = Model_ResultPart::partDoc();
153 if (aDoc.get() && aDoc->isOpened()) {
154 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
155 TopoDS_Compound aResultComp;
156 BRep_Builder aBuilder;
157 aBuilder.MakeCompound(aResultComp);
159 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
160 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
161 // "object" method filters out disabled and concealed anyway, so don't check
162 if (aBody.get() && aBody->shape().get()) {
163 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
164 if (!aShape.IsNull()) {
165 aBuilder.Add(aResultComp, aShape);
171 myShape = aResultComp;
175 if (!myShape.IsNull())
176 aResult->setImpl(new TopoDS_Shape(myShape));
180 // Returns true is transformation matrices are equal
181 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
182 for(int aRow = 1; aRow < 4; aRow++) {
183 for(int aCol = 1; aCol < 5; aCol++) {
184 double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
185 if (Abs(aDiff) > 1.e-9)
192 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
195 theIndex = 0; // not initialized
197 if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
198 ResultPartPtr anOrigResult = baseRef();
199 // searching in the origin the shape equal to the given but with myTrsf
200 TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
201 gp_Trsf aSelTrsf = aSelection.Location().Transformation();
202 TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
203 if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
204 TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
205 for(; anExp.More(); anExp.Next()) {
206 if (anExp.Current().IsPartner(aSelection)) {
207 TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
208 //if (anOrigMoved.IsSame(aSelection)) {
209 if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
210 std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
211 anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
212 return anOrigResult->nameInPart(anOrigSel, theIndex);
217 // something is not right
221 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
225 // getting an access to the document of part
226 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
227 if (!aDoc.get()) // the part document is not presented for the moment
229 TDF_Label anAccessLabel = aDoc->generalLabel();
230 // make the selection attribute anyway: otherwise just by name it is not stable to search the result
232 // for this the context result is needed
234 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
235 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
236 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
237 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
238 TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
239 // check is body contain the selected sub-shape
240 for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
241 if (aShape.IsEqual(anExp.Current())) {
248 if (aContext.get()) {
249 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
250 aSelAttr->append(aContext, theShape);
251 theIndex = aSelAttr->size();
252 AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
253 return aNewAttr->namingName();
258 bool Model_ResultPart::updateInPart(const int theIndex)
260 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
262 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
263 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
264 if (aThisAttr.get()) {
265 return aThisAttr->update();
268 return false; // something is wrong
271 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
272 const std::string& theName, const std::string& theType, int& theIndex)
274 theIndex = 0; // not found yet
275 std::shared_ptr<GeomAPI_Shape> aResult;
276 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
277 if (!aDoc.get()) // the part document is not presented for the moment
280 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
281 // check this selection is already there: reuse it
282 int aSize = aSelAttr->size();
283 for(int a = 0; a < aSize; a++) {
284 if (aSelAttr->value(a)->namingName() == theName) {
286 return aSelAttr->value(a)->value();
290 aSelAttr->append(theName, theType);
291 theIndex = aSelAttr->size();
292 aResult = aSelAttr->value(theIndex - 1)->value();
296 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
298 std::shared_ptr<GeomAPI_Shape> aResult;
299 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
300 if (!aDoc.get()) // the part document is not presented for the moment
303 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
304 aResult = aSelAttr->value(theIndex - 1)->value();
308 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
309 std::string& theDefault)
311 theSection = "Visualization";
312 theName = "result_part_color";
313 theDefault = DEFAULT_COLOR();
316 void Model_ResultPart::updateShape()
322 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
323 const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
326 if (theTransformation.get()) {
327 myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
329 // the result must be explicitly updated
330 static Events_Loop* aLoop = Events_Loop::loop();
331 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
332 ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update