Salome HOME
2a06eae0c6c5ef6f01121721d88f6a4206b1b99c
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ThruSectionsDriver.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <GEOMImpl_ThruSectionsDriver.hxx>
24 #include <GEOMImpl_IThruSections.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27 #include <GEOMUtils.hxx>
28
29 #include <TColStd_HSequenceOfTransient.hxx>
30 #include <Precision.hxx>
31 #include <BRepOffsetAPI_ThruSections.hxx>
32 #include <BRepBuilderAPI_MakeWire.hxx>
33 #include <TopoDS.hxx>
34
35 #include <TopAbs.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Wire.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Shape.hxx>
40
41 #include <Standard_TypeMismatch.hxx>
42 #include <Standard_ConstructionError.hxx>
43
44 //=======================================================================
45 //function : GetID
46 //purpose  :
47 //=======================================================================
48 const Standard_GUID& GEOMImpl_ThruSectionsDriver::GetID()
49 {
50   static Standard_GUID aThruSectionsDriver("FF1BB971-E99C-4f89-B989-5B48E061049B");
51   return aThruSectionsDriver;
52 }
53
54
55 //=======================================================================
56 //function : GEOMImpl_ThruSectionsDriver
57 //purpose  :
58 //=======================================================================
59 GEOMImpl_ThruSectionsDriver::GEOMImpl_ThruSectionsDriver()
60 {
61 }
62
63 //=======================================================================
64 //function : Execute
65 //purpose  :
66 //=======================================================================
67 Standard_Integer GEOMImpl_ThruSectionsDriver::Execute(Handle(TFunction_Logbook)& log) const
68 {
69   if (Label().IsNull()) return 0;
70   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
71
72   GEOMImpl_IThruSections aCI (aFunction);
73   Standard_Integer aType = aFunction->GetType();
74
75   Standard_Boolean isSolid = (aCI.GetSolidMode() == 1);
76   Handle(TColStd_HSequenceOfTransient) aSeqSections = aCI.GetSections();
77
78   if( aSeqSections.IsNull())
79     return 0;
80
81   Standard_Integer aNbSections = aSeqSections->Length();
82   Standard_Real aPreci = Max(aCI.GetPrecision(),Precision::Confusion());
83   if(!aNbSections )
84     return 0;
85
86   BRepOffsetAPI_ThruSections aBuilder(isSolid,aType ==THRUSECTIONS_RULED,aPreci);
87   
88  
89   aBuilder.CheckCompatibility(Standard_False);
90   //added sections for building surface
91   Standard_Integer i =1;
92   Standard_Integer nbAdded =0;
93   for( ; i <= aNbSections; i++,nbAdded++)
94   {
95     Handle(Standard_Transient) anItem = aSeqSections->Value(i);
96     if(anItem.IsNull())
97       continue;
98
99     Handle(GEOM_Function) aSection = Handle(GEOM_Function)::DownCast(anItem);
100     if(aSection.IsNull())
101       continue;
102
103     TopoDS_Shape aShapeSection = aSection->GetValue();
104     TopAbs_ShapeEnum aTypeSect = aShapeSection.ShapeType();
105     if(aTypeSect == TopAbs_WIRE)
106       aBuilder.AddWire(TopoDS::Wire(aShapeSection));
107
108     else if(aTypeSect == TopAbs_EDGE) {
109       TopoDS_Edge anEdge = TopoDS::Edge(aShapeSection);
110       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(anEdge);
111       aBuilder.AddWire(aWire);
112     }
113     else if(aTypeSect == TopAbs_VERTEX) {
114       TopoDS_Vertex aVert = TopoDS::Vertex(aShapeSection);
115       aBuilder.AddVertex(aVert);
116     }
117     else
118        nbAdded--; 
119   }  
120   if(!nbAdded)
121      Standard_TypeMismatch::Raise("ThruSections aborted : invalid types of sections");
122   //make surface by sections
123   aBuilder.Build();
124   TopoDS_Shape aShape = aBuilder.Shape();
125   if (aShape.IsNull()) {
126     return 0;
127   }
128
129   if ( !GEOMUtils::CheckShape(aShape) && !GEOMUtils::FixShapeTolerance(aShape) ) {
130     //algorithm through section creates on the arcs invalid shapes gka
131     //  Standard_ConstructionError::Raise("Algorithm has produced an invalid shape result");
132   }
133
134   aFunction->SetValue(aShape);
135
136   log->SetTouched(Label());
137
138   return 1;
139 }
140
141 //================================================================================
142 /*!
143  * \brief Returns a name of creation operation and names and values of creation parameters
144  */
145 //================================================================================
146
147 bool GEOMImpl_ThruSectionsDriver::
148 GetCreationInformation(std::string&             theOperationName,
149                        std::vector<GEOM_Param>& theParams)
150 {
151   if (Label().IsNull()) return 0;
152   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
153
154   GEOMImpl_IThruSections aCI( function );
155   Standard_Integer aType = function->GetType();
156
157   theOperationName = "MakeThruSections";
158
159   switch ( aType ) {
160   case THRUSECTIONS_RULED:
161   case THRUSECTIONS_SMOOTHED:
162     AddParam( theParams, "Sections", aCI.GetSections() );
163     AddParam( theParams, "Is solid", aCI.GetSolidMode() );
164     AddParam( theParams, "Precision", aCI.GetPrecision() );
165     AddParam( theParams, "Is ruled", aType == THRUSECTIONS_RULED );
166     break;
167   default:
168     return false;
169   }
170   
171   return true;
172 }
173
174 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ThruSectionsDriver,GEOM_BaseDriver)