Salome HOME
add Picture import
[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], aTriVertexes[3], Standard_True);
65
66     BRepBuilderAPI_Sewing aSewingTool;
67     aSewingTool.Init (1.0e-06, Standard_True);
68
69     TopoDS_Compound aComp;
70     BRep_Builder BuildTool;
71     BuildTool.MakeCompound(aComp);
72
73     if(!aWire.IsNull())
74     {
75       aFace = BRepBuilderAPI_MakeFace (aWire);
76       if (!aFace.IsNull())
77       {
78         BuildTool.Add (aComp, aFace);
79       }
80     }
81
82     aSewingTool.Load (aComp);
83     aSewingTool.Perform();
84     aResShape = aSewingTool.SewedShape();
85     if (aResShape.IsNull())
86     {
87       aResShape = aComp;
88     }
89   }
90   catch (Standard_Failure const& anException) {
91     theError = anException.GetMessageString();
92     aResShape.Nullify();
93   }
94
95   std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
96   aGeomShape->setImpl(new TopoDS_Shape(aResShape));  
97
98   return aGeomShape;
99 }