0b79579c1bb497cdb3a8f19452dd80106e445bce
[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 //#define SEL_DEB 1
33 #ifdef OCCT_DEBUG
34 #include <TDF_Tool.hxx>
35 #include <TDF_MapIteratorOfLabelMap.hxx>
36 #endif
37 //=======================================================================
38 //function : DNaming_SelectionDriver
39 //purpose  : Constructor
40 //=======================================================================
41 DNaming_SelectionDriver::DNaming_SelectionDriver()
42 {}
43
44 //=======================================================================
45 //function : Validate
46 //purpose  : Validates labels of a function in <theLog>.
47 //=======================================================================
48 void DNaming_SelectionDriver::Validate(TFunction_Logbook& ) const
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 //=======================================================================
56 Standard_Boolean DNaming_SelectionDriver::MustExecute(const TFunction_Logbook& ) const {
57   return Standard_True;
58 }
59
60 #ifdef OCCT_DEBUG
61 #include <BRepTools.hxx>
62 static 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>
84
85 Standard_Integer DNaming_SelectionDriver::Execute(TFunction_Logbook& theLog) const
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;
108   aMap = theLog.ChangeValid();
109 #ifdef OCCT_DEBUG
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;
125 //  TDF_Tool::OutReferences(aLabel, /*aFilterForReferers, aFilterForReferences, */outRefs);
126 //***
127
128   if(aSelector.Solve(aMap)) {
129     theLog.SetValid(aRLabel);    
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 {
143 #ifdef OCCT_DEBUG
144           Write(aNS->Get(), "Selection_Result.brep");
145           Handle(TopoDS_TShape) aTS = aNS->Get().TShape();
146           cout << "TShape = " << (Standard_Address)aTS <<endl;
147 #endif 
148           if(aIsWire && aNS->Get().ShapeType()== TopAbs_COMPOUND) {
149             TopoDS_Shape aWireShape;
150             TNaming_Tool::FindShape(aMap, aMap, aNS, aWireShape);
151             TNaming_Builder aBuilder(aRLabel);
152             aBuilder.Select(aWireShape, aWireShape);
153             aFunction->SetFailure(DONE);
154           } else
155             if(aPrevShapeType == aNS->Get().ShapeType()) {
156               aFunction->SetFailure(DONE);
157             } else {
158 #ifdef OCCT_DEBUG           
159               cout <<"%%%WARNING: DNaming_SelectionDriver::Execute: The Shape after solving changed type = "<<aNS->Get().ShapeType()<< endl;
160 #endif
161               aFunction->SetFailure(DONE);
162             }
163         }
164     }
165   }
166   else {
167     aFunction->SetFailure(NOTDONE);
168     cout << "%%%WARNING: DNaming_SelectionDriver::Execute: Selection is Not solved !!!" << endl;
169     return 1;
170   }
171   return 0;
172 }