0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / QADNaming / QADNaming_BasicCommands.cxx
CommitLineData
b311480e 1// Created on: 1997-01-13
2// Created by: VAUTHIER Jean-Claude
3// Copyright (c) 1997-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17#include <QADNaming.hxx>
18#include <TCollection_AsciiString.hxx>
19#include <TDF_Data.hxx>
7fd59977 20#include <TDF_Tool.hxx>
21
22#include <DDF.hxx>
23
24#include <TNaming.hxx>
7fd59977 25#include <TNaming_NewShapeIterator.hxx>
26#include <TNaming_OldShapeIterator.hxx>
27#include <TNaming_Iterator.hxx>
28#include <TNaming_Tool.hxx>
29#include <TNaming_MapOfNamedShape.hxx>
7fd59977 30#include <Draw.hxx>
31#include <Draw_Interpretor.hxx>
7fd59977 32#include <TopoDS_Shape.hxx>
33#include <DBRep.hxx>
34
35
57c28b61 36//#ifdef _MSC_VER
7fd59977 37#include <stdio.h>
38//#endif
39
40// POP : first Wrong Declaration : now it is correct
41// second not used
42//extern void QADNaming_BuildMap(TDF_LabelMap& Updated, const TDF_Label& Lab);
43
44
45//=======================================================================
46//function : Ascendants
47//purpose :
48//=======================================================================
49
50static Standard_Integer Ascendants (Draw_Interpretor& di, Standard_Integer n, const char ** a)
51{
52 if (n < 3) return 1;
53
54 char name[100];
55
56 Handle(TDF_Data) ND;
57// Handle(TNaming_UsedShapes) US;
58
59 if (!DDF::GetDF(a[1],ND)) return 1;
60// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
61
62 TopoDS_Shape S = DBRep::Get (a[2]);
63 if (S.IsNull ()) return 1;
64
65 Standard_Integer T;
66
91322f44 67 if (n > 3) T = Draw::Atoi(a[3]);
7fd59977 68 else T = ND->Transaction ();
69
70 //TNaming_OldShapeIterator it (S, T, US);
71 TNaming_OldShapeIterator it (S, T, ND->Root());
72 Standard_Integer i = 0;
73 TCollection_AsciiString entry;
74 for (;it.More (); it.Next ()) {
75 S = it.Shape ();
91322f44 76 Sprintf (name,"%s_%s_%d",a[2],"old", i++);
7fd59977 77 DBRep::Set (name,it.Shape());
78 TDF_Label Label = it.Label ();
79 TDF_Tool::Entry(Label,entry);
80 di<<entry.ToCString()<<"\n";
81 }
82 return 0;
83}
84
85
86//=======================================================================
87//function : Descendants
88//purpose :
89//=======================================================================
90
91static Standard_Integer Descendants (Draw_Interpretor& di, Standard_Integer n, const char ** a)
92
93{
94 if (n < 3) return 1;
95
96 char name[100];
97 Handle(TDF_Data) ND;
98// Handle(TNaming_UsedShapes) US;
99 if (!DDF::GetDF(a[1],ND)) return 1;
100// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
101
102
103 TopoDS_Shape S = DBRep::Get (a[2]);
104 if (S.IsNull ()) return 1;
105
106 Standard_Integer T;
107
91322f44 108 if (n > 3) T = Draw::Atoi(a[3]);
7fd59977 109 else T = ND->Transaction ();
110
111 TNaming_NewShapeIterator it (S, T, ND->Root());
112 Standard_Integer i = 0;
113 TCollection_AsciiString entry;
114 for (;it.More (); it.Next ()) {
115 S = it.Shape ();
91322f44 116 Sprintf (name,"%s_%s_%d",a[2],"new", i++);
7fd59977 117 DBRep::Set (name,it.Shape ());
118 TDF_Label Label = it.Label ();
119 TDF_Tool::Entry(Label,entry);
120 di<<entry.ToCString()<<"\n";
121 }
122
123 return 0;
124}
125
126//=======================================================================
127//function : GetEntry
128//purpose :
129//=======================================================================
130
131static Standard_Integer Getentry (Draw_Interpretor& di, Standard_Integer n, const char ** a)
132{
133 if (n < 3) return 1;
134 Handle(TDF_Data) ND;
135// Handle(TNaming_UsedShapes) US;
136
137 if (!DDF::GetDF(a[1],ND)) return 1;
138// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
139
140 TopoDS_Shape S = DBRep::Get(a[2]);
141 if (S.IsNull()) {
316ea293 142 di <<"No shape selected\n";
7fd59977 143 di << 0;
144 return 0;
145 }
9fd2d2c3 146 Standard_Integer aStatus = 0;
147 TCollection_AsciiString Name = QADNaming::GetEntry (S, ND, aStatus);
148 if (aStatus == 0) {
7fd59977 149 di <<"E_NoName";
150 }
151 else {
152 di <<Name.ToCString();
9fd2d2c3 153 if (aStatus == 2) {
586db386 154 di <<"Several shapes have the same name\n";
7fd59977 155 }
156 }
157 return 0;
158}
159
160//=======================================================================
161//function : NamedShape
316ea293 162//purpose : retrieve label of Primitive or a Generated shape
7fd59977 163//=======================================================================
164
165static Standard_Integer NamedShape(Draw_Interpretor& di, Standard_Integer n, const char ** a)
166{
167 if (n < 3) return 1;
168 Handle(TDF_Data) ND;
169// Handle(TNaming_UsedShapes) US;
170
171 if (!DDF::GetDF(a[1],ND)) return 1;
172 TopoDS_Shape SS = DBRep::Get(a[2]);
173 if (SS.IsNull()) {
316ea293 174 di <<"No shape selected\n";
7fd59977 175 di << 0;
176 return 0;
177 }
178
179 Handle(TNaming_NamedShape) NS = TNaming_Tool::NamedShape (SS,ND->Root());
180
181 if (NS.IsNull()) {
182 di <<"E_NoName";
183 return 0;
184 }
185 TCollection_AsciiString Name; TDF_Tool::Entry(NS->Label(),Name);
186 di <<Name.ToCString();
187 return 0;
188}
189
190//=======================================================================
191//function : Currentshape
192//purpose :
193//=======================================================================
194
195static Standard_Integer Currentshape (Draw_Interpretor& , Standard_Integer n, const char ** a)
196{
197 if (n < 4) return 1;
198
199 Handle(TDF_Data) ND;
200 if (!DDF::GetDF(a[1],ND)) return 1;
201
202 Standard_CString LabelName = a[2];
203 TopoDS_Shape S = QADNaming::CurrentShape(LabelName,ND);
204 if (!S.IsNull()) {
205 if (n == 4) DBRep::Set(a[3],S);
206 else DBRep::Set(a[2],S);
207 return 0;
208 }
209 return 0;
210}
211
212//=======================================================================
213//function : Initialshape
214//purpose :
215//=======================================================================
216
217static Standard_Integer Initialshape (Draw_Interpretor& di, Standard_Integer n, const char ** a)
218{
219 if (n < 4) return 1;
220
221 Handle(TDF_Data) ND;
222
223 if (!DDF::GetDF(a[1],ND)) return 1;
224
225 TopoDS_Shape NS = DBRep::Get(a[2]);
226 if (NS.IsNull()) return 1;
227
228 TDF_LabelList Labels;
229 TopoDS_Shape S = TNaming_Tool::InitialShape (NS,ND->Root(),Labels);
230 if (!S.IsNull()) {
231 DBRep::Set (a[3], S);
232 }
233 TDF_ListIteratorOfLabelList itL(Labels);
234
235 TCollection_AsciiString entry;
236 if (itL.More()) {
237 TDF_Tool::Entry(itL.Value(),entry);
238 di << entry.ToCString();
239 itL.Next();
240 }
241 for (; itL.More(); itL.Next()) {
242 TDF_Tool::Entry(itL.Value(),entry);
243 di <<" , "<< entry.ToCString();
244 }
586db386 245 di <<".\n";
7fd59977 246 return 0;
247}
248
249//=======================================================================
250//function : GetShape
251//purpose :
252//=======================================================================
253
254static Standard_Integer Exploreshape (Draw_Interpretor& di, Standard_Integer n, const char ** a)
255{
256 char name[100];
257
258 if (n < 4) return 1;
259 Handle(TDF_Data) ND;
260// Handle(TNaming_UsedShapes) US;
261
262 if (!DDF::GetDF(a[1],ND)) return 1;
263// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
264
265 Standard_Integer Trans = ND->Transaction();
91322f44 266 if (n == 5) { Trans = (Standard_Integer ) Draw::Atof(a[4]);}
7fd59977 267
268 TDF_Label Lab;
269 DDF::FindLabel(ND,a[2],Lab);
270 Handle(TNaming_NamedShape) NS;
271 if (!Lab.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
586db386 272 di <<"No shape\n";
7fd59977 273 return 0;
274 }
275
04232180 276 //TNaming::Print(NS->Evolution(),std::cout);
7fd59977 277 Standard_SStream aSStream;
278 TNaming::Print(NS->Evolution(),aSStream);
279 di << aSStream;
280
281 Standard_Integer NbShapes = 1;
282
283 for (TNaming_Iterator itL(Lab,Trans) ; itL.More(); itL.Next()) {
284 if (!itL.OldShape().IsNull()) {
91322f44 285 Sprintf(name,"%s%s_%d","old",a[3],NbShapes);
7fd59977 286 DBRep::Set (name,itL.OldShape());
287 }
288 if (!itL.NewShape().IsNull()) {
91322f44 289 Sprintf(name,"%s_%d",a[3],NbShapes);
7fd59977 290 DBRep::Set (name,itL.NewShape());
291 }
292 NbShapes++;
293 }
294 di <<"\n";
295 if (NbShapes == 0) {
586db386 296 di <<"No shape\n";
7fd59977 297 }
298
299 return 0;
300}
301
302//=======================================================================
303//function : GeneratedShape
304//purpose : Generatedshape df shape Generationentry [drawname]
305//=======================================================================
306
307static Standard_Integer Generatedshape (Draw_Interpretor& di,
308 Standard_Integer nb,
309 const char ** arg)
310{
311 TopoDS_Shape S;
312 Handle(TNaming_NamedShape) A;
313 if (nb >= 4) {
314 Handle(TDF_Data) DF;
315 if (!DDF::GetDF(arg[1],DF)) return 1;
316 TopoDS_Shape Gen = DBRep::Get(arg[2]);
317 Handle(TNaming_NamedShape) Generation;
318 if (!DDF::Find(DF,arg[3],TNaming_NamedShape::GetID(),Generation)) return 1;
319 S = TNaming_Tool::GeneratedShape(Gen,Generation);
320 if (!S.IsNull()) {
321 if (nb == 4) DBRep::Set(arg[4],S);
322 else DBRep::Set(arg[3],S);
323 return 0;
324 }
325 }
586db386 326 di << "GetShape : Error\n";
7fd59977 327 return 1;
328}
329
330//=======================================================================
331//function : DDataStd_GetShape
332//purpose :
333//=======================================================================
334
335static Standard_Integer Getshape (Draw_Interpretor& di,
336 Standard_Integer nb,
337 const char ** arg)
338{
339 TopoDS_Shape s;
340 Handle(TNaming_NamedShape) A;
341 if (nb >= 3) {
342 Handle(TDF_Data) DF;
343 if (!DDF::GetDF(arg[1],DF)) return 1;
344 if (!DDF::Find(DF,arg[2],TNaming_NamedShape::GetID(),A)) return 1;
345 s = TNaming_Tool::GetShape(A);
346 if (!s.IsNull()) {
347 if (nb == 4) DBRep::Set(arg[3],s);
348 else DBRep::Set(arg[2],s);
349 return 0;
350 }
351 }
586db386 352 di << "DDataStd_GetShape : Error\n";
7fd59977 353 return 1;
354}
355
356//=======================================================================
357//function : Collect
358//purpose :
359//=======================================================================
360
361static Standard_Integer Collect (Draw_Interpretor& di,
362 Standard_Integer nb,
363 const char ** arg)
364{
365 TNaming_MapOfNamedShape MNS;
366 Handle(TNaming_NamedShape) A;
367 Standard_Boolean OnlyModif = 1;
368
369 if (nb >= 3) {
370 Handle(TDF_Data) DF;
371 if (!DDF::GetDF(arg[1],DF)) return 1;
372 if (!DDF::Find(DF,arg[2],TNaming_NamedShape::GetID(),A)) return 1;
373 if (nb >= 4) {
dde68833 374 OnlyModif = Draw::Atoi(arg[3]) != 0;
7fd59977 375 }
376 TNaming_Tool::Collect(A,MNS,OnlyModif);
377 for (TNaming_MapIteratorOfMapOfNamedShape it(MNS); it.More(); it.Next()) {
378 TCollection_AsciiString Name;
379 TDF_Tool::Entry(it.Key()->Label(),Name);
380 di <<Name.ToCString()<<" ";
381 }
382 }
383 return 1;
384}
385
386//=======================================================================
387//function : GetCreationEntry
316ea293 388//purpose : retrieve label of Primitive or a Generated shape
7fd59977 389//=======================================================================
390
391static Standard_Integer Getcreationentry (Draw_Interpretor& di, Standard_Integer n, const char ** a)
392{
393 if (n < 3) return 1;
394 Handle(TDF_Data) ND;
395// Handle(TNaming_UsedShapes) US;
396
397 if (!DDF::GetDF(a[1],ND)) return 1;
398// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
399
400 TopoDS_Shape SS = DBRep::Get(a[2]);
401
402 if (SS.IsNull()) {
316ea293 403 di <<"No shape selected\n";
7fd59977 404 di << 0;
405 return 0;
406 }
407
408 TDF_LabelList Labels;
409 TopoDS_Shape S = TNaming_Tool::InitialShape(SS, ND->Root(), Labels);
410
411 if (S.IsNull()) {
412 di <<"E_NoName";
413 return 0;
414 }
9fd2d2c3 415 Standard_Integer aStatus = 0;
416 TCollection_AsciiString Name = QADNaming::GetEntry (S, ND, aStatus);
417 if (aStatus == 0) {
7fd59977 418 di <<"E_NoName";
419 }
420 else {
421 di <<Name.ToCString();
9fd2d2c3 422 if (aStatus == 2) {
586db386 423 di <<"Several shapes have the same name\n";
7fd59977 424 }
425 }
426 return 0;
427}
428
429
430//=======================================================================
431//function : BasicCommands
432//purpose :
433//=======================================================================
434
435void QADNaming::BasicCommands(Draw_Interpretor& theCommands)
436{
437 static Standard_Boolean done = Standard_False;
438 if (done) return;
439 done = Standard_True;
440
441 const char* g = "Naming data commands";
442
443 // Exploration
444 theCommands.Add("Ascendants", "Ascendants df shape [trans]", __FILE__,Ascendants, g);
445
446 theCommands.Add("Descendants", "Descendants df shape [trans]", __FILE__,Descendants, g);
447
448 theCommands.Add("ExploreShape","ExploreShape df entry res [trans]",__FILE__,Exploreshape, g);
449
450 theCommands.Add("GetEntry", "GetEntry df shape", __FILE__,Getentry, g);
451
452 theCommands.Add("GetCreationEntry", "GetCreationEntry df shape",__FILE__,Getcreationentry,g);
453
454 theCommands.Add("NamedShape", "NamedShape df shape", __FILE__,NamedShape, g);
455
456 theCommands.Add("InitialShape","InitialShape df shape res", __FILE__,Initialshape, g);
457
458 theCommands.Add("CurrentShape","Currentshape df entry [drawname]", __FILE__,Currentshape, g);
459
460 theCommands.Add("GetShape", "GetShape df entry [drawname]", __FILE__,Getshape , g);
461
462 theCommands.Add("Collect", "Collect df entry [onlymodif 0/1]",__FILE__,Collect , g);
463
464 theCommands.Add ("GeneratedShape",
465 "Generatedshape df shape Generationentry [drawname]",
466 __FILE__,Generatedshape,g);
467}