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>
14 #include <TopoDS_Compound.hxx>
15 #include <BRep_Builder.hxx>
17 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
19 return data()->document("PartDocument")->value();
22 std::shared_ptr<ModelAPI_Feature> Model_ResultPart::owner()
24 return std::shared_ptr<ModelAPI_Feature>(); // return empty pointer
27 Model_ResultPart::Model_ResultPart()
29 myIsDisabled = true; // by default it is not initialized and false to be after created
30 setIsConcealed(false);
33 void Model_ResultPart::setData(std::shared_ptr<ModelAPI_Data> theData)
35 ModelAPI_Result::setData(theData);
37 data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId());
41 void Model_ResultPart::activate()
43 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
45 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
46 std::shared_ptr<ModelAPI_Document> aDoc = document()->subDocument(data()->name());
48 aDocRef->setValue(aDoc);
51 if (aDocRef->value().get()) {
52 SessionPtr aMgr = ModelAPI_Session::get();
53 bool isNewTransaction = !aMgr->isOperation();
54 // activation may cause changes in current features in document, so it must be in transaction
55 if (isNewTransaction) {
56 aMgr->startOperation("Activation");
58 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
59 if (isNewTransaction) {
60 aMgr->finishOperation();
65 bool Model_ResultPart::isActivated()
67 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
68 return aDocRef->value().get();
71 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
74 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
75 DocumentPtr aDoc = Model_ResultPart::partDoc();
77 // make the current feature the last in any case: to update shapes defore deactivation too
78 FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
79 ModelAPI_Feature::group(), aDoc->size(ModelAPI_Feature::group()) - 1));
80 aDoc->setCurrentFeature(aLastFeature, false);
81 if (theFlag) { // disable, so make all features disabled too
82 // update the shape just before the deactivation: it will be used outside of part
85 aDoc->setCurrentFeature(FeaturePtr(), false);
93 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
95 if (myShape.IsNull()) {
96 DocumentPtr aDoc = Model_ResultPart::partDoc();
98 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
99 TopoDS_Compound aResultComp;
100 BRep_Builder aBuilder;
101 aBuilder.MakeCompound(aResultComp);
103 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
104 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
105 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
106 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
107 if (!aShape.IsNull()) {
108 aBuilder.Add(aResultComp, aShape);
114 myShape = aResultComp;
118 if (myShape.IsNull())
119 return std::shared_ptr<GeomAPI_Shape>();
120 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
121 aResult->setImpl(new TopoDS_Shape(myShape));
125 #include <Model_Document.h>
126 #include <TNaming_Tool.hxx>
127 #include <TNaming_NamedShape.hxx>
128 #include <TNaming_Iterator.hxx>
129 #include <TDataStd_Name.hxx>
131 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape)
133 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
134 // getting an access to the document of part
135 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
136 if (!aDoc.get()) // the part document is not presented for the moment
138 TDF_Label anAccessLabel = aDoc->generalLabel();
141 // check if the subShape is already in DF
142 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, anAccessLabel);
143 Handle(TDataStd_Name) anAttr;
144 if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document
145 if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
146 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
148 const TDF_Label& aLabel = aDoc->findNamingName(aName);
150 static const std::string aPostFix("_");
151 TNaming_Iterator anItL(aNS);
152 for(int i = 1; anItL.More(); anItL.Next(), i++) {
153 if(anItL.NewShape() == aShape) {
155 aName += TCollection_AsciiString (i).ToCString();
165 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(const std::string& theName)
167 /// TODO: not implemented yet
168 return std::shared_ptr<GeomAPI_Shape>();
172 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
173 std::string& theDefault)
175 theSection = "Visualization";
176 theName = "result_part_color";
177 theDefault = DEFAULT_COLOR();