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