1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <GEOMImpl_PrismDriver.hxx>
24 #include <GEOMImpl_IPrism.hxx>
25 #include <GEOMImpl_IShapesOperations.hxx>
26 #include <GEOMImpl_IMeasureOperations.hxx>
27 #include <GEOMImpl_GlueDriver.hxx>
28 #include <GEOMImpl_PipeDriver.hxx>
29 #include <GEOMImpl_Types.hxx>
30 #include <GEOM_Function.hxx>
32 #include <BRepPrimAPI_MakePrism.hxx>
34 #include <BRep_Builder.hxx>
35 #include <BRepBuilderAPI_MakeEdge.hxx>
36 #include <BRepBuilderAPI_MakeWire.hxx>
37 #include <BRepBuilderAPI_MakeVertex.hxx>
38 #include <BRepBuilderAPI_Sewing.hxx>
39 #include <BRepBuilderAPI_Transform.hxx>
40 #include <BRepCheck_Shell.hxx>
41 #include <BRepClass3d_SolidClassifier.hxx>
42 #include <BRep_Tool.hxx>
46 #include <TopExp_Explorer.hxx>
48 #include <TopoDS_Compound.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Shape.hxx>
51 #include <TopoDS_Shell.hxx>
52 #include <TopoDS_Solid.hxx>
53 #include <TopoDS_Vertex.hxx>
54 #include <TopTools_HSequenceOfShape.hxx>
55 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
57 #include <Precision.hxx>
61 #include <gp_Trsf.hxx>
63 #include <Standard_Stream.hxx>
65 #include <Standard_ConstructionError.hxx>
67 //=======================================================================
70 //=======================================================================
71 const Standard_GUID& GEOMImpl_PrismDriver::GetID()
73 static Standard_GUID aPrismDriver("FF1BBB17-5D14-4df2-980B-3A668264EA16");
78 //=======================================================================
79 //function : GEOMImpl_PrismDriver
81 //=======================================================================
82 GEOMImpl_PrismDriver::GEOMImpl_PrismDriver()
86 //=======================================================================
89 //=======================================================================
90 Standard_Integer GEOMImpl_PrismDriver::Execute(TFunction_Logbook& log) const
92 if (Label().IsNull()) return 0;
93 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
95 GEOMImpl_IPrism aCI (aFunction);
96 Standard_Integer aType = aFunction->GetType();
100 if (aType == PRISM_BASE_VEC_H || aType == PRISM_BASE_VEC_H_2WAYS) {
101 Handle(GEOM_Function) aRefBase = aCI.GetBase();
102 Handle(GEOM_Function) aRefVector = aCI.GetVector();
103 TopoDS_Shape aShapeBase = aRefBase->GetValue();
104 TopoDS_Shape aShapeVec = aRefVector->GetValue();
105 if (aShapeVec.ShapeType() == TopAbs_EDGE) {
106 TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
107 TopoDS_Vertex V1, V2;
108 TopExp::Vertices(anE, V1, V2, Standard_True);
109 if (!V1.IsNull() && !V2.IsNull()) {
110 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
111 if (Abs(aCI.GetH()) < Precision::Confusion()) {
112 Standard_ConstructionError::Raise("Absolute value of prism height is too small");
114 if (aV.Magnitude() > Precision::Confusion()) {
116 if (aType != PRISM_BASE_DXDYDZ_2WAYS && aCI.GetScale() > Precision::Confusion()) {
117 aShape = MakeScaledPrism(aShapeBase, aV * aCI.GetH(), aCI.GetScale());
120 if (aType == PRISM_BASE_VEC_H_2WAYS) {
122 aTrsf.SetTranslation((-aV) * aCI.GetH());
123 BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
124 aShapeBase = aTransformation.Shape();
125 aCI.SetH(aCI.GetH()*2);
127 aShape = BRepPrimAPI_MakePrism(aShapeBase, aV * aCI.GetH(), Standard_False).Shape();
132 } else if (aType == PRISM_BASE_TWO_PNT || aType == PRISM_BASE_TWO_PNT_2WAYS) {
133 Handle(GEOM_Function) aRefBase = aCI.GetBase();
134 Handle(GEOM_Function) aRefPnt1 = aCI.GetFirstPoint();
135 Handle(GEOM_Function) aRefPnt2 = aCI.GetLastPoint();
136 TopoDS_Shape aShapeBase = aRefBase->GetValue();
137 TopoDS_Shape aShapePnt1 = aRefPnt1->GetValue();
138 TopoDS_Shape aShapePnt2 = aRefPnt2->GetValue();
139 if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
140 aShapePnt2.ShapeType() == TopAbs_VERTEX) {
141 TopoDS_Vertex V1 = TopoDS::Vertex(aShapePnt1);
142 TopoDS_Vertex V2 = TopoDS::Vertex(aShapePnt2);
143 if (!V1.IsNull() && !V2.IsNull()) {
144 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
145 if (aV.Magnitude() > gp::Resolution()) {
146 if (aType != PRISM_BASE_DXDYDZ_2WAYS && aCI.GetScale() > Precision::Confusion()) {
147 aShape = MakeScaledPrism(aShapeBase, aV, aCI.GetScale());
150 if (aType == PRISM_BASE_TWO_PNT_2WAYS) {
152 aTrsf.SetTranslation(-aV);
153 BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
154 aShapeBase = aTransformation.Shape();
157 aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
162 } else if (aType == PRISM_BASE_DXDYDZ || aType == PRISM_BASE_DXDYDZ_2WAYS) {
163 Handle(GEOM_Function) aRefBase = aCI.GetBase();
164 TopoDS_Shape aShapeBase = aRefBase->GetValue();
165 gp_Vec aV (aCI.GetDX(), aCI.GetDY(), aCI.GetDZ());
166 if (aV.Magnitude() > gp::Resolution()) {
167 if (aType != PRISM_BASE_DXDYDZ_2WAYS && aCI.GetScale() > Precision::Confusion()) {
168 aShape = MakeScaledPrism(aShapeBase, aV, aCI.GetScale());
171 if (aType == PRISM_BASE_DXDYDZ_2WAYS) {
173 aTrsf.SetTranslation(-aV);
174 BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
175 aShapeBase = aTransformation.Shape();
178 aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
183 if (aShape.IsNull()) return 0;
185 TopoDS_Shape aRes = GEOMImpl_IShapesOperations::CompsolidToCompound(aShape);
186 aFunction->SetValue(aRes);
188 log.SetTouched(Label());
193 //=======================================================================
194 //function : MakeScaledPrism
196 //=======================================================================
197 TopoDS_Shape GEOMImpl_PrismDriver::MakeScaledPrism (const TopoDS_Shape& theShapeBase,
198 const gp_Vec& theVector,
199 const Standard_Real theScaleFactor,
200 const gp_Pnt& theCDG,
206 // 1. aCDG = geompy.MakeCDG(theBase)
207 gp_Pnt aCDG = theCDG;
209 gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(theShapeBase);
210 aCDG = aPos.Location();
212 TopoDS_Shape aShapeCDG_1 = BRepBuilderAPI_MakeVertex(aCDG).Shape();
214 // Process case of several given shapes
215 if (theShapeBase.ShapeType() == TopAbs_COMPOUND ||
216 theShapeBase.ShapeType() == TopAbs_SHELL) {
218 TopoDS_Shape aShapeI;
219 TopoDS_Compound aCompound;
220 B.MakeCompound(aCompound);
221 TopoDS_Iterator It (theShapeBase, Standard_True, Standard_True);
222 for (; It.More(); It.Next()) {
224 aShapeI = MakeScaledPrism(It.Value(), theVector, theScaleFactor, aCDG, true);
225 B.Add(aCompound, aShapeI);
230 aShape = GEOMImpl_GlueDriver::GlueFaces(aCompound, Precision::Confusion(), Standard_True);
234 // 2. Scale = geompy.MakeScaleTransform(theBase, aCDG, theScaleFactor)
236 // Bug 6839: Check for standalone (not included in faces) degenerated edges
237 TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
238 TopExp::MapShapesAndAncestors(theShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
239 Standard_Integer i, nbE = aEFMap.Extent();
240 for (i = 1; i <= nbE; i++) {
241 TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
242 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
243 const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
244 if (aFaces.IsEmpty())
245 Standard_ConstructionError::Raise
246 ("Scaling aborted : cannot scale standalone degenerated edge");
252 aTrsf.SetScale(aCDG, theScaleFactor);
253 BRepBuilderAPI_Transform aBRepTrsf (theShapeBase, aTrsf, Standard_False);
254 TopoDS_Shape aScale = aBRepTrsf.Shape();
256 // 3. aBase2 = geompy.MakeTranslationVectorDistance(Scale, theVec, theH)
258 aTrsf3.SetTranslation(theVector);
259 TopLoc_Location aLocOrig = aScale.Location();
260 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
261 TopLoc_Location aLocRes (aTrsf3 * aTrsfOrig);
262 TopoDS_Shape aBase2 = aScale.Located(aLocRes);
264 // 4. aCDG_2 = geompy.MakeTranslationVectorDistance(aCDG, theVec, theH)
265 gp_Pnt aCDG_2 = aCDG.Translated(theVector);
266 TopoDS_Shape aShapeCDG_2 = BRepBuilderAPI_MakeVertex(aCDG_2).Shape();
268 // 5. Vector = geompy.MakeVector(aCDG, aCDG_2)
269 TopoDS_Shape aShapeVec = BRepBuilderAPI_MakeEdge(aCDG, aCDG_2).Shape();
270 TopoDS_Edge anEdge = TopoDS::Edge(aShapeVec);
271 TopoDS_Wire aWirePath = BRepBuilderAPI_MakeWire(anEdge);
273 // 6. aPrism = geompy.MakePipeWithDifferentSections([theBase, aBase2], [aCDG, aCDG_2], Vector, False, False)
274 Handle(TopTools_HSequenceOfShape) aBases = new TopTools_HSequenceOfShape;
275 aBases->Append(theShapeBase);
276 aBases->Append(aBase2);
278 Handle(TopTools_HSequenceOfShape) aLocs = new TopTools_HSequenceOfShape;
279 aLocs->Append(aShapeCDG_1);
280 aLocs->Append(aShapeCDG_2);
282 aShape = GEOMImpl_PipeDriver::CreatePipeWithDifferentSections(aWirePath, aBases, aLocs, false, false);
284 // 7. Make a solid, if possible
285 if (theShapeBase.ShapeType() == TopAbs_FACE) {
286 BRepBuilderAPI_Sewing aSewing (Precision::Confusion()*10.0);
287 TopExp_Explorer expF (aShape, TopAbs_FACE);
288 Standard_Integer ifa = 0;
289 for (; expF.More(); expF.Next()) {
290 aSewing.Add(expF.Current());
297 TopoDS_Shape sh = aSewing.SewedShape();
298 if (sh.ShapeType() == TopAbs_FACE && ifa == 1) {
299 // case for creation of shell from one face
306 TopExp_Explorer exp (sh, TopAbs_SHELL);
307 Standard_Integer ish = 0;
308 for (; exp.More(); exp.Next()) {
309 aShell = exp.Current();
315 BRepCheck_Shell chkShell (TopoDS::Shell(aShell));
316 if (chkShell.Closed() == BRepCheck_NoError) {
320 BRepClass3d_SolidClassifier SC (Sol);
321 SC.PerformInfinitePoint(Precision::Confusion());
322 if (SC.State() == TopAbs_IN) {
324 B.Add(Sol, aShell.Reversed());
334 //=======================================================================
335 //function : GEOMImpl_PrismDriver_Type_
337 //=======================================================================
338 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PrismDriver_Type_()
341 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
342 if (aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
343 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
344 if (aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
345 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
346 if (aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
348 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
349 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PrismDriver",
350 sizeof(GEOMImpl_PrismDriver),
352 (Standard_Address)_Ancestors,
353 (Standard_Address)NULL);
358 //=======================================================================
359 //function : DownCast
361 //=======================================================================
362 const Handle(GEOMImpl_PrismDriver) Handle(GEOMImpl_PrismDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
364 Handle(GEOMImpl_PrismDriver) _anOtherObject;
366 if (!AnObject.IsNull()) {
367 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PrismDriver))) {
368 _anOtherObject = Handle(GEOMImpl_PrismDriver)((Handle(GEOMImpl_PrismDriver)&)AnObject);
372 return _anOtherObject;