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