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 <ModelAPI_AttributeDocRef.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_AttributeIntArray.h>
14 #include <Model_Document.h>
16 #include <TNaming_Tool.hxx>
17 #include <TNaming_NamedShape.hxx>
18 #include <TNaming_Iterator.hxx>
19 #include <TDataStd_Name.hxx>
20 #include <TopoDS_Compound.hxx>
21 #include <BRep_Builder.hxx>
23 void Model_ResultPart::initAttributes()
25 // append the color attribute. It is empty, the attribute will be filled by a request
26 DataPtr aData = data();
27 aData->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
30 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
32 return data()->document("PartDocument")->value();
35 std::shared_ptr<ModelAPI_Feature> Model_ResultPart::owner()
37 return std::shared_ptr<ModelAPI_Feature>(); // return empty pointer
40 Model_ResultPart::Model_ResultPart()
42 myIsDisabled = true; // by default it is not initialized and false to be after created
43 setIsConcealed(false);
46 void Model_ResultPart::setData(std::shared_ptr<ModelAPI_Data> theData)
48 ModelAPI_Result::setData(theData);
50 data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId());
54 void Model_ResultPart::activate()
56 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
58 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
59 std::shared_ptr<ModelAPI_Document> aDoc = document()->subDocument(data()->name());
61 aDocRef->setValue(aDoc);
64 if (aDocRef->value().get()) {
65 SessionPtr aMgr = ModelAPI_Session::get();
66 bool isNewTransaction = !aMgr->isOperation();
67 // activation may cause changes in current features in document, so it must be in transaction
68 if (isNewTransaction) {
69 aMgr->startOperation("Activation");
71 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
72 if (isNewTransaction) {
73 aMgr->finishOperation();
78 bool Model_ResultPart::isActivated()
80 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
81 return aDocRef->value().get();
84 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
87 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
88 DocumentPtr aDoc = Model_ResultPart::partDoc();
89 if (aDoc.get() && aDoc->isOpened()) {
90 // make the current feature the last in any case: to update shapes defore deactivation too
91 FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
92 ModelAPI_Feature::group(), aDoc->size(ModelAPI_Feature::group()) - 1));
93 aDoc->setCurrentFeature(aLastFeature, false);
94 if (theFlag) { // disable, so make all features disabled too
95 // update the shape just before the deactivation: it will be used outside of part
98 aDoc->setCurrentFeature(FeaturePtr(), false);
106 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
108 if (myShape.IsNull()) {
109 DocumentPtr aDoc = Model_ResultPart::partDoc();
111 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
112 TopoDS_Compound aResultComp;
113 BRep_Builder aBuilder;
114 aBuilder.MakeCompound(aResultComp);
116 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
117 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
118 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
119 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
120 if (!aShape.IsNull()) {
121 aBuilder.Add(aResultComp, aShape);
127 myShape = aResultComp;
131 if (myShape.IsNull())
132 return std::shared_ptr<GeomAPI_Shape>();
133 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
134 aResult->setImpl(new TopoDS_Shape(myShape));
138 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape)
140 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
141 // getting an access to the document of part
142 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
143 if (!aDoc.get()) // the part document is not presented for the moment
145 TDF_Label anAccessLabel = aDoc->generalLabel();
148 // check if the subShape is already in DF
149 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, anAccessLabel);
150 Handle(TDataStd_Name) anAttr;
151 if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document
152 if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
153 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
155 const TDF_Label& aLabel = aDoc->findNamingName(aName);
157 static const std::string aPostFix("_");
158 TNaming_Iterator anItL(aNS);
159 for(int i = 1; anItL.More(); anItL.Next(), i++) {
160 if(anItL.NewShape() == aShape) {
162 aName += TCollection_AsciiString (i).ToCString();
172 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(const std::string& theName)
174 /// TODO: not implemented yet
175 return std::shared_ptr<GeomAPI_Shape>();
179 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
180 std::string& theDefault)
182 theSection = "Visualization";
183 theName = "result_part_color";
184 theDefault = DEFAULT_COLOR();
187 void Model_ResultPart::updateShape()