0026005: Problem with transient TFunction_Logbook
[occt.git] / src / DNaming / DNaming_SelectionDriver.cxx
CommitLineData
b311480e 1// Created on: 2009-05-05
2// Created by: Sergey ZARITCHNY
973c2be1 3// Copyright (c) 2009-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
42cf5bc1 16
17#include <DNaming_SelectionDriver.hxx>
18#include <ModelDefinitions.hxx>
19#include <Standard_Type.hxx>
20#include <TDataStd_Integer.hxx>
7fd59977 21#include <TDF_Label.hxx>
22#include <TDF_LabelMap.hxx>
42cf5bc1 23#include <TFunction_Function.hxx>
7fd59977 24#include <TFunction_Logbook.hxx>
7fd59977 25#include <TNaming_Builder.hxx>
42cf5bc1 26#include <TNaming_NamedShape.hxx>
7fd59977 27#include <TNaming_Selector.hxx>
28#include <TNaming_Tool.hxx>
42cf5bc1 29#include <TopAbs.hxx>
30#include <TopoDS_Shape.hxx>
7fd59977 31
32//#define SEL_DEB 1
0797d9d3 33#ifdef OCCT_DEBUG
7fd59977 34#include <TDF_Tool.hxx>
35#include <TDF_MapIteratorOfLabelMap.hxx>
36#endif
37//=======================================================================
38//function : DNaming_SelectionDriver
39//purpose : Constructor
40//=======================================================================
41DNaming_SelectionDriver::DNaming_SelectionDriver()
42{}
43
44//=======================================================================
45//function : Validate
46//purpose : Validates labels of a function in <theLog>.
47//=======================================================================
f486f64d 48void DNaming_SelectionDriver::Validate(Handle(TFunction_Logbook)& ) const
7fd59977 49{}
50
51//=======================================================================
52//function : MustExecute
53//purpose : Analyse in <theLog> if the loaded function must be
54// executed (i.e.arguments are modified) or not.
55//=======================================================================
f486f64d 56Standard_Boolean DNaming_SelectionDriver::MustExecute(const Handle(TFunction_Logbook)& ) const {
7fd59977 57 return Standard_True;
58}
59
0797d9d3 60#ifdef OCCT_DEBUG
7fd59977 61#include <BRepTools.hxx>
62static void Write(const TopoDS_Shape& shape,
63 const Standard_CString filename)
64{
65 ofstream save;
66 save.open(filename);
67 save << "DBRep_DrawableShape" << endl << endl;
68 if(!shape.IsNull()) BRepTools::Write(shape, save);
69 save.close();
70}
71#endif
72//=======================================================================
73//function : Execute
74//purpose : Execute the function and push in <theLog> the impacted
75// labels (see method SetImpacted).
76//=======================================================================
77#include <TNaming_ListOfNamedShape.hxx>
78#include <TDF_AttributeMap.hxx>
79#include <TDF_IDFilter.hxx>
80#include <TDF_Tool.hxx>
81#include <TDF_MapIteratorOfAttributeMap.hxx>
82#include <TCollection_AsciiString.hxx>
83#include <TDF_ChildIterator.hxx>
74d80fb9 84
f486f64d 85Standard_Integer DNaming_SelectionDriver::Execute(Handle(TFunction_Logbook)& theLog) const
7fd59977 86{
87 Handle(TFunction_Function) aFunction;
88 Label().FindAttribute(TFunction_Function::GetID(),aFunction);
89 if(aFunction.IsNull()) return -1;
90
91
92 TDF_Label aRLabel = RESPOSITION(aFunction);
93 if(aRLabel.IsNull()) return -1;
94
95 Standard_Boolean aIsWire = Standard_False;
96 TopAbs_ShapeEnum aPrevShapeType = TopAbs_SHAPE;
97 Handle(TNaming_NamedShape) aNShape;
98 if(aRLabel.FindAttribute(TNaming_NamedShape::GetID(),aNShape)) {
99 if(!aNShape.IsNull() && !aNShape->IsEmpty()) {
100 aPrevShapeType = aNShape->Get().ShapeType();
101 if (aPrevShapeType == TopAbs_WIRE) aIsWire = Standard_True;
102 }
103 }
104
105 TNaming_Selector aSelector(aRLabel);
106
107 TDF_LabelMap aMap;
f486f64d 108 theLog->GetValid(aMap);
0797d9d3 109#ifdef OCCT_DEBUG
7fd59977 110 cout <<"#E_DNaming_SelectionDriver:: Valid Label Map:"<<endl;
111 TDF_MapIteratorOfLabelMap anItr(aMap);
112 for (; anItr.More(); anItr.Next()) {
113 const TDF_Label& aLabel = anItr.Key();
114 TCollection_AsciiString anEntry;
115 TDF_Tool::Entry(aLabel, anEntry);
116 cout << "\tLabel = " << anEntry << endl;
117 }
118#endif
119//***
120// TDF_IDFilter aFilterForReferers;
121// aFilterForReferers.Keep(TNaming_NamedShape::GetID());
122// TDF_IDFilter aFilterForReferences;
123// aFilterForReferences.Keep(TNaming_NamedShape::GetID());
124// TDF_LabelMap aMap1;
7fd59977 125// TDF_Tool::OutReferences(aLabel, /*aFilterForReferers, aFilterForReferences, */outRefs);
126//***
127
128 if(aSelector.Solve(aMap)) {
f486f64d 129 theLog->SetValid(aRLabel);
7fd59977 130 Handle(TNaming_NamedShape) aNS;
131 if(!aRLabel.FindAttribute(TNaming_NamedShape::GetID(),aNS)) {
132 cout <<"%%%WARNING: DNaming_SelectionDriver::NamedShape is not found"<<endl;
133 }
134 else {
135 if(aNS.IsNull()) {
136 cout << "%%%WARNING: DNaming_SelectionDriver::NamedShape is NULL" <<endl;
137 } else
138 if(aNS->IsEmpty()) {
139 cout << "%%%WARNING: DNaming_SelectionDriver::NamedShape is EMPTY on Label = ";
140 aNS->Label().EntryDump(cout); cout << endl;
141
142 } else {
0797d9d3 143#ifdef OCCT_DEBUG
7fd59977 144 Write(aNS->Get(), "Selection_Result.brep");
1c29294e 145 cout << "TShape = " << aNS->Get().TShape().get() <<endl;
7fd59977 146#endif
147 if(aIsWire && aNS->Get().ShapeType()== TopAbs_COMPOUND) {
148 TopoDS_Shape aWireShape;
149 TNaming_Tool::FindShape(aMap, aMap, aNS, aWireShape);
150 TNaming_Builder aBuilder(aRLabel);
151 aBuilder.Select(aWireShape, aWireShape);
152 aFunction->SetFailure(DONE);
153 } else
154 if(aPrevShapeType == aNS->Get().ShapeType()) {
155 aFunction->SetFailure(DONE);
156 } else {
0797d9d3 157#ifdef OCCT_DEBUG
7fd59977 158 cout <<"%%%WARNING: DNaming_SelectionDriver::Execute: The Shape after solving changed type = "<<aNS->Get().ShapeType()<< endl;
159#endif
160 aFunction->SetFailure(DONE);
161 }
162 }
163 }
164 }
165 else {
166 aFunction->SetFailure(NOTDONE);
167 cout << "%%%WARNING: DNaming_SelectionDriver::Execute: Selection is Not solved !!!" << endl;
168 return 1;
169 }
170 return 0;
171}