Salome HOME
CEA : Lot2 - Add new filters
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ImageImport.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <GeomAlgoAPI_ImageImport.h>
21
22 #include <QPixmap>
23 #include <BRep_Builder.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <BRepBuilderAPI_MakePolygon.hxx>
26 #include <BRepBuilderAPI_MakeVertex.hxx>
27 #include <BRepBuilderAPI_Sewing.hxx>
28 #include <gp_Pnt.hxx>
29 #include <OSD_Path.hxx>
30 #include <RWStl.hxx>
31 #include <TopoDS_Compound.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Face.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Shell.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Wire.hxx>
38
39 std::shared_ptr<GeomAPI_Shape> ImageImport(const std::string& theFileName,
40                                          std::string& theError)
41 {
42   TopoDS_Shape aResShape;
43   try
44   {
45     QPixmap* pixmap = new QPixmap(theFileName.c_str());
46     int height            =  pixmap->height();
47     int width             =  pixmap->width();
48
49     delete pixmap;
50
51     TopoDS_Vertex aTriVertexes[4];
52     gp_Pnt aPnt1( -0.5*width, -0.5*height, 0);
53     gp_Pnt aPnt2( 0.5*width, -0.5*height, 0);
54     gp_Pnt aPnt3(  0.5*width,  0.5*height, 0 );;
55     gp_Pnt aPnt4( -0.5*width,  0.5*height, 0 );
56     TopoDS_Face aFace;
57     TopoDS_Wire aWire;
58
59     aTriVertexes[0] = BRepBuilderAPI_MakeVertex (aPnt1);
60     aTriVertexes[1] = BRepBuilderAPI_MakeVertex (aPnt2);
61     aTriVertexes[2] = BRepBuilderAPI_MakeVertex (aPnt3);
62     aTriVertexes[3] = BRepBuilderAPI_MakeVertex (aPnt4);
63
64     aWire = BRepBuilderAPI_MakePolygon (aTriVertexes[0], aTriVertexes[1], aTriVertexes[2],
65         aTriVertexes[3], Standard_True);
66
67     BRepBuilderAPI_Sewing aSewingTool;
68     aSewingTool.Init (1.0e-06, Standard_True);
69
70     TopoDS_Compound aComp;
71     BRep_Builder BuildTool;
72     BuildTool.MakeCompound(aComp);
73
74     if(!aWire.IsNull())
75     {
76       aFace = BRepBuilderAPI_MakeFace (aWire);
77       if (!aFace.IsNull())
78       {
79         BuildTool.Add (aComp, aFace);
80       }
81     }
82
83     aSewingTool.Load (aComp);
84     aSewingTool.Perform();
85     aResShape = aSewingTool.SewedShape();
86     if (aResShape.IsNull())
87     {
88       aResShape = aComp;
89     }
90   }
91   catch (Standard_Failure const& anException) {
92     theError = anException.GetMessageString();
93     aResShape.Nullify();
94   }
95
96   std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
97   aGeomShape->setImpl(new TopoDS_Shape(aResShape));
98
99   return aGeomShape;
100 }