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>
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());
39 data()->addAttribute(DEFLECTION_ID(), ModelAPI_AttributeDouble::typeId());
41 if (aDocRef->isInitialized() && // initialized immideately means already exist and will be loaded
42 !Model_Application::getApplication()->hasDocument(aDocRef->docId()))
43 Model_Application::getApplication()->setLoadByDemand(data()->name());
46 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
49 return baseRef()->partDoc();
51 DocumentPtr aRes = data()->document(DOC_REF())->value();
55 Model_ResultPart::Model_ResultPart()
59 void Model_ResultPart::activate()
62 baseRef()->activate();
66 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
68 // activation may cause changes in current features in document, so it must be in transaction
69 bool isNewTransaction = false;
70 SessionPtr aMgr = ModelAPI_Session::get();
71 if (!aMgr->isOperation()) { // open transaction even document is not created to set current docs in setActiveDocument
72 aMgr->startOperation("Activation");
73 isNewTransaction = true;
75 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
76 Handle(Model_Application) anApp = Model_Application::getApplication();
77 if (anApp->isLoadByDemand(data()->name())) {
78 anApp->loadDocument(data()->name(), aDocRef->docId()); // if it is just ne part, load may fail
80 anApp->createDocument(aDocRef->docId());
82 std::shared_ptr<ModelAPI_Document> aDoc = aDocRef->value();
84 aDoc->synchronizeTransactions();
85 aDocRef->setValue(aDoc);
88 if (aDocRef->value().get()) {
89 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
91 if (isNewTransaction) {
92 aMgr->finishOperation();
96 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
99 return baseRef()->original();
101 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
104 bool Model_ResultPart::isActivated()
107 if (!baseRef().get()) // may be on close
109 return baseRef()->isActivated();
112 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
113 return aDocRef->value().get() != NULL;
116 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
119 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
120 if (!myTrsf.get()) { // disable of base result part
121 DocumentPtr aDoc = Model_ResultPart::partDoc();
122 if (aDoc.get() && aDoc->isOpened()) {
123 // make the current feature the last in any case: to update shapes defore deactivation too
124 int aSize = aDoc->size(ModelAPI_Feature::group());
125 FeaturePtr aLastFeature;
127 aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
128 ModelAPI_Feature::group(), aSize - 1));
130 aDoc->setCurrentFeature(aLastFeature, false);
131 if (theFlag) { // disable, so make all features disabled too
132 // update the shape just before the deactivation: it will be used outside of part
135 aDoc->setCurrentFeature(FeaturePtr(), false);
136 // in order to update OB sub-elements of document before the document closing
137 Events_Loop* aLoop = Events_Loop::loop();
138 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
139 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
140 aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
149 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
151 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
152 if (myShape.IsNull()) { // shape is not produced yet, create it
153 if (myTrsf.get()) { // get shape of the base result and apply the transformation
154 ResultPtr anOrigResult = baseRef();
155 std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
156 if (anOrigShape.get()) {
157 TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
158 if (!aShape.IsNull()) {
159 aShape.Move(*(myTrsf.get()));
161 aResult->setImpl(new TopoDS_Shape(aShape));
166 DocumentPtr aDoc = Model_ResultPart::partDoc();
167 if (aDoc.get() && aDoc->isOpened()) {
168 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
169 TopoDS_Compound aResultComp;
170 BRep_Builder aBuilder;
171 aBuilder.MakeCompound(aResultComp);
173 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
174 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
175 // "object" method filters out disabled and concealed anyway, so don't check
176 if (aBody.get() && aBody->shape().get()) {
177 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
178 if (!aShape.IsNull()) {
179 aBuilder.Add(aResultComp, aShape);
185 myShape = aResultComp;
190 if (!myShape.IsNull())
191 aResult->setImpl(new TopoDS_Shape(myShape));
195 // Returns true is transformation matrices are equal
196 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
197 for(int aRow = 1; aRow < 4; aRow++) {
198 for(int aCol = 1; aCol < 5; aCol++) {
199 double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
200 if (Abs(aDiff) > 1.e-9)
207 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
210 theIndex = 0; // not initialized
212 if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
213 ResultPartPtr anOrigResult = baseRef();
214 // searching in the origin the shape equal to the given but with myTrsf
215 TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
216 gp_Trsf aSelTrsf = aSelection.Location().Transformation();
217 TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
218 if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
219 TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
220 for(; anExp.More(); anExp.Next()) {
221 if (anExp.Current().IsPartner(aSelection)) {
222 TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
223 //if (anOrigMoved.IsSame(aSelection)) {
224 if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
225 std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
226 anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
227 return anOrigResult->nameInPart(anOrigSel, theIndex);
232 // something is not right
236 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
240 // getting an access to the document of part
241 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
242 if (!aDoc.get()) // the part document is not presented for the moment
244 TDF_Label anAccessLabel = aDoc->generalLabel();
245 // make the selection attribute anyway: otherwise just by name it is not stable to search the result
247 // for this the context result is needed
249 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
250 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
251 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
252 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
253 TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
254 // check is body contain the selected sub-shape
255 for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
256 if (aShape.IsEqual(anExp.Current())) {
263 if (aContext.get()) {
264 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
265 aSelAttr->append(aContext, theShape);
266 theIndex = aSelAttr->size();
267 AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
268 return aNewAttr->namingName();
273 bool Model_ResultPart::updateInPart(const int theIndex)
275 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
277 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
278 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
279 if (aThisAttr.get()) {
280 return aThisAttr->update();
283 return false; // something is wrong
286 gp_Trsf Model_ResultPart::sumTrsf() {
290 aResult = aResult * baseRef()->sumTrsf();
295 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(
296 const std::string& theName, const std::string& theType, int& theIndex)
298 theIndex = 0; // not found yet
299 std::shared_ptr<GeomAPI_Shape> aResult;
300 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
301 if (!aDoc.get()) // the part document is not presented for the moment
304 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
305 // check this selection is already there: reuse it
306 int aSize = aSelAttr->size();
307 for(int a = 0; a < aSize; a++) {
308 if (aSelAttr->value(a)->namingName() == theName) {
310 return aSelAttr->value(a)->value();
314 aSelAttr->append(theName, theType);
315 theIndex = aSelAttr->size();
316 aResult = aSelAttr->value(theIndex - 1)->value();
317 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
318 gp_Trsf aSumTrsf = sumTrsf();
319 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
320 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
325 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::selectionValue(const int theIndex)
327 std::shared_ptr<GeomAPI_Shape> aResult;
328 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
329 if (!aDoc.get()) // the part document is not presented for the moment
332 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
333 aResult = aSelAttr->value(theIndex - 1)->value();
334 if (myTrsf.get() && aResult.get() && !aResult->isNull()) {
335 gp_Trsf aSumTrsf = sumTrsf();
336 TopoDS_Shape anOrigMoved = aResult->impl<TopoDS_Shape>().Moved(aSumTrsf);
337 aResult->setImpl(new TopoDS_Shape(anOrigMoved));
342 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
343 std::string& theDefault)
345 theSection = "Visualization";
346 theName = "result_part_color";
347 theDefault = DEFAULT_COLOR();
350 void Model_ResultPart::updateShape()
356 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
357 const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
360 if (theTransformation.get()) {
361 myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
363 // the result must be explicitly updated
364 static Events_Loop* aLoop = Events_Loop::loop();
365 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
366 ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update