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
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.
18 #include <BRep_Tool.hxx>
19 #include <BRepTools.hxx>
24 #include <gp_Trsf.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>
43 #include <TopExp_Explorer.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>
53 //////#include <Draw_PluginMacro.hxx>
54 //////#include <DBrowser.hxx>
55 static Standard_Integer QANewDBRepNaming_CheckNaming(Draw_Interpretor&,Standard_Integer,const char ** a);
57 //static Standard_Integer QANewDBRepNaming_myCheckNaming(Draw_Interpretor&,Standard_Integer,const char ** a);
59 static Standard_Integer QANewDBRepNaming_CheckSelectShape(Draw_Interpretor&,Standard_Integer,const char ** a);
61 static Standard_Integer QANewDBRepNaming_CheckSolve(Draw_Interpretor&,Standard_Integer,const char ** a);
63 #include <QADNaming.hxx>
65 //=======================================================================
66 //function : AllCommands
68 //=======================================================================
70 void QANewDBRepNaming::AllCommands(Draw_Interpretor& theCommands)
72 static Standard_Boolean done = Standard_False;
76 QANewDBRepNaming::PrimitiveCommands (theCommands);
77 QANewDBRepNaming::FeatureCommands (theCommands);
79 const char* g = "Check Naming command" ;
81 theCommands.Add ("CheckNaming",
82 "CheckNaming Doc TestLabel Full(1/0) (Label/Viewer) [Label] [DX[DY[DZ]]]",
83 __FILE__, QANewDBRepNaming_CheckNaming, g);
85 theCommands.Add ("CheckSelectShape",
86 "CheckSelectShape Doc Label SubShapeType",
87 __FILE__, QANewDBRepNaming_CheckSelectShape, g);
89 theCommands.Add ("CheckSolve",
90 "CheckSolve Doc Label",
91 __FILE__, QANewDBRepNaming_CheckSolve, g);
93 const char* com = "set Draw_NamingData 1";
94 theCommands.Eval(com);
97 //=======================================================================
98 //function : CheckNaming
99 //purpose : "CheckNaming Doc TestLabel Full(1/0) ShapeFrom(Viewer/Label) [ShapeLabel]"
100 //=======================================================================
102 static Standard_Integer QANewDBRepNaming_CheckNaming(Draw_Interpretor& di,Standard_Integer nb, const char ** a)
105 if(!DDF::GetDF(a[1], DF)) return 1;
107 DDF::AddLabel(DF, a[2], L);
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;
115 DDF::AddLabel(DF, a[5], L2);
116 if (!L2.FindAttribute(TNaming_NamedShape::GetID(), atiNS)) {
117 di<<"Error: Cannot find shape on label.\n";
121 aSelectedShape=TNaming_Tool::GetShape(atiNS);
124 if (!strcmp(a[4],"Viewer"))
125 aSelectedShape = ViewerTest::PickShape(TopAbs_SHAPE);
127 di<<"Error: Incorrect argument #4\n";
131 if (aSelectedShape.IsNull()) {
132 di<<"Nothing has been selected ... try again\n";
136 TopTools_IndexedMapOfShape allSubShapes;
137 TopExp::MapShapes(aSelectedShape, allSubShapes);
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<<" ";
146 // DBRep::Set("Selected", aSubShape);
148 TNaming_Selector SL (aSubLabel);
150 // cout << i << " ";TopAbs::Print(aSelectedShape.ShapeType(), cout); cout<<" ";
153 if(SL.Select(aSubShape, aSelectedShape) == Standard_False) {
154 // if(SL.Select(aSubShape, amyNS->Get()) == Standard_False)
155 di << "Selection error!!!\n";
158 Handle(TNaming_NamedShape) aNS;
159 if (!aSubLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
160 di<<"Selection didn't produced a Named Shape ...\n";
163 const TopoDS_Shape& aResultOfSelection = TNaming_Tool::GetShape(aNS);
164 if (!aSubShape.IsSame(aResultOfSelection)) {
165 di<<"Failure of selection\n";
168 } catch (Standard_Failure) {
169 di<<"!!! Solving Failed !!!\n";
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;
182 aDX = Draw::Atof(a[6]);
183 if(nb > 7) aDY = Draw::Atof(a[7]);
184 if(nb > 8) aDZ = Draw::Atof(a[8]);
187 // cout<<"TRANSLATION ... DX = "<<aDX <<" DY = "<< aDY <<" DZ = " << aDZ <<endl;
188 gp_Vec aTranslation(aDX, aDY, aDZ);
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);
205 Handle(TNaming_NamedShape) aNS;
206 if (!anOriginLabel.FindAttribute(TNaming_NamedShape::GetID(), aNS)) return 1;
207 aSelectedShape = TNaming_Tool::GetShape(aNS);
209 TopTools_IndexedMapOfShape allTranslatedSubShapes;
210 TopExp::MapShapes(aSelectedShape, allTranslatedSubShapes);
212 Standard_Boolean isFailured = Standard_False;
214 // cout<<"SOLVING ..."<<endl;
216 TDF_ChildIterator itr(L.Root(), Standard_True);
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;
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;
234 const Handle(TNaming_NamedShape)& aResultOfSolving = SLSolving.NamedShape();
235 if (aResultOfSolving.IsNull()) {
236 // cout<<"Failure of Solving: it didn't produced a shape!"<<endl;
238 isFailured = Standard_True;
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);
251 // cout<<"Failure of TNaming_Tool::FindShape()!"<<endl;
253 Sprintf(aNotSolved,"%s %d",aNotSolved,i);
254 isFailured = Standard_True;
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));
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());
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;
279 if (allTranslatedSubShapes(i).ShapeType()==TopAbs_VERTEX)
280 aCoord2=BRep_Tool::Pnt(TopoDS::Vertex(allTranslatedSubShapes(i))).Coord();
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());
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;
294 if (!aCoord1.IsEqual(aCoord2,Precision::Confusion()) || aRes.ShapeType() != allTranslatedSubShapes(i).ShapeType()) {
297 isFailured = Standard_True;
306 //mpv: let's no catch returns 1 in tclsh return 1;
308 // cout<<"Good!"<<endl;
312 //=======================================================================
313 //function : CheckSelectShape
314 //purpose : "CheckSelectShape Doc Label SubShapeType"
315 //=======================================================================
317 static Standard_Integer QANewDBRepNaming_CheckSelectShape(Draw_Interpretor& di, Standard_Integer /*nb*/, const char ** a)
320 if(!DDF::GetDF(a[1], DF)) return 1;
322 DDF::AddLabel(DF, a[2], L);
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";
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 ";
343 //NS->Label().EntryDump(cout);
344 Standard_SStream aSStream;
345 NS->Label().EntryDump(aSStream);
348 contextFound = Standard_True;
352 if (contextFound) break;
354 aContext = TopoDS_Shape();
357 TNaming_Selector aSelector(L);
358 if (aContext.IsNull()) {
359 aSelector.Select(aSelectedShape);
361 aSelector.Select(aSelectedShape, aContext);
366 //=======================================================================
367 //function : CheckSelectShape
368 //purpose : "CheckSelectShape Doc Label SubShapeType"
369 //=======================================================================
371 static Standard_Integer QANewDBRepNaming_CheckSolve(Draw_Interpretor&,Standard_Integer /*nb*/, const char ** a)
374 if(!DDF::GetDF(a[1], DF)) return 1;
376 DDF::AddLabel(DF, a[2], L);
377 Handle(TNaming_NamedShape) aNS;
378 L.FindAttribute(TNaming_NamedShape::GetID(), aNS);