1 // Copyright (C) 2007-2008 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 <Standard_Stream.hxx>
24 #include <BRepOffsetAPI_MakeFilling.hxx>
26 #include <GEOMImpl_BlockDriver.hxx>
27 #include <GEOMImpl_IBlocks.hxx>
28 #include <GEOMImpl_IBlockTrsf.hxx>
29 #include <GEOMImpl_GlueDriver.hxx>
30 #include <GEOMImpl_Types.hxx>
31 #include <GEOMImpl_ILocalOperations.hxx>
32 #include <GEOMImpl_Block6Explorer.hxx>
33 #include <GEOMImpl_IBlocksOperations.hxx>
35 #include <GEOM_Function.hxx>
37 #include <ShHealOper_Sewing.hxx>
38 #include <ShHealOper_ShapeProcess.hxx>
39 #include <GEOMAlgo_Gluer.hxx>
40 #include <BlockFix_BlockFixAPI.hxx>
42 #include "utilities.h"
44 #include <TNaming_CopyShape.hxx>
46 #include <BRepAdaptor_Curve.hxx>
47 #include <BRepLib.hxx>
48 #include <BRep_Tool.hxx>
49 #include <BRepTools.hxx>
50 #include <BRepGProp.hxx>
51 #include <BRep_Builder.hxx>
52 #include <BRepTools_Quilt.hxx>
53 #include <BRepTools_WireExplorer.hxx>
54 #include <BRepBuilderAPI_MakeEdge.hxx>
55 #include <BRepBuilderAPI_MakeWire.hxx>
56 #include <BRepBuilderAPI_MakePolygon.hxx>
57 #include <BRepBuilderAPI_Transform.hxx>
58 #include <BRepCheck_Analyzer.hxx>
59 #include <BRepClass_FaceClassifier.hxx>
60 #include <BRepClass3d_SolidClassifier.hxx>
61 #include <BRepExtrema_ExtPF.hxx>
62 #include <BRepExtrema_DistShapeShape.hxx>
66 #include <TopoDS_Shape.hxx>
67 #include <TopoDS_Edge.hxx>
68 #include <TopoDS_Wire.hxx>
69 #include <TopoDS_Shell.hxx>
70 #include <TopoDS_Solid.hxx>
71 #include <TopoDS_Compound.hxx>
72 #include <TopoDS_Iterator.hxx>
74 #include <TopExp_Explorer.hxx>
75 #include <TopTools_MapOfShape.hxx>
76 #include <TopTools_MapIteratorOfMapOfShape.hxx>
77 #include <TopTools_Array1OfShape.hxx>
78 #include <TopTools_SequenceOfShape.hxx>
79 #include <TopTools_ListOfShape.hxx>
80 #include <TopTools_ListIteratorOfListOfShape.hxx>
81 #include <TopTools_IndexedMapOfShape.hxx>
82 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
84 #include <GProp_GProps.hxx>
86 #include <Geom_Line.hxx>
87 #include <GC_MakeLine.hxx>
92 #include <Precision.hxx>
93 #include <TColgp_Array1OfPnt.hxx>
94 #include <TColStd_Array1OfInteger.hxx>
95 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
96 #include <StdFail_NotDone.hxx>
97 #include <Standard_NullObject.hxx>
98 #include <Standard_TypeMismatch.hxx>
99 #include <Standard_ConstructionError.hxx>
101 //=======================================================================
104 //=======================================================================
105 const Standard_GUID& GEOMImpl_BlockDriver::GetID()
107 static Standard_GUID aBlockDriver("FF1BBB67-5D14-4df2-980B-3A668264EA16");
112 //=======================================================================
113 //function : GEOMImpl_BlockDriver
115 //=======================================================================
116 GEOMImpl_BlockDriver::GEOMImpl_BlockDriver()
120 //=======================================================================
123 //=======================================================================
124 Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
126 if (Label().IsNull()) return 0;
127 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
129 Standard_Integer aType = aFunction->GetType();
134 Standard_Real prec = Precision::Confusion();
136 Standard_Integer aNbSub = 0;
137 if (aType == BLOCK_COMPOUND_GLUE) {
141 } else if (aType == BLOCK_FACE_TWO_EDGES ||
142 aType == BLOCK_TWO_FACES) {
146 } else if (aType == BLOCK_FACE_FOUR_PNT ||
147 aType == BLOCK_FACE_FOUR_EDGES) {
151 } else if (aType == BLOCK_SIX_FACES) {
159 GEOMImpl_IBlocks aCI (aFunction);
160 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
161 Standard_Integer nbshapes = aShapes->Length();
163 if (nbshapes != aNbSub) {
164 Standard_TypeMismatch::Raise
165 ("Number of elements for object construction does not correspond to the used constructor");
168 TopTools_Array1OfShape anArgs (1, aNbSub);
169 Standard_Integer argi;
170 for (argi = 1; argi <= aNbSub; argi++) {
171 Handle(GEOM_Function) aRef = Handle(GEOM_Function)::DownCast(aShapes->Value(argi));
172 TopoDS_Shape anArg = aRef->GetValue();
173 if (anArg.IsNull()) {
174 Standard_NullObject::Raise("Null shape is given as argument");
176 anArgs(argi) = anArg;
179 if (aType == BLOCK_FACE_FOUR_EDGES) {
181 // Make face from four edges
182 if (anArgs(1).ShapeType() != TopAbs_EDGE || anArgs(2).ShapeType() != TopAbs_EDGE ||
183 anArgs(3).ShapeType() != TopAbs_EDGE || anArgs(4).ShapeType() != TopAbs_EDGE) {
184 Standard_TypeMismatch::Raise("Shape for face construction is not an edge");
187 // count corner vertices
188 TopTools_MapOfShape aVertMap;
189 for (Standard_Integer ii = 1; ii <= 4; ii++) {
190 TopoDS_Edge anEdge = TopoDS::Edge(anArgs(ii));
191 TopoDS_Vertex V1, V2;
192 TopExp::Vertices(anEdge, V1, V2, Standard_True);
193 if (V1.IsNull() || V2.IsNull()) {
194 Standard_NullObject::Raise("Bad edge for face construction: vertex is not defined");
196 if (BRepTools::Compare(V1,V2)) {
197 Standard_ConstructionError::Raise("Edge ends are too close");
199 Standard_Boolean isCoin1 = Standard_False, isCoin2 = Standard_False;
200 TopTools_MapIteratorOfMapOfShape anIter (aVertMap);
201 for (; anIter.More(); anIter.Next()) {
202 TopoDS_Vertex V = TopoDS::Vertex(anIter.Key());
203 if (BRepTools::Compare(V,V1)) isCoin1 = Standard_True;
204 if (BRepTools::Compare(V,V2)) isCoin2 = Standard_True;
206 if (!isCoin1) aVertMap.Add(V1);
207 if (!isCoin2) aVertMap.Add(V2);
209 if (aVertMap.Extent() != 4) {
210 Standard_ConstructionError::Raise("The edges must form a closed wire");
213 TopoDS_Edge anEdge1 = TopoDS::Edge(anArgs(1));
214 TopoDS_Edge anEdge2 = TopoDS::Edge(anArgs(2));
215 TopoDS_Edge anEdge3 = TopoDS::Edge(anArgs(3));
216 TopoDS_Edge anEdge4 = TopoDS::Edge(anArgs(4));
218 // check, if anEdge1 has common/coincident vertex with anEdge2,
219 Standard_Boolean isConnected12 = Standard_False;
220 TopoDS_Vertex V11, V12, V21, V22;
221 TopExp::Vertices(anEdge1, V11, V12, Standard_True);
222 TopExp::Vertices(anEdge2, V21, V22, Standard_True);
223 if (BRepTools::Compare(V11,V21) || BRepTools::Compare(V11,V22) ||
224 BRepTools::Compare(V12,V21) || BRepTools::Compare(V12,V22)) {
225 // the edges have common vertex
226 isConnected12 = Standard_True;
229 // build wire in right order, corresponding to edges connexity
230 BRepBuilderAPI_MakeWire* MW;
232 MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge2, anEdge3, anEdge4);
234 MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge3, anEdge2, anEdge4);
237 Standard_ConstructionError::Raise
238 ("Impossible to build a connected wire from the given edges");
240 TopoDS_Wire aWire = *MW;
243 // check the wire closure
244 TopoDS_Vertex aV1, aV2;
245 TopExp::Vertices(aWire, aV1, aV2);
246 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
247 aWire.Closed( true );
249 if (!aWire.Closed()) {
250 Standard_ConstructionError::Raise
251 ("Impossible to build a closed wire from the given edges");
254 // try to build face on the wire
255 GEOMImpl_Block6Explorer::MakeFace(aWire, Standard_False, aShape);
256 if (aShape.IsNull()) {
257 Standard_ConstructionError::Raise("Face construction failed");
260 } else if (aType == BLOCK_FACE_TWO_EDGES) {
262 // Make face from two opposite edges
263 if (anArgs(1).ShapeType() != TopAbs_EDGE ||
264 anArgs(2).ShapeType() != TopAbs_EDGE) {
265 Standard_TypeMismatch::Raise("Shape for face construction is not an edge");
268 TopoDS_Edge anEdge1 = TopoDS::Edge(anArgs(1));
269 TopoDS_Edge anEdge2 = TopoDS::Edge(anArgs(2));
271 if (anEdge1.IsSame(anEdge2)) {
272 Standard_ConstructionError::Raise("The edges must be different");
275 // create two edges, linking ends of the given edges
276 TopoDS_Vertex V11, V12, V21, V22;
277 TopExp::Vertices(anEdge1, V11, V12, Standard_False);
278 TopExp::Vertices(anEdge2, V21, V22, Standard_False);
279 if (V11.IsNull() || V12.IsNull() ||
280 V21.IsNull() || V22.IsNull()) {
281 Standard_NullObject::Raise("Bad edge for face construction: vertex is not defined");
284 BRepAdaptor_Curve C1 (anEdge1);
285 BRepAdaptor_Curve C2 (anEdge2);
286 gp_Pnt P11, P12, P21, P22;
288 // Mantis issue 0020599: Creation of a quadrangle face from 2 edges: SIGSEGV
289 P11 = C1.Value(C1.FirstParameter());
290 P12 = C1.Value(C1.LastParameter());
291 P21 = C2.Value(C2.FirstParameter());
292 P22 = C2.Value(C2.LastParameter());
293 //gp_Pnt P11 = BRep_Tool::Pnt(V11);
294 //gp_Pnt P12 = BRep_Tool::Pnt(V12);
295 //gp_Pnt P21 = BRep_Tool::Pnt(V21);
296 //gp_Pnt P22 = BRep_Tool::Pnt(V22);
298 if (P11.Distance(P21) < prec || P12.Distance(P22) < prec ||
299 P11.Distance(P22) < prec || P12.Distance(P21) < prec) {
300 Standard_ConstructionError::Raise("Given edges have too close ends");
303 Standard_Real per11 = P11.Distance(P21) + P12.Distance(P22);
304 Standard_Real per12 = P11.Distance(P22) + P12.Distance(P21);
311 // Mantis issue 0020599: Creation of a quadrangle face from 2 edges: SIGSEGV
313 Handle(Geom_Line) Line1 = GC_MakeLine(P11, P21).Value();
314 Handle(Geom_Line) Line2 = GC_MakeLine(P12, P22).Value();
316 BB.MakeEdge(anEdge3, Line1, Precision::Confusion());
317 BB.Range(anEdge3, 0., P11.Distance(P21));
318 BB.Add(anEdge3, V11.Oriented(TopAbs_FORWARD));
319 BB.Add(anEdge3, V21.Oriented(TopAbs_REVERSED));
321 BB.MakeEdge(anEdge4, Line2, Precision::Confusion());
322 BB.Range(anEdge4, 0., P12.Distance(P22));
323 BB.Add(anEdge4, V12.Oriented(TopAbs_FORWARD));
324 BB.Add(anEdge4, V22.Oriented(TopAbs_REVERSED));
327 Handle(Geom_Line) Line1 = GC_MakeLine(P11, P22).Value();
328 Handle(Geom_Line) Line2 = GC_MakeLine(P12, P21).Value();
330 BB.MakeEdge(anEdge3, Line1, Precision::Confusion());
331 BB.Range(anEdge3, 0., P11.Distance(P22));
332 BB.Add(anEdge3, V11.Oriented(TopAbs_FORWARD));
333 BB.Add(anEdge3, V22.Oriented(TopAbs_REVERSED));
335 BB.MakeEdge(anEdge4, Line2, Precision::Confusion());
336 BB.Range(anEdge4, 0., P12.Distance(P21));
337 BB.Add(anEdge4, V12.Oriented(TopAbs_FORWARD));
338 BB.Add(anEdge4, V21.Oriented(TopAbs_REVERSED));
340 //if (per11 < per12) {
341 // anEdge3 = BRepBuilderAPI_MakeEdge(V11, V21);
342 // anEdge4 = BRepBuilderAPI_MakeEdge(V12, V22);
344 // anEdge3 = BRepBuilderAPI_MakeEdge(V11, V22);
345 // anEdge4 = BRepBuilderAPI_MakeEdge(V12, V21);
349 BRepBuilderAPI_MakeWire* MW;
350 MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge3, anEdge2, anEdge4);
352 Standard_ConstructionError::Raise("Wire construction failed");
355 TopoDS_Wire aWire = *MW;
358 TopoDS_Vertex aV1, aV2;
359 TopExp::Vertices(aWire, aV1, aV2);
360 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
361 aWire.Closed( true );
363 if (!aWire.Closed()) {
364 Standard_ConstructionError::Raise
365 ("Impossible to build a closed wire from the given edges");
368 // try to build face on the wire
369 GEOMImpl_Block6Explorer::MakeFace(aWire, Standard_False, aShape);
370 if (aShape.IsNull()) {
371 Standard_ConstructionError::Raise("Face construction failed");
374 } else if (aType == BLOCK_FACE_FOUR_PNT) {
376 // Make face from four corner vertices
377 if (anArgs(1).ShapeType() != TopAbs_VERTEX ||
378 anArgs(2).ShapeType() != TopAbs_VERTEX ||
379 anArgs(3).ShapeType() != TopAbs_VERTEX ||
380 anArgs(4).ShapeType() != TopAbs_VERTEX) {
381 Standard_TypeMismatch::Raise("Shape for face construction is not a vertex");
384 TopoDS_Vertex V1 = TopoDS::Vertex(anArgs(1));
385 TopoDS_Vertex V2 = TopoDS::Vertex(anArgs(2));
386 TopoDS_Vertex V3 = TopoDS::Vertex(anArgs(3));
387 TopoDS_Vertex V4 = TopoDS::Vertex(anArgs(4));
389 gp_Pnt P1 = BRep_Tool::Pnt(V1);
390 gp_Pnt P2 = BRep_Tool::Pnt(V2);
391 gp_Pnt P3 = BRep_Tool::Pnt(V3);
392 gp_Pnt P4 = BRep_Tool::Pnt(V4);
394 if (P1.Distance(P2) < prec || P1.Distance(P3) < prec ||
395 P1.Distance(P4) < prec || P2.Distance(P3) < prec ||
396 P2.Distance(P4) < prec || P3.Distance(P4) < prec) {
397 Standard_ConstructionError::Raise("Four not coincident points must be given");
400 // calculate perimeters
401 Standard_Real per1234 = P1.Distance(P2) + P2.Distance(P3) +
402 P3.Distance(P4) + P4.Distance(P1);
403 Standard_Real per1243 = P1.Distance(P2) + P2.Distance(P4) +
404 P4.Distance(P3) + P3.Distance(P1);
405 Standard_Real per1324 = P1.Distance(P3) + P3.Distance(P2) +
406 P2.Distance(P4) + P4.Distance(P1);
409 if (per1243 < per1234 && per1243 < per1324) {
410 TopoDS_Vertex Vtmp = V3;
413 } else if (per1324 < per1234 && per1324 < per1243) {
414 TopoDS_Vertex Vtmp = V3;
421 BRepBuilderAPI_MakePolygon aMkPoly (V1, V2, V3, V4, Standard_True);
422 if (!aMkPoly.IsDone()) {
423 Standard_ConstructionError::Raise("Polygon construction failed");
426 // try to build face on the wire
428 GEOMImpl_Block6Explorer::MakeFace(aMkPoly, Standard_False, aShape);
429 if (aShape.IsNull()) {
430 Standard_ConstructionError::Raise("Face construction failed");
433 } else if (aType == BLOCK_SIX_FACES || aType == BLOCK_TWO_FACES) {
435 BRepTools_Quilt Glue;
437 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) {
535 // Make blocks compound from a compound
536 if (anArgs(1).ShapeType() != TopAbs_COMPOUND &&
537 anArgs(2).ShapeType() != TopAbs_COMPSOLID) {
538 Standard_TypeMismatch::Raise("Not a compound given");
541 TopoDS_Shape aCompound = anArgs(1);
543 // Glue coincident faces and edges (with Partition algorithm).
544 //NMTAlgo_Splitter1 PS;
545 //PS.AddShape(aCompound);
547 //PS.SetRemoveWebs(Standard_False);
548 // PS.Build(aCompound.ShapeType());
549 //PS.Build(TopAbs_SOLID);
550 //aShape = PS.Shape();
552 GEOMAlgo_Gluer aGluer;
553 aGluer.SetShape(aCompound);
554 aGluer.SetCheckGeometry(Standard_True);
556 aShape = aGluer.Result();
562 } else { // Multi-transformations and compound improving
564 if (aType == BLOCK_REMOVE_EXTRA ||
565 aType == BLOCK_COMPOUND_IMPROVE) {
567 GEOMImpl_IBlockTrsf aCI (aFunction);
568 Handle(GEOM_Function) aRefShape = aCI.GetOriginal();
569 TopoDS_Shape aBlockOrComp = aRefShape->GetValue();
570 if (aBlockOrComp.IsNull()) {
571 Standard_NullObject::Raise("Null Shape given");
574 // 1. Improve solids with seam and/or degenerated edges
575 BlockFix_BlockFixAPI aTool;
576 //aTool.Tolerance() = toler;
577 aTool.OptimumNbFaces() = aCI.GetOptimumNbFaces();
578 aTool.SetShape(aBlockOrComp);
581 TopoDS_Shape aFixedExtra = aTool.Shape();
584 BRepCheck_Analyzer ana (aFixedExtra, false);
585 if (!ana.IsValid()) {
587 ShHealOper_ShapeProcess aHealer;
588 aHealer.Perform(aFixedExtra, aFixed);
589 if (aHealer.isDone())
590 aFixedExtra = aFixed;
593 if (aType == BLOCK_REMOVE_EXTRA)
595 aShape = aFixedExtra;
597 if (aShape == aBlockOrComp) {
598 MESSAGE("No modifications have been done");
601 else // aType == BLOCK_COMPOUND_IMPROVE
603 // 2. Separate non-blocks
604 TopTools_ListOfShape BLO; // All blocks from the given compound
605 TopTools_ListOfShape NOT; // Not blocks
606 TopTools_ListOfShape EXT; // Hexahedral solids, having degenerated and/or seam edges
607 GEOMImpl_IBlocksOperations::AddBlocksFrom(aFixedExtra, BLO, NOT, EXT);
609 if (NOT.Extent() > 0) {
610 MESSAGE("Some non-blocks have been removed");
613 // 3. Warn about staying extra-edges
614 if (EXT.Extent() > 0) {
615 MESSAGE("Warning: Not all seam or degenerated edges was removed");
618 // ??? Throw away standalone blocks ???
620 // 4. Create compound of all blocks
621 TopoDS_Compound aComp;
623 BB.MakeCompound(aComp);
624 TopTools_ListIteratorOfListOfShape BLOit (BLO);
625 for (; BLOit.More(); BLOit.Next()) {
626 BB.Add(aComp, BLOit.Value());
630 aShape = GEOMImpl_GlueDriver::GlueFaces(aComp, Precision::Confusion(), Standard_False);
633 } else if (aType == BLOCK_MULTI_TRANSFORM_1D ||
634 aType == BLOCK_MULTI_TRANSFORM_2D) {
637 GEOMImpl_IBlockTrsf aCI (aFunction);
638 Handle(GEOM_Function) aRefShape = aCI.GetOriginal();
639 TopoDS_Shape aBlockIni = aRefShape->GetValue();
640 if (aBlockIni.IsNull()) {
641 Standard_NullObject::Raise("Null Block");
644 // Copy block to avoid problems (PAL6706)
645 TColStd_IndexedDataMapOfTransientTransient aMap;
647 TNaming_CopyShape::CopyTool(aBlockIni, aMap, aBlock);
649 // Block tolerance in vertices
650 Standard_Real aTol = prec;
651 TopExp_Explorer expV (aBlock, TopAbs_VERTEX);
652 TopTools_MapOfShape mapShape;
653 for (; expV.More(); expV.Next()) {
654 if (mapShape.Add(expV.Current())) {
655 TopoDS_Vertex aV = TopoDS::Vertex(expV.Current());
656 aTol = Max(BRep_Tool::Tolerance(aV), aTol);
660 if (aType == BLOCK_MULTI_TRANSFORM_1D) {
661 // Retrieve a faces by Ids
662 Standard_Integer aFace1Id = aCI.GetFace1U();
663 Standard_Integer aFace2Id = aCI.GetFace2U();
664 TopoDS_Shape aFace1, aFace2;
665 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1Id, aFace1)) {
666 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
668 if (aFace1.ShapeType() != TopAbs_FACE) {
669 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
673 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2Id, aFace2)) {
674 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
676 if (aFace2.ShapeType() != TopAbs_FACE) {
677 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
681 Standard_Integer aNbIter = aCI.GetNbIterU();
683 MultiTransformate1D(aBlock, aFace1, aFace2, aNbIter, aMulti);
685 } else { // aType == BLOCK_MULTI_TRANSFORM_2D
686 // Retrieve a faces by Ids
687 Standard_Integer aFace1UId = aCI.GetFace1U();
688 Standard_Integer aFace2UId = aCI.GetFace2U();
689 Standard_Integer aFace1VId = aCI.GetFace1V();
690 Standard_Integer aFace2VId = aCI.GetFace2V();
692 TopoDS_Shape aFace1U, aFace2U, aFace1V, aFace2V;
693 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1UId, aFace1U) ||
694 !GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1VId, aFace1V)) {
695 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
698 if (aFace1U.ShapeType() != TopAbs_FACE ||
699 aFace1V.ShapeType() != TopAbs_FACE) {
700 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
704 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2UId, aFace2U)) {
705 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
708 if (aFace2U.ShapeType() != TopAbs_FACE) {
709 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
714 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2VId, aFace2V)) {
715 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
718 if (aFace2V.ShapeType() != TopAbs_FACE) {
719 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
723 Standard_Integer aNbIterU = aCI.GetNbIterU();
724 Standard_Integer aNbIterV = aCI.GetNbIterV();
726 MultiTransformate2D(aBlock,
727 aFace1U, aFace2U, aNbIterU,
728 aFace1V, aFace2V, aNbIterV, aMulti);
731 if (aMulti.IsNull()) {
732 StdFail_NotDone::Raise("Multi-transformation failed");
735 // Glue faces of the multi-block
736 aShape = GEOMImpl_GlueDriver::GlueFaces(aMulti, aTol, Standard_False);
738 } else { // unknown function type
743 if (aShape.IsNull()) return 0;
745 aFunction->SetValue(aShape);
747 log.SetTouched(Label());
752 //=======================================================================
753 //function : MultiTransformate1D
755 //=======================================================================
756 void GEOMImpl_BlockDriver::MultiTransformate1D (const TopoDS_Shape& theBlock,
757 const TopoDS_Shape& theFace1,
758 const TopoDS_Shape& theFace2,
759 const Standard_Integer theNbIter,
760 TopoDS_Shape& theResult) const
762 // Construct Tool, where <theFace1> will be the first face,
763 // and a face, opposite to <theFace1>, will be the second face
764 GEOMImpl_Block6Explorer aBlockTool;
765 aBlockTool.InitByBlockAndFace(theBlock, theFace1);
767 // Find IDs of the faces
768 Standard_Integer dir_face1 = 1, dir_face2 = 2;
769 if (!theFace2.IsNull()) {
770 dir_face2 = aBlockTool.GetFaceID(theFace2);
773 // Find three pairs of points
774 Standard_Integer v11_id = 0, v12_id = 0, v13_id = 0; // vertices of the first face
775 Standard_Integer v21_id = 0, v22_id = 0, v23_id = 0; // vertices of the second face
777 if (dir_face2 == 2) { // <theFace2> is opposite to <theFace1>
779 // We will take vertices with equal local numbers on the faces,
780 // as the Block6Explorer gives equal local numbers
781 // to the linked vertices on the opposite faces,
782 // i.e. v1* is linked with the v2* by an edge:
786 // / | / | dir_face1 - bottom
788 // /________ / | dir_face2 - top
797 v11_id = aBlockTool.GetVertexID(dir_face1, 1);
798 v12_id = aBlockTool.GetVertexID(dir_face1, 2);
799 v13_id = aBlockTool.GetVertexID(dir_face1, 4);
801 v21_id = aBlockTool.GetVertexID(dir_face2, 1);
802 v22_id = aBlockTool.GetVertexID(dir_face2, 2);
803 v23_id = aBlockTool.GetVertexID(dir_face2, 4);
809 // / | / | dir_face1 - bottom
811 // /________ / | dir_face2 - right (for example)
814 // | / | /v12 = v22 (common_vertex2)
818 // v13 v11 = v21 (common_vertex1)
820 Standard_Integer common_edge_id = aBlockTool.FindCommonEdgeID(dir_face1, dir_face2);
821 Standard_Integer common_vertex1 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 1);
822 Standard_Integer common_vertex2 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 2);
824 Standard_Integer not_common_v1 = 0;
825 Standard_Integer vid = 1;
826 Standard_Boolean isFound = Standard_False;
827 while (!isFound && vid <= 4) {
828 not_common_v1 = aBlockTool.GetVertexID(dir_face1, vid);
829 isFound = (not_common_v1 != common_vertex2 &&
830 aBlockTool.FindEdgeID(not_common_v1, common_vertex1) != 0);
834 Standard_Integer not_common_v2 = 0;
836 isFound = Standard_False;
837 while (!isFound && vid <= 4) {
838 not_common_v2 = aBlockTool.GetVertexID(dir_face2, vid);
839 isFound = (not_common_v2 != common_vertex2 &&
840 aBlockTool.FindEdgeID(not_common_v2, common_vertex1) != 0);
844 v11_id = common_vertex1;
845 v12_id = common_vertex2;
846 v13_id = not_common_v1;
848 v21_id = common_vertex1;
849 v22_id = common_vertex2;
850 v23_id = not_common_v2;
853 // Construct a transformation operator
854 TopoDS_Vertex V11 = TopoDS::Vertex(aBlockTool.GetVertex(v11_id));
855 TopoDS_Vertex V12 = TopoDS::Vertex(aBlockTool.GetVertex(v12_id));
856 TopoDS_Vertex V13 = TopoDS::Vertex(aBlockTool.GetVertex(v13_id));
858 TopoDS_Vertex V21 = TopoDS::Vertex(aBlockTool.GetVertex(v21_id));
859 TopoDS_Vertex V22 = TopoDS::Vertex(aBlockTool.GetVertex(v22_id));
860 TopoDS_Vertex V23 = TopoDS::Vertex(aBlockTool.GetVertex(v23_id));
862 // Axes of the first direction face
863 gp_Pnt P1 = BRep_Tool::Pnt(V11);
864 gp_Vec VecN1 (P1, BRep_Tool::Pnt(V12));
865 gp_Vec VecX1 (P1, BRep_Tool::Pnt(V13));
866 gp_Ax3 Ax1 (P1, VecN1, VecX1);
868 // Axes of the second direction face
869 gp_Pnt P2 = BRep_Tool::Pnt(V21);
870 gp_Vec VecN2 (P2, BRep_Tool::Pnt(V22));
871 gp_Vec VecX2 (P2, BRep_Tool::Pnt(V23));
872 gp_Ax3 Ax2 (P2, VecN2, VecX2);
875 aTrsf.SetDisplacement(Ax1, Ax2);
877 // Check, that <theFace2> is similar to <theFace1>.
878 // Actually, we need only to check right position of one
879 // vertex, not involved into the transformation construction.
880 if (!aBlockTool.IsSimilarFaces(dir_face1, dir_face2, aTrsf)) {
881 Standard_ConstructionError::Raise("The direction faces are not similar");
884 // Perform multi-transformation
885 TopoDS_Compound aCompound;
887 B.MakeCompound(aCompound);
889 TopoDS_Shape aPrevShape = theBlock;
890 for (Standard_Integer i = 0; i < theNbIter; i++) {
891 B.Add(aCompound, aPrevShape);
892 BRepBuilderAPI_Transform aTransformation (aPrevShape, aTrsf, Standard_False);
893 aPrevShape = aTransformation.Shape();
895 theResult = aCompound;
898 //=======================================================================
899 //function : MultiTransformate2D
901 //=======================================================================
902 void GEOMImpl_BlockDriver::MultiTransformate2D (const TopoDS_Shape& theBlock,
903 const TopoDS_Shape& theFace1U,
904 const TopoDS_Shape& theFace2U,
905 const Standard_Integer theNbIterU,
906 const TopoDS_Shape& theFace1V,
907 const TopoDS_Shape& theFace2V,
908 const Standard_Integer theNbIterV,
909 TopoDS_Shape& theResult) const
911 // Construct Tool, where <theFace1U> will be the first face,
912 // and a face, opposite to <theFace1U>, will be the second face
913 GEOMImpl_Block6Explorer aBlockTool;
914 aBlockTool.InitByBlockAndFace(theBlock, theFace1U);
916 gp_Trsf aTrsfU, aTrsfV;
918 for (Standard_Integer uv = 1; uv <= 2; uv++) {
920 TopoDS_Shape theFace1 = theFace1U;
921 TopoDS_Shape theFace2 = theFace2U;
924 theFace1 = theFace1V;
925 theFace2 = theFace2V;
928 // Find IDs of the faces
929 Standard_Integer dir_face1 = aBlockTool.GetFaceID(theFace1);
930 Standard_Integer dir_face2 = 0;
931 Standard_Integer opp_face1 = aBlockTool.GetOppositeFaceID(dir_face1);
932 if (theFace2.IsNull()) {
933 dir_face2 = opp_face1;
935 dir_face2 = aBlockTool.GetFaceID(theFace2);
938 // Find three pairs of points
939 Standard_Integer v11_id = 0, v12_id = 0, v13_id = 0; // vertices of the first face
940 Standard_Integer v21_id = 0, v22_id = 0, v23_id = 0; // vertices of the second face
942 if (dir_face2 == opp_face1) { // <theFace2> is opposite to <theFace1>
944 // We will take vertices with equal local numbers on the faces,
945 // as the Block6Explorer gives equal local numbers
946 // to the linked vertices on the opposite faces,
947 // i.e. v1* is linked with the v2* by an edge:
949 v11_id = aBlockTool.GetVertexID(dir_face1, 1);
950 v12_id = aBlockTool.GetVertexID(dir_face1, 2);
951 v13_id = aBlockTool.GetVertexID(dir_face1, 4);
953 v21_id = aBlockTool.GetVertexID(dir_face2, 1);
954 v22_id = aBlockTool.GetVertexID(dir_face2, 2);
955 v23_id = aBlockTool.GetVertexID(dir_face2, 4);
959 Standard_Integer common_edge_id = aBlockTool.FindCommonEdgeID(dir_face1, dir_face2);
960 Standard_Integer common_vertex1 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 1);
961 Standard_Integer common_vertex2 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 2);
963 Standard_Integer not_common_v1 = 0;
964 Standard_Integer vid = 1;
965 Standard_Boolean isFound = Standard_False;
966 while (!isFound && vid <= 4) {
967 not_common_v1 = aBlockTool.GetVertexID(dir_face1, vid);
968 isFound = (not_common_v1 != common_vertex2 &&
969 aBlockTool.FindEdgeID(not_common_v1, common_vertex1) != 0);
973 Standard_Integer not_common_v2 = 0;
975 isFound = Standard_False;
976 while (!isFound && vid <= 4) {
977 not_common_v2 = aBlockTool.GetVertexID(dir_face2, vid);
978 isFound = (not_common_v2 != common_vertex2 &&
979 aBlockTool.FindEdgeID(not_common_v2, common_vertex1) != 0);
983 v11_id = common_vertex1;
984 v12_id = common_vertex2;
985 v13_id = not_common_v1;
987 v21_id = common_vertex1;
988 v22_id = common_vertex2;
989 v23_id = not_common_v2;
992 // Construct a transformation operator
993 TopoDS_Vertex V11 = TopoDS::Vertex(aBlockTool.GetVertex(v11_id));
994 TopoDS_Vertex V12 = TopoDS::Vertex(aBlockTool.GetVertex(v12_id));
995 TopoDS_Vertex V13 = TopoDS::Vertex(aBlockTool.GetVertex(v13_id));
997 TopoDS_Vertex V21 = TopoDS::Vertex(aBlockTool.GetVertex(v21_id));
998 TopoDS_Vertex V22 = TopoDS::Vertex(aBlockTool.GetVertex(v22_id));
999 TopoDS_Vertex V23 = TopoDS::Vertex(aBlockTool.GetVertex(v23_id));
1001 // Axes of the first direction face
1002 gp_Pnt P1 = BRep_Tool::Pnt(V11);
1003 gp_Vec VecN1 (P1, BRep_Tool::Pnt(V12));
1004 gp_Vec VecX1 (P1, BRep_Tool::Pnt(V13));
1005 gp_Ax3 Ax1 (P1, VecN1, VecX1);
1007 // Axes of the second direction face
1008 gp_Pnt P2 = BRep_Tool::Pnt(V21);
1009 gp_Vec VecN2 (P2, BRep_Tool::Pnt(V22));
1010 gp_Vec VecX2 (P2, BRep_Tool::Pnt(V23));
1011 gp_Ax3 Ax2 (P2, VecN2, VecX2);
1014 aTrsf.SetDisplacement(Ax1, Ax2);
1023 // Check, that <theFace2> is similar to <theFace1>.
1024 // Actually, we need only to check right position of one
1025 // vertex, not involved into the transformation construction.
1026 if (!aBlockTool.IsSimilarFaces(dir_face1, dir_face2, aTrsf)) {
1027 Standard_ConstructionError::Raise("The direction faces are not similar");
1031 // Perform multi-transformation
1032 TopoDS_Compound aCompound;
1034 B.MakeCompound(aCompound);
1036 TopoDS_Shape aPrevShapeU = theBlock;
1037 for (int i = 0; i < theNbIterU; i++) {
1038 TopoDS_Shape aPrevShapeV = aPrevShapeU;
1039 for (int j = 0; j < theNbIterV; j++) {
1040 B.Add(aCompound, aPrevShapeV);
1041 BRepBuilderAPI_Transform aTransformationV (aPrevShapeV, aTrsfV, Standard_False);
1042 aPrevShapeV = aTransformationV.Shape();
1044 BRepBuilderAPI_Transform aTransformationU (aPrevShapeU, aTrsfU, Standard_False);
1045 aPrevShapeU = aTransformationU.Shape();
1046 // Correction of the second transformation according to the first transformation
1047 Ax1V.Transform(aTrsfU);
1048 Ax2V.Transform(aTrsfU);
1049 aTrsfV.SetDisplacement(Ax1V, Ax2V);
1052 theResult = aCompound;
1055 //=======================================================================
1056 //function : GEOMImpl_BlockDriver_Type_
1058 //=======================================================================
1059 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BlockDriver_Type_()
1062 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
1063 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
1064 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
1065 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
1066 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
1067 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
1070 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
1071 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BlockDriver",
1072 sizeof(GEOMImpl_BlockDriver),
1074 (Standard_Address)_Ancestors,
1075 (Standard_Address)NULL);
1080 //=======================================================================
1081 //function : DownCast
1083 //=======================================================================
1084 const Handle(GEOMImpl_BlockDriver) Handle(GEOMImpl_BlockDriver)::DownCast
1085 (const Handle(Standard_Transient)& AnObject)
1087 Handle(GEOMImpl_BlockDriver) _anOtherObject;
1089 if (!AnObject.IsNull()) {
1090 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BlockDriver))) {
1091 _anOtherObject = Handle(GEOMImpl_BlockDriver)((Handle(GEOMImpl_BlockDriver)&)AnObject);
1095 return _anOtherObject;