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 <Model_Document.h>
15 #include <TNaming_Tool.hxx>
16 #include <TNaming_NamedShape.hxx>
17 #include <TNaming_Iterator.hxx>
18 #include <TDataStd_Name.hxx>
19 #include <TopoDS_Compound.hxx>
20 #include <BRep_Builder.hxx>
22 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
24 return data()->document("PartDocument")->value();
27 std::shared_ptr<ModelAPI_Feature> Model_ResultPart::owner()
29 return std::shared_ptr<ModelAPI_Feature>(); // return empty pointer
32 Model_ResultPart::Model_ResultPart()
34 myIsDisabled = true; // by default it is not initialized and false to be after created
35 setIsConcealed(false);
38 void Model_ResultPart::setData(std::shared_ptr<ModelAPI_Data> theData)
40 ModelAPI_Result::setData(theData);
42 data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId());
46 void Model_ResultPart::activate()
48 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
50 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
51 std::shared_ptr<ModelAPI_Document> aDoc = document()->subDocument(data()->name());
53 aDocRef->setValue(aDoc);
56 if (aDocRef->value().get()) {
57 SessionPtr aMgr = ModelAPI_Session::get();
58 bool isNewTransaction = !aMgr->isOperation();
59 // activation may cause changes in current features in document, so it must be in transaction
60 if (isNewTransaction) {
61 aMgr->startOperation("Activation");
63 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
64 if (isNewTransaction) {
65 aMgr->finishOperation();
70 bool Model_ResultPart::isActivated()
72 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
73 return aDocRef->value().get();
76 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
79 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
80 DocumentPtr aDoc = Model_ResultPart::partDoc();
82 // make the current feature the last in any case: to update shapes defore deactivation too
83 FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
84 ModelAPI_Feature::group(), aDoc->size(ModelAPI_Feature::group()) - 1));
85 aDoc->setCurrentFeature(aLastFeature, false);
86 if (theFlag) { // disable, so make all features disabled too
87 // update the shape just before the deactivation: it will be used outside of part
90 aDoc->setCurrentFeature(FeaturePtr(), false);
98 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
100 if (myShape.IsNull()) {
101 DocumentPtr aDoc = Model_ResultPart::partDoc();
103 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
104 TopoDS_Compound aResultComp;
105 BRep_Builder aBuilder;
106 aBuilder.MakeCompound(aResultComp);
108 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
109 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
110 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
111 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
112 if (!aShape.IsNull()) {
113 aBuilder.Add(aResultComp, aShape);
119 myShape = aResultComp;
123 if (myShape.IsNull())
124 return std::shared_ptr<GeomAPI_Shape>();
125 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
126 aResult->setImpl(new TopoDS_Shape(myShape));
130 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape)
132 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
133 // getting an access to the document of part
134 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
135 if (!aDoc.get()) // the part document is not presented for the moment
137 TDF_Label anAccessLabel = aDoc->generalLabel();
140 // check if the subShape is already in DF
141 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, anAccessLabel);
142 Handle(TDataStd_Name) anAttr;
143 if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document
144 if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
145 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
147 const TDF_Label& aLabel = aDoc->findNamingName(aName);
149 static const std::string aPostFix("_");
150 TNaming_Iterator anItL(aNS);
151 for(int i = 1; anItL.More(); anItL.Next(), i++) {
152 if(anItL.NewShape() == aShape) {
154 aName += TCollection_AsciiString (i).ToCString();
164 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(const std::string& theName)
166 /// TODO: not implemented yet
167 return std::shared_ptr<GeomAPI_Shape>();
171 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
172 std::string& theDefault)
174 theSection = "Visualization";
175 theName = "result_part_color";
176 theDefault = DEFAULT_COLOR();
179 void Model_ResultPart::updateShape()