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 <ModelAPI_AttributeDouble.h>
18 #include <Model_Document.h>
19 #include <Model_Application.h>
20 #include <Events_Loop.h>
21 #include <ModelAPI_Events.h>
23 #include <TNaming_Tool.hxx>
24 #include <TNaming_NamedShape.hxx>
25 #include <TDataStd_Name.hxx>
26 #include <TopoDS_Compound.hxx>
27 #include <BRep_Builder.hxx>
28 #include <TopExp_Explorer.hxx>
31 std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
33 void Model_ResultPart::initAttributes()
35 // append the color attribute. It is empty, the attribute will be filled by a request
36 AttributeDocRefPtr aDocRef = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(
37 data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId()));
38 data()->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
39 data()->addAttribute(BASE_REF_ID(), ModelAPI_AttributeReference::typeId());
40 data()->addAttribute(DEFLECTION_ID(), ModelAPI_AttributeDouble::typeId());
42 if (aDocRef->isInitialized() && // initialized immideately means already exist and will be loaded
43 !Model_Application::getApplication()->hasDocument(aDocRef->docId()))
44 Model_Application::getApplication()->setLoadByDemand(data()->name());
47 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
50 return baseRef()->partDoc();
52 DocumentPtr aRes = data()->document(DOC_REF())->value();
56 Model_ResultPart::Model_ResultPart()
60 void Model_ResultPart::activate()
63 baseRef()->activate();
67 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
69 // activation may cause changes in current features in document, so it must be in transaction
70 bool isNewTransaction = false;
71 SessionPtr aMgr = ModelAPI_Session::get();
72 if (!aMgr->isOperation()) {
73 // open transaction even document is not created to set current docs in setActiveDocument
74 aMgr->startOperation("Activation");
75 isNewTransaction = true;
77 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
78 Handle(Model_Application) anApp = Model_Application::getApplication();
79 if (anApp->isLoadByDemand(data()->name())) {
80 anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just ne part, load may fail
82 anApp->createDocument(aDocRef->docId());
84 std::shared_ptr<ModelAPI_Document> aDoc = aDocRef->value();
86 aDoc->synchronizeTransactions();
87 aDocRef->setValue(aDoc);
90 if (aDocRef->value().get()) {
91 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
93 if (isNewTransaction) {
94 aMgr->finishOperation();
98 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
101 return baseRef()->original();
103 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
106 bool Model_ResultPart::isActivated()
109 if (!baseRef().get()) // may be on close
111 return baseRef()->isActivated();
114 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
115 return aDocRef->value().get() != NULL;
118 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
121 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
122 if (!myTrsf.get()) { // disable of base result part
123 DocumentPtr aDoc = Model_ResultPart::partDoc();
124 if (aDoc.get() && aDoc->isOpened()) {
125 // make the current feature the last in any case: to update shapes defore deactivation too
126 int aSize = aDoc->size(ModelAPI_Feature::group());
127 FeaturePtr aLastFeature;
129 aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
130 ModelAPI_Feature::group(), aSize - 1));
132 aDoc->setCurrentFeature(aLastFeature, false);
133 if (theFlag) { // disable, so make all features disabled too
134 // update the shape just before the deactivation: it will be used outside of part
137 aDoc->setCurrentFeature(FeaturePtr(), false);
138 // in order to update OB sub-elements of document before the document closing
139 Events_Loop* aLoop = Events_Loop::loop();
140 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
141 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
142 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
151 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
153 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
154 if (myShape.IsNull()) { // shape is not produced yet, create it
155 if (myTrsf.get()) { // get shape of the base result and apply the transformation
156 ResultPtr anOrigResult = baseRef();
157 std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
158 if (anOrigShape.get()) {
159 TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
160 if (!aShape.IsNull()) {
161 aShape.Move(*(myTrsf.get()));
163 aResult->setImpl(new TopoDS_Shape(aShape));
168 DocumentPtr aDoc = Model_ResultPart::partDoc();
169 if (aDoc.get() && aDoc->isOpened()) {
170 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
171 TopoDS_Compound aResultComp;
172 BRep_Builder aBuilder;
173 aBuilder.MakeCompound(aResultComp);
175 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
176 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
177 // "object" method filters out disabled and concealed anyway, so don't check
178 if (aBody.get() && aBody->shape().get()) {
179 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
180 if (!aShape.IsNull()) {
181 aBuilder.Add(aResultComp, aShape);
187 myShape = aResultComp;
192 if (!myShape.IsNull())
193 aResult->setImpl(new TopoDS_Shape(myShape));
197 // Returns true is transformation matrices are equal
198 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
199 for(int aRow = 1; aRow < 4; aRow++) {
200 for(int aCol = 1; aCol < 5; aCol++) {
201 double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
202 if (Abs(aDiff) > 1.e-9)
209 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
212 theIndex = 0; // not initialized
214 if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
215 ResultPartPtr anOrigResult = baseRef();
216 // searching in the origin the shape equal to the given but with myTrsf
217 TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
218 gp_Trsf aSelTrsf = aSelection.Location().Transformation();
219 TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
220 if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
221 TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
222 for(; anExp.More(); anExp.Next()) {
223 if (anExp.Current().IsPartner(aSelection)) {
224 TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
225 //if (anOrigMoved.IsSame(aSelection)) {
226 if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
227 std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
228 anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
229 return anOrigResult->nameInPart(anOrigSel, theIndex);
234 // something is not right
238 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
242 // getting an access to the document of part
243 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
244 if (!aDoc.get()) // the part document is not presented for the moment
246 TDF_Label anAccessLabel = aDoc->generalLabel();
247 // make the selection attribute anyway:
248 // otherwise just by name it is not stable to search the result
250 // for this the context result is needed
252 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
253 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
254 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
255 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
256 TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
257 // check is body contain the selected sub-shape
258 for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
259 if (aShape.IsEqual(anExp.Current())) {
266 if (aContext.get()) {
267 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
268 aSelAttr->append(aContext, theShape);
269 theIndex = aSelAttr->size();
270 AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
271 return aNewAttr->namingName();
276 bool Model_ResultPart::updateInPart(const int theIndex)
278 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
280 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
281 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
282 if (aThisAttr.get()) {
283 return aThisAttr->update();
286 return false; // something is wrong
289 gp_Trsf Model_ResultPart::sumTrsf() {
293 aResult = aResult * baseRef()->sumTrsf();
298 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
299 const std::string& theName, const std::string& theType, int& theIndex)
301 theIndex = 0; // not found yet
302 std::shared_ptr<GeomAPI_Shape> aResult;
303 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
304 if (!aDoc.get()) // the part document is not presented for the moment
307 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
308 // check this selection is already there: reuse it
309 int aSize = aSelAttr->size();
310 for(int a = 0; a < aSize; a++) {
311 if (aSelAttr->value(a)->namingName() == theName) {
313 return aSelAttr->value(a)->value();
317 aSelAttr->append(theName, theType);
318 theIndex = aSelAttr->size();
319 aResult = aSelAttr->value(theIndex - 1)->value();
320 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
321 gp_Trsf aSumTrsf = sumTrsf();
322 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
323 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
328 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
330 std::shared_ptr<GeomAPI_Shape> aResult;
331 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
332 if (!aDoc.get()) // the part document is not presented for the moment
335 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
336 aResult = aSelAttr->value(theIndex - 1)->value();
337 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
338 gp_Trsf aSumTrsf = sumTrsf();
339 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
340 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
345 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
346 std::string& theDefault)
348 theSection = "Visualization";
349 theName = "result_part_color";
350 theDefault = DEFAULT_COLOR();
353 void Model_ResultPart::updateShape()
359 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
360 const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
363 if (theTransformation.get()) {
364 myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
366 // the result must be explicitly updated
367 static Events_Loop* aLoop = Events_Loop::loop();
368 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
369 ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update