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