]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_PrismDriver.cxx
Salome HOME
e5e82de53e5d2b9a71e676362d9d8f39bb9b4d85
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PrismDriver.cxx
1 //  Copyright (C) 2007-2010  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.
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 #include <GEOMImpl_PrismDriver.hxx>
23
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>
31
32 #include <BRepPrimAPI_MakePrism.hxx>
33
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>
43
44 #include <TopAbs.hxx>
45 #include <TopExp.hxx>
46 #include <TopExp_Explorer.hxx>
47 #include <TopoDS.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>
56
57 #include <Precision.hxx>
58 #include <gp_Ax3.hxx>
59 #include <gp_Pnt.hxx>
60 #include <gp_Vec.hxx>
61 #include <gp_Trsf.hxx>
62
63 #include <Standard_Stream.hxx>
64
65 #include <Standard_ConstructionError.hxx>
66
67 //=======================================================================
68 //function : GetID
69 //purpose  :
70 //=======================================================================
71 const Standard_GUID& GEOMImpl_PrismDriver::GetID()
72 {
73   static Standard_GUID aPrismDriver("FF1BBB17-5D14-4df2-980B-3A668264EA16");
74   return aPrismDriver;
75 }
76
77
78 //=======================================================================
79 //function : GEOMImpl_PrismDriver
80 //purpose  :
81 //=======================================================================
82 GEOMImpl_PrismDriver::GEOMImpl_PrismDriver()
83 {
84 }
85
86 //=======================================================================
87 //function : Execute
88 //purpose  :
89 //=======================================================================
90 Standard_Integer GEOMImpl_PrismDriver::Execute(TFunction_Logbook& log) const
91 {
92   if (Label().IsNull()) return 0;
93   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
94
95   GEOMImpl_IPrism aCI (aFunction);
96   Standard_Integer aType = aFunction->GetType();
97
98   TopoDS_Shape aShape;
99
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");
113         }
114         if (aV.Magnitude() > Precision::Confusion()) {
115           aV.Normalize();
116           if (aType != PRISM_BASE_DXDYDZ_2WAYS && aCI.GetScale() > Precision::Confusion()) {
117             aShape = MakeScaledPrism(aShapeBase, aV * aCI.GetH(), aCI.GetScale());
118           }
119           else {
120             if (aType == PRISM_BASE_VEC_H_2WAYS) {
121               gp_Trsf aTrsf;
122               aTrsf.SetTranslation((-aV) * aCI.GetH());
123               BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
124               aShapeBase = aTransformation.Shape();
125               aCI.SetH(aCI.GetH()*2);
126             }
127             aShape = BRepPrimAPI_MakePrism(aShapeBase, aV * aCI.GetH(), Standard_False).Shape();
128           }
129         }
130       }
131     }
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());
148           }
149           else {
150             if (aType == PRISM_BASE_TWO_PNT_2WAYS) {
151               gp_Trsf aTrsf;
152               aTrsf.SetTranslation(-aV);
153               BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
154               aShapeBase = aTransformation.Shape();
155               aV = aV * 2;
156             }
157             aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
158           }
159         }
160       }
161     }
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());
169       }
170       else {
171         if (aType == PRISM_BASE_DXDYDZ_2WAYS) {
172           gp_Trsf aTrsf;
173           aTrsf.SetTranslation(-aV);
174           BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
175           aShapeBase = aTransformation.Shape();
176           aV = aV * 2;
177         }
178         aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
179       }
180     }
181   }
182
183   if (aShape.IsNull()) return 0;
184
185   TopoDS_Shape aRes = GEOMImpl_IShapesOperations::CompsolidToCompound(aShape);
186   aFunction->SetValue(aRes);
187
188   log.SetTouched(Label());
189
190   return 1;
191 }
192
193 //=======================================================================
194 //function : MakeScaledPrism
195 //purpose  :
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,
201                                                     bool                isCDG)
202 {
203   TopoDS_Shape aShape;
204   BRep_Builder B;
205
206   // 1. aCDG = geompy.MakeCDG(theBase)
207   gp_Pnt aCDG = theCDG;
208   if (!isCDG) {
209     gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(theShapeBase);
210     aCDG = aPos.Location();
211   }
212   TopoDS_Shape aShapeCDG_1 = BRepBuilderAPI_MakeVertex(aCDG).Shape();
213
214   // Process case of several given shapes
215   if (theShapeBase.ShapeType() == TopAbs_COMPOUND ||
216       theShapeBase.ShapeType() == TopAbs_SHELL) {
217     int nbSub = 0;
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()) {
223       nbSub++;
224       aShapeI = MakeScaledPrism(It.Value(), theVector, theScaleFactor, aCDG, true);
225       B.Add(aCompound, aShapeI);
226     }
227     if (nbSub == 1)
228       aShape = aShapeI;
229     else if (nbSub > 1)
230       aShape = GEOMImpl_GlueDriver::GlueFaces(aCompound, Precision::Confusion(), Standard_True);
231     return aShape;
232   }
233
234   // 2. Scale = geompy.MakeScaleTransform(theBase, aCDG, theScaleFactor)
235
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");
247     }
248   }
249
250   // Perform Scaling
251   gp_Trsf aTrsf;
252   aTrsf.SetScale(aCDG, theScaleFactor);
253   BRepBuilderAPI_Transform aBRepTrsf (theShapeBase, aTrsf, Standard_False);
254   TopoDS_Shape aScale = aBRepTrsf.Shape();
255
256   // 3. aBase2 = geompy.MakeTranslationVectorDistance(Scale, theVec, theH)
257   gp_Trsf aTrsf3;
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);
263
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();
267
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);
272
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);
277
278   Handle(TopTools_HSequenceOfShape) aLocs = new TopTools_HSequenceOfShape;
279   aLocs->Append(aShapeCDG_1);
280   aLocs->Append(aShapeCDG_2);
281
282   aShape = GEOMImpl_PipeDriver::CreatePipeWithDifferentSections(aWirePath, aBases, aLocs, false, false);
283
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());
291       ifa++;
292     }
293     if (ifa > 0) {
294       aSewing.Perform();
295       TopoDS_Shape aShell;
296
297       TopoDS_Shape sh = aSewing.SewedShape();
298       if (sh.ShapeType() == TopAbs_FACE && ifa == 1) {
299         // case for creation of shell from one face
300         TopoDS_Shell ss;
301         B.MakeShell(ss);
302         B.Add(ss,sh);
303         aShell = ss;
304       }
305       else {
306         TopExp_Explorer exp (sh, TopAbs_SHELL);
307         Standard_Integer ish = 0;
308         for (; exp.More(); exp.Next()) {
309           aShell = exp.Current();
310           ish++;
311         }
312         if (ish != 1)
313           aShell = sh;
314       }
315       BRepCheck_Shell chkShell (TopoDS::Shell(aShell));
316       if (chkShell.Closed() == BRepCheck_NoError) {
317         TopoDS_Solid Sol;
318         B.MakeSolid(Sol);
319         B.Add(Sol, aShell);
320         BRepClass3d_SolidClassifier SC (Sol);
321         SC.PerformInfinitePoint(Precision::Confusion());
322         if (SC.State() == TopAbs_IN) {
323           B.MakeSolid(Sol);
324           B.Add(Sol, aShell.Reversed());
325         }
326         aShape = Sol;
327       }
328     }
329   }
330
331   return aShape;
332 }
333
334 //=======================================================================
335 //function : GEOMImpl_PrismDriver_Type_
336 //purpose  :
337 //=======================================================================
338 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PrismDriver_Type_()
339 {
340
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);
347
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),
351                                                          1,
352                                                          (Standard_Address)_Ancestors,
353                                                          (Standard_Address)NULL);
354
355   return _aType;
356 }
357
358 //=======================================================================
359 //function : DownCast
360 //purpose  :
361 //=======================================================================
362 const Handle(GEOMImpl_PrismDriver) Handle(GEOMImpl_PrismDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
363 {
364   Handle(GEOMImpl_PrismDriver) _anOtherObject;
365
366   if (!AnObject.IsNull()) {
367      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PrismDriver))) {
368        _anOtherObject = Handle(GEOMImpl_PrismDriver)((Handle(GEOMImpl_PrismDriver)&)AnObject);
369      }
370   }
371
372   return _anOtherObject;
373 }