0030182: Visualization, Image_AlienPixMap - support reading encoded image from memory...
[occt.git] / src / QABugs / QABugs_1.cxx
CommitLineData
b311480e 1// Created on: 2002-05-21
2// Created by: QA Admin
973c2be1 3// Copyright (c) 2002-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
1cd84fee 16#include <QABugs.hxx>
7fd59977 17
18#include <Draw_Interpretor.hxx>
19#include <DBRep.hxx>
20#include <DrawTrSurf.hxx>
21#include <AIS_InteractiveContext.hxx>
22#include <ViewerTest.hxx>
23#include <AIS_Shape.hxx>
24#include <TopoDS_Shape.hxx>
25
26#include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
27#include <TColStd_ListIteratorOfListOfInteger.hxx>
28#include <TColStd_MapOfInteger.hxx>
29
30#include <TDocStd_Document.hxx>
31#include <TDocStd_Application.hxx>
32#include <DDocStd.hxx>
33#include <TDocStd_Owner.hxx>
34#include <TDF_Label.hxx>
35#include <DDF.hxx>
36#include <TPrsStd_AISViewer.hxx>
37#include <TPrsStd_AISPresentation.hxx>
38
39#include <Draw_Viewer.hxx>
40#include <Draw.hxx>
41
57c28b61 42#ifndef _WIN32
7fd59977 43extern Draw_Viewer dout;
44#else
45Standard_IMPORT Draw_Viewer dout;
46#endif
47
48#include <BRep_Builder.hxx>
49#include <BRepTools.hxx>
50#include <TopoDS_Wire.hxx>
51#include <BRepBuilderAPI_MakeFace.hxx>
52#include <TopoDS.hxx>
53
57c28b61 54#if ! defined(_WIN32)
7fd59977 55extern ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
56#else
57Standard_EXPORT ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
58#endif
59
60static TColStd_MapOfInteger theactivatedmodes(8);
61
62#include <AIS_PlaneTrihedron.hxx>
63#include <TopoDS_Face.hxx>
64#include <TopExp_Explorer.hxx>
65#include <TopoDS_Edge.hxx>
66#include <BRepAdaptor_Curve.hxx>
67#include <GC_MakePlane.hxx>
68
7fd59977 69static Standard_Integer OCC159bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
70{
71 if ( argc != 2) {
586db386 72 di << "ERROR : Usage : " << argv[0] << " Doc\n";
7fd59977 73 return 1;
74 }
75
76 Handle(TDocStd_Document) D;
77 if (!DDocStd::GetDocument(argv[1],D)) return 1;
78
79 Standard_Integer DocRefCount1 = D->GetRefCount();
80 di << "DocRefCount1 = " << DocRefCount1 << "\n";
81
82 Handle(TDocStd_Owner) Owner;
83 if (!D->Main().Root().FindAttribute(TDocStd_Owner::GetID(),Owner)) return 1;
84
85 Handle(TDocStd_Document) OwnerD1 = Owner->GetDocument();
86 if (OwnerD1.IsNull()) {
586db386 87 di << "DocOwner1 = NULL\n";
7fd59977 88 } else {
586db386 89 di << "DocOwner1 = NOTNULL\n";
7fd59977 90 }
91
6fe96f84 92 Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 93 A->Close(D);
94
1c8fc6be 95 if (Handle(Draw_Drawable3D) DD = Draw::GetExisting (argv[1]))
96 {
97 dout.RemoveDrawable (DD);
98 }
7fd59977 99
100 Handle(TDocStd_Document) OwnerD2 = Owner->GetDocument();
101 if (OwnerD2.IsNull()) {
586db386 102 di << "DocOwner2 = NULL\n";
7fd59977 103 } else {
586db386 104 di << "DocOwner2 = NOTNULL\n";
7fd59977 105 }
106
107 Standard_Integer DocRefCount2 = D->GetRefCount();
108 di << "DocRefCount2 = " << DocRefCount2 << "\n";
109
110 return 0;
111}
112
113static Standard_Integer OCC145bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
114{
115 if ( argc != 3) {
586db386 116 di << "ERROR : Usage : " << argv[0] << " Shape MaxNbr\n";
7fd59977 117 return 1;
118 }
119
120 TCollection_AsciiString aFileName = argv[1];
91322f44 121 Standard_Integer aMaxNbr = Draw::Atoi(argv[2]);
7fd59977 122
123 BRep_Builder aBld;
124 TopoDS_Shape aShape;
125
126 if (!BRepTools::Read(aShape, aFileName.ToCString(), aBld)) {
586db386 127 di << "ERROR :Could not read a shape!!!\n";
7fd59977 128 return 1;
129 }
130
131 Standard_Integer i;
132 TopoDS_Wire aWire = TopoDS::Wire(aShape);
133
134 for (i = 1; i <= aMaxNbr; i++) {
135 BRepBuilderAPI_MakeFace aMF(aWire);
136 if (!aMF.IsDone()) {
586db386 137 di << "ERROR : Could not make a face\n";
7fd59977 138 return 1;
139 }
140 }
141
142 return 0;
143}
144
145static Standard_Integer OCC73_SelectionMode (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
146{
147 if ( argc < 3) {
586db386 148 di << "ERROR : Usage : " << argv[0] << " DOC entry [SelectionMode]\n";
7fd59977 149 return 1;
150 }
151
152 Handle(TDocStd_Document) D;
153 //cout << "OCC73_SelectionMode 1" << endl;
154 if (!DDocStd::GetDocument(argv[1],D)) return 1;
155 TDF_Label L;
156 //cout << "OCC73_SelectionMode 2" << endl;
157 if (!DDF::FindLabel(D->GetData(),argv[2],L)) return 1;
158
159 Handle(TPrsStd_AISViewer) viewer;
160 //cout << "OCC73_SelectionMode 3" << endl;
161 if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;
162
163 Handle(TPrsStd_AISPresentation) prs;
164 //cout << "OCC73_SelectionMode 4" << endl;
165 if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {
166 if( argc == 4 ) {
91322f44 167 prs->SetSelectionMode((Standard_Integer)Draw::Atoi(argv[3]));
7fd59977 168 TPrsStd_AISViewer::Update(L);
169 }
170 else {
171 Standard_Integer SelectionMode = prs->SelectionMode();
172 //cout << "SelectionMode = " << SelectionMode << endl;
173 di<<SelectionMode;
174 }
175 }
176 //cout << "OCC73_SelectionMode 5" << endl;
177
178 return 0;
179}
180
181static Standard_Integer OCC10bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
182{
183 Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
184 if(aContext.IsNull()) {
185 di << "use 'vinit' command before " << argv[0] << "\n";
186 return 1;
187 }
188
189 if(argc != 4) {
586db386 190 di << "Usage : " << argv[0] << " name plane Length\n";
7fd59977 191 return 1;
192 }
193
194 TopoDS_Shape S = DBRep::Get( argv[2] );
195 if ( S.IsNull() ) {
586db386 196 di << "Shape is empty\n";
7fd59977 197 return 1;
198 }
199
200 TCollection_AsciiString name(argv[1]);
91322f44 201 Standard_Real Length = Draw::Atof(argv[3]);
7fd59977 202
203 // Construction de l'AIS_PlaneTrihedron
204 Handle(AIS_PlaneTrihedron) theAISPlaneTri;
205
206 Standard_Boolean IsBound = GetMapOfAIS().IsBound2(name);
207 if (IsBound) {
208 // on recupere la shape dans la map des objets displayes
8f521168 209 Handle(AIS_InteractiveObject) aShape = GetMapOfAIS().Find2(name);
7fd59977 210
211 // On verifie que l'AIS InteraciveObject est bien
212 // un AIS_PlaneTrihedron
213 if (aShape->Type()==AIS_KOI_Datum && aShape->Signature()==4) {
214 // On downcast aShape de AIS_InteractiveObject a AIS_PlaneTrihedron
c5f3a425 215 theAISPlaneTri = Handle(AIS_PlaneTrihedron)::DownCast (aShape);
7fd59977 216
217 theAISPlaneTri->SetLength(Length);
218
219 aContext->Redisplay(theAISPlaneTri, Standard_False);
220 aContext->UpdateCurrentViewer();
221 }
222 } else {
223 TopoDS_Face FaceB=TopoDS::Face(S);
224
225 // Construction du Plane
226 // recuperation des edges des faces.
227 TopExp_Explorer FaceExpB(FaceB,TopAbs_EDGE);
228
229 TopoDS_Edge EdgeB=TopoDS::Edge(FaceExpB.Current() );
230 // declarations
231 gp_Pnt A,B,C;
232
233 // si il y a plusieurs edges
234 if (FaceExpB.More() ) {
235 FaceExpB.Next();
236 TopoDS_Edge EdgeC=TopoDS::Edge(FaceExpB.Current() );
237 BRepAdaptor_Curve theCurveB(EdgeB);
238 BRepAdaptor_Curve theCurveC(EdgeC);
239 A=theCurveC.Value(0.1);
240 B=theCurveC.Value(0.9);
241 C=theCurveB.Value(0.5);
242 }
243 else {
244 // FaceB a 1 unique edge courbe
245 BRepAdaptor_Curve theCurveB(EdgeB);
246 A=theCurveB.Value(0.1);
247 B=theCurveB.Value(0.9);
248 C=theCurveB.Value(0.5);
249 }
250 // Construction du Geom_Plane
251 GC_MakePlane MkPlane(A,B,C);
252 Handle(Geom_Plane) theGeomPlane=MkPlane.Value();
253
254 // on le display & bind
255 theAISPlaneTri= new AIS_PlaneTrihedron(theGeomPlane );
256
257 theAISPlaneTri->SetLength(Length);
258
259 GetMapOfAIS().Bind ( theAISPlaneTri, name);
0577ae8c 260 aContext->Display (theAISPlaneTri, Standard_True);
7fd59977 261 }
262
263 Standard_Real getLength = theAISPlaneTri->GetLength();
264 di << "Length = " << Length << "\n";
265 di << "getLength = " << getLength << "\n";
266
267 if (getLength == Length) {
586db386 268 di << "OCC10: OK\n";
7fd59977 269 } else {
586db386 270 di << "OCC10: ERROR\n";
7fd59977 271 }
272
273 return 0;
274}
275
276static Standard_Integer OCC74bug_set (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
277{
278 Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
279 if(aContext.IsNull()) {
586db386 280 di << argv[0] << "ERROR : use 'vinit' command before \n";
7fd59977 281 return 1;
282 }
283
284 if ( argc != 3) {
586db386 285 di << "ERROR : Usage : " << argv[0] << " shape mode; set selection mode\n";
7fd59977 286 return 1;
287 }
288
eb4320f2 289 Standard_Boolean updateviewer = Standard_True;
7fd59977 290
291 ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
292
293 TCollection_AsciiString aName(argv[1]);
294 Handle(AIS_InteractiveObject) AISObj;
295
91322f44 296 Standard_Integer SelectMode = Draw::Atoi(argv[2]);
8f521168 297 if (!aMap.Find2 (aName, AISObj)
298 || AISObj.IsNull())
299 {
586db386 300 di << "Use 'vdisplay' before\n";
7fd59977 301 return 1;
7fd59977 302 }
8f521168 303
304 aContext->Erase(AISObj, updateviewer);
305 aContext->UpdateCurrentViewer();
306 aContext->SetAutoActivateSelection (Standard_False);
307 aContext->Display(AISObj, updateviewer);
308 aContext->Activate (AISObj, SelectMode);
309 aContext->UpdateCurrentViewer();
7fd59977 310 return 0;
311}
312
313static Standard_Integer OCC74bug_get (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
314{
315 Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
316 if(aContext.IsNull()) {
586db386 317 di << argv[0] << "ERROR : use 'vinit' command before \n";
7fd59977 318 return 1;
319 }
320
321 if ( argc != 2) {
586db386 322 di << "ERROR : Usage : " << argv[0] << " shape; get selection mode\n";
7fd59977 323 return 1;
324 }
7fd59977 325
326 ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
327
328 TCollection_AsciiString aName(argv[1]);
329 Handle(AIS_InteractiveObject) AISObj;
8f521168 330 if (!aMap.Find2(aName, AISObj)
331 || AISObj.IsNull())
332 {
586db386 333 di << "Use 'vdisplay' before\n";
7fd59977 334 return 1;
7fd59977 335 }
336
8f521168 337 TColStd_ListOfInteger anActivatedModes;
338 aContext->ActivatedModes (AISObj, anActivatedModes);
339 Standard_Integer aMode = anActivatedModes.IsEmpty() ? -1 : anActivatedModes.Last();
340 di << aMode << "\n";
7fd59977 341 return 0;
342}
343
344#include <BRepPrimAPI_MakeBox.hxx>
345#include <TDF_Data.hxx>
346#include <TNaming_Builder.hxx>
347#include <TNaming_NamedShape.hxx>
ec357c5c 348#include <AIS_InteractiveObject.hxx>
7fd59977 349
350static Standard_Integer OCC361bug (Draw_Interpretor& di, Standard_Integer nb, const char ** a)
351{
352 if ( nb != 2) {
586db386 353 di << "ERROR : Usage : " << a[0] << " Doc\n";
354 di << "-1\n";
7fd59977 355 return -1;
356 }
357
358 Handle(TDocStd_Document) D;
359 if (!DDocStd::GetDocument(a[1],D)) {
586db386 360 di << "-2\n";
7fd59977 361 return 1;
362 }
363
364 BRepPrimAPI_MakeBox aBox(gp_Pnt(0, 0, 0), 100, 100, 100);
365 TopoDS_Shape aTBox = aBox.Shape();
366 aTBox.Orientation(TopAbs_FORWARD);
367
d01cc61d 368 TDF_Label aTestLabel = D->Main();
7fd59977 369
370 TNaming_Builder aBuilder(aTestLabel);
371 aBuilder.Generated(aTBox);
372
373 TopoDS_Shape aTBox1 = aTBox;
374 aTBox1.Orientation(TopAbs_REVERSED);
375 aTestLabel.ForgetAllAttributes();
7fd59977 376
f11b9af4 377 TNaming_Builder aBuilder2(aTestLabel);
378 aBuilder2.Generated( aTBox1);
379
380 aTBox = aBuilder2.NamedShape()->Get();
7fd59977 381 if(aTBox.Orientation() != TopAbs_REVERSED) {
586db386 382 di << "1\n";
7fd59977 383 } else {
586db386 384 di << "0\n";
7fd59977 385 }
386 return 0;
387}
388
88b12b7c 389#include <Graphic3d_Texture2Dmanual.hxx>
390#include <Image_AlienPixMap.hxx>
391#include <OSD_OpenFile.hxx>
392#include <Prs3d_ShadingAspect.hxx>
393#include <Standard_ArrayStreamBuffer.hxx>
394//=======================================================================
395//function : OCC30182
396//purpose : Testing different interfaces of Image_AlienPixMap::Load()
397//=======================================================================
398static Standard_Integer OCC30182 (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
399{
400 if (ViewerTest::CurrentView().IsNull())
401 {
402 std::cout << "Error: no active view\n";
403 return 1;
404 }
405
406 TCollection_AsciiString aPrsName, anImgPath;
407 Standard_Integer anOffset = 0;
408 Standard_Integer aSrc = 0; // 0 - file name, 1 - file stream, 2 - memory buffer
409 for (Standard_Integer anArgIter = 1; anArgIter < theNbArgs; ++anArgIter)
410 {
411 TCollection_AsciiString anArg (theArgVec[anArgIter]);
412 anArg.LowerCase();
413 if (anArg == "-offset"
414 && anArgIter + 1 < theNbArgs)
415 {
416 anOffset = Draw::Atoi (theArgVec[++anArgIter]);
417 }
418 else if (anArg == "-filename")
419 {
420 aSrc = 0;
421 }
422 else if (anArg == "-stream")
423 {
424 aSrc = 1;
425 }
426 else if (anArg == "-mem"
427 || anArg == "-memory")
428 {
429 aSrc = 2;
430 }
431 else if (aPrsName.IsEmpty())
432 {
433 aPrsName = theArgVec[anArgIter];
434 }
435 else if (anImgPath.IsEmpty())
436 {
437 anImgPath = theArgVec[anArgIter];
438 }
439 else
440 {
441 std::cout << "Syntax error at '" << anArg << "'\n";
442 return 1;
443 }
444 }
445 if (anImgPath.IsEmpty())
446 {
447 std::cout << "Syntax error: wrong number of arguments\n";
448 return 1;
449 }
450
451 Handle(Image_AlienPixMap) anImage = new Image_AlienPixMap();
452 if (aSrc == 0)
453 {
454 if (!anImage->Load (anImgPath))
455 {
456 return 0;
457 }
458 }
459 else
460 {
461 std::ifstream aFile;
462 OSD_OpenStream (aFile, anImgPath.ToCString(), std::ios::in | std::ios::binary);
463 if (!aFile.is_open())
464 {
465 std::cout << "Syntax error: image file '" << anImgPath << "' cannot be found\n";
466 return 1;
467 }
468 if (anOffset != 0)
469 {
470 aFile.seekg (anOffset);
471 }
472
473 if (aSrc == 2)
474 {
475 aFile.seekg (0, std::ios::end);
476 Standard_Integer aLen = (Standard_Integer )aFile.tellg() - anOffset;
477 aFile.seekg (anOffset);
478 if (aLen <= 0)
479 {
480 std::cout << "Syntax error: wrong offset\n";
481 return 1;
482 }
483 NCollection_Array1<Standard_Byte> aBuff (1, aLen);
484 if (!aFile.read ((char* )&aBuff.ChangeFirst(), aBuff.Size()))
485 {
486 std::cout << "Error: unable to read file\n";
487 return 1;
488 }
489 aFile.close();
490 if (!anImage->Load (&aBuff.ChangeFirst(), aBuff.Size(), anImgPath))
491 {
492 return 0;
493 }
494 }
495 else
496 {
497 if (!anImage->Load (aFile, anImgPath))
498 {
499 return 0;
500 }
501 }
502 }
503
504 TopoDS_Shape aShape = BRepPrimAPI_MakeBox (100.0 * anImage->Ratio(), 100.0, 1.0).Shape();
505 Handle(AIS_Shape) aPrs = new AIS_Shape (aShape);
506 aPrs->SetDisplayMode (AIS_Shaded);
507 aPrs->Attributes()->SetupOwnShadingAspect();
508 const Handle(Graphic3d_AspectFillArea3d)& anAspect = aPrs->Attributes()->ShadingAspect()->Aspect();
509 anAspect->SetShadingModel (Graphic3d_TOSM_UNLIT);
510 anAspect->SetTextureMapOn (true);
511 anAspect->SetTextureMap (new Graphic3d_Texture2Dmanual (anImage));
512 if (anImage->IsTopDown())
513 {
514 anAspect->TextureMap()->GetParams()->SetTranslation(Graphic3d_Vec2 (0.0f, -1.0f));
515 anAspect->TextureMap()->GetParams()->SetScale (Graphic3d_Vec2 (1.0f, -1.0f));
516 }
517
518 ViewerTest::Display (aPrsName, aPrs, true, true);
519 return 0;
520}
521
1cd84fee 522void QABugs::Commands_1(Draw_Interpretor& theCommands) {
523 const char *group = "QABugs";
7fd59977 524
7fd59977 525 theCommands.Add ("OCC159", "OCC159 Doc", __FILE__, OCC159bug, group);
526 theCommands.Add ("OCC145", "OCC145 Shape MaxNbr", __FILE__, OCC145bug, group);
527
528 theCommands.Add ("OCC73_SelectionMode", "OCC73_SelectionMode DOC entry [SelectionMode]", __FILE__, OCC73_SelectionMode, group);
529
530 theCommands.Add ("OCC10", "OCC10 Shape MaxNbr", __FILE__, OCC10bug, group);
531
532 theCommands.Add ("OCC74_set", "OCC74_set shape mode; set selection mode", __FILE__, OCC74bug_set, group);
533 theCommands.Add ("OCC74_get", "OCC74_get shape; get selection mode", __FILE__, OCC74bug_get, group);
534
88b12b7c 535 theCommands.Add ("OCC361", "OCC361 Doc ", __FILE__, OCC361bug, group);
536 theCommands.Add ("OCC30182",
537 "OCC30182 name image [-offset Start] [-fileName] [-stream] [-memory]\n"
538 "Decodes image either by passing file name, file stream or memory stream", __FILE__, OCC30182, group);
7fd59977 539 return;
540}