0028579: Get rid of the obsolete QANewModTopOpe_* and QANewBRepNaming_* algorithms
[occt.git] / src / QANewDBRepNaming / QANewDBRepNaming.cxx
1 // Created on: 1999-12-08
2 // Created by: Vladislav ROMASHKO
3 // Copyright (c) 1999-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
18 #include <BRep_Tool.hxx>
19 #include <BRepTools.hxx>
20 #include <DBRep.hxx>
21 #include <DDF.hxx>
22 #include <Draw.hxx>
23 #include <gp_Pnt.hxx>
24 #include <gp_Trsf.hxx>
25 #include <gp_Vec.hxx>
26 #include <gp_XYZ.hxx>
27 #include <Precision.hxx>
28 #include <QANewDBRepNaming.hxx>
29 #include <Standard_ErrorHandler.hxx>
30 #include <TDF_ChildIterator.hxx>
31 #include <TDF_Data.hxx>
32 #include <TDF_Label.hxx>
33 #include <TDF_LabelMap.hxx>
34 #include <TNaming.hxx>
35 #include <TNaming_Builder.hxx>
36 #include <TNaming_Name.hxx>
37 #include <TNaming_NamedShape.hxx>
38 #include <TNaming_Naming.hxx>
39 #include <TNaming_NamingTool.hxx>
40 #include <TNaming_Selector.hxx>
41 #include <TNaming_Tool.hxx>
42 #include <TopExp.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopoDS.hxx>
45 #include <TopoDS_Iterator.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS_Vertex.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_MapOfShape.hxx>
50 #include <ViewerTest.hxx>
51
52 #include <stdio.h>
53 //////#include <Draw_PluginMacro.hxx>
54 //////#include <DBrowser.hxx>
55 static Standard_Integer QANewDBRepNaming_CheckNaming(Draw_Interpretor&,Standard_Integer,const char ** a);
56
57 //static Standard_Integer QANewDBRepNaming_myCheckNaming(Draw_Interpretor&,Standard_Integer,const char ** a);
58
59 static Standard_Integer QANewDBRepNaming_CheckSelectShape(Draw_Interpretor&,Standard_Integer,const char ** a);
60
61 static Standard_Integer QANewDBRepNaming_CheckSolve(Draw_Interpretor&,Standard_Integer,const char ** a);
62
63 #include <QADNaming.hxx>
64
65 //=======================================================================
66 //function : AllCommands
67 //purpose  : 
68 //=======================================================================
69
70 void  QANewDBRepNaming::AllCommands(Draw_Interpretor& theCommands)
71 {
72   static Standard_Boolean done = Standard_False;
73   if (done) return;
74   done = Standard_True;
75
76   QANewDBRepNaming::PrimitiveCommands (theCommands);
77   QANewDBRepNaming::FeatureCommands (theCommands);
78
79   const char* g = "Check Naming command" ;
80
81   theCommands.Add ("CheckNaming", 
82                    "CheckNaming Doc TestLabel Full(1/0) (Label/Viewer) [Label] [DX[DY[DZ]]]",
83                    __FILE__, QANewDBRepNaming_CheckNaming, g); 
84
85   theCommands.Add ("CheckSelectShape", 
86                    "CheckSelectShape Doc Label SubShapeType",
87                    __FILE__, QANewDBRepNaming_CheckSelectShape, g); 
88
89   theCommands.Add ("CheckSolve", 
90                    "CheckSolve Doc Label",
91                    __FILE__, QANewDBRepNaming_CheckSolve, g);   
92
93   const char* com = "set Draw_NamingData 1";
94   theCommands.Eval(com);
95 }
96
97 //=======================================================================
98 //function : CheckNaming
99 //purpose  : "CheckNaming Doc TestLabel Full(1/0) ShapeFrom(Viewer/Label) [ShapeLabel]" 
100 //=======================================================================
101
102 static Standard_Integer QANewDBRepNaming_CheckNaming(Draw_Interpretor& di,Standard_Integer nb, const char ** a)
103 {
104   Handle(TDF_Data) DF;
105   if(!DDF::GetDF(a[1], DF)) return 1;
106   TDF_Label L;
107   DDF::AddLabel(DF, a[2], L);
108
109 //  cout<<"SELECT A SHAPE ..."<<endl; 
110   Handle(TNaming_NamedShape) amyNS;
111   TopoDS_Shape aSelectedShape;
112   if (!strcmp(a[4],"Label")){
113     Handle(TNaming_NamedShape) atiNS=new TNaming_NamedShape;
114     TDF_Label L2;
115     DDF::AddLabel(DF, a[5], L2);
116     if (!L2.FindAttribute(TNaming_NamedShape::GetID(), atiNS)) {
117       di<<"Error: Cannot find shape on label.\n";
118       return 1;
119     }
120     amyNS = atiNS;
121     aSelectedShape=TNaming_Tool::GetShape(atiNS);
122   }
123   else
124   if (!strcmp(a[4],"Viewer"))
125     aSelectedShape = ViewerTest::PickShape(TopAbs_SHAPE);
126   else{
127     di<<"Error: Incorrect argument #4\n";
128     return 1;
129   }
130
131   if (aSelectedShape.IsNull()) {
132     di<<"Nothing has been selected ... try again\n";
133     return 1;
134   }
135
136   TopTools_IndexedMapOfShape allSubShapes;
137   TopExp::MapShapes(aSelectedShape, allSubShapes);
138
139   Standard_Integer i;
140
141   for (i = 1; i <= allSubShapes.Extent(); i++) {
142     const TDF_Label& aSubLabel = L.FindChild(i);
143     const TopoDS_Shape& aSubShape = allSubShapes(i);
144 //    cout<<i<<"  ";TopAbs::Print(aSubShape.ShapeType(), cout); cout<<"  ";
145     
146 //     DBRep::Set("Selected", aSubShape);
147
148     TNaming_Selector SL (aSubLabel);
149       
150 //    cout << i << " ";TopAbs::Print(aSelectedShape.ShapeType(), cout); cout<<"  ";
151     try {
152       OCC_CATCH_SIGNALS
153       if(SL.Select(aSubShape, aSelectedShape) == Standard_False) {
154         //    if(SL.Select(aSubShape, amyNS->Get()) == Standard_False) 
155         di << "Selection error!!!\n";
156         return 1;
157       }
158       Handle(TNaming_NamedShape) aNS;
159       if (!aSubLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
160         di<<"Selection didn't produced a Named Shape ...\n";
161         return 1;
162       }
163       const TopoDS_Shape& aResultOfSelection = TNaming_Tool::GetShape(aNS);
164       if (!aSubShape.IsSame(aResultOfSelection)) {
165         di<<"Failure of selection\n";
166         return 1;
167       } 
168     } catch (Standard_Failure) {
169       di<<"!!! Solving Failed !!!\n";
170       continue;
171     }
172     
173   }
174
175 // ***
176
177   if (!Draw::Atoi(a[3])) return 0;
178   Standard_Real aDX = 100.0;
179   Standard_Real aDY = 100.0;
180   Standard_Real aDZ = 100.0;
181   if(nb > 6) {
182     aDX = Draw::Atof(a[6]);
183     if(nb > 7) aDY = Draw::Atof(a[7]);
184     if(nb > 8) aDZ = Draw::Atof(a[8]);
185   }
186 //  cout<<endl;
187 //  cout<<"TRANSLATION ... DX = "<<aDX <<" DY = "<< aDY <<" DZ = " << aDZ <<endl;
188   gp_Vec aTranslation(aDX, aDY, aDZ);
189   gp_Trsf aTrsf;
190   aTrsf.SetTranslation(aTranslation);
191   TopLoc_Location aLoc(aTrsf);
192   const TDF_Label& anOriginLabel = TNaming_Tool::NamedShape(aSelectedShape, L)->Label();
193   if (anOriginLabel.IsNull()) di<<"Origin is null\n";
194 //  TNaming::Displace(anOriginLabel, aLoc);
195 //  cout<<"Translate label ";anOriginLabel.EntryDump(cout);cout<<endl;
196   TDF_ChildIterator Labelitr(L.Root(), Standard_False);
197   for (; Labelitr.More(); Labelitr.Next()) {
198 //    if (Labelitr.Value() == anOriginLabel) break;
199 //    cout<<"Translate label ";Labelitr.Value().EntryDump(cout);cout<<endl;
200     TNaming::Displace(Labelitr.Value(), aLoc);
201   }
202
203
204   {
205     Handle(TNaming_NamedShape) aNS;
206     if (!anOriginLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return 1;
207     aSelectedShape = TNaming_Tool::GetShape(aNS);
208   }    
209   TopTools_IndexedMapOfShape allTranslatedSubShapes;
210   TopExp::MapShapes(aSelectedShape, allTranslatedSubShapes);
211
212   Standard_Boolean isFailured = Standard_False;
213
214 //  cout<<"SOLVING ..."<<endl;
215   TDF_LabelMap scope;
216   TDF_ChildIterator itr(L.Root(), Standard_True);
217   i = 1;
218   TCollection_AsciiString aNotSolved;
219   for (itr.Initialize(L, Standard_False); itr.More(); itr.Next(), i++) {
220     Handle(TNaming_NamedShape) aNS;
221     if (!itr.Value().FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
222 //      cout<<"Wrong selection ..."<<endl;
223       return 1;
224     }
225 //    cout<<i<<"  ";TopAbs::Print(TypeOfNaming, cout); cout<<"  ";
226 //     DBRep::Set("Solved", aNS->Get());
227     TNaming_Selector SLSolving(itr.Value());
228     if (!SLSolving.Solve(scope)) {
229       isFailured = Standard_True;
230       aNotSolved += " ";
231       aNotSolved += i;
232       continue;
233     }
234     const Handle(TNaming_NamedShape)& aResultOfSolving = SLSolving.NamedShape();
235     if (aResultOfSolving.IsNull()) {
236 //       cout<<"Failure of Solving: it didn't produced a shape!"<<endl;
237 //       return 1;
238       isFailured = Standard_True;
239       aNotSolved += " ";
240       aNotSolved += i;
241       continue;
242     }      
243     TopoDS_Shape aRes;
244 /*    if (TypeOfNaming == TopAbs_WIRE || TypeOfNaming == TopAbs_SHELL) {
245       TopoDS_Iterator itr(aResultOfSolving->Get());
246       TopoDS_Shape aSubShape;
247       if (itr.More()) aSubShape = itr.Value();
248       TDF_LabelMap one,two;
249       TNaming_Tool::FindShape(one, two, aResultOfSolving, aRes);
250       if (aRes.IsNull()) {
251 //      cout<<"Failure of TNaming_Tool::FindShape()!"<<endl;
252 //      return 1;
253         Sprintf(aNotSolved,"%s %d",aNotSolved,i);
254         isFailured = Standard_True;
255 //      break;
256         continue;
257       }
258     } else */
259     aRes = aResultOfSolving->Get();
260     if (!aRes.IsSame(allTranslatedSubShapes(i))) {
261 //       cout<<"Failure of solving of "<<i<<"-th sub label"<<endl;
262 //       TNaming::DumpShape(aRes);
263 //       TNaming::DumpShape(allTranslatedSubShapes(i));
264 //       return 1;
265       gp_XYZ aCoord1(0,0,0),aCoord2(0,0,0);
266       TopTools_MapOfShape aVertices;
267       if (aRes.ShapeType()==TopAbs_VERTEX) aCoord1=BRep_Tool::Pnt(TopoDS::Vertex(aRes)).Coord(); else {
268         TopExp_Explorer anExp(aRes,TopAbs_VERTEX);
269         for(;anExp.More();anExp.Next()) {
270           // mpv: that's only stub for the seam edges bug
271           if (aVertices.Contains(anExp.Current())) continue;
272           aVertices.Add(anExp.Current());
273           
274           aCoord1.Add(BRep_Tool::Pnt(TopoDS::Vertex(anExp.Current())).Coord());
275 //        gp_XYZ a = BRep_Tool::Pnt(TopoDS::Vertex(anExp.Current())).Coord();
276 //        cout<<"a1 ("<<a.X()<<" "<<a.Y()<<" "<<a.Z()<<")"<<endl;
277         }
278       }
279       if (allTranslatedSubShapes(i).ShapeType()==TopAbs_VERTEX)
280         aCoord2=BRep_Tool::Pnt(TopoDS::Vertex(allTranslatedSubShapes(i))).Coord();
281       else {
282         aVertices.Clear();
283         TopExp_Explorer anExp(allTranslatedSubShapes(i),TopAbs_VERTEX);
284         for(;anExp.More();anExp.Next()) {
285           // mpv: that's only stub for the seam edges bug
286           if (aVertices.Contains(anExp.Current())) continue;
287           aVertices.Add(anExp.Current());
288
289           aCoord2.Add(BRep_Tool::Pnt(TopoDS::Vertex(anExp.Current())).Coord());
290 //        gp_XYZ a = BRep_Tool::Pnt(TopoDS::Vertex(anExp.Current())).Coord();
291 //        cout<<"a1 ("<<a.X()<<" "<<a.Y()<<" "<<a.Z()<<")"<<endl;
292         }
293       }
294       if (!aCoord1.IsEqual(aCoord2,Precision::Confusion()) || aRes.ShapeType() != allTranslatedSubShapes(i).ShapeType()) {
295             aNotSolved += " ";
296         aNotSolved += i;
297             isFailured = Standard_True;
298             continue;
299       }
300     }    
301 //    cout<<endl;
302   }
303
304   if (isFailured) {
305     di<<aNotSolved;
306 //mpv: let's no catch returns 1 in tclsh    return 1;
307   }
308 //  cout<<"Good!"<<endl;
309   return 0;  
310 }
311   
312 //=======================================================================
313 //function : CheckSelectShape
314 //purpose  : "CheckSelectShape Doc Label SubShapeType" 
315 //=======================================================================
316
317 static Standard_Integer QANewDBRepNaming_CheckSelectShape(Draw_Interpretor& di, Standard_Integer /*nb*/, const char ** a)
318 {
319   Handle(TDF_Data) DF;
320   if(!DDF::GetDF(a[1], DF)) return 1;
321   TDF_Label L;
322   DDF::AddLabel(DF, a[2], L);
323
324   di<<"SELECT A SUB-SHAPE ...\n"; 
325   TopoDS_Shape aSelectedShape = ViewerTest::PickShape((TopAbs_ShapeEnum)Draw::Atoi(a[3]));
326   if (aSelectedShape.IsNull()) {
327     di<<"QANewDBRepNaming_CheckSelectShape(): Nothing is selected ...\n";
328     return 1;
329   }
330   // Find the context:
331   TopoDS_Shape aContext;
332   TDF_ChildIterator itr(L.Root());
333   Standard_Boolean contextFound = Standard_False;
334   for (; itr.More(); itr.Next()) {
335     Handle(TNaming_NamedShape) NS;
336     if (itr.Value().FindAttribute(TNaming_NamedShape::GetID(), NS)) {
337       aContext = NS->Get();
338       TopExp_Explorer expl(aContext, (TopAbs_ShapeEnum)Draw::Atoi(a[3]));
339       for (; expl.More(); expl.Next()) {
340         if (expl.Current().IsSame(aSelectedShape)) {
341           di<<"QANewDBRepNaming_CheckSelectShape(): The context is found at ";
342
343           //NS->Label().EntryDump(cout);
344           Standard_SStream aSStream;
345           NS->Label().EntryDump(aSStream);
346           di << aSStream;
347           di<<"\n";
348           contextFound = Standard_True;
349           break;
350         }
351       }
352       if (contextFound) break;
353     }
354     aContext = TopoDS_Shape();
355   }
356   // Selection:
357   TNaming_Selector aSelector(L);
358   if (aContext.IsNull()) {
359     aSelector.Select(aSelectedShape);
360   } else {
361     aSelector.Select(aSelectedShape, aContext);
362   }
363   return 0;
364 }
365
366 //=======================================================================
367 //function : CheckSelectShape
368 //purpose  : "CheckSelectShape Doc Label SubShapeType" 
369 //=======================================================================
370
371 static Standard_Integer QANewDBRepNaming_CheckSolve(Draw_Interpretor&,Standard_Integer /*nb*/, const char ** a)
372 {
373   Handle(TDF_Data) DF;
374   if(!DDF::GetDF(a[1], DF)) return 1;
375   TDF_Label L;
376   DDF::AddLabel(DF, a[2], L);
377   Handle(TNaming_NamedShape) aNS;
378   L.FindAttribute(TNaming_NamedShape::GetID(), aNS);
379
380   return 0;
381 }