Salome HOME
199fd1870eda3890d80db4326807be18205876bf
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Loft.cpp
1 // Copyright (C) 2014-2022  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_DFLoader.h"
21 #include "GeomAlgoAPI_Loft.h"
22 #include "GeomAlgoAPI_WireBuilder.h"
23
24 #include <BRepOffsetAPI_ThruSections.hxx>
25 #include <Precision.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopoDS.hxx>
28
29 //==================================================================================================
30 GeomAlgoAPI_Loft::GeomAlgoAPI_Loft(const GeomShapePtr theFirstShape, const GeomShapePtr theSecondShape)
31 {
32   build(theFirstShape, theSecondShape);
33 }
34
35 //==================================================================================================
36 void GeomAlgoAPI_Loft::build(const GeomShapePtr theFirstShape,
37                              const GeomShapePtr theSecondShape)
38 {
39   // Getting base shape.
40   if(!theFirstShape.get() || !theSecondShape.get()) {
41     return;
42   }
43   TopoDS_Shape aFirstShape = theFirstShape->impl<TopoDS_Shape>();
44   if(aFirstShape.IsNull()) {
45     return;
46   }
47   TopoDS_Shape aSecondShape= theSecondShape->impl<TopoDS_Shape>();
48   if(aSecondShape.IsNull()) {
49     return;
50   }
51
52   bool anIsSolid = false;
53
54   TopoDS_Shape aFirstShapeOut;
55   TopoDS_Shape aSecondShapeOut;
56   if (aFirstShape.ShapeType() == TopAbs_FACE) {
57     TopExp_Explorer anExp(aFirstShape, TopAbs_WIRE);
58     aFirstShapeOut = anExp.Current();
59     TopExp_Explorer anExp2(aSecondShape, TopAbs_WIRE);
60     aSecondShapeOut = anExp2.Current();
61     anIsSolid = true;
62   }
63
64   if (aFirstShape.ShapeType() == TopAbs_WIRE) {
65     aFirstShapeOut = aFirstShape;
66     aSecondShapeOut = aSecondShape;
67   }
68
69   if (aFirstShape.ShapeType() == TopAbs_EDGE)
70   {
71     GeomShapePtr aFirstWire, aSecondWire;
72     ListOfShape aFirstEdge, aSecondEdge;
73
74     // Convert first edge to wire
75     aFirstEdge.push_back(theFirstShape);
76     aFirstWire = GeomAlgoAPI_WireBuilder::wire(aFirstEdge);
77     TopoDS_Shape aFirstShape = aFirstWire->impl<TopoDS_Shape>();
78
79     // Convert first edge to wire
80     aSecondEdge.push_back(theSecondShape);
81     aSecondWire = GeomAlgoAPI_WireBuilder::wire(aSecondEdge);
82     TopoDS_Shape aSecondShape = aSecondWire->impl<TopoDS_Shape>();
83
84     aFirstShapeOut = aFirstShape;
85     aSecondShapeOut = aSecondShape;
86   }
87
88   // Initialize and build
89   BRepOffsetAPI_ThruSections * ThruSections =
90                               new BRepOffsetAPI_ThruSections(anIsSolid);
91   ThruSections->AddWire( TopoDS::Wire( aFirstShapeOut ) );
92   ThruSections->AddWire( TopoDS::Wire( aSecondShapeOut) );
93
94   try
95   {
96     ThruSections->Build();
97   }
98   catch (Standard_Failure& aFail)
99   {
100     delete ThruSections;
101     return;
102   }
103   // Checking result.
104   if(!ThruSections->IsDone() || ThruSections->Shape().IsNull()) {
105     delete ThruSections;
106     return;
107   }
108
109   this->initialize(ThruSections);
110
111   // Setting result.
112   TopoDS_Shape aResult = ThruSections->Shape();
113   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
114   GeomShapePtr aGeomSh(new GeomAPI_Shape());
115   aGeomSh->setImpl(new TopoDS_Shape(aResult));
116   this->setShape(aGeomSh);
117   this->setDone(true);
118 }