0024157: Parallelization of assembly part of BO
[occt.git] / src / DNaming / DNaming_BasicCommands.cxx
CommitLineData
b311480e 1// Created on: 1997-01-13
2// Created by: VAUTHIER Jean-Claude
3// Copyright (c) 1997-1999 Matra Datavision
4// Copyright (c) 1999-2012 OPEN CASCADE SAS
5//
6// The content of this file is subject to the Open CASCADE Technology Public
7// License Version 6.5 (the "License"). You may not use the content of this file
8// except in compliance with the License. Please obtain a copy of the License
9// at http://www.opencascade.org and read it completely before using this file.
10//
11// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13//
14// The Original Code and all software distributed under the License is
15// distributed on an "AS IS" basis, without warranty of any kind, and the
16// Initial Developer hereby disclaims all such warranties, including without
17// limitation, any warranties of merchantability, fitness for a particular
18// purpose or non-infringement. Please see the License for the specific terms
19// and conditions governing the rights and limitations under the License.
20
7fd59977 21
22#include <DNaming.hxx>
23#include <TCollection_AsciiString.hxx>
24#include <TDF_Data.hxx>
25#include <TDF_ListIteratorOfLabelList.hxx>
26#include <TDF_LabelList.hxx>
27#include <TDF_LabelMap.hxx>
28#include <TDF_Tool.hxx>
29#include <DDF.hxx>
30#include <DDF_Data.hxx>
31#include <TNaming.hxx>
32#include <TNaming_NamedShape.hxx>
33#include <TNaming_NewShapeIterator.hxx>
34#include <TNaming_OldShapeIterator.hxx>
35#include <TNaming_Iterator.hxx>
36#include <TNaming_Tool.hxx>
37#include <TNaming_MapOfNamedShape.hxx>
38#include <TNaming_MapIteratorOfMapOfNamedShape.hxx>
39#include <Draw_Drawable3D.hxx>
40#include <Draw.hxx>
41#include <Draw_Interpretor.hxx>
42#include <TopTools_ListOfShape.hxx>
43#include <TopTools_ListIteratorOfListOfShape.hxx>
44#include <TopTools_DataMapOfShapeShape.hxx>
45#include <TopoDS_Shape.hxx>
46#include <DBRep.hxx>
47
48#include <TNaming_Builder.hxx>
49#include <TDataStd_Name.hxx>
50#include <TDF_TagSource.hxx>
51#include <TopExp_Explorer.hxx>
52
53#include <gp.hxx>
54#include <gp_Vec.hxx>
55#include <gp_Ax1.hxx>
56#include <gp_Ax2.hxx>
57#include <gp_Ax3.hxx>
58#include <gp_Trsf.hxx>
59
60#include <TColStd_HArray1OfInteger.hxx>
61//#ifdef WNT
62#include <stdio.h>
63//#endif
64
65// POP : first Wrong Declaration : now it is correct
66// second not used
67//extern void DNaming_BuildMap(TDF_LabelMap& Updated, const TDF_Label& Lab);
68
69
70//=======================================================================
71//function : Ascendants
72//purpose :
73//=======================================================================
74static Standard_Integer Ascendants (Draw_Interpretor& di, Standard_Integer n, const char** a)
75{
76 if (n < 3) return 1;
77
78 char name[100];
79
80 Handle(TDF_Data) ND;
81// Handle(TNaming_UsedShapes) US;
82
83 if (!DDF::GetDF(a[1],ND)) return 1;
84// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
85
86 TopoDS_Shape S = DBRep::Get (a[2]);
87 if (S.IsNull ()) return 1;
88
89 Standard_Integer T;
90
91322f44 91 if (n > 3) T = Draw::Atoi(a[3]);
7fd59977 92 else T = ND->Transaction ();
93
94 //TNaming_OldShapeIterator it (S, T, US);
95 TNaming_OldShapeIterator it (S, T, ND->Root());
96 Standard_Integer i = 0;
97 TCollection_AsciiString entry;
98 for (;it.More (); it.Next ()) {
99 S = it.Shape ();
91322f44 100 Sprintf (name,"%s_%s_%d",a[2],"old", i++);
7fd59977 101 DBRep::Set (name,it.Shape());
102 TDF_Label Label = it.Label ();
103 TDF_Tool::Entry(Label,entry);
104 di<<entry.ToCString()<<"\n";
105 }
106 return 0;
107}
108
109
110//=======================================================================
111//function : Descendants
112//purpose :
113//=======================================================================
114static Standard_Integer Descendants (Draw_Interpretor& di, Standard_Integer n, const char** a)
115
116{
117 if (n < 3) return 1;
118
119 char name[100];
120 Handle(TDF_Data) ND;
121// Handle(TNaming_UsedShapes) US;
122 if (!DDF::GetDF(a[1],ND)) return 1;
123// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
124
125
126 TopoDS_Shape S = DBRep::Get (a[2]);
127 if (S.IsNull ()) return 1;
128
129 Standard_Integer T;
130
91322f44 131 if (n > 3) T = Draw::Atoi(a[3]);
7fd59977 132 else T = ND->Transaction ();
133
134 TNaming_NewShapeIterator it (S, T, ND->Root());
135 Standard_Integer i = 0;
136 TCollection_AsciiString entry;
137 for (;it.More (); it.Next ()) {
138 S = it.Shape ();
91322f44 139 Sprintf (name,"%s_%s_%d",a[2],"new", i++);
7fd59977 140 DBRep::Set (name,it.Shape ());
141 TDF_Label Label = it.Label ();
142 TDF_Tool::Entry(Label,entry);
143 di<<entry.ToCString()<<"\n";
144 }
145
146 return 0;
147}
148
149//=======================================================================
150//function : GetEntry
151//purpose :
152//=======================================================================
153static Standard_Integer Getentry (Draw_Interpretor& di, Standard_Integer n, const char** a)
154{
155 if (n < 3) return 1;
156 Handle(TDF_Data) ND;
157// Handle(TNaming_UsedShapes) US;
158
159 if (!DDF::GetDF(a[1],ND)) return 1;
160// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
161
162 TopoDS_Shape S = DBRep::Get(a[2]);
163 if (S.IsNull()) {
164 di <<"Noone shape selected"<<"\n";
165 //di << 0;
166 return 0;
167 }
168 Standard_Integer Status ;
169 TCollection_AsciiString Name = DNaming::GetEntry(S,ND,Status);
170 if (Status == 0) {
171 di <<"E_NoName";
172 }
173 else {
174 di <<Name.ToCString();
175 if (Status == 2) {
176 di <<"Several shapes have the same name"<<"\n";
177 }
178 }
179 return 0;
180}
181
182//=======================================================================
183//function : NamedShape
184//purpose : retrive label of Primitive or a Generated shape
185//=======================================================================
186static Standard_Integer NamedShape(Draw_Interpretor& di, Standard_Integer n, const char** a)
187{
188 if (n < 3) return 1;
189 Handle(TDF_Data) ND;
190// Handle(TNaming_UsedShapes) US;
191
192 if (!DDF::GetDF(a[1],ND)) return 1;
193 TopoDS_Shape SS = DBRep::Get(a[2]);
194 if (SS.IsNull()) {
195 di <<"Noone shape selected"<<"\n";
196 //di << 0;
197 return 0;
198 }
199
200 Handle(TNaming_NamedShape) NS = TNaming_Tool::NamedShape (SS,ND->Root());
201
202 if (NS.IsNull()) {
203 di <<"E_NoName";
204 return 0;
205 }
206 TCollection_AsciiString Name; TDF_Tool::Entry(NS->Label(),Name);
207 di <<Name.ToCString();
208 return 0;
209}
210
211//=======================================================================
212//function : Currentshape
213//purpose :
214//=======================================================================
215static Standard_Integer Currentshape (Draw_Interpretor& , Standard_Integer n, const char** a)
216{
217 if (n < 4) return 1;
218
219 Handle(TDF_Data) ND;
220 if (!DDF::GetDF(a[1],ND)) return 1;
221
222 Standard_CString LabelName = a[2];
223 TopoDS_Shape S = DNaming::CurrentShape(LabelName,ND);
224 if (!S.IsNull()) {
225 if (n == 4) DBRep::Set(a[3],S);
226 else DBRep::Set(a[2],S);
227 return 0;
228 }
229 return 0;
230}
231
232//=======================================================================
233//function : Initialshape
234//purpose :
235//=======================================================================
236static Standard_Integer Initialshape (Draw_Interpretor& di, Standard_Integer n, const char** a)
237{
238 if (n < 4) return 1;
239
240 Handle(TDF_Data) ND;
241
242 if (!DDF::GetDF(a[1],ND)) return 1;
243
244 TopoDS_Shape NS = DBRep::Get(a[2]);
245 if (NS.IsNull()) return 1;
246
247 TDF_LabelList Labels;
248 TopoDS_Shape S = TNaming_Tool::InitialShape (NS,ND->Root(),Labels);
249 if (!S.IsNull()) {
250 DBRep::Set (a[3], S);
251 }
252 TDF_ListIteratorOfLabelList itL(Labels);
253
254 TCollection_AsciiString entry;
255 if (itL.More()) {
256 TDF_Tool::Entry(itL.Value(),entry);
257 di << entry.ToCString();
258 itL.Next();
259 }
260 for (; itL.More(); itL.Next()) {
261 TDF_Tool::Entry(itL.Value(),entry);
262 di <<" , "<< entry.ToCString();
263 }
264 di <<"."<<"\n";
265 return 0;
266}
267
268//=======================================================================
269//function : GetShape
270//purpose :
271//=======================================================================
272static Standard_Integer Exploreshape (Draw_Interpretor& di, Standard_Integer n, const char** a)
273{
274 char name[100];
275
276 if (n < 4) return 1;
277 Handle(TDF_Data) ND;
278// Handle(TNaming_UsedShapes) US;
279
280 if (!DDF::GetDF(a[1],ND)) return 1;
281// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
282
283 Standard_Integer Trans = ND->Transaction();
91322f44 284 if (n == 5) { Trans = (Standard_Integer ) Draw::Atof(a[4]);}
7fd59977 285
286 TDF_Label Lab;
287 DDF::FindLabel(ND,a[2],Lab);
288 Handle(TNaming_NamedShape) NS;
289 if (!Lab.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
290 di <<"No shape"<<"\n";
291 return 0;
292 }
293
294 //TNaming::Print(NS->Evolution(),cout);
295 Standard_SStream aStream;
296 TNaming::Print(NS->Evolution(),aStream);
297 di << aStream << "\n";
298
299 Standard_Integer NbShapes = 1;
300
301 for (TNaming_Iterator itL(Lab,Trans) ; itL.More(); itL.Next()) {
302 if (!itL.OldShape().IsNull()) {
91322f44 303 Sprintf(name,"%s%s_%d","old",a[3],NbShapes);
7fd59977 304 DBRep::Set (name,itL.OldShape());
305 }
306 if (!itL.NewShape().IsNull()) {
91322f44 307 Sprintf(name,"%s_%d",a[3],NbShapes);
7fd59977 308 DBRep::Set (name,itL.NewShape());
309 }
310 NbShapes++;
311 }
312 di <<"\n";
313 if (NbShapes == 0) {
314 di <<"No shape"<<"\n";
315 }
316
317 return 0;
318}
319
320//=======================================================================
321//function : GeneratedShape
322//purpose : Generatedshape df shape Generationentry [drawname]
323//=======================================================================
324
325static Standard_Integer Generatedshape (Draw_Interpretor& di,
326 Standard_Integer nb,
327 const char** arg)
328{
329 TopoDS_Shape S;
330 Handle(TNaming_NamedShape) A;
331 if (nb >= 4) {
332 Handle(TDF_Data) DF;
333 if (!DDF::GetDF(arg[1],DF)) return 1;
334 TopoDS_Shape Gen = DBRep::Get(arg[2]);
335 Handle(TNaming_NamedShape) Generation;
336 if (!DDF::Find(DF,arg[3],TNaming_NamedShape::GetID(),Generation)) return 1;
337 S = TNaming_Tool::GeneratedShape(Gen,Generation);
338 if (!S.IsNull()) {
339 if (nb == 4) DBRep::Set(arg[4],S);
340 else DBRep::Set(arg[3],S);
341 return 0;
342 }
343 }
344 di << "GetShape : Error" << "\n";
345 return 1;
346}
347
348//=======================================================================
349//function : DDataStd_GetShape
350//purpose :
351//=======================================================================
352
353static Standard_Integer Getshape (Draw_Interpretor& di,
354 Standard_Integer nb,
355 const char** arg)
356{
357 TopoDS_Shape s;
358 Handle(TNaming_NamedShape) A;
359 if (nb >= 3) {
360 Handle(TDF_Data) DF;
361 if (!DDF::GetDF(arg[1],DF)) return 1;
362 if (!DDF::Find(DF,arg[2],TNaming_NamedShape::GetID(),A)) return 1;
363 s = TNaming_Tool::GetShape(A);
364 if (!s.IsNull()) {
365 if (nb == 4) DBRep::Set(arg[3],s);
366 else DBRep::Set(arg[2],s);
367 return 0;
368 }
369 }
370 di << "DDataStd_GetShape : Error" << "\n";
371 return 1;
372}
373//=======================================================================
374//function : Collect
375//purpose :
376//=======================================================================
377
378static Standard_Integer Collect (Draw_Interpretor& di,
379 Standard_Integer nb,
380 const char** arg)
381{
382 TNaming_MapOfNamedShape MNS;
383 Handle(TNaming_NamedShape) A;
384 Standard_Boolean OnlyModif = 1;
385
386 if (nb >= 3) {
387 Handle(TDF_Data) DF;
388 if (!DDF::GetDF(arg[1],DF)) return 1;
389 if (!DDF::Find(DF,arg[2],TNaming_NamedShape::GetID(),A)) return 1;
390 if (nb >= 4) {
91322f44 391 OnlyModif = Draw::Atoi(arg[3]);
7fd59977 392 }
393 TNaming_Tool::Collect(A,MNS,OnlyModif);
394 for (TNaming_MapIteratorOfMapOfNamedShape it(MNS); it.More(); it.Next()) {
395 TCollection_AsciiString Name;
396 TDF_Tool::Entry(it.Key()->Label(),Name);
397 di <<Name.ToCString()<<" ";
398 }
399 }
400 return 1;
401}
402//=======================================================================
403//function : GetCreationEntry
404//purpose : retrive label of Primitive or a Generated shape
405//=======================================================================
406static Standard_Integer Getcreationentry (Draw_Interpretor& di, Standard_Integer n, const char** a)
407{
408 if (n < 3) return 1;
409 Handle(TDF_Data) ND;
410// Handle(TNaming_UsedShapes) US;
411
412 if (!DDF::GetDF(a[1],ND)) return 1;
413// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
414
415 TopoDS_Shape SS = DBRep::Get(a[2]);
416
417 if (SS.IsNull()) {
418 di <<"Noone shape selected"<<"\n";
419 //di << 0;
420 return 0;
421 }
422
423 TDF_LabelList Labels;
424 TopoDS_Shape S = TNaming_Tool::InitialShape(SS, ND->Root(), Labels);
425
426 if (S.IsNull()) {
427 di <<"E_NoName";
428 return 0;
429 }
430 Standard_Integer Status ;
431
432 TCollection_AsciiString Name = DNaming::GetEntry(S,ND,Status);
433 if (Status == 0) {
434 di <<"E_NoName";
435 }
436 else {
437 di <<Name.ToCString();
438 if (Status == 2) {
439 di <<"Several shapes have the same name"<<"\n";
440 }
441 }
442 return 0;
443}
444
445//=======================================================================
446//function : ImportShape
447//purpose : "ImportShape Doc entry Shape Name"
448//=======================================================================
449
450static Standard_Integer DNaming_ImportShape (Draw_Interpretor& di,
451 Standard_Integer nb,
452 const char** a)
453{
454 if (nb >= 4) {
455 Handle(TDF_Data) aDF;
456 if(!DDF::GetDF(a[1], aDF)) return 1;
457 TDF_Label L;
458 DDF::AddLabel(aDF, a[2], L);
459 const TopoDS_Shape& aShape = DBRep::Get(a[3]);
460 if(aShape.IsNull()) return 1;
461 if(nb == 5) {
462 TDataStd_Name::Set(L, a[4]);
463 }
464
465 DNaming::LoadImportedShape(L, aShape);
466
467 DDF::ReturnLabel(di, L);
468 return 0;
469 }
470 cout << "DNaming_NewShape : Error" << endl;
471 return 1;
472}
473
474//=======================================================================
475//function : BasicCommands
476//purpose :
477//=======================================================================
478
479void DNaming::BasicCommands(Draw_Interpretor& theCommands)
480{
481 static Standard_Boolean done = Standard_False;
482 if (done) return;
483 done = Standard_True;
484
485 const char* g = "Naming data commands";
486
487 // Exploration
488 theCommands.Add("Ascendants", "Ascendants df shape [trans]", __FILE__,Ascendants, g);
489 theCommands.Add("Descendants", "Descendants df shape [trans]", __FILE__,Descendants, g);
490 theCommands.Add("ExploreShape","ExploreShape df entry res [trans]",__FILE__,Exploreshape, g);
491 theCommands.Add("GetEntry", "GetEntry df shape", __FILE__,Getentry, g);
492 theCommands.Add("GetCreationEntry", "GetCreationEntry df shape",__FILE__,Getcreationentry,g);
493 theCommands.Add("NamedShape", "NamedShape df shape", __FILE__,NamedShape, g);
494 theCommands.Add("InitialShape","InitialShape df shape res", __FILE__,Initialshape, g);
495 theCommands.Add("CurrentShape","Currentshape df entry [drawname]", __FILE__,Currentshape, g);
496 theCommands.Add("GetShape", "GetShape df entry [drawname]", __FILE__,Getshape , g);
85b70a3b
V
497 theCommands.Add("Collect", "Collect df entry [onlymodif 0/1]",__FILE__,Collect , g);
498 theCommands.Add ("GeneratedShape", "Generatedshape df shape Generationentry [drawname]", __FILE__,Generatedshape,g);
499 theCommands.Add("ImportShape", "ImportShape Doc Entry Shape [Name]",__FILE__,DNaming_ImportShape, g);
500}