0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / QADNaming / QADNaming_SelectionCommands.cxx
1 // Created on: 1997-10-20
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 <QADNaming.hxx>
18
19 #include <DDF.hxx>
20
21 #include <TNaming_NamedShape.hxx>
22 #include <TNaming_ListOfNamedShape.hxx>
23 #include <TNaming_ListIteratorOfListOfNamedShape.hxx>
24 #include <TNaming_Tool.hxx>
25 #include <TDF_Label.hxx>
26 #include <TDF_AttributeMap.hxx>
27 #include <TDF_MapIteratorOfAttributeMap.hxx>
28 #include <TDF_ChildIterator.hxx>
29 #include <TDF_Tool.hxx>
30 #include <TNaming_Name.hxx>
31 #include <TNaming.hxx>
32 #include <TNaming_Naming.hxx>
33 #include <TNaming_MapOfNamedShape.hxx>
34 #include <TNaming_MapIteratorOfMapOfNamedShape.hxx>
35 #include <TDF_ChildIterator.hxx>
36 #include <TNaming_Selector.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopAbs.hxx>
39 #include <TCollection_AsciiString.hxx>
40 #include <TDF_LabelMap.hxx>
41
42 #include <DBRep.hxx>
43 #include <Draw_Appli.hxx>
44 #include <Draw.hxx>
45 #include <Draw_Interpretor.hxx>
46 #include <Draw_Drawable3D.hxx>
47 #include <DBRep.hxx>
48 #include <DBRep_DrawableShape.hxx>
49 #include <stdio.h>
50
51 //=======================================================================
52 //function : Display
53 //purpose  : 
54 //=======================================================================
55
56 static void Display (const Standard_CString Name, const TopoDS_Shape& S)
57 {  
58   //char* name = Name;
59   static Standard_Integer nbIsos  = 2;
60   static Standard_Real size    = 100.;
61   static Standard_Integer discret = 30;
62
63   Handle(DBRep_DrawableShape) D = new DBRep_DrawableShape(S,
64                                                           Draw_jaune,
65                                                           Draw_vert,
66                                                           Draw_bleu,
67                                                           Draw_rouge,
68                                                           size,
69                                                           nbIsos,
70                                                           discret);
71   Draw::Set(Name,D);
72 }
73
74 //=======================================================================
75 //function : DumpNaming
76 //purpose  : 
77 //=======================================================================
78
79 static void DumpNaming (const Handle(TNaming_Naming)& naming, Draw_Interpretor& di) 
80 {
81   TCollection_AsciiString Entry;
82   const TNaming_Name& AName = naming->GetName();
83
84   //TNaming::Print(AName.Type(),std::cout);
85   Standard_SStream aSStream1;
86   TNaming::Print(AName.Type(),aSStream1);
87   di << aSStream1;
88   di << " ";
89
90   //TopAbs::Print(AName.ShapeType(),std::cout);
91   Standard_SStream aSStream2;
92   TopAbs::Print(AName.ShapeType(),aSStream2);
93   di << aSStream2;
94
95   const TNaming_ListOfNamedShape& NSS = AName.Arguments();
96   for (TNaming_ListIteratorOfListOfNamedShape it(NSS); it.More(); it.Next()) {
97     TDF_Tool::Entry(it.Value()->Label(),Entry); 
98     di << " " << Entry.ToCString();
99   }
100   if(!AName.StopNamedShape().IsNull()) {
101     TDF_Tool::Entry(AName.StopNamedShape()->Label(),Entry); 
102     di << " Stop " << Entry.ToCString();
103   }
104 }
105
106 //=======================================================================
107 //function : SelectShape ou SelectGeometry
108 //purpose  : "Select DF entry shape [context]", 
109 //=======================================================================
110
111 static Standard_Integer QADNaming_Select (Draw_Interpretor& di, Standard_Integer n, const char** a)
112 {
113   if (n == 4 || n == 5) {
114     Standard_Boolean geometry = (strcmp(a[0],"SelectGeometry")==0?1:0); 
115     Handle(TDF_Data) DF;
116     if(!DDF::GetDF(a[1], DF))  return 1;
117     TDF_Label L;
118     DDF::AddLabel(DF,a[2],L);
119     TNaming_Selector SL (L);
120     if (n == 4) {
121       TopoDS_Shape S = DBRep::Get(a[3], TopAbs_SHAPE);  
122       SL.Select (S, geometry); 
123     }
124     if (n == 5) {  
125       TopoDS_Shape S = DBRep::Get(a[3], TopAbs_SHAPE);
126       TopoDS_Shape C = DBRep::Get(a[4], TopAbs_SHAPE);
127       SL.Select (S, C, geometry); 
128     }
129     return 0;  
130   }
131   di << "QADNaming_Select : Error\n";
132   return 1;
133 }
134
135 //=======================================================================
136 //function : SolveSelection
137 //purpose  : "SolveSelection DF entry", 
138 //=======================================================================
139
140 static Standard_Integer QADNaming_SolveSelection (Draw_Interpretor& di, Standard_Integer n, const char** a)
141 {
142   if (n >= 3) {
143     TDF_Label aLabel;
144     if (!QADNaming::Entry(a, aLabel)) return 1;
145     char name[100];
146     TNaming_Selector SL (aLabel);
147     TDF_LabelMap valid;
148     Standard_Integer i;
149     for(i=3;i<n;i++) {
150       TDF_Label aValidLab;
151       if (!DDF::FindLabel(aLabel.Data(),a[i],aValidLab)) di<<"Warning: label "<<a[i]<<" not exists\n";
152       else valid.Add(aValidLab);
153     }
154     Standard_Boolean done = SL.Solve(valid);
155     TopoDS_Shape Res = TNaming_Tool::CurrentShape(SL.NamedShape());
156     //TopoDS_Shape Res = TNaming_Tool::CurrentShape(NS);
157     Sprintf (name,"%s_%s","new",a[2]);
158     Display (name,Res);
159     return done?0:1;
160   }
161   di << "Usage:  DumpSelection DF entry [validlabel1 validlabel2 ...]\n";
162   return 1;
163 }
164
165 //=======================================================================
166 //function : DumpSelection
167 //purpose  : DumpSelection DF entry (R)
168 //=======================================================================
169
170 static Standard_Integer QADNaming_DumpSelection (Draw_Interpretor& di, 
171                                               Standard_Integer n, const char** a)
172 {
173   if (n == 3 || n == 4) { 
174     Handle(TDF_Data) DF;
175     if (!DDF::GetDF(a[1],DF)) return 1;  
176     TDF_Label L;
177     if (!DDF::FindLabel(DF,a[2],L)) return 1;
178     Handle(TNaming_Naming) naming;
179     if (!L.FindAttribute(TNaming_Naming::GetID(),naming)) {  
180       di <<"QADNaming_DumpSelection : not a selection\n";
181       return 1;
182     }
183     DumpNaming(naming, di);
184     di << "\n";
185     if (n == 4) { 
186       Standard_Integer depth = L.Depth();  
187       Standard_Integer curdepth = 0;
188       TCollection_AsciiString Entry;
189       TDF_ChildIterator it (naming->Label(),Standard_True);
190       for (;it.More();it.Next()) {
191         if (it.Value().FindAttribute(TNaming_Naming::GetID(),naming)) { 
192           curdepth = (naming->Label().Depth()- depth);
193           for (Standard_Integer i = 1; i <= curdepth; i++) di << " ";    
194           TDF_Tool::Entry (naming->Label(),Entry); 
195           di << Entry.ToCString() << " "; 
196           DumpNaming(naming, di);
197           di << "\n";
198         }
199       }
200     }
201     return 0;
202   }
203   di << "QADNaming_DumpSelection : Error\n";
204   return 1;  
205 }
206
207 //=======================================================================
208 //function : ArgsSelection
209 //purpose  : ArgsSelection DF entry
210 //=======================================================================
211
212 static Standard_Integer QADNaming_ArgsSelection (Draw_Interpretor& di, 
213                                               Standard_Integer n, const char** a)
214 {
215   if (n == 3) { 
216     Handle(TDF_Data) DF;
217     if (!DDF::GetDF(a[1],DF)) return 1;  
218     TDF_Label L;
219     if (!DDF::FindLabel(DF,a[2],L)) return 1;
220     Handle(TNaming_Naming) naming;
221     if (!L.FindAttribute(TNaming_Naming::GetID(),naming)) {  
222       di <<"QADNaming_DumpSelection : not a selection\n";
223       return 1;
224     }  
225     TCollection_AsciiString Entry;
226     TNaming_Selector SL (L);
227     di <<" Selection Arguments : ";
228     TDF_AttributeMap args;
229     SL.Arguments(args);
230     for (TDF_MapIteratorOfAttributeMap it(args); it.More(); it.Next()) {
231       TDF_Tool::Entry(it.Key()->Label(),Entry); 
232       di << Entry.ToCString() << " ";
233     }
234     di << "\n";
235     return 0;
236   }
237   di << "QADNaming_ArgsSelection : Error\n";
238   return 1;  
239 }
240
241 //=======================================================================
242 //function : CollectAttachment
243 //purpose  : 
244 //=======================================================================
245
246 static void CollectAttachment (const TDF_Label& root,
247                                const Handle(TNaming_Naming)& naming, 
248                                TNaming_MapOfNamedShape& attachment)
249 {
250   TNaming_ListIteratorOfListOfNamedShape itarg;
251   const TNaming_ListOfNamedShape& args = naming->GetName().Arguments();
252   for (itarg.Initialize(args);itarg.More();itarg.Next()) {
253     if (!itarg.Value()->Label().IsDescendant(root)) attachment.Add(itarg.Value());
254   }  
255   Handle(TNaming_Naming) subnaming; 
256   for (TDF_ChildIterator it(naming->Label(),Standard_True);it.More();it.Next()) {
257     if (it.Value().FindAttribute(TNaming_Naming::GetID(),subnaming)) {   
258       const TNaming_ListOfNamedShape& subargs = subnaming->GetName().Arguments();
259       for (itarg.Initialize(subargs);itarg.More();itarg.Next()) {
260         if (!itarg.Value()->Label().IsDescendant(root)) attachment.Add(itarg.Value());
261       }
262     }
263   }
264 }  
265
266
267 //=======================================================================
268 //function : Attachment
269 //purpose  : Attachment DF entry
270 //=======================================================================
271
272 static Standard_Integer QADNaming_Attachment (Draw_Interpretor& di, 
273                                             Standard_Integer n, 
274                                             const char** a)
275 {
276   if (n == 3) { 
277     Handle(TDF_Data) DF;
278     if (!DDF::GetDF(a[1],DF)) return 1;  
279     TDF_Label L;
280     if (!DDF::FindLabel(DF,a[2],L)) return 1;  
281     Handle(TNaming_Naming) naming; 
282     TNaming_MapOfNamedShape attachment;
283     if (L.FindAttribute(TNaming_Naming::GetID(),naming)) {
284       CollectAttachment (L,naming,attachment);
285     }
286     else {
287       for (TDF_ChildIterator it (L,Standard_True); it.More();it.Next()) {
288         if (it.Value().FindAttribute(TNaming_Naming::GetID(),naming)) {
289           CollectAttachment (L,naming,attachment);
290           it.NextBrother();
291         }
292       }
293     }
294     TCollection_AsciiString Entry;   
295     TDF_Tool::Entry(L,Entry); 
296     di << " Attachment of " << Entry.ToCString();
297     di << "\n";
298     for (TNaming_MapIteratorOfMapOfNamedShape ita (attachment); ita.More(); ita.Next()) {
299       TDF_Tool::Entry (ita.Key()->Label(),Entry); 
300       di << Entry.ToCString() << " ";
301     }
302     di << "\n";
303     return 0;
304   }
305   di << "QADNaming_Attachment : Error\n";
306   return 1;  
307 }
308
309 //=======================================================================
310 //function : SelectionCommands
311 //purpose  : 
312 //=======================================================================
313
314 void QADNaming::SelectionCommands(Draw_Interpretor& theCommands) 
315 {
316   
317   static Standard_Boolean done = Standard_False;
318   if (done) return;
319   done = Standard_True;
320   
321   const char* g = "Naming data commands";
322
323   theCommands.Add("SelectShape",
324                   "SelectShape DF entry shape [context]", 
325                   __FILE__, QADNaming_Select, g);
326   
327   theCommands.Add("SelectGeometry",
328                   "SelectGeometry DF entry shape [context]", 
329                   __FILE__, QADNaming_Select, g);
330   
331   theCommands.Add("DumpSelection",         
332                   "DumpSelected DF entry", 
333                   __FILE__, QADNaming_DumpSelection, g);    
334
335   theCommands.Add("ArgsSelection",         
336                   "ArgsSelection DF entry", 
337                   __FILE__, QADNaming_ArgsSelection, g);  
338
339   theCommands.Add("SolveSelection",         
340                   "DumpSelection DF entry [validlabel1 validlabel2 ...]", 
341                   __FILE__, QADNaming_SolveSelection, g);  
342
343   theCommands.Add("Attachment",         
344                   "Attachment DF entry", 
345                   __FILE__, QADNaming_Attachment, g);
346
347 }