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 <Model_Document.h>
18 #include <Events_Loop.h>
19 #include <ModelAPI_Events.h>
21 #include <TNaming_Tool.hxx>
22 #include <TNaming_NamedShape.hxx>
23 #include <TNaming_Iterator.hxx>
24 #include <TDataStd_Name.hxx>
25 #include <TopoDS_Compound.hxx>
26 #include <BRep_Builder.hxx>
27 #include <TopExp_Explorer.hxx>
29 #define baseRef() std::dynamic_pointer_cast<Model_ResultPart>(data()->reference(BASE_REF_ID())->value())
31 void Model_ResultPart::initAttributes()
33 // append the color attribute. It is empty, the attribute will be filled by a request
34 data()->addAttribute(DOC_REF(), ModelAPI_AttributeDocRef::typeId());
35 data()->addAttribute(COLOR_ID(), ModelAPI_AttributeIntArray::typeId());
36 data()->addAttribute(BASE_REF_ID(), ModelAPI_AttributeReference::typeId());
39 std::shared_ptr<ModelAPI_Document> Model_ResultPart::partDoc()
42 return baseRef()->partDoc();
44 return data()->document("PartDocument")->value();
47 Model_ResultPart::Model_ResultPart()
49 myIsDisabled = true; // by default it is not initialized and false to be after created
50 setIsConcealed(false);
53 void Model_ResultPart::activate()
56 baseRef()->activate();
60 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
62 if (!aDocRef->value().get()) { // create (or open) a document if it is not yet created
63 std::shared_ptr<ModelAPI_Document> aDoc = document()->subDocument(data()->name());
65 aDocRef->setValue(aDoc);
68 if (aDocRef->value().get()) {
69 SessionPtr aMgr = ModelAPI_Session::get();
70 bool isNewTransaction = !aMgr->isOperation();
71 // activation may cause changes in current features in document, so it must be in transaction
72 if (isNewTransaction) {
73 aMgr->startOperation("Activation");
75 ModelAPI_Session::get()->setActiveDocument(aDocRef->value());
76 if (isNewTransaction) {
77 aMgr->finishOperation();
82 std::shared_ptr<ModelAPI_ResultPart> Model_ResultPart::original()
85 return baseRef()->original();
87 return std::dynamic_pointer_cast<ModelAPI_ResultPart>(data()->owner());
90 bool Model_ResultPart::isActivated()
93 return baseRef()->isActivated();
96 std::shared_ptr<ModelAPI_AttributeDocRef> aDocRef = data()->document(DOC_REF());
97 return aDocRef->value().get() != NULL;
100 bool Model_ResultPart::setDisabled(std::shared_ptr<ModelAPI_Result> theThis,
103 if (ModelAPI_ResultPart::setDisabled(theThis, theFlag)) {
104 if (!myTrsf.get()) { // disable of base result part
105 DocumentPtr aDoc = Model_ResultPart::partDoc();
106 if (aDoc.get() && aDoc->isOpened()) {
107 // make the current feature the last in any case: to update shapes defore deactivation too
108 FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aDoc->object(
109 ModelAPI_Feature::group(), aDoc->size(ModelAPI_Feature::group()) - 1));
110 aDoc->setCurrentFeature(aLastFeature, false);
111 if (theFlag) { // disable, so make all features disabled too
112 // update the shape just before the deactivation: it will be used outside of part
115 aDoc->setCurrentFeature(FeaturePtr(), false);
124 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shape()
126 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
127 if (myTrsf.get()) { // get shape of the base result and apply the transformation
128 ResultPtr anOrigResult = baseRef();
129 std::shared_ptr<GeomAPI_Shape> anOrigShape = anOrigResult->shape();
130 if (anOrigShape.get()) {
131 TopoDS_Shape aShape = anOrigShape->impl<TopoDS_Shape>();
132 if (!aShape.IsNull()) {
133 aShape.Move(*(myTrsf.get()));
134 aResult->setImpl(new TopoDS_Shape(aShape));
139 if (myShape.IsNull()) { // shape is not produced yet, create it
140 DocumentPtr aDoc = Model_ResultPart::partDoc();
141 if (aDoc.get() && aDoc->isOpened()) {
142 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
143 TopoDS_Compound aResultComp;
144 BRep_Builder aBuilder;
145 aBuilder.MakeCompound(aResultComp);
147 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
148 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
149 // "object" method filters out disabled and concealed anyway, so don't check
150 if (aBody.get() && aBody->shape().get()) {
151 TopoDS_Shape aShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
152 if (!aShape.IsNull()) {
153 aBuilder.Add(aResultComp, aShape);
159 myShape = aResultComp;
163 if (!myShape.IsNull())
164 aResult->setImpl(new TopoDS_Shape(myShape));
168 // Returns true is transformation matrices are equal
169 static bool IsEqualTrsf(gp_Trsf& theT1, gp_Trsf theT2) {
170 for(int aRow = 1; aRow < 4; aRow++) {
171 for(int aCol = 1; aCol < 5; aCol++) {
172 double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
173 if (Abs(aDiff) > 1.e-9)
180 std::string Model_ResultPart::nameInPart(const std::shared_ptr<GeomAPI_Shape>& theShape,
183 theIndex = 0; // not initialized
185 if (myTrsf.get()) { // if this is moved copy of part => return the name of original shape
186 ResultPartPtr anOrigResult = baseRef();
187 // searching in the origin the shape equal to the given but with myTrsf
188 TopoDS_Shape aSelection = theShape->impl<TopoDS_Shape>();
189 gp_Trsf aSelTrsf = aSelection.Location().Transformation();
190 TopoDS_Shape anOrigMain = anOrigResult->shape()->impl<TopoDS_Shape>();
191 if (!aSelection.IsNull() && !anOrigMain.IsNull()) {
192 TopExp_Explorer anExp(anOrigMain, aSelection.ShapeType());
193 for(; anExp.More(); anExp.Next()) {
194 if (anExp.Current().IsPartner(aSelection)) {
195 TopoDS_Shape anOrigMoved = anExp.Current().Moved(*(myTrsf.get()));
196 //if (anOrigMoved.IsSame(aSelection)) {
197 if (IsEqualTrsf(aSelTrsf, anOrigMoved.Location().Transformation())) {
198 std::shared_ptr<GeomAPI_Shape> anOrigSel(new GeomAPI_Shape);
199 anOrigSel->setImpl(new TopoDS_Shape(anExp.Current()));
200 return anOrigResult->nameInPart(anOrigSel, theIndex);
205 // something is not right
209 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
213 // getting an access to the document of part
214 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
215 if (!aDoc.get()) // the part document is not presented for the moment
217 TDF_Label anAccessLabel = aDoc->generalLabel();
220 // check if the subShape is already in DF
221 Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(aShape, anAccessLabel);
222 Handle(TDataStd_Name) anAttr;
223 if(!aNS.IsNull() && !aNS->IsEmpty()) { // in the document
224 if(aNS->Label().FindAttribute(TDataStd_Name::GetID(), anAttr)) {
225 aName = TCollection_AsciiString(anAttr->Get()).ToCString();
227 const TDF_Label& aLabel = aDoc->findNamingName(aName);
229 static const std::string aPostFix("_");
230 TNaming_Iterator anItL(aNS);
231 for(int i = 1; anItL.More(); anItL.Next(), i++) {
232 if(anItL.NewShape() == aShape) {
234 aName += TCollection_AsciiString (i).ToCString();
241 if (aName.empty()) { // not found, so use the selection mechanism
242 // for this the context result is needed
244 const std::string& aBodyGroup = ModelAPI_ResultBody::group();
245 for(int a = aDoc->size(aBodyGroup) - 1; a >= 0; a--) {
246 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(aDoc->object(aBodyGroup, a));
247 if (aBody.get() && aBody->shape().get() && !aBody->isDisabled()) {
248 TopoDS_Shape aBodyShape = *(aBody->shape()->implPtr<TopoDS_Shape>());
249 // check is body contain the selected sub-shape
250 for(TopExp_Explorer anExp(aBodyShape, aShape.ShapeType()); anExp.More(); anExp.Next()) {
251 if (aShape.IsEqual(anExp.Current())) {
258 if (aContext.get()) {
259 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
260 aSelAttr->append(aContext, theShape);
261 theIndex = aSelAttr->size();
262 AttributeSelectionPtr aNewAttr = aSelAttr->value(theIndex - 1);
263 return aNewAttr->namingName();
269 bool Model_ResultPart::updateInPart(const int theIndex)
271 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(partDoc());
273 AttributeSelectionListPtr aSelAttr = aDoc->selectionInPartFeature();
274 AttributeSelectionPtr aThisAttr = aSelAttr->value(theIndex - 1);
275 if (aThisAttr.get()) {
276 return aThisAttr->update();
279 return false; // something is wrong
282 std::shared_ptr<GeomAPI_Shape> Model_ResultPart::shapeInPart(const std::string& theName)
284 /// TODO: not implemented yet
285 return std::shared_ptr<GeomAPI_Shape>();
289 void Model_ResultPart::colorConfigInfo(std::string& theSection, std::string& theName,
290 std::string& theDefault)
292 theSection = "Visualization";
293 theName = "result_part_color";
294 theDefault = DEFAULT_COLOR();
297 void Model_ResultPart::updateShape()
303 void Model_ResultPart::setTrsf(std::shared_ptr<ModelAPI_Result> theThis,
304 const std::shared_ptr<GeomAPI_Trsf>& theTransformation)
307 if (theTransformation.get()) {
308 myTrsf = std::shared_ptr<gp_Trsf>(new gp_Trsf(theTransformation->impl<gp_Trsf>()));
310 // the result must be explicitly updated
311 static Events_Loop* aLoop = Events_Loop::loop();
312 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
313 ModelAPI_EventCreator::get()->sendUpdated(theThis, EVENT_DISP); // flush is in preview-update