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 <Model_Application.h>
19 #include <Events_Loop.h>
20 #include <ModelAPI_Events.h>
22 #include <TNaming_Tool.hxx>
23 #include <TNaming_NamedShape.hxx>
24 #include <TNaming_Iterator.hxx>
25 #include <TDataStd_Name.hxx>
26 #include <TopoDS_Compound.hxx>
27 #include <BRep_Builder.hxx>
28 #include <TopExp_Explorer.hxx>
30 #define baseRef() std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
32 void Model_ResultPart::initAttributes()
34 // append the color attribute. It is empty, the attribute will be filled by a request
35 AttributeDocRefPtr aDocRef = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(
36 data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId()));
37 data()->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
38 data()->addAttribute(BASE_REF_ID(), ModelAPI_AttributeReference::typeId());
40 if (aDocRef->isInitialized() && // initialized immideately means already exist and will be loaded
41 !Model_Application::getApplication()->hasDocument(aDocRef->docId()))
42 Model_Application::getApplication()->setLoadByDemand(data()->name());
45 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
48 return baseRef()->partDoc();
50 DocumentPtr aRes = data()->document(DOC_REF())->value();
54 Model_ResultPart::Model_ResultPart()
58 void Model_ResultPart::activate()
61 baseRef()->activate();
65 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
67 // activation may cause changes in current features in document, so it must be in transaction
68 bool isNewTransaction = false;
69 SessionPtr aMgr = ModelAPI_Session::get();
70 if (!aMgr->isOperation()) { // open transaction even document is not created to set current docs in setActiveDocument
71 aMgr->startOperation("Activation");
72 isNewTransaction = true;
74 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
75 Handle(Model_Application) anApp = Model_Application::getApplication();
76 if (anApp->isLoadByDemand(data()->name())) {
77 anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just ne part, load may fail
79 anApp->createDocument(aDocRef->docId());
81 std::shared_ptr<ModelAPI_Document> aDoc = aDocRef->value();
83 aDoc->synchronizeTransactions();
84 aDocRef->setValue(aDoc);
87 if (aDocRef->value().get()) {
88 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
90 if (isNewTransaction) {
91 aMgr->finishOperation();
95 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
98 return baseRef()->original();
100 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
103 bool Model_ResultPart::isActivated()
106 if (!baseRef().get()) // may be on close
108 return baseRef()->isActivated();
111 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
112 return aDocRef->value().get() != NULL;
115 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
118 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
119 if (!myTrsf.get()) { // disable of base result part
120 DocumentPtr aDoc = Model_ResultPart::partDoc();
121 if (aDoc.get() && aDoc->isOpened()) {
122 // make the current feature the last in any case: to update shapes defore deactivation too
123 int aSize = aDoc->size(ModelAPI_Feature::group());
124 FeaturePtr aLastFeature;
126 aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
127 ModelAPI_Feature::group(), aSize - 1));
129 aDoc->setCurrentFeature(aLastFeature, false);
130 if (theFlag) { // disable, so make all features disabled too
131 // update the shape just before the deactivation: it will be used outside of part
134 aDoc->setCurrentFeature(FeaturePtr(), false);
135 // in order to update OB sub-elements of document before the document closing
136 Events_Loop* aLoop = Events_Loop::loop();
137 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
138 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
139 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
148 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
150 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
151 if (myTrsf.get()) { // get shape of the base result and apply the transformation
152 ResultPtr anOrigResult = baseRef();
153 std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
154 if (anOrigShape.get()) {
155 TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
156 if (!aShape.IsNull()) {
157 aShape.Move(*(myTrsf.get()));
158 aResult->setImpl(new TopoDS_Shape(aShape));
163 if (myShape.IsNull()) { // shape is not produced yet, create it
164 DocumentPtr aDoc = Model_ResultPart::partDoc();
165 if (aDoc.get() && aDoc->isOpened()) {
166 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
167 TopoDS_Compound aResultComp;
168 BRep_Builder aBuilder;
169 aBuilder.MakeCompound(aResultComp);
171 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
172 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
173 // "object" method filters out disabled and concealed anyway, so don't check
174 if (aBody.get() && aBody->shape().get()) {
175 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
176 if (!aShape.IsNull()) {
177 aBuilder.Add(aResultComp, aShape);
183 myShape = aResultComp;
187 if (!myShape.IsNull())
188 aResult->setImpl(new TopoDS_Shape(myShape));
192 // Returns true is transformation matrices are equal
193 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
194 for(int aRow = 1; aRow < 4; aRow++) {
195 for(int aCol = 1; aCol < 5; aCol++) {
196 double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
197 if (Abs(aDiff) > 1.e-9)
204 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
207 theIndex = 0; // not initialized
209 if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
210 ResultPartPtr anOrigResult = baseRef();
211 // searching in the origin the shape equal to the given but with myTrsf
212 TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
213 gp_Trsf aSelTrsf = aSelection.Location().Transformation();
214 TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
215 if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
216 TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
217 for(; anExp.More(); anExp.Next()) {
218 if (anExp.Current().IsPartner(aSelection)) {
219 TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
220 //if (anOrigMoved.IsSame(aSelection)) {
221 if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
222 std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
223 anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
224 return anOrigResult->nameInPart(anOrigSel, theIndex);
229 // something is not right
233 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
237 // getting an access to the document of part
238 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
239 if (!aDoc.get()) // the part document is not presented for the moment
241 TDF_Label anAccessLabel = aDoc->generalLabel();
242 // make the selection attribute anyway: otherwise just by name it is not stable to search the result
244 // for this the context result is needed
246 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
247 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
248 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
249 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
250 TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
251 // check is body contain the selected sub-shape
252 for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
253 if (aShape.IsEqual(anExp.Current())) {
260 if (aContext.get()) {
261 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
262 aSelAttr->append(aContext, theShape);
263 theIndex = aSelAttr->size();
264 AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
265 return aNewAttr->namingName();
270 bool Model_ResultPart::updateInPart(const int theIndex)
272 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
274 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
275 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
276 if (aThisAttr.get()) {
277 return aThisAttr->update();
280 return false; // something is wrong
283 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
284 const std::string& theName, const std::string& theType, int& theIndex)
286 theIndex = 0; // not found yet
287 std::shared_ptr<GeomAPI_Shape> aResult;
288 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
289 if (!aDoc.get()) // the part document is not presented for the moment
292 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
293 // check this selection is already there: reuse it
294 int aSize = aSelAttr->size();
295 for(int a = 0; a < aSize; a++) {
296 if (aSelAttr->value(a)->namingName() == theName) {
298 return aSelAttr->value(a)->value();
302 aSelAttr->append(theName, theType);
303 theIndex = aSelAttr->size();
304 aResult = aSelAttr->value(theIndex - 1)->value();
308 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
310 std::shared_ptr<GeomAPI_Shape> aResult;
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
315 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
316 aResult = aSelAttr->value(theIndex - 1)->value();
320 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
321 std::string& theDefault)
323 theSection = "Visualization";
324 theName = "result_part_color";
325 theDefault = DEFAULT_COLOR();
328 void Model_ResultPart::updateShape()
334 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
335 const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
338 if (theTransformation.get()) {
339 myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
341 // the result must be explicitly updated
342 static Events_Loop* aLoop = Events_Loop::loop();
343 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
344 ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update