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 <Model_Document.h>
17 #include <Events_Loop.h>
18 #include <ModelAPI_Events.h>
20 #include <TNaming_Tool.hxx>
21 #include <TNaming_NamedShape.hxx>
22 #include <TNaming_Iterator.hxx>
23 #include <TDataStd_Name.hxx>
24 #include <TopoDS_Compound.hxx>
25 #include <BRep_Builder.hxx>
26 #include <TopExp_Explorer.hxx>
28 void Model_ResultPart::initAttributes()
30 // append the color attribute. It is empty, the attribute will be filled by a request
31 DataPtr aData = data();
32 aData->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
35 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
37 return data()->document("PartDocument")->value();
40 std::shared_ptr<ModelAPI_Feature> Model_ResultPart::owner()
42 return std::shared_ptr<ModelAPI_Feature>(); // return empty pointer
45 Model_ResultPart::Model_ResultPart()
47 myIsDisabled = true; // by default it is not initialized and false to be after created
48 setIsConcealed(false);
51 void Model_ResultPart::setData(std::shared_ptr<ModelAPI_Data> theData)
53 ModelAPI_Result::setData(theData);
55 data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId());
59 void Model_ResultPart::activate()
61 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
63 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
64 std::shared_ptr<ModelAPI_Document> aDoc = document()->subDocument(data()->name());
66 aDocRef->setValue(aDoc);
69 if (aDocRef->value().get()) {
70 SessionPtr aMgr = ModelAPI_Session::get();
71 bool isNewTransaction = !aMgr->isOperation();
72 // activation may cause changes in current features in document, so it must be in transaction
73 if (isNewTransaction) {
74 aMgr->startOperation("Activation");
76 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
77 if (isNewTransaction) {
78 aMgr->finishOperation();
83 bool Model_ResultPart::isActivated()
85 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
86 return aDocRef->value().get();
89 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
92 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
93 DocumentPtr aDoc = Model_ResultPart::partDoc();
94 if (aDoc.get() && aDoc->isOpened()) {
95 // make the current feature the last in any case: to update shapes defore deactivation too
96 FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
97 ModelAPI_Feature::group(), aDoc->size(ModelAPI_Feature::group()) - 1));
98 aDoc->setCurrentFeature(aLastFeature, false);
99 if (theFlag) { // disable, so make all features disabled too
100 // update the shape just before the deactivation: it will be used outside of part
103 aDoc->setCurrentFeature(FeaturePtr(), false);
111 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
113 if (myShape.IsNull()) {
114 DocumentPtr aDoc = Model_ResultPart::partDoc();
115 if (aDoc.get() && aDoc->isOpened()) {
116 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
117 TopoDS_Compound aResultComp;
118 BRep_Builder aBuilder;
119 aBuilder.MakeCompound(aResultComp);
121 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
122 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
123 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
124 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
125 if (!aShape.IsNull()) {
126 aBuilder.Add(aResultComp, aShape);
132 myShape = aResultComp;
136 if (myShape.IsNull())
137 return std::shared_ptr<GeomAPI_Shape>();
138 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
139 aResult->setImpl(new TopoDS_Shape(myShape));
143 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
146 theIndex = 0; // not initialized
147 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
150 if (data()->isOwner(this)) { // if this is moved copy of part => return the name of original shape
151 FeaturePtr anOrigFeature =
152 std::dynamic_pointer_cast<ModelAPI_Feature>(data()->attribute(COLOR_ID())->owner());
153 if (anOrigFeature.get()) {
154 if (anOrigFeature->firstResult().get() && anOrigFeature->firstResult()->shape().get()) {
155 TopoDS_Shape anOrigShape = anOrigFeature->firstResult()->shape()->impl<TopoDS_Shape>();
156 if (!anOrigShape.IsNull()) {
157 TopExp_Explorer anExp(anOrigShape, aShape.ShapeType());
158 for(; anExp.More(); anExp.Next()) {
159 if (aShape.IsPartner(anExp.Current())) {
160 std::shared_ptr<GeomAPI_Shape> anOrigGeomShape(new GeomAPI_Shape);
161 anOrigGeomShape->setImpl(new TopoDS_Shape(anExp.Current()));
163 return std::dynamic_pointer_cast<Model_ResultPart>(anOrigFeature->firstResult())->
164 nameInPart(theShape, theIndex);
171 // getting an access to the document of part
172 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
173 if (!aDoc.get()) // the part document is not presented for the moment
175 TDF_Label anAccessLabel = aDoc->generalLabel();
178 // check if the subShape is already in DF
179 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, anAccessLabel);
180 Handle(TDataStd_Name) anAttr;
181 if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document
182 if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
183 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
185 const TDF_Label& aLabel = aDoc->findNamingName(aName);
187 static const std::string aPostFix("_");
188 TNaming_Iterator anItL(aNS);
189 for(int i = 1; anItL.More(); anItL.Next(), i++) {
190 if(anItL.NewShape() == aShape) {
192 aName += TCollection_AsciiString (i).ToCString();
199 if (aName.empty()) { // not found, so use the selection mechanism
200 // for this the context result is needed
202 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
203 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
204 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
205 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
206 TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
207 // check is body contain the selected sub-shape
208 for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
209 if (aShape.IsEqual(anExp.Current())) {
216 if (aContext.get()) {
217 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
218 aSelAttr->append(aContext, theShape);
219 theIndex = aSelAttr->size();
220 AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
221 return aNewAttr->namingName();
227 bool Model_ResultPart::updateInPart(const int theIndex)
229 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
231 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
232 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
233 if (aThisAttr.get()) {
234 return aThisAttr->update();
237 return false; // something is wrong
240 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(const std::string& theName)
242 /// TODO: not implemented yet
243 return std::shared_ptr<GeomAPI_Shape>();
247 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
248 std::string& theDefault)
250 theSection = "Visualization";
251 theName = "result_part_color";
252 theDefault = DEFAULT_COLOR();
255 void Model_ResultPart::updateShape()
260 void Model_ResultPart::setShape(std::shared_ptr<ModelAPI_Result> theThis,
261 const std::shared_ptr<GeomAPI_Shape>& theTransformed)
263 myShape = theTransformed->impl<TopoDS_Shape>();
264 // the result must be explicitly updated
265 static Events_Loop* aLoop = Events_Loop::loop();
266 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
267 ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update