1 // Copyright (C) 2007-2011 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
23 #include <Standard_Stream.hxx>
25 #include <BRepOffsetAPI_MakeFilling.hxx>
27 #include <GEOMImpl_BlockDriver.hxx>
28 #include <GEOMImpl_IBlocks.hxx>
29 #include <GEOMImpl_IBlockTrsf.hxx>
30 #include <GEOMImpl_GlueDriver.hxx>
31 #include <GEOMImpl_Types.hxx>
32 #include <GEOMImpl_ILocalOperations.hxx>
33 #include <GEOMImpl_Block6Explorer.hxx>
34 #include <GEOMImpl_IBlocksOperations.hxx>
36 #include <GEOM_Function.hxx>
38 #include <ShHealOper_Sewing.hxx>
39 #include <ShHealOper_ShapeProcess.hxx>
40 //#include <GEOMAlgo_Gluer.hxx>
41 #include <BlockFix_BlockFixAPI.hxx>
43 #include "utilities.h"
45 #include <TNaming_CopyShape.hxx>
47 #include <BRepAdaptor_Curve.hxx>
48 #include <BRepLib.hxx>
49 #include <BRep_Tool.hxx>
50 #include <BRepTools.hxx>
51 #include <BRepGProp.hxx>
52 #include <BRep_Builder.hxx>
53 #include <BRepTools_Quilt.hxx>
54 #include <BRepTools_WireExplorer.hxx>
55 #include <BRepBuilderAPI_MakeEdge.hxx>
56 #include <BRepBuilderAPI_MakeWire.hxx>
57 #include <BRepBuilderAPI_MakePolygon.hxx>
58 #include <BRepBuilderAPI_Transform.hxx>
59 #include <BRepCheck_Analyzer.hxx>
60 #include <BRepClass_FaceClassifier.hxx>
61 #include <BRepClass3d_SolidClassifier.hxx>
62 #include <BRepExtrema_ExtPF.hxx>
63 #include <BRepExtrema_DistShapeShape.hxx>
67 #include <TopoDS_Shape.hxx>
68 #include <TopoDS_Edge.hxx>
69 #include <TopoDS_Wire.hxx>
70 #include <TopoDS_Shell.hxx>
71 #include <TopoDS_Solid.hxx>
72 #include <TopoDS_Compound.hxx>
73 #include <TopoDS_Iterator.hxx>
75 #include <TopExp_Explorer.hxx>
76 #include <TopTools_MapOfShape.hxx>
77 #include <TopTools_MapIteratorOfMapOfShape.hxx>
78 #include <TopTools_Array1OfShape.hxx>
79 #include <TopTools_SequenceOfShape.hxx>
80 #include <TopTools_ListOfShape.hxx>
81 #include <TopTools_ListIteratorOfListOfShape.hxx>
82 #include <TopTools_IndexedMapOfShape.hxx>
83 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
85 #include <GProp_GProps.hxx>
87 #include <Geom_Line.hxx>
88 #include <GC_MakeLine.hxx>
93 #include <Precision.hxx>
94 #include <TColgp_Array1OfPnt.hxx>
95 #include <TColStd_Array1OfInteger.hxx>
96 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
97 #include <StdFail_NotDone.hxx>
98 #include <Standard_NullObject.hxx>
99 #include <Standard_TypeMismatch.hxx>
100 #include <Standard_ConstructionError.hxx>
102 //=======================================================================
105 //=======================================================================
106 const Standard_GUID& GEOMImpl_BlockDriver::GetID()
108 static Standard_GUID aBlockDriver("FF1BBB67-5D14-4df2-980B-3A668264EA16");
113 //=======================================================================
114 //function : GEOMImpl_BlockDriver
116 //=======================================================================
117 GEOMImpl_BlockDriver::GEOMImpl_BlockDriver()
121 //=======================================================================
124 //=======================================================================
125 Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
127 if (Label().IsNull()) return 0;
128 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
130 Standard_Integer aType = aFunction->GetType();
135 Standard_Real prec = Precision::Confusion();
137 Standard_Integer aNbSub = 0;
138 if (aType == BLOCK_COMPOUND_GLUE) {
142 } else if (aType == BLOCK_FACE_TWO_EDGES ||
143 aType == BLOCK_TWO_FACES) {
147 } else if (aType == BLOCK_FACE_FOUR_PNT ||
148 aType == BLOCK_FACE_FOUR_EDGES) {
152 } else if (aType == BLOCK_SIX_FACES) {
160 GEOMImpl_IBlocks aCI (aFunction);
161 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
162 Standard_Integer nbshapes = aShapes->Length();
164 if (nbshapes != aNbSub) {
165 Standard_TypeMismatch::Raise
166 ("Number of elements for object construction does not correspond to the used constructor");
169 TopTools_Array1OfShape anArgs (1, aNbSub);
170 Standard_Integer argi;
171 for (argi = 1; argi <= aNbSub; argi++) {
172 Handle(GEOM_Function) aRef = Handle(GEOM_Function)::DownCast(aShapes->Value(argi));
173 TopoDS_Shape anArg = aRef->GetValue();
174 if (anArg.IsNull()) {
175 Standard_NullObject::Raise("Null shape is given as argument");
177 anArgs(argi) = anArg;
180 if (aType == BLOCK_FACE_FOUR_EDGES) {
182 // Make face from four edges
183 if (anArgs(1).ShapeType() != TopAbs_EDGE || anArgs(2).ShapeType() != TopAbs_EDGE ||
184 anArgs(3).ShapeType() != TopAbs_EDGE || anArgs(4).ShapeType() != TopAbs_EDGE) {
185 Standard_TypeMismatch::Raise("Shape for face construction is not an edge");
188 // count corner vertices
189 TopTools_MapOfShape aVertMap;
190 for (Standard_Integer ii = 1; ii <= 4; ii++) {
191 TopoDS_Edge anEdge = TopoDS::Edge(anArgs(ii));
192 TopoDS_Vertex V1, V2;
193 TopExp::Vertices(anEdge, V1, V2, Standard_True);
194 if (V1.IsNull() || V2.IsNull()) {
195 Standard_NullObject::Raise("Bad edge for face construction: vertex is not defined");
197 if (BRepTools::Compare(V1,V2)) {
198 Standard_ConstructionError::Raise("Edge ends are too close");
200 Standard_Boolean isCoin1 = Standard_False, isCoin2 = Standard_False;
201 TopTools_MapIteratorOfMapOfShape anIter (aVertMap);
202 for (; anIter.More(); anIter.Next()) {
203 TopoDS_Vertex V = TopoDS::Vertex(anIter.Key());
204 if (BRepTools::Compare(V,V1)) isCoin1 = Standard_True;
205 if (BRepTools::Compare(V,V2)) isCoin2 = Standard_True;
207 if (!isCoin1) aVertMap.Add(V1);
208 if (!isCoin2) aVertMap.Add(V2);
210 if (aVertMap.Extent() != 4) {
211 Standard_ConstructionError::Raise("The edges must form a closed wire");
214 TopoDS_Edge anEdge1 = TopoDS::Edge(anArgs(1));
215 TopoDS_Edge anEdge2 = TopoDS::Edge(anArgs(2));
216 TopoDS_Edge anEdge3 = TopoDS::Edge(anArgs(3));
217 TopoDS_Edge anEdge4 = TopoDS::Edge(anArgs(4));
219 // check, if anEdge1 has common/coincident vertex with anEdge2,
220 Standard_Boolean isConnected12 = Standard_False;
221 TopoDS_Vertex V11, V12, V21, V22;
222 TopExp::Vertices(anEdge1, V11, V12, Standard_True);
223 TopExp::Vertices(anEdge2, V21, V22, Standard_True);
224 if (BRepTools::Compare(V11,V21) || BRepTools::Compare(V11,V22) ||
225 BRepTools::Compare(V12,V21) || BRepTools::Compare(V12,V22)) {
226 // the edges have common vertex
227 isConnected12 = Standard_True;
230 // build wire in right order, corresponding to edges connexity
231 BRepBuilderAPI_MakeWire* MW;
233 MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge2, anEdge3, anEdge4);
235 MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge3, anEdge2, anEdge4);
238 Standard_ConstructionError::Raise
239 ("Impossible to build a connected wire from the given edges");
241 TopoDS_Wire aWire = *MW;
244 // check the wire closure
245 TopoDS_Vertex aV1, aV2;
246 TopExp::Vertices(aWire, aV1, aV2);
247 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
248 aWire.Closed( true );
250 if (!aWire.Closed()) {
251 Standard_ConstructionError::Raise
252 ("Impossible to build a closed wire from the given edges");
255 // try to build face on the wire
256 GEOMImpl_Block6Explorer::MakeFace(aWire, Standard_False, aShape);
257 if (aShape.IsNull()) {
258 Standard_ConstructionError::Raise("Face construction failed");
261 } else if (aType == BLOCK_FACE_TWO_EDGES) {
263 // Make face from two opposite edges
264 if (anArgs(1).ShapeType() != TopAbs_EDGE ||
265 anArgs(2).ShapeType() != TopAbs_EDGE) {
266 Standard_TypeMismatch::Raise("Shape for face construction is not an edge");
269 TopoDS_Edge anEdge1 = TopoDS::Edge(anArgs(1));
270 TopoDS_Edge anEdge2 = TopoDS::Edge(anArgs(2));
272 if (anEdge1.IsSame(anEdge2)) {
273 Standard_ConstructionError::Raise("The edges must be different");
276 // create two edges, linking ends of the given edges
277 TopoDS_Vertex V11, V12, V21, V22;
278 TopExp::Vertices(anEdge1, V11, V12, Standard_False);
279 TopExp::Vertices(anEdge2, V21, V22, Standard_False);
280 if (V11.IsNull() || V12.IsNull() ||
281 V21.IsNull() || V22.IsNull()) {
282 Standard_NullObject::Raise("Bad edge for face construction: vertex is not defined");
285 BRepAdaptor_Curve C1 (anEdge1);
286 BRepAdaptor_Curve C2 (anEdge2);
287 gp_Pnt P11, P12, P21, P22;
289 // Mantis issue 0020599: Creation of a quadrangle face from 2 edges: SIGSEGV
290 P11 = C1.Value(C1.FirstParameter());
291 P12 = C1.Value(C1.LastParameter());
292 P21 = C2.Value(C2.FirstParameter());
293 P22 = C2.Value(C2.LastParameter());
294 //gp_Pnt P11 = BRep_Tool::Pnt(V11);
295 //gp_Pnt P12 = BRep_Tool::Pnt(V12);
296 //gp_Pnt P21 = BRep_Tool::Pnt(V21);
297 //gp_Pnt P22 = BRep_Tool::Pnt(V22);
299 if (P11.Distance(P21) < prec || P12.Distance(P22) < prec ||
300 P11.Distance(P22) < prec || P12.Distance(P21) < prec) {
301 Standard_ConstructionError::Raise("Given edges have too close ends");
304 Standard_Real per11 = P11.Distance(P21) + P12.Distance(P22);
305 Standard_Real per12 = P11.Distance(P22) + P12.Distance(P21);
312 // Mantis issue 0020599: Creation of a quadrangle face from 2 edges: SIGSEGV
314 Handle(Geom_Line) Line1 = GC_MakeLine(P11, P21).Value();
315 Handle(Geom_Line) Line2 = GC_MakeLine(P12, P22).Value();
317 BB.MakeEdge(anEdge3, Line1, Precision::Confusion());
318 BB.Range(anEdge3, 0., P11.Distance(P21));
319 BB.Add(anEdge3, V11.Oriented(TopAbs_FORWARD));
320 BB.Add(anEdge3, V21.Oriented(TopAbs_REVERSED));
322 BB.MakeEdge(anEdge4, Line2, Precision::Confusion());
323 BB.Range(anEdge4, 0., P12.Distance(P22));
324 BB.Add(anEdge4, V12.Oriented(TopAbs_FORWARD));
325 BB.Add(anEdge4, V22.Oriented(TopAbs_REVERSED));
328 Handle(Geom_Line) Line1 = GC_MakeLine(P11, P22).Value();
329 Handle(Geom_Line) Line2 = GC_MakeLine(P12, P21).Value();
331 BB.MakeEdge(anEdge3, Line1, Precision::Confusion());
332 BB.Range(anEdge3, 0., P11.Distance(P22));
333 BB.Add(anEdge3, V11.Oriented(TopAbs_FORWARD));
334 BB.Add(anEdge3, V22.Oriented(TopAbs_REVERSED));
336 BB.MakeEdge(anEdge4, Line2, Precision::Confusion());
337 BB.Range(anEdge4, 0., P12.Distance(P21));
338 BB.Add(anEdge4, V12.Oriented(TopAbs_FORWARD));
339 BB.Add(anEdge4, V21.Oriented(TopAbs_REVERSED));
341 //if (per11 < per12) {
342 // anEdge3 = BRepBuilderAPI_MakeEdge(V11, V21);
343 // anEdge4 = BRepBuilderAPI_MakeEdge(V12, V22);
345 // anEdge3 = BRepBuilderAPI_MakeEdge(V11, V22);
346 // anEdge4 = BRepBuilderAPI_MakeEdge(V12, V21);
350 BRepBuilderAPI_MakeWire* MW;
351 MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge3, anEdge2, anEdge4);
353 Standard_ConstructionError::Raise("Wire construction failed");
356 TopoDS_Wire aWire = *MW;
359 TopoDS_Vertex aV1, aV2;
360 TopExp::Vertices(aWire, aV1, aV2);
361 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
362 aWire.Closed( true );
364 if (!aWire.Closed()) {
365 Standard_ConstructionError::Raise
366 ("Impossible to build a closed wire from the given edges");
369 // try to build face on the wire
370 GEOMImpl_Block6Explorer::MakeFace(aWire, Standard_False, aShape);
371 if (aShape.IsNull()) {
372 Standard_ConstructionError::Raise("Face construction failed");
375 } else if (aType == BLOCK_FACE_FOUR_PNT) {
377 // Make face from four corner vertices
378 if (anArgs(1).ShapeType() != TopAbs_VERTEX ||
379 anArgs(2).ShapeType() != TopAbs_VERTEX ||
380 anArgs(3).ShapeType() != TopAbs_VERTEX ||
381 anArgs(4).ShapeType() != TopAbs_VERTEX) {
382 Standard_TypeMismatch::Raise("Shape for face construction is not a vertex");
385 TopoDS_Vertex V1 = TopoDS::Vertex(anArgs(1));
386 TopoDS_Vertex V2 = TopoDS::Vertex(anArgs(2));
387 TopoDS_Vertex V3 = TopoDS::Vertex(anArgs(3));
388 TopoDS_Vertex V4 = TopoDS::Vertex(anArgs(4));
390 gp_Pnt P1 = BRep_Tool::Pnt(V1);
391 gp_Pnt P2 = BRep_Tool::Pnt(V2);
392 gp_Pnt P3 = BRep_Tool::Pnt(V3);
393 gp_Pnt P4 = BRep_Tool::Pnt(V4);
395 if (P1.Distance(P2) < prec || P1.Distance(P3) < prec ||
396 P1.Distance(P4) < prec || P2.Distance(P3) < prec ||
397 P2.Distance(P4) < prec || P3.Distance(P4) < prec) {
398 Standard_ConstructionError::Raise("Four not coincident points must be given");
401 // calculate perimeters
402 Standard_Real per1234 = P1.Distance(P2) + P2.Distance(P3) +
403 P3.Distance(P4) + P4.Distance(P1);
404 Standard_Real per1243 = P1.Distance(P2) + P2.Distance(P4) +
405 P4.Distance(P3) + P3.Distance(P1);
406 Standard_Real per1324 = P1.Distance(P3) + P3.Distance(P2) +
407 P2.Distance(P4) + P4.Distance(P1);
410 if (per1243 < per1234 && per1243 < per1324) {
411 TopoDS_Vertex Vtmp = V3;
414 } else if (per1324 < per1234 && per1324 < per1243) {
415 TopoDS_Vertex Vtmp = V3;
422 BRepBuilderAPI_MakePolygon aMkPoly (V1, V2, V3, V4, Standard_True);
423 if (!aMkPoly.IsDone()) {
424 Standard_ConstructionError::Raise("Polygon construction failed");
427 // try to build face on the wire
429 GEOMImpl_Block6Explorer::MakeFace(aMkPoly, Standard_False, aShape);
430 if (aShape.IsNull()) {
431 Standard_ConstructionError::Raise("Face construction failed");
434 } else if (aType == BLOCK_SIX_FACES || aType == BLOCK_TWO_FACES) {
436 BRepTools_Quilt Glue;
438 if (aType == BLOCK_SIX_FACES) {
439 // Make block (hexahedral solid) from six faces
440 for (Standard_Integer ind = 1; ind <= nbshapes; ind++) {
441 if (anArgs(ind).ShapeType() != TopAbs_FACE) {
442 Standard_TypeMismatch::Raise("Shape for block construction is not a face");
444 Glue.Add(anArgs(ind));
449 // Make block (hexahedral solid) from two opposite faces
450 if (anArgs(1).ShapeType() != TopAbs_FACE ||
451 anArgs(2).ShapeType() != TopAbs_FACE) {
452 Standard_TypeMismatch::Raise("Shape for block construction is not a face");
455 // Get wires of the given faces
456 TopExp_Explorer wires1 (anArgs(1), TopAbs_WIRE);
457 TopExp_Explorer wires2 (anArgs(2), TopAbs_WIRE);
458 if (!wires1.More() || !wires2.More()) {
459 Standard_ConstructionError::Raise("A face for the block has no wires");
461 TopoDS_Shape aWire1 = wires1.Current();
462 TopoDS_Shape aWire2 = wires2.Current();
465 if (wires1.More() || wires2.More()) {
466 Standard_ConstructionError::Raise("A face for the block has more than one wire");
469 GEOMImpl_Block6Explorer aBlockTool;
470 aBlockTool.InitByTwoFaces(anArgs(1), anArgs(2));
472 // Construct the linking faces and add them in the gluing tool
474 Glue.Add(aBlockTool.GetFace(3, Standard_True));
475 Glue.Add(aBlockTool.GetFace(4, Standard_True));
476 Glue.Add(aBlockTool.GetFace(5, Standard_True));
477 Glue.Add(aBlockTool.GetFace(6, Standard_True));
481 TopExp_Explorer exp (Glue.Shells(), TopAbs_SHELL);
482 Standard_Integer ish = 0;
483 TopTools_MapOfShape mapShape;
484 for (; exp.More(); exp.Next()) {
485 if (mapShape.Add(exp.Current())) {
486 aShape = exp.Current();
492 aShape = Glue.Shells();
493 Standard_Real aTol = prec; // Precision::Confusion()
494 TopExp_Explorer expF (aShape, TopAbs_FACE);
495 TopTools_MapOfShape mapF;
498 for (; expF.More(); expF.Next()) {
499 if (mapF.Add(expF.Current())) {
500 B.Add(Shell, expF.Current());
501 Standard_Real aToler = BRep_Tool::Tolerance(TopoDS::Face(expF.Current()));
506 ShHealOper_Sewing aHealer (Shell, aTol);
507 if (aHealer.Perform())
508 aShape = aHealer.GetResultShape();
510 Standard_ConstructionError::Raise
511 ("Impossible to build a connected shell on the given faces");
514 if (aType == BLOCK_SIX_FACES) {
515 if (!aShape.Closed()) {
516 Standard_ConstructionError::Raise
517 ("Impossible to build a closed shell on the given faces");
524 BRepClass3d_SolidClassifier SC (Sol);
525 SC.PerformInfinitePoint(prec);
526 if (SC.State() == TopAbs_IN) {
528 B.Add(Sol, aShape.Reversed());
531 BRepLib::SameParameter(aShape, 1.E-5, Standard_True);
533 else if (aType == BLOCK_COMPOUND_GLUE) {
534 // Make blocks compound from a compound
535 if (anArgs(1).ShapeType() != TopAbs_COMPOUND &&
536 anArgs(2).ShapeType() != TopAbs_COMPSOLID) {
537 Standard_TypeMismatch::Raise("Not a compound given");
540 TopoDS_Shape aCompound = anArgs(1);
542 // Glue coincident faces and edges
543 aShape = GEOMImpl_GlueDriver::GlueFaces(aCompound, Precision::Confusion(), Standard_True);
544 //GEOMAlgo_Gluer aGluer;
545 //aGluer.SetShape(aCompound);
546 //aGluer.SetCheckGeometry(Standard_True);
548 //aShape = aGluer.Result();
553 } else { // Multi-transformations and compound improving
555 if (aType == BLOCK_REMOVE_EXTRA ||
556 aType == BLOCK_COMPOUND_IMPROVE) {
558 GEOMImpl_IBlockTrsf aCI (aFunction);
559 Handle(GEOM_Function) aRefShape = aCI.GetOriginal();
560 TopoDS_Shape aBlockOrComp = aRefShape->GetValue();
561 if (aBlockOrComp.IsNull()) {
562 Standard_NullObject::Raise("Null Shape given");
565 // 1. Improve solids with seam and/or degenerated edges
566 BlockFix_BlockFixAPI aTool;
567 //aTool.Tolerance() = toler;
568 aTool.OptimumNbFaces() = aCI.GetOptimumNbFaces();
569 aTool.SetShape(aBlockOrComp);
572 TopoDS_Shape aFixedExtra = aTool.Shape();
575 BRepCheck_Analyzer ana (aFixedExtra, false);
576 if (!ana.IsValid()) {
578 ShHealOper_ShapeProcess aHealer;
579 aHealer.Perform(aFixedExtra, aFixed);
580 if (aHealer.isDone())
581 aFixedExtra = aFixed;
584 if (aType == BLOCK_REMOVE_EXTRA)
586 aShape = aFixedExtra;
588 if (aShape == aBlockOrComp) {
589 MESSAGE("No modifications have been done");
592 else // aType == BLOCK_COMPOUND_IMPROVE
594 // 2. Separate non-blocks
595 TopTools_ListOfShape BLO; // All blocks from the given compound
596 TopTools_ListOfShape NOT; // Not blocks
597 TopTools_ListOfShape EXT; // Hexahedral solids, having degenerated and/or seam edges
598 GEOMImpl_IBlocksOperations::AddBlocksFrom(aFixedExtra, BLO, NOT, EXT);
600 if (NOT.Extent() > 0) {
601 MESSAGE("Some non-blocks have been removed");
604 // 3. Warn about staying extra-edges
605 if (EXT.Extent() > 0) {
606 MESSAGE("Warning: Not all seam or degenerated edges was removed");
609 // ??? Throw away standalone blocks ???
611 // 4. Create compound of all blocks
612 TopoDS_Compound aComp;
614 BB.MakeCompound(aComp);
615 TopTools_ListIteratorOfListOfShape BLOit (BLO);
616 for (; BLOit.More(); BLOit.Next()) {
617 BB.Add(aComp, BLOit.Value());
621 aShape = GEOMImpl_GlueDriver::GlueFaces(aComp, Precision::Confusion(), Standard_False);
624 } else if (aType == BLOCK_MULTI_TRANSFORM_1D ||
625 aType == BLOCK_MULTI_TRANSFORM_2D) {
628 GEOMImpl_IBlockTrsf aCI (aFunction);
629 Handle(GEOM_Function) aRefShape = aCI.GetOriginal();
630 TopoDS_Shape aBlockIni = aRefShape->GetValue();
631 if (aBlockIni.IsNull()) {
632 Standard_NullObject::Raise("Null Block");
635 // Copy block to avoid problems (PAL6706)
636 TColStd_IndexedDataMapOfTransientTransient aMap;
638 TNaming_CopyShape::CopyTool(aBlockIni, aMap, aBlock);
640 // Block tolerance in vertices
641 Standard_Real aTol = prec;
642 TopExp_Explorer expV (aBlock, TopAbs_VERTEX);
643 TopTools_MapOfShape mapShape;
644 for (; expV.More(); expV.Next()) {
645 if (mapShape.Add(expV.Current())) {
646 TopoDS_Vertex aV = TopoDS::Vertex(expV.Current());
647 aTol = Max(BRep_Tool::Tolerance(aV), aTol);
651 if (aType == BLOCK_MULTI_TRANSFORM_1D) {
652 // Retrieve a faces by Ids
653 Standard_Integer aFace1Id = aCI.GetFace1U();
654 Standard_Integer aFace2Id = aCI.GetFace2U();
655 TopoDS_Shape aFace1, aFace2;
656 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1Id, aFace1)) {
657 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
659 if (aFace1.ShapeType() != TopAbs_FACE) {
660 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
664 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2Id, aFace2)) {
665 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
667 if (aFace2.ShapeType() != TopAbs_FACE) {
668 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
672 Standard_Integer aNbIter = aCI.GetNbIterU();
674 MultiTransformate1D(aBlock, aFace1, aFace2, aNbIter, aMulti);
676 } else { // aType == BLOCK_MULTI_TRANSFORM_2D
677 // Retrieve a faces by Ids
678 Standard_Integer aFace1UId = aCI.GetFace1U();
679 Standard_Integer aFace2UId = aCI.GetFace2U();
680 Standard_Integer aFace1VId = aCI.GetFace1V();
681 Standard_Integer aFace2VId = aCI.GetFace2V();
683 TopoDS_Shape aFace1U, aFace2U, aFace1V, aFace2V;
684 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1UId, aFace1U) ||
685 !GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1VId, aFace1V)) {
686 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
689 if (aFace1U.ShapeType() != TopAbs_FACE ||
690 aFace1V.ShapeType() != TopAbs_FACE) {
691 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
695 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2UId, aFace2U)) {
696 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
699 if (aFace2U.ShapeType() != TopAbs_FACE) {
700 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
705 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2VId, aFace2V)) {
706 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
709 if (aFace2V.ShapeType() != TopAbs_FACE) {
710 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
714 Standard_Integer aNbIterU = aCI.GetNbIterU();
715 Standard_Integer aNbIterV = aCI.GetNbIterV();
717 MultiTransformate2D(aBlock,
718 aFace1U, aFace2U, aNbIterU,
719 aFace1V, aFace2V, aNbIterV, aMulti);
722 if (aMulti.IsNull()) {
723 StdFail_NotDone::Raise("Multi-transformation failed");
726 // Glue faces of the multi-block
727 aShape = GEOMImpl_GlueDriver::GlueFaces(aMulti, aTol, Standard_False);
729 } else { // unknown function type
734 if (aShape.IsNull()) return 0;
736 aFunction->SetValue(aShape);
738 log.SetTouched(Label());
743 //=======================================================================
744 //function : MultiTransformate1D
746 //=======================================================================
747 void GEOMImpl_BlockDriver::MultiTransformate1D (const TopoDS_Shape& theBlock,
748 const TopoDS_Shape& theFace1,
749 const TopoDS_Shape& theFace2,
750 const Standard_Integer theNbIter,
751 TopoDS_Shape& theResult) const
753 // Construct Tool, where <theFace1> will be the first face,
754 // and a face, opposite to <theFace1>, will be the second face
755 GEOMImpl_Block6Explorer aBlockTool;
756 aBlockTool.InitByBlockAndFace(theBlock, theFace1);
758 // Find IDs of the faces
759 Standard_Integer dir_face1 = 1, dir_face2 = 2;
760 if (!theFace2.IsNull()) {
761 dir_face2 = aBlockTool.GetFaceID(theFace2);
764 // Find three pairs of points
765 Standard_Integer v11_id = 0, v12_id = 0, v13_id = 0; // vertices of the first face
766 Standard_Integer v21_id = 0, v22_id = 0, v23_id = 0; // vertices of the second face
768 if (dir_face2 == 2) { // <theFace2> is opposite to <theFace1>
770 // We will take vertices with equal local numbers on the faces,
771 // as the Block6Explorer gives equal local numbers
772 // to the linked vertices on the opposite faces,
773 // i.e. v1* is linked with the v2* by an edge:
777 // / | / | dir_face1 - bottom
779 // /________ / | dir_face2 - top
788 v11_id = aBlockTool.GetVertexID(dir_face1, 1);
789 v12_id = aBlockTool.GetVertexID(dir_face1, 2);
790 v13_id = aBlockTool.GetVertexID(dir_face1, 4);
792 v21_id = aBlockTool.GetVertexID(dir_face2, 1);
793 v22_id = aBlockTool.GetVertexID(dir_face2, 2);
794 v23_id = aBlockTool.GetVertexID(dir_face2, 4);
800 // / | / | dir_face1 - bottom
802 // /________ / | dir_face2 - right (for example)
805 // | / | /v12 = v22 (common_vertex2)
809 // v13 v11 = v21 (common_vertex1)
811 Standard_Integer common_edge_id = aBlockTool.FindCommonEdgeID(dir_face1, dir_face2);
812 Standard_Integer common_vertex1 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 1);
813 Standard_Integer common_vertex2 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 2);
815 Standard_Integer not_common_v1 = 0;
816 Standard_Integer vid = 1;
817 Standard_Boolean isFound = Standard_False;
818 while (!isFound && vid <= 4) {
819 not_common_v1 = aBlockTool.GetVertexID(dir_face1, vid);
820 isFound = (not_common_v1 != common_vertex2 &&
821 aBlockTool.FindEdgeID(not_common_v1, common_vertex1) != 0);
825 Standard_Integer not_common_v2 = 0;
827 isFound = Standard_False;
828 while (!isFound && vid <= 4) {
829 not_common_v2 = aBlockTool.GetVertexID(dir_face2, vid);
830 isFound = (not_common_v2 != common_vertex2 &&
831 aBlockTool.FindEdgeID(not_common_v2, common_vertex1) != 0);
835 v11_id = common_vertex1;
836 v12_id = common_vertex2;
837 v13_id = not_common_v1;
839 v21_id = common_vertex1;
840 v22_id = common_vertex2;
841 v23_id = not_common_v2;
844 // Construct a transformation operator
845 TopoDS_Vertex V11 = TopoDS::Vertex(aBlockTool.GetVertex(v11_id));
846 TopoDS_Vertex V12 = TopoDS::Vertex(aBlockTool.GetVertex(v12_id));
847 TopoDS_Vertex V13 = TopoDS::Vertex(aBlockTool.GetVertex(v13_id));
849 TopoDS_Vertex V21 = TopoDS::Vertex(aBlockTool.GetVertex(v21_id));
850 TopoDS_Vertex V22 = TopoDS::Vertex(aBlockTool.GetVertex(v22_id));
851 TopoDS_Vertex V23 = TopoDS::Vertex(aBlockTool.GetVertex(v23_id));
853 // Axes of the first direction face
854 gp_Pnt P1 = BRep_Tool::Pnt(V11);
855 gp_Vec VecN1 (P1, BRep_Tool::Pnt(V12));
856 gp_Vec VecX1 (P1, BRep_Tool::Pnt(V13));
857 gp_Ax3 Ax1 (P1, VecN1, VecX1);
859 // Axes of the second direction face
860 gp_Pnt P2 = BRep_Tool::Pnt(V21);
861 gp_Vec VecN2 (P2, BRep_Tool::Pnt(V22));
862 gp_Vec VecX2 (P2, BRep_Tool::Pnt(V23));
863 gp_Ax3 Ax2 (P2, VecN2, VecX2);
866 aTrsf.SetDisplacement(Ax1, Ax2);
868 // Check, that <theFace2> is similar to <theFace1>.
869 // Actually, we need only to check right position of one
870 // vertex, not involved into the transformation construction.
871 if (!aBlockTool.IsSimilarFaces(dir_face1, dir_face2, aTrsf)) {
872 Standard_ConstructionError::Raise("The direction faces are not similar");
875 // Perform multi-transformation
876 TopoDS_Compound aCompound;
878 B.MakeCompound(aCompound);
880 TopoDS_Shape aPrevShape = theBlock;
881 for (Standard_Integer i = 0; i < theNbIter; i++) {
882 B.Add(aCompound, aPrevShape);
883 BRepBuilderAPI_Transform aTransformation (aPrevShape, aTrsf, Standard_False);
884 aPrevShape = aTransformation.Shape();
886 theResult = aCompound;
889 //=======================================================================
890 //function : MultiTransformate2D
892 //=======================================================================
893 void GEOMImpl_BlockDriver::MultiTransformate2D (const TopoDS_Shape& theBlock,
894 const TopoDS_Shape& theFace1U,
895 const TopoDS_Shape& theFace2U,
896 const Standard_Integer theNbIterU,
897 const TopoDS_Shape& theFace1V,
898 const TopoDS_Shape& theFace2V,
899 const Standard_Integer theNbIterV,
900 TopoDS_Shape& theResult) const
902 // Construct Tool, where <theFace1U> will be the first face,
903 // and a face, opposite to <theFace1U>, will be the second face
904 GEOMImpl_Block6Explorer aBlockTool;
905 aBlockTool.InitByBlockAndFace(theBlock, theFace1U);
907 gp_Trsf aTrsfU, aTrsfV;
909 for (Standard_Integer uv = 1; uv <= 2; uv++) {
911 TopoDS_Shape theFace1 = theFace1U;
912 TopoDS_Shape theFace2 = theFace2U;
915 theFace1 = theFace1V;
916 theFace2 = theFace2V;
919 // Find IDs of the faces
920 Standard_Integer dir_face1 = aBlockTool.GetFaceID(theFace1);
921 Standard_Integer dir_face2 = 0;
922 Standard_Integer opp_face1 = aBlockTool.GetOppositeFaceID(dir_face1);
923 if (theFace2.IsNull()) {
924 dir_face2 = opp_face1;
926 dir_face2 = aBlockTool.GetFaceID(theFace2);
929 // Find three pairs of points
930 Standard_Integer v11_id = 0, v12_id = 0, v13_id = 0; // vertices of the first face
931 Standard_Integer v21_id = 0, v22_id = 0, v23_id = 0; // vertices of the second face
933 if (dir_face2 == opp_face1) { // <theFace2> is opposite to <theFace1>
935 // We will take vertices with equal local numbers on the faces,
936 // as the Block6Explorer gives equal local numbers
937 // to the linked vertices on the opposite faces,
938 // i.e. v1* is linked with the v2* by an edge:
940 v11_id = aBlockTool.GetVertexID(dir_face1, 1);
941 v12_id = aBlockTool.GetVertexID(dir_face1, 2);
942 v13_id = aBlockTool.GetVertexID(dir_face1, 4);
944 v21_id = aBlockTool.GetVertexID(dir_face2, 1);
945 v22_id = aBlockTool.GetVertexID(dir_face2, 2);
946 v23_id = aBlockTool.GetVertexID(dir_face2, 4);
950 Standard_Integer common_edge_id = aBlockTool.FindCommonEdgeID(dir_face1, dir_face2);
951 Standard_Integer common_vertex1 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 1);
952 Standard_Integer common_vertex2 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 2);
954 Standard_Integer not_common_v1 = 0;
955 Standard_Integer vid = 1;
956 Standard_Boolean isFound = Standard_False;
957 while (!isFound && vid <= 4) {
958 not_common_v1 = aBlockTool.GetVertexID(dir_face1, vid);
959 isFound = (not_common_v1 != common_vertex2 &&
960 aBlockTool.FindEdgeID(not_common_v1, common_vertex1) != 0);
964 Standard_Integer not_common_v2 = 0;
966 isFound = Standard_False;
967 while (!isFound && vid <= 4) {
968 not_common_v2 = aBlockTool.GetVertexID(dir_face2, vid);
969 isFound = (not_common_v2 != common_vertex2 &&
970 aBlockTool.FindEdgeID(not_common_v2, common_vertex1) != 0);
974 v11_id = common_vertex1;
975 v12_id = common_vertex2;
976 v13_id = not_common_v1;
978 v21_id = common_vertex1;
979 v22_id = common_vertex2;
980 v23_id = not_common_v2;
983 // Construct a transformation operator
984 TopoDS_Vertex V11 = TopoDS::Vertex(aBlockTool.GetVertex(v11_id));
985 TopoDS_Vertex V12 = TopoDS::Vertex(aBlockTool.GetVertex(v12_id));
986 TopoDS_Vertex V13 = TopoDS::Vertex(aBlockTool.GetVertex(v13_id));
988 TopoDS_Vertex V21 = TopoDS::Vertex(aBlockTool.GetVertex(v21_id));
989 TopoDS_Vertex V22 = TopoDS::Vertex(aBlockTool.GetVertex(v22_id));
990 TopoDS_Vertex V23 = TopoDS::Vertex(aBlockTool.GetVertex(v23_id));
992 // Axes of the first direction face
993 gp_Pnt P1 = BRep_Tool::Pnt(V11);
994 gp_Vec VecN1 (P1, BRep_Tool::Pnt(V12));
995 gp_Vec VecX1 (P1, BRep_Tool::Pnt(V13));
996 gp_Ax3 Ax1 (P1, VecN1, VecX1);
998 // Axes of the second direction face
999 gp_Pnt P2 = BRep_Tool::Pnt(V21);
1000 gp_Vec VecN2 (P2, BRep_Tool::Pnt(V22));
1001 gp_Vec VecX2 (P2, BRep_Tool::Pnt(V23));
1002 gp_Ax3 Ax2 (P2, VecN2, VecX2);
1005 aTrsf.SetDisplacement(Ax1, Ax2);
1014 // Check, that <theFace2> is similar to <theFace1>.
1015 // Actually, we need only to check right position of one
1016 // vertex, not involved into the transformation construction.
1017 if (!aBlockTool.IsSimilarFaces(dir_face1, dir_face2, aTrsf)) {
1018 Standard_ConstructionError::Raise("The direction faces are not similar");
1022 // Perform multi-transformation
1023 TopoDS_Compound aCompound;
1025 B.MakeCompound(aCompound);
1027 TopoDS_Shape aPrevShapeU = theBlock;
1028 for (int i = 0; i < theNbIterU; i++) {
1029 TopoDS_Shape aPrevShapeV = aPrevShapeU;
1030 for (int j = 0; j < theNbIterV; j++) {
1031 B.Add(aCompound, aPrevShapeV);
1032 BRepBuilderAPI_Transform aTransformationV (aPrevShapeV, aTrsfV, Standard_False);
1033 aPrevShapeV = aTransformationV.Shape();
1035 BRepBuilderAPI_Transform aTransformationU (aPrevShapeU, aTrsfU, Standard_False);
1036 aPrevShapeU = aTransformationU.Shape();
1037 // Correction of the second transformation according to the first transformation
1038 Ax1V.Transform(aTrsfU);
1039 Ax2V.Transform(aTrsfU);
1040 aTrsfV.SetDisplacement(Ax1V, Ax2V);
1043 theResult = aCompound;
1046 //=======================================================================
1047 //function : GEOMImpl_BlockDriver_Type_
1049 //=======================================================================
1050 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BlockDriver_Type_()
1053 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
1054 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
1055 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
1056 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
1057 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
1058 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
1061 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
1062 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BlockDriver",
1063 sizeof(GEOMImpl_BlockDriver),
1065 (Standard_Address)_Ancestors,
1066 (Standard_Address)NULL);
1071 //=======================================================================
1072 //function : DownCast
1074 //=======================================================================
1075 const Handle(GEOMImpl_BlockDriver) Handle(GEOMImpl_BlockDriver)::DownCast
1076 (const Handle(Standard_Transient)& AnObject)
1078 Handle(GEOMImpl_BlockDriver) _anOtherObject;
1080 if (!AnObject.IsNull()) {
1081 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BlockDriver))) {
1082 _anOtherObject = Handle(GEOMImpl_BlockDriver)((Handle(GEOMImpl_BlockDriver)&)AnObject);
1086 return _anOtherObject;