1 // Created on: 1997-01-13
2 // Created by: VAUTHIER Jean-Claude
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <DNaming.hxx>
18 #include <TCollection_AsciiString.hxx>
19 #include <TDF_Data.hxx>
20 #include <TDF_ListIteratorOfLabelList.hxx>
21 #include <TDF_LabelList.hxx>
22 #include <TDF_LabelMap.hxx>
23 #include <TDF_Tool.hxx>
25 #include <DDF_Data.hxx>
26 #include <TNaming.hxx>
27 #include <TNaming_NamedShape.hxx>
28 #include <TNaming_NewShapeIterator.hxx>
29 #include <TNaming_OldShapeIterator.hxx>
30 #include <TNaming_Iterator.hxx>
31 #include <TNaming_Tool.hxx>
32 #include <TNaming_MapOfNamedShape.hxx>
33 #include <TNaming_MapIteratorOfMapOfNamedShape.hxx>
34 #include <Draw_Drawable3D.hxx>
36 #include <Draw_Interpretor.hxx>
37 #include <TopTools_ListOfShape.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39 #include <TopTools_DataMapOfShapeShape.hxx>
40 #include <TopoDS_Shape.hxx>
43 #include <TNaming_Builder.hxx>
44 #include <TDataStd_Name.hxx>
45 #include <TDF_TagSource.hxx>
46 #include <TopExp_Explorer.hxx>
53 #include <gp_Trsf.hxx>
55 #include <TColStd_HArray1OfInteger.hxx>
60 // POP : first Wrong Declaration : now it is correct
62 //extern void DNaming_BuildMap(TDF_LabelMap& Updated, const TDF_Label& Lab);
65 //=======================================================================
66 //function : Ascendants
68 //=======================================================================
69 static Standard_Integer Ascendants (Draw_Interpretor& di, Standard_Integer n, const char** a)
76 // Handle(TNaming_UsedShapes) US;
78 if (!DDF::GetDF(a[1],ND)) return 1;
79 // ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
81 TopoDS_Shape S = DBRep::Get (a[2]);
82 if (S.IsNull ()) return 1;
86 if (n > 3) T = Draw::Atoi(a[3]);
87 else T = ND->Transaction ();
89 //TNaming_OldShapeIterator it (S, T, US);
90 TNaming_OldShapeIterator it (S, T, ND->Root());
91 Standard_Integer i = 0;
92 TCollection_AsciiString entry;
93 for (;it.More (); it.Next ()) {
95 Sprintf (name,"%s_%s_%d",a[2],"old", i++);
96 DBRep::Set (name,it.Shape());
97 TDF_Label Label = it.Label ();
98 TDF_Tool::Entry(Label,entry);
99 di<<entry.ToCString()<<"\n";
105 //=======================================================================
106 //function : Descendants
108 //=======================================================================
109 static Standard_Integer Descendants (Draw_Interpretor& di, Standard_Integer n, const char** a)
116 // Handle(TNaming_UsedShapes) US;
117 if (!DDF::GetDF(a[1],ND)) return 1;
118 // ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
121 TopoDS_Shape S = DBRep::Get (a[2]);
122 if (S.IsNull ()) return 1;
126 if (n > 3) T = Draw::Atoi(a[3]);
127 else T = ND->Transaction ();
129 TNaming_NewShapeIterator it (S, T, ND->Root());
130 Standard_Integer i = 0;
131 TCollection_AsciiString entry;
132 for (;it.More (); it.Next ()) {
134 Sprintf (name,"%s_%s_%d",a[2],"new", i++);
135 DBRep::Set (name,it.Shape ());
136 TDF_Label Label = it.Label ();
137 TDF_Tool::Entry(Label,entry);
138 di<<entry.ToCString()<<"\n";
144 //=======================================================================
145 //function : GetEntry
147 //=======================================================================
148 static Standard_Integer Getentry (Draw_Interpretor& di, Standard_Integer n, const char** a)
152 // Handle(TNaming_UsedShapes) US;
154 if (!DDF::GetDF(a[1],ND)) return 1;
155 // ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
157 TopoDS_Shape S = DBRep::Get(a[2]);
159 di <<"Noone shape selected"<<"\n";
163 Standard_Integer Status ;
164 TCollection_AsciiString Name = DNaming::GetEntry(S,ND,Status);
169 di <<Name.ToCString();
171 di <<"Several shapes have the same name"<<"\n";
177 //=======================================================================
178 //function : NamedShape
179 //purpose : retrive label of Primitive or a Generated shape
180 //=======================================================================
181 static Standard_Integer NamedShape(Draw_Interpretor& di, Standard_Integer n, const char** a)
185 // Handle(TNaming_UsedShapes) US;
187 if (!DDF::GetDF(a[1],ND)) return 1;
188 TopoDS_Shape SS = DBRep::Get(a[2]);
190 di <<"Noone shape selected"<<"\n";
195 Handle(TNaming_NamedShape) NS = TNaming_Tool::NamedShape (SS,ND->Root());
201 TCollection_AsciiString Name; TDF_Tool::Entry(NS->Label(),Name);
202 di <<Name.ToCString();
206 //=======================================================================
207 //function : Currentshape
209 //=======================================================================
210 static Standard_Integer Currentshape (Draw_Interpretor& , Standard_Integer n, const char** a)
215 if (!DDF::GetDF(a[1],ND)) return 1;
217 Standard_CString LabelName = a[2];
218 TopoDS_Shape S = DNaming::CurrentShape(LabelName,ND);
220 if (n == 4) DBRep::Set(a[3],S);
221 else DBRep::Set(a[2],S);
227 //=======================================================================
228 //function : Initialshape
230 //=======================================================================
231 static Standard_Integer Initialshape (Draw_Interpretor& di, Standard_Integer n, const char** a)
237 if (!DDF::GetDF(a[1],ND)) return 1;
239 TopoDS_Shape NS = DBRep::Get(a[2]);
240 if (NS.IsNull()) return 1;
242 TDF_LabelList Labels;
243 TopoDS_Shape S = TNaming_Tool::InitialShape (NS,ND->Root(),Labels);
245 DBRep::Set (a[3], S);
247 TDF_ListIteratorOfLabelList itL(Labels);
249 TCollection_AsciiString entry;
251 TDF_Tool::Entry(itL.Value(),entry);
252 di << entry.ToCString();
255 for (; itL.More(); itL.Next()) {
256 TDF_Tool::Entry(itL.Value(),entry);
257 di <<" , "<< entry.ToCString();
263 //=======================================================================
264 //function : GetShape
266 //=======================================================================
267 static Standard_Integer Exploreshape (Draw_Interpretor& di, Standard_Integer n, const char** a)
273 // Handle(TNaming_UsedShapes) US;
275 if (!DDF::GetDF(a[1],ND)) return 1;
276 // ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
278 Standard_Integer Trans = ND->Transaction();
279 if (n == 5) { Trans = (Standard_Integer ) Draw::Atof(a[4]);}
282 DDF::FindLabel(ND,a[2],Lab);
283 Handle(TNaming_NamedShape) NS;
284 if (!Lab.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
285 di <<"No shape"<<"\n";
289 //TNaming::Print(NS->Evolution(),cout);
290 Standard_SStream aStream;
291 TNaming::Print(NS->Evolution(),aStream);
292 di << aStream << "\n";
294 Standard_Integer NbShapes = 1;
296 for (TNaming_Iterator itL(Lab,Trans) ; itL.More(); itL.Next()) {
297 if (!itL.OldShape().IsNull()) {
298 Sprintf(name,"%s%s_%d","old",a[3],NbShapes);
299 DBRep::Set (name,itL.OldShape());
301 if (!itL.NewShape().IsNull()) {
302 Sprintf(name,"%s_%d",a[3],NbShapes);
303 DBRep::Set (name,itL.NewShape());
309 di <<"No shape"<<"\n";
315 //=======================================================================
316 //function : GeneratedShape
317 //purpose : Generatedshape df shape Generationentry [drawname]
318 //=======================================================================
320 static Standard_Integer Generatedshape (Draw_Interpretor& di,
325 Handle(TNaming_NamedShape) A;
328 if (!DDF::GetDF(arg[1],DF)) return 1;
329 TopoDS_Shape Gen = DBRep::Get(arg[2]);
330 Handle(TNaming_NamedShape) Generation;
331 if (!DDF::Find(DF,arg[3],TNaming_NamedShape::GetID(),Generation)) return 1;
332 S = TNaming_Tool::GeneratedShape(Gen,Generation);
334 if (nb == 4) DBRep::Set(arg[4],S);
335 else DBRep::Set(arg[3],S);
339 di << "GetShape : Error" << "\n";
343 //=======================================================================
344 //function : DDataStd_GetShape
346 //=======================================================================
348 static Standard_Integer Getshape (Draw_Interpretor& di,
353 Handle(TNaming_NamedShape) A;
356 if (!DDF::GetDF(arg[1],DF)) return 1;
357 if (!DDF::Find(DF,arg[2],TNaming_NamedShape::GetID(),A)) return 1;
358 s = TNaming_Tool::GetShape(A);
360 if (nb == 4) DBRep::Set(arg[3],s);
361 else DBRep::Set(arg[2],s);
365 di << "DDataStd_GetShape : Error" << "\n";
368 //=======================================================================
371 //=======================================================================
373 static Standard_Integer Collect (Draw_Interpretor& di,
377 TNaming_MapOfNamedShape MNS;
378 Handle(TNaming_NamedShape) A;
379 Standard_Boolean OnlyModif = 1;
383 if (!DDF::GetDF(arg[1],DF)) return 1;
384 if (!DDF::Find(DF,arg[2],TNaming_NamedShape::GetID(),A)) return 1;
386 OnlyModif = Draw::Atoi(arg[3]);
388 TNaming_Tool::Collect(A,MNS,OnlyModif);
389 for (TNaming_MapIteratorOfMapOfNamedShape it(MNS); it.More(); it.Next()) {
390 TCollection_AsciiString Name;
391 TDF_Tool::Entry(it.Key()->Label(),Name);
392 di <<Name.ToCString()<<" ";
397 //=======================================================================
398 //function : GetCreationEntry
399 //purpose : retrive label of Primitive or a Generated shape
400 //=======================================================================
401 static Standard_Integer Getcreationentry (Draw_Interpretor& di, Standard_Integer n, const char** a)
405 // Handle(TNaming_UsedShapes) US;
407 if (!DDF::GetDF(a[1],ND)) return 1;
408 // ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
410 TopoDS_Shape SS = DBRep::Get(a[2]);
413 di <<"Noone shape selected"<<"\n";
418 TDF_LabelList Labels;
419 TopoDS_Shape S = TNaming_Tool::InitialShape(SS, ND->Root(), Labels);
425 Standard_Integer Status ;
427 TCollection_AsciiString Name = DNaming::GetEntry(S,ND,Status);
432 di <<Name.ToCString();
434 di <<"Several shapes have the same name"<<"\n";
440 //=======================================================================
441 //function : ImportShape
442 //purpose : "ImportShape Doc entry Shape Name"
443 //=======================================================================
445 static Standard_Integer DNaming_ImportShape (Draw_Interpretor& di,
450 Handle(TDF_Data) aDF;
451 if(!DDF::GetDF(a[1], aDF)) return 1;
453 DDF::AddLabel(aDF, a[2], L);
454 const TopoDS_Shape& aShape = DBRep::Get(a[3]);
455 if(aShape.IsNull()) return 1;
457 TDataStd_Name::Set(L, a[4]);
460 DNaming::LoadImportedShape(L, aShape);
462 DDF::ReturnLabel(di, L);
465 cout << "DNaming_NewShape : Error" << endl;
469 //=======================================================================
470 //function : BasicCommands
472 //=======================================================================
474 void DNaming::BasicCommands(Draw_Interpretor& theCommands)
476 static Standard_Boolean done = Standard_False;
478 done = Standard_True;
480 const char* g = "Naming data commands";
483 theCommands.Add("Ascendants", "Ascendants df shape [trans]", __FILE__,Ascendants, g);
484 theCommands.Add("Descendants", "Descendants df shape [trans]", __FILE__,Descendants, g);
485 theCommands.Add("ExploreShape","ExploreShape df entry res [trans]",__FILE__,Exploreshape, g);
486 theCommands.Add("GetEntry", "GetEntry df shape", __FILE__,Getentry, g);
487 theCommands.Add("GetCreationEntry", "GetCreationEntry df shape",__FILE__,Getcreationentry,g);
488 theCommands.Add("NamedShape", "NamedShape df shape", __FILE__,NamedShape, g);
489 theCommands.Add("InitialShape","InitialShape df shape res", __FILE__,Initialshape, g);
490 theCommands.Add("CurrentShape","Currentshape df entry [drawname]", __FILE__,Currentshape, g);
491 theCommands.Add("GetShape", "GetShape df entry [drawname]", __FILE__,Getshape , g);
492 theCommands.Add("Collect", "Collect df entry [onlymodif 0/1]",__FILE__,Collect , g);
493 theCommands.Add ("GeneratedShape", "Generatedshape df shape Generationentry [drawname]", __FILE__,Generatedshape,g);
494 theCommands.Add("ImportShape", "ImportShape Doc Entry Shape [Name]",__FILE__,DNaming_ImportShape, g);