Salome HOME
bos #26449: SHAPER: save imported images
[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 <BRep_Builder.hxx>
23 #include <BRepBuilderAPI_MakeFace.hxx>
24 #include <BRepBuilderAPI_MakePolygon.hxx>
25 #include <BRepBuilderAPI_MakeVertex.hxx>
26 #include <BRepBuilderAPI_Sewing.hxx>
27 #include <gp_Pnt.hxx>
28 #include <OSD_Path.hxx>
29 #include <RWStl.hxx>
30 #include <TopoDS_Compound.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <TopoDS_Face.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Shell.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopoDS_Wire.hxx>
37
38 std::shared_ptr<GeomAPI_Shape> ImageImport(const int width,
39                                            const int height,
40                                            std::string& theError)
41 {
42   TopoDS_Shape aResShape;
43   try
44   {
45     TopoDS_Vertex aTriVertexes[4];
46     gp_Pnt aPnt1( -0.5*width, -0.5*height, 0);
47     gp_Pnt aPnt2( 0.5*width, -0.5*height, 0);
48     gp_Pnt aPnt3(  0.5*width,  0.5*height, 0 );;
49     gp_Pnt aPnt4( -0.5*width,  0.5*height, 0 );
50     TopoDS_Face aFace;
51     TopoDS_Wire aWire;
52
53     aTriVertexes[0] = BRepBuilderAPI_MakeVertex (aPnt1);
54     aTriVertexes[1] = BRepBuilderAPI_MakeVertex (aPnt2);
55     aTriVertexes[2] = BRepBuilderAPI_MakeVertex (aPnt3);
56     aTriVertexes[3] = BRepBuilderAPI_MakeVertex (aPnt4);
57
58     aWire = BRepBuilderAPI_MakePolygon (aTriVertexes[0], aTriVertexes[1], aTriVertexes[2],
59         aTriVertexes[3], Standard_True);
60
61     BRepBuilderAPI_Sewing aSewingTool;
62     aSewingTool.Init (1.0e-06, Standard_True);
63
64     TopoDS_Compound aComp;
65     BRep_Builder BuildTool;
66     BuildTool.MakeCompound(aComp);
67
68     if(!aWire.IsNull())
69     {
70       aFace = BRepBuilderAPI_MakeFace (aWire);
71       if (!aFace.IsNull())
72       {
73         BuildTool.Add (aComp, aFace);
74       }
75     }
76
77     aSewingTool.Load (aComp);
78     aSewingTool.Perform();
79     aResShape = aSewingTool.SewedShape();
80     if (aResShape.IsNull())
81     {
82       aResShape = aComp;
83     }
84   }
85   catch (Standard_Failure const& anException) {
86     theError = anException.GetMessageString();
87     aResShape.Nullify();
88   }
89
90   std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
91   aGeomShape->setImpl(new TopoDS_Shape(aResShape));
92
93   return aGeomShape;
94 }