0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / TNaming / TNaming_Identifier.cxx
1 // Created on: 1997-03-04
2 // Created by: Yves FRICAUD
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <TDF_ChildIterator.hxx>
19 #include <TDF_Label.hxx>
20 #include <TNaming_Identifier.hxx>
21 #include <TNaming_Iterator.hxx>
22 #include <TNaming_Localizer.hxx>
23 #include <TNaming_MapIteratorOfMapOfNamedShape.hxx>
24 #include <TNaming_MapOfNamedShape.hxx>
25 #include <TNaming_NamedShape.hxx>
26 #include <TNaming_Tool.hxx>
27 #include <TNaming_UsedShapes.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopTools_MapIteratorOfMapOfShape.hxx>
30 #include <TopTools_MapOfShape.hxx>
31
32 //#define MDTV_DEB_IDF
33 #ifdef OCCT_DEBUG_IDF
34 #include <TCollection_AsciiString.hxx>
35 #include <TDF_Tool.hxx>
36 #include <BRepTools.hxx>
37
38 //unreferenced function, commented  
39 /*static void ModDbgTools_Write(const TopoDS_Shape& shape,
40                       const Standard_CString filename) 
41 {
42   std::ofstream save;
43   save.open(filename);
44   save << "DBRep_DrawableShape" << std::endl << std::endl;
45   if(!shape.IsNull()) BRepTools::Write(shape, save);
46   save.close();
47 }
48
49 static void ModDbgTools_WriteCurrentShape(const Handle(TNaming_NamedShape) & NS)
50 {
51   TCollection_AsciiString entry;
52   TDF_Tool::Entry(NS->Label(), entry);
53   if (!NS.IsNull())
54     {
55       TopoDS_Shape Sh = TNaming_Tool::CurrentShape (NS);
56       if(!Sh.IsNull()) {
57         TCollection_AsciiString Entry = entry.Cat("_Cur.brep");
58         ModDbgTools_Write(Sh, Entry.ToCString());
59       }
60       else 
61         std::cout << "ModDbgTools::Write>>> TopoDS_Shape IS NULL on Entry = "<< entry << std::endl;
62     }
63   else
64     std::cout << "ModDbgTools::Write>>>  CurrentShape of TNaming_NamedShape IS NULL on Entry = "<< entry << std::endl;
65 }*/
66 #endif
67
68 //=======================================================================
69 //function : TNaming_Identifier
70 //purpose  : 
71 //=======================================================================
72
73 TNaming_Identifier::TNaming_Identifier(const TDF_Label&        LabAcces,
74                                        const TopoDS_Shape&     S,
75                                        const TopoDS_Shape&     Context,
76                                        const Standard_Boolean  /*OneOnly*/)
77 :myTDFAcces(LabAcces), myShape(S),
78 myDone(Standard_False),myIsFeature(Standard_False)
79 {
80   Init(Context); 
81 }
82
83 //=======================================================================
84 //function : TNaming_Identifier
85 //purpose  : 
86 //=======================================================================
87
88 TNaming_Identifier::TNaming_Identifier(const TDF_Label&        LabAcces,
89                                        const TopoDS_Shape&                     S,
90                                        const Handle(TNaming_NamedShape)&       ContextNS,
91                                        const Standard_Boolean                  /*OneOnly*/)
92 :myTDFAcces(LabAcces), myShape(S),
93 myDone(Standard_False),myIsFeature(Standard_False)
94 {
95   const TopoDS_Shape& aContext = TNaming_Tool::GetShape (ContextNS);
96   Init(aContext); 
97 }
98
99 //=======================================================================
100 //function : Init
101 //purpose  : 
102 //=======================================================================                                         
103 void TNaming_Identifier::Init(const TopoDS_Shape&     Context)
104 {
105   Handle(TNaming_UsedShapes) US;
106   myTDFAcces.Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
107   
108   TNaming_Localizer Localizer;
109   Localizer.Init (US,myTDFAcces.Transaction());
110   
111   Handle(TNaming_NamedShape) NS;
112   NS   = TNaming_Tool::NamedShape(myShape, myTDFAcces);
113   if (NS.IsNull()) {
114     AncestorIdentification(Localizer, Context);
115     return;
116   }
117 #ifdef OCCT_DEBUG_IDF
118   TCollection_AsciiString entry;
119   TDF_Tool::Entry(NS->Label(), entry);
120   std::cout << "Identifier:: (S) Label = " << entry <<std::endl;
121 #endif
122   //-------------------------------------
123   // S est New dans un NamedShape
124   //-------------------------------------
125   TNaming_Evolution Evol = NS->Evolution();
126   if (Evol == TNaming_PRIMITIVE) {
127     PrimitiveIdentification(Localizer,NS);
128   }
129   else if (Evol == TNaming_GENERATED) {
130     GeneratedIdentification(Localizer,NS);
131   }
132   else {
133     Identification(Localizer,NS);
134   }
135 }                                         
136
137 //=======================================================================
138 //function : Type
139 //purpose  : 
140 //=======================================================================
141
142 TNaming_NameType TNaming_Identifier::Type() const
143 {
144   return myType;
145 }
146
147 //=======================================================================
148 //function : IsDone
149 //purpose  : 
150 //=======================================================================
151
152 Standard_Boolean TNaming_Identifier::IsDone() const 
153 {
154   return myDone;
155 }
156
157
158 //=======================================================================
159 //function : IsFeature
160 //purpose  : 
161 //=======================================================================
162
163 Standard_Boolean TNaming_Identifier::IsFeature()
164 {
165   return myIsFeature;
166 }
167
168
169 //=======================================================================
170 //function : Feature
171 //purpose  : 
172 //=======================================================================
173
174 Handle(TNaming_NamedShape) TNaming_Identifier::Feature() const
175
176   return myPrimitiveArgs.First();
177 }
178
179
180 //=======================================================================
181 //function : InitArgs
182 //purpose  : 
183 //=======================================================================
184
185 void TNaming_Identifier::InitArgs() 
186 {
187 }
188
189
190 //=======================================================================
191 //function : MoreArgs
192 //purpose  : 
193 //=======================================================================
194
195 Standard_Boolean TNaming_Identifier::MoreArgs() const
196 {
197   return (!myPrimitiveArgs.IsEmpty() || !myShapeArgs.IsEmpty());
198 }
199
200 //=======================================================================
201 //function : ArgIsFeature
202 //purpose  : 
203 //=======================================================================
204
205 Standard_Boolean TNaming_Identifier::ArgIsFeature() const
206 {
207   return (!myPrimitiveArgs.IsEmpty());
208 }
209
210 //=======================================================================
211 //function : Next
212 //purpose  : 
213 //=======================================================================
214
215 void TNaming_Identifier::NextArg() 
216 {
217   if      (!myPrimitiveArgs.IsEmpty()) myPrimitiveArgs.RemoveFirst();
218   else if (!myShapeArgs    .IsEmpty()) myShapeArgs    .RemoveFirst();
219 }
220
221 //=======================================================================
222 //function : FeatureArg
223 //purpose  : 
224 //=======================================================================
225
226 Handle(TNaming_NamedShape) TNaming_Identifier::FeatureArg() 
227 {
228   return myPrimitiveArgs.First();
229 }
230
231
232 //=======================================================================
233 //function : ShapeArg
234 //purpose  : 
235 //=======================================================================
236
237 TopoDS_Shape TNaming_Identifier::ShapeArg() 
238 {
239   return myShapeArgs.First();
240 }
241
242
243 //=======================================================================
244 //function : ShapeContext
245 //purpose  : 
246 //=======================================================================
247
248 TopoDS_Shape TNaming_Identifier::ShapeContext() const
249 {
250   const TopoDS_Shape& S = myShapeArgs.First();
251   TopoDS_Shape  SC;
252   Handle(TNaming_NamedShape) NS = TNaming_Tool::NamedShape(S,myTDFAcces);
253   TNaming_Localizer::FindShapeContext (NS, S, SC); // szy ==> added par. S
254   return SC;
255 }
256
257
258 //=======================================================================
259 //function : Handle(TNaming_NamedShape)
260 //purpose  : 
261 //=======================================================================
262
263 Handle(TNaming_NamedShape) TNaming_Identifier::NamedShapeOfGeneration() const
264 {
265   return myNSContext;
266 }
267
268 //=======================================================================
269 //function : AncestorIdentification
270 //purpose  : 
271 //=======================================================================
272
273 void TNaming_Identifier::AncestorIdentification(TNaming_Localizer& Localizer,
274                                                 const TopoDS_Shape& Context)
275 {
276  TopTools_MapOfShape AncInFeature;
277  if (Context.IsNull()) {
278    myType = TNaming_UNKNOWN;
279    myDone = 0;
280    return;
281  }
282  Localizer.FindFeaturesInAncestors (myShape, Context, AncInFeature); 
283  
284  if (AncInFeature.IsEmpty()) {
285    myDone = 0;
286    return;
287  }
288  myType = TNaming_INTERSECTION;
289  TopTools_MapIteratorOfMapOfShape itS(AncInFeature);
290  for (; itS.More(); itS.Next()) myShapeArgs.Append(itS.Key());
291  myDone = 1;
292 }
293
294 //=======================================================================
295 //function : Imported
296 //purpose  : 
297 //=======================================================================
298
299 Standard_Boolean IsImported(const Handle(TNaming_NamedShape)& NS)
300 {
301   TDF_Label Lab = NS->Label();
302   TDF_Label Father = Lab.Father();
303   for (TDF_ChildIterator  cit(Father); cit.More(); cit.Next()) {
304     if (cit.Value() != Lab) return 0;
305   }
306
307   TNaming_Iterator it(NS);
308   if (!it.More()) return 0;
309   it.Next();  if (!it.More()) return 0;
310   //plus d un shape.
311 #ifdef OCCT_DEBUG
312   std::cout <<"WARNING IMPORTED"<<std::endl;
313 #endif
314   return 1;
315 }
316
317 //=======================================================================
318 //function : PrimitiveIdentification
319 //purpose  : 
320 //=======================================================================
321
322 void TNaming_Identifier::PrimitiveIdentification (TNaming_Localizer& /*L*/,
323                                                   const Handle(TNaming_NamedShape)& NS)
324 {
325   myPrimitiveArgs.Append(NS);
326   myIsFeature = Standard_True;
327   myType      = TNaming_MODIFUNTIL;
328   //Reconnaissance Shape Mort.
329   if (IsImported(NS)) {
330     myType = TNaming_CONSTSHAPE;
331     myIsFeature = Standard_False;  
332   }
333   myDone = 1;
334 }
335
336
337
338 //=======================================================================
339 //function : GeneratedIdentification
340 //purpose  : Finds generators of identifying shape <myShape> and keeps
341 //         : them in <myShapeArgs>
342 //=======================================================================
343
344 void TNaming_Identifier::GeneratedIdentification(TNaming_Localizer& /*Localizer*/,
345                                                  const Handle(TNaming_NamedShape)& NS)
346 {
347 //  TopoDS_Shape Gen; //szy 16.10.03
348   TopTools_ListOfShape aListOfGenerators;
349   TNaming_Localizer::FindGenerator (NS,myShape,aListOfGenerators);
350   myShapeArgs.Append(aListOfGenerators);
351   myNSContext = NS;
352   myType      = TNaming_GENERATION;
353   myDone = 1;  
354 }
355
356 //=======================================================================
357 //function : Identification
358 //purpose  : 
359 //=======================================================================
360
361 void TNaming_Identifier::Identification(TNaming_Localizer&          Localizer,
362                                         const Handle(TNaming_NamedShape)& NS)
363 {  
364
365   TNaming_MapOfNamedShape Primitives;
366   TopTools_MapOfShape     Shapes;
367
368   Localizer.Backward(NS,myShape,Primitives,Shapes);
369   
370   if (Primitives.IsEmpty() && Shapes.IsEmpty()) {
371     myDone = 0;
372     return;
373   }
374   myType = TNaming_INTERSECTION;
375   TNaming_MapIteratorOfMapOfNamedShape itP(Primitives);
376   for (; itP.More(); itP.Next()) myPrimitiveArgs.Append(itP.Key());
377   TopTools_MapIteratorOfMapOfShape itS(Shapes);
378   for (; itS.More(); itS.Next()) myShapeArgs.Append(itS.Key());
379   
380   if (myPrimitiveArgs.Extent() == 1 && myShapeArgs.IsEmpty()) {
381     myType      = TNaming_MODIFUNTIL;
382     myIsFeature = Standard_True;  
383     //Reconnaissance Shape Mort.
384     if (IsImported(myPrimitiveArgs.First())) {
385       myType = TNaming_CONSTSHAPE;
386       myIsFeature = Standard_False;  
387     }
388   }
389   myDone = 1;
390 }