1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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>
42 #include <BlockFix_UnionFaces.hxx>
44 #include "utilities.h"
46 #include <TNaming_CopyShape.hxx>
48 #include <BRepAdaptor_Curve.hxx>
49 #include <BRepLib.hxx>
50 #include <BRep_Tool.hxx>
51 #include <BRepTools.hxx>
52 #include <BRepGProp.hxx>
53 #include <BRep_Builder.hxx>
54 #include <BRepTools_Quilt.hxx>
55 #include <BRepTools_WireExplorer.hxx>
56 #include <BRepBuilderAPI_MakeEdge.hxx>
57 #include <BRepBuilderAPI_MakeWire.hxx>
58 #include <BRepBuilderAPI_MakePolygon.hxx>
59 #include <BRepBuilderAPI_Transform.hxx>
60 #include <BRepCheck_Analyzer.hxx>
61 #include <BRepClass_FaceClassifier.hxx>
62 #include <BRepClass3d_SolidClassifier.hxx>
63 #include <BRepExtrema_ExtPF.hxx>
64 #include <BRepExtrema_DistShapeShape.hxx>
68 #include <TopoDS_Shape.hxx>
69 #include <TopoDS_Edge.hxx>
70 #include <TopoDS_Wire.hxx>
71 #include <TopoDS_Shell.hxx>
72 #include <TopoDS_Solid.hxx>
73 #include <TopoDS_Compound.hxx>
74 #include <TopoDS_Iterator.hxx>
76 #include <TopExp_Explorer.hxx>
77 #include <TopTools_MapOfShape.hxx>
78 #include <TopTools_MapIteratorOfMapOfShape.hxx>
79 #include <TopTools_Array1OfShape.hxx>
80 #include <TopTools_SequenceOfShape.hxx>
81 #include <TopTools_ListOfShape.hxx>
82 #include <TopTools_ListIteratorOfListOfShape.hxx>
83 #include <TopTools_IndexedMapOfShape.hxx>
84 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
86 #include <GProp_GProps.hxx>
88 #include <Geom_Line.hxx>
89 #include <GC_MakeLine.hxx>
94 #include <Precision.hxx>
95 #include <TColgp_Array1OfPnt.hxx>
96 #include <TColStd_Array1OfInteger.hxx>
97 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
98 #include <StdFail_NotDone.hxx>
99 #include <Standard_NullObject.hxx>
100 #include <Standard_TypeMismatch.hxx>
101 #include <Standard_ConstructionError.hxx>
103 //=======================================================================
106 //=======================================================================
107 const Standard_GUID& GEOMImpl_BlockDriver::GetID()
109 static Standard_GUID aBlockDriver("FF1BBB67-5D14-4df2-980B-3A668264EA16");
114 //=======================================================================
115 //function : GEOMImpl_BlockDriver
117 //=======================================================================
118 GEOMImpl_BlockDriver::GEOMImpl_BlockDriver()
122 //=======================================================================
125 //=======================================================================
126 Standard_Integer GEOMImpl_BlockDriver::Execute(LOGBOOK& log) const
128 if (Label().IsNull()) return 0;
129 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
131 Standard_Integer aType = aFunction->GetType();
136 Standard_Real prec = Precision::Confusion();
138 Standard_Integer aNbSub = 0;
139 if (aType == BLOCK_COMPOUND_GLUE) {
143 } else if (aType == BLOCK_FACE_TWO_EDGES ||
144 aType == BLOCK_TWO_FACES) {
148 } else if (aType == BLOCK_FACE_FOUR_PNT ||
149 aType == BLOCK_FACE_FOUR_EDGES) {
153 } else if (aType == BLOCK_SIX_FACES) {
161 GEOMImpl_IBlocks aCI (aFunction);
162 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
163 Standard_Integer nbshapes = aShapes->Length();
165 if (nbshapes != aNbSub) {
166 Standard_TypeMismatch::Raise
167 ("Number of elements for object construction does not correspond to the used constructor");
170 TopTools_Array1OfShape anArgs (1, aNbSub);
171 Standard_Integer argi;
172 for (argi = 1; argi <= aNbSub; argi++) {
173 Handle(GEOM_Function) aRef = Handle(GEOM_Function)::DownCast(aShapes->Value(argi));
174 TopoDS_Shape anArg = aRef->GetValue();
175 if (anArg.IsNull()) {
176 Standard_NullObject::Raise("Null shape is given as argument");
178 anArgs(argi) = anArg;
181 if (aType == BLOCK_FACE_FOUR_EDGES) {
183 // Make face from four edges
184 if (anArgs(1).ShapeType() != TopAbs_EDGE || anArgs(2).ShapeType() != TopAbs_EDGE ||
185 anArgs(3).ShapeType() != TopAbs_EDGE || anArgs(4).ShapeType() != TopAbs_EDGE) {
186 Standard_TypeMismatch::Raise("Shape for face construction is not an edge");
189 // count corner vertices
190 TopTools_MapOfShape aVertMap;
191 for (Standard_Integer ii = 1; ii <= 4; ii++) {
192 TopoDS_Edge anEdge = TopoDS::Edge(anArgs(ii));
193 TopoDS_Vertex V1, V2;
194 TopExp::Vertices(anEdge, V1, V2, Standard_True);
195 if (V1.IsNull() || V2.IsNull()) {
196 Standard_NullObject::Raise("Bad edge for face construction: vertex is not defined");
198 if (BRepTools::Compare(V1,V2)) {
199 Standard_ConstructionError::Raise("Edge ends are too close");
201 Standard_Boolean isCoin1 = Standard_False, isCoin2 = Standard_False;
202 TopTools_MapIteratorOfMapOfShape anIter (aVertMap);
203 for (; anIter.More(); anIter.Next()) {
204 TopoDS_Vertex V = TopoDS::Vertex(anIter.Key());
205 if (BRepTools::Compare(V,V1)) isCoin1 = Standard_True;
206 if (BRepTools::Compare(V,V2)) isCoin2 = Standard_True;
208 if (!isCoin1) aVertMap.Add(V1);
209 if (!isCoin2) aVertMap.Add(V2);
211 if (aVertMap.Extent() != 4) {
212 Standard_ConstructionError::Raise("The edges must form a closed wire");
215 TopoDS_Edge anEdge1 = TopoDS::Edge(anArgs(1));
216 TopoDS_Edge anEdge2 = TopoDS::Edge(anArgs(2));
217 TopoDS_Edge anEdge3 = TopoDS::Edge(anArgs(3));
218 TopoDS_Edge anEdge4 = TopoDS::Edge(anArgs(4));
220 // check, if anEdge1 has common/coincident vertex with anEdge2,
221 Standard_Boolean isConnected12 = Standard_False;
222 TopoDS_Vertex V11, V12, V21, V22;
223 TopExp::Vertices(anEdge1, V11, V12, Standard_True);
224 TopExp::Vertices(anEdge2, V21, V22, Standard_True);
225 if (BRepTools::Compare(V11,V21) || BRepTools::Compare(V11,V22) ||
226 BRepTools::Compare(V12,V21) || BRepTools::Compare(V12,V22)) {
227 // the edges have common vertex
228 isConnected12 = Standard_True;
231 // build wire in right order, corresponding to edges connexity
232 BRepBuilderAPI_MakeWire* MW;
234 MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge2, anEdge3, anEdge4);
236 MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge3, anEdge2, anEdge4);
239 Standard_ConstructionError::Raise
240 ("Impossible to build a connected wire from the given edges");
242 TopoDS_Wire aWire = *MW;
245 // check the wire closure
246 TopoDS_Vertex aV1, aV2;
247 TopExp::Vertices(aWire, aV1, aV2);
248 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
249 aWire.Closed( true );
251 if (!aWire.Closed()) {
252 Standard_ConstructionError::Raise
253 ("Impossible to build a closed wire from the given edges");
256 // try to build face on the wire
257 GEOMImpl_Block6Explorer::MakeFace(aWire, Standard_False, aShape);
258 if (aShape.IsNull()) {
259 Standard_ConstructionError::Raise("Face construction failed");
262 } else if (aType == BLOCK_FACE_TWO_EDGES) {
264 // Make face from two opposite edges
265 if (anArgs(1).ShapeType() != TopAbs_EDGE ||
266 anArgs(2).ShapeType() != TopAbs_EDGE) {
267 Standard_TypeMismatch::Raise("Shape for face construction is not an edge");
270 TopoDS_Edge anEdge1 = TopoDS::Edge(anArgs(1));
271 TopoDS_Edge anEdge2 = TopoDS::Edge(anArgs(2));
273 if (anEdge1.IsSame(anEdge2)) {
274 Standard_ConstructionError::Raise("The edges must be different");
277 // create two edges, linking ends of the given edges
278 TopoDS_Vertex V11, V12, V21, V22;
279 TopExp::Vertices(anEdge1, V11, V12, Standard_False);
280 TopExp::Vertices(anEdge2, V21, V22, Standard_False);
281 if (V11.IsNull() || V12.IsNull() ||
282 V21.IsNull() || V22.IsNull()) {
283 Standard_NullObject::Raise("Bad edge for face construction: vertex is not defined");
286 BRepAdaptor_Curve C1 (anEdge1);
287 BRepAdaptor_Curve C2 (anEdge2);
288 gp_Pnt P11, P12, P21, P22;
290 // Mantis issue 0020599: Creation of a quadrangle face from 2 edges: SIGSEGV
291 P11 = C1.Value(C1.FirstParameter());
292 P12 = C1.Value(C1.LastParameter());
293 P21 = C2.Value(C2.FirstParameter());
294 P22 = C2.Value(C2.LastParameter());
295 //gp_Pnt P11 = BRep_Tool::Pnt(V11);
296 //gp_Pnt P12 = BRep_Tool::Pnt(V12);
297 //gp_Pnt P21 = BRep_Tool::Pnt(V21);
298 //gp_Pnt P22 = BRep_Tool::Pnt(V22);
300 if (P11.Distance(P21) < prec || P12.Distance(P22) < prec ||
301 P11.Distance(P22) < prec || P12.Distance(P21) < prec) {
302 Standard_ConstructionError::Raise("Given edges have too close ends");
305 Standard_Real per11 = P11.Distance(P21) + P12.Distance(P22);
306 Standard_Real per12 = P11.Distance(P22) + P12.Distance(P21);
313 // Mantis issue 0020599: Creation of a quadrangle face from 2 edges: SIGSEGV
315 Handle(Geom_Line) Line1 = GC_MakeLine(P11, P21).Value();
316 Handle(Geom_Line) Line2 = GC_MakeLine(P12, P22).Value();
318 BB.MakeEdge(anEdge3, Line1, Precision::Confusion());
319 BB.Range(anEdge3, 0., P11.Distance(P21));
320 BB.Add(anEdge3, V11.Oriented(TopAbs_FORWARD));
321 BB.Add(anEdge3, V21.Oriented(TopAbs_REVERSED));
323 BB.MakeEdge(anEdge4, Line2, Precision::Confusion());
324 BB.Range(anEdge4, 0., P12.Distance(P22));
325 BB.Add(anEdge4, V12.Oriented(TopAbs_FORWARD));
326 BB.Add(anEdge4, V22.Oriented(TopAbs_REVERSED));
329 Handle(Geom_Line) Line1 = GC_MakeLine(P11, P22).Value();
330 Handle(Geom_Line) Line2 = GC_MakeLine(P12, P21).Value();
332 BB.MakeEdge(anEdge3, Line1, Precision::Confusion());
333 BB.Range(anEdge3, 0., P11.Distance(P22));
334 BB.Add(anEdge3, V11.Oriented(TopAbs_FORWARD));
335 BB.Add(anEdge3, V22.Oriented(TopAbs_REVERSED));
337 BB.MakeEdge(anEdge4, Line2, Precision::Confusion());
338 BB.Range(anEdge4, 0., P12.Distance(P21));
339 BB.Add(anEdge4, V12.Oriented(TopAbs_FORWARD));
340 BB.Add(anEdge4, V21.Oriented(TopAbs_REVERSED));
342 //if (per11 < per12) {
343 // anEdge3 = BRepBuilderAPI_MakeEdge(V11, V21);
344 // anEdge4 = BRepBuilderAPI_MakeEdge(V12, V22);
346 // anEdge3 = BRepBuilderAPI_MakeEdge(V11, V22);
347 // anEdge4 = BRepBuilderAPI_MakeEdge(V12, V21);
351 BRepBuilderAPI_MakeWire* MW;
352 MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge3, anEdge2, anEdge4);
354 Standard_ConstructionError::Raise("Wire construction failed");
357 TopoDS_Wire aWire = *MW;
360 TopoDS_Vertex aV1, aV2;
361 TopExp::Vertices(aWire, aV1, aV2);
362 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
363 aWire.Closed( true );
365 if (!aWire.Closed()) {
366 Standard_ConstructionError::Raise
367 ("Impossible to build a closed wire from the given edges");
370 // try to build face on the wire
371 GEOMImpl_Block6Explorer::MakeFace(aWire, Standard_False, aShape);
372 if (aShape.IsNull()) {
373 Standard_ConstructionError::Raise("Face construction failed");
376 } else if (aType == BLOCK_FACE_FOUR_PNT) {
378 // Make face from four corner vertices
379 if (anArgs(1).ShapeType() != TopAbs_VERTEX ||
380 anArgs(2).ShapeType() != TopAbs_VERTEX ||
381 anArgs(3).ShapeType() != TopAbs_VERTEX ||
382 anArgs(4).ShapeType() != TopAbs_VERTEX) {
383 Standard_TypeMismatch::Raise("Shape for face construction is not a vertex");
386 TopoDS_Vertex V1 = TopoDS::Vertex(anArgs(1));
387 TopoDS_Vertex V2 = TopoDS::Vertex(anArgs(2));
388 TopoDS_Vertex V3 = TopoDS::Vertex(anArgs(3));
389 TopoDS_Vertex V4 = TopoDS::Vertex(anArgs(4));
391 gp_Pnt P1 = BRep_Tool::Pnt(V1);
392 gp_Pnt P2 = BRep_Tool::Pnt(V2);
393 gp_Pnt P3 = BRep_Tool::Pnt(V3);
394 gp_Pnt P4 = BRep_Tool::Pnt(V4);
396 if (P1.Distance(P2) < prec || P1.Distance(P3) < prec ||
397 P1.Distance(P4) < prec || P2.Distance(P3) < prec ||
398 P2.Distance(P4) < prec || P3.Distance(P4) < prec) {
399 Standard_ConstructionError::Raise("Four not coincident points must be given");
402 // calculate perimeters
403 Standard_Real per1234 = P1.Distance(P2) + P2.Distance(P3) +
404 P3.Distance(P4) + P4.Distance(P1);
405 Standard_Real per1243 = P1.Distance(P2) + P2.Distance(P4) +
406 P4.Distance(P3) + P3.Distance(P1);
407 Standard_Real per1324 = P1.Distance(P3) + P3.Distance(P2) +
408 P2.Distance(P4) + P4.Distance(P1);
411 if (per1243 < per1234 && per1243 < per1324) {
412 TopoDS_Vertex Vtmp = V3;
415 } else if (per1324 < per1234 && per1324 < per1243) {
416 TopoDS_Vertex Vtmp = V3;
423 BRepBuilderAPI_MakePolygon aMkPoly (V1, V2, V3, V4, Standard_True);
424 if (!aMkPoly.IsDone()) {
425 Standard_ConstructionError::Raise("Polygon construction failed");
428 // try to build face on the wire
430 GEOMImpl_Block6Explorer::MakeFace(aMkPoly, Standard_False, aShape);
431 if (aShape.IsNull()) {
432 Standard_ConstructionError::Raise("Face construction failed");
435 } else if (aType == BLOCK_SIX_FACES || aType == BLOCK_TWO_FACES) {
437 BRepTools_Quilt Glue;
439 if (aType == BLOCK_SIX_FACES) {
440 // Make block (hexahedral solid) from six faces
441 for (Standard_Integer ind = 1; ind <= nbshapes; ind++) {
442 if (anArgs(ind).ShapeType() != TopAbs_FACE) {
443 Standard_TypeMismatch::Raise("Shape for block construction is not a face");
445 Glue.Add(anArgs(ind));
450 // Make block (hexahedral solid) from two opposite faces
451 if (anArgs(1).ShapeType() != TopAbs_FACE ||
452 anArgs(2).ShapeType() != TopAbs_FACE) {
453 Standard_TypeMismatch::Raise("Shape for block construction is not a face");
456 // Get wires of the given faces
457 TopExp_Explorer wires1 (anArgs(1), TopAbs_WIRE);
458 TopExp_Explorer wires2 (anArgs(2), TopAbs_WIRE);
459 if (!wires1.More() || !wires2.More()) {
460 Standard_ConstructionError::Raise("A face for the block has no wires");
462 TopoDS_Shape aWire1 = wires1.Current();
463 TopoDS_Shape aWire2 = wires2.Current();
466 if (wires1.More() || wires2.More()) {
467 Standard_ConstructionError::Raise("A face for the block has more than one wire");
470 GEOMImpl_Block6Explorer aBlockTool;
471 aBlockTool.InitByTwoFaces(anArgs(1), anArgs(2));
473 // Construct the linking faces and add them in the gluing tool
475 Glue.Add(aBlockTool.GetFace(3, Standard_True));
476 Glue.Add(aBlockTool.GetFace(4, Standard_True));
477 Glue.Add(aBlockTool.GetFace(5, Standard_True));
478 Glue.Add(aBlockTool.GetFace(6, Standard_True));
482 TopExp_Explorer exp (Glue.Shells(), TopAbs_SHELL);
483 Standard_Integer ish = 0;
484 TopTools_MapOfShape mapShape;
485 for (; exp.More(); exp.Next()) {
486 if (mapShape.Add(exp.Current())) {
487 aShape = exp.Current();
493 aShape = Glue.Shells();
494 Standard_Real aTol = prec; // Precision::Confusion()
495 TopExp_Explorer expF (aShape, TopAbs_FACE);
496 TopTools_MapOfShape mapF;
499 for (; expF.More(); expF.Next()) {
500 if (mapF.Add(expF.Current())) {
501 B.Add(Shell, expF.Current());
502 Standard_Real aToler = BRep_Tool::Tolerance(TopoDS::Face(expF.Current()));
507 ShHealOper_Sewing aHealer (Shell, aTol);
508 if (aHealer.Perform())
509 aShape = aHealer.GetResultShape();
511 Standard_ConstructionError::Raise
512 ("Impossible to build a connected shell on the given faces");
515 if (aType == BLOCK_SIX_FACES) {
516 if (!aShape.Closed()) {
517 Standard_ConstructionError::Raise
518 ("Impossible to build a closed shell on the given faces");
525 BRepClass3d_SolidClassifier SC (Sol);
526 SC.PerformInfinitePoint(prec);
527 if (SC.State() == TopAbs_IN) {
529 B.Add(Sol, aShape.Reversed());
532 BRepLib::SameParameter(aShape, 1.E-5, Standard_True);
534 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
544 aShape = GEOMImpl_GlueDriver::GlueFaces(aCompound, Precision::Confusion(), Standard_True);
545 //GEOMAlgo_Gluer aGluer;
546 //aGluer.SetShape(aCompound);
547 //aGluer.SetCheckGeometry(Standard_True);
549 //aShape = aGluer.Result();
554 } else { // Multi-transformations and compound improving
556 if (aType == BLOCK_REMOVE_EXTRA ||
557 aType == BLOCK_COMPOUND_IMPROVE) {
559 GEOMImpl_IBlockTrsf aCI (aFunction);
560 Handle(GEOM_Function) aRefShape = aCI.GetOriginal();
561 TopoDS_Shape aBlockOrComp = aRefShape->GetValue();
562 if (aBlockOrComp.IsNull()) {
563 Standard_NullObject::Raise("Null Shape given");
566 // Copy shape to avoid problems (Mantis issue 0021683)
567 TopoDS_Shape aShapeCopy;
568 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
569 TNaming_CopyShape::CopyTool(aBlockOrComp, aMapTShapes, aShapeCopy);
570 aBlockOrComp = aShapeCopy;
572 // 1. Improve solids with seam and/or degenerated edges
573 BlockFix_BlockFixAPI aTool;
574 //aTool.Tolerance() = toler;
575 aTool.OptimumNbFaces() = aCI.GetOptimumNbFaces();
576 aTool.SetShape(aBlockOrComp);
579 TopoDS_Shape aFixedExtra = aTool.Shape();
582 BRepCheck_Analyzer ana (aFixedExtra, false);
583 if (!ana.IsValid()) {
585 ShHealOper_ShapeProcess aHealer;
586 aHealer.Perform(aFixedExtra, aFixed);
587 if (aHealer.isDone())
588 aFixedExtra = aFixed;
591 if (aType == BLOCK_REMOVE_EXTRA)
593 aShape = aFixedExtra;
595 if (aShape == aBlockOrComp) {
596 MESSAGE("No modifications have been done");
599 else // aType == BLOCK_COMPOUND_IMPROVE
601 // 2. Separate non-blocks
602 TopTools_ListOfShape BLO; // All blocks from the given compound
603 TopTools_ListOfShape NOT; // Not blocks
604 TopTools_ListOfShape EXT; // Hexahedral solids, having degenerated and/or seam edges
605 TopTools_ListOfShape NOQ; // All non-quadrangular faces
606 GEOMImpl_IBlocksOperations::AddBlocksFrom(aFixedExtra, BLO, NOT, EXT, NOQ);
608 if (NOT.Extent() > 0) {
609 MESSAGE("Some non-blocks have been removed");
612 // 3. Warn about staying extra-edges
613 if (EXT.Extent() > 0) {
614 MESSAGE("Warning: Not all seam or degenerated edges was removed");
617 // ??? Throw away standalone blocks ???
619 // 4. Create compound of all blocks
620 TopoDS_Compound aComp;
622 BB.MakeCompound(aComp);
623 TopTools_ListIteratorOfListOfShape BLOit (BLO);
624 for (; BLOit.More(); BLOit.Next()) {
625 BB.Add(aComp, BLOit.Value());
629 aShape = GEOMImpl_GlueDriver::GlueFaces(aComp, Precision::Confusion(), Standard_False);
632 } else if (aType == BLOCK_MULTI_TRANSFORM_1D ||
633 aType == BLOCK_MULTI_TRANSFORM_2D) {
636 GEOMImpl_IBlockTrsf aCI (aFunction);
637 Handle(GEOM_Function) aRefShape = aCI.GetOriginal();
638 TopoDS_Shape aBlockIni = aRefShape->GetValue();
639 if (aBlockIni.IsNull()) {
640 Standard_NullObject::Raise("Null Block");
643 // Copy block to avoid problems (PAL6706)
644 TColStd_IndexedDataMapOfTransientTransient aMap;
646 TNaming_CopyShape::CopyTool(aBlockIni, aMap, aBlock);
648 // Block tolerance in vertices
649 Standard_Real aTol = prec;
650 TopExp_Explorer expV (aBlock, TopAbs_VERTEX);
651 TopTools_MapOfShape mapShape;
652 for (; expV.More(); expV.Next()) {
653 if (mapShape.Add(expV.Current())) {
654 TopoDS_Vertex aV = TopoDS::Vertex(expV.Current());
655 aTol = Max(BRep_Tool::Tolerance(aV), aTol);
659 if (aType == BLOCK_MULTI_TRANSFORM_1D) {
660 // Retrieve a faces by Ids
661 Standard_Integer aFace1Id = aCI.GetFace1U();
662 Standard_Integer aFace2Id = aCI.GetFace2U();
663 TopoDS_Shape aFace1, aFace2;
664 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1Id, aFace1)) {
665 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
667 if (aFace1.ShapeType() != TopAbs_FACE) {
668 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
672 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2Id, aFace2)) {
673 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
675 if (aFace2.ShapeType() != TopAbs_FACE) {
676 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
680 Standard_Integer aNbIter = aCI.GetNbIterU();
682 MultiTransformate1D(aBlock, aFace1, aFace2, aNbIter, aMulti);
684 } else { // aType == BLOCK_MULTI_TRANSFORM_2D
685 // Retrieve a faces by Ids
686 Standard_Integer aFace1UId = aCI.GetFace1U();
687 Standard_Integer aFace2UId = aCI.GetFace2U();
688 Standard_Integer aFace1VId = aCI.GetFace1V();
689 Standard_Integer aFace2VId = aCI.GetFace2V();
691 TopoDS_Shape aFace1U, aFace2U, aFace1V, aFace2V;
692 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1UId, aFace1U) ||
693 !GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace1VId, aFace1V)) {
694 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
697 if (aFace1U.ShapeType() != TopAbs_FACE ||
698 aFace1V.ShapeType() != TopAbs_FACE) {
699 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
703 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2UId, aFace2U)) {
704 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
707 if (aFace2U.ShapeType() != TopAbs_FACE) {
708 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
713 if (!GEOMImpl_ILocalOperations::GetSubShape(aBlock, aFace2VId, aFace2V)) {
714 Standard_NullObject::Raise("Can not retrieve a sub-shape with given Id");
717 if (aFace2V.ShapeType() != TopAbs_FACE) {
718 Standard_TypeMismatch::Raise("Sub-shape with given Id is not a face");
722 Standard_Integer aNbIterU = aCI.GetNbIterU();
723 Standard_Integer aNbIterV = aCI.GetNbIterV();
725 MultiTransformate2D(aBlock,
726 aFace1U, aFace2U, aNbIterU,
727 aFace1V, aFace2V, aNbIterV, aMulti);
730 if (aMulti.IsNull()) {
731 StdFail_NotDone::Raise("Multi-transformation failed");
734 // Glue faces of the multi-block
735 aShape = GEOMImpl_GlueDriver::GlueFaces(aMulti, aTol, Standard_False);
737 } else if (aType == BLOCK_UNION_FACES) {
738 GEOMImpl_IBlockTrsf aCI (aFunction);
739 Handle(GEOM_Function) aRefShape = aCI.GetOriginal();
740 TopoDS_Shape aBlockOrComp = aRefShape->GetValue();
741 if (aBlockOrComp.IsNull()) {
742 Standard_NullObject::Raise("Null Shape given");
745 BlockFix_UnionFaces aFaceUnifier;
747 aFaceUnifier.GetOptimumNbFaces() = 0; // To force union faces.
748 aShape = aFaceUnifier.Perform(aBlockOrComp);
749 } else { // unknown function type
754 if (aShape.IsNull()) return 0;
756 aFunction->SetValue(aShape);
758 #if OCC_VERSION_MAJOR < 7
759 log.SetTouched(Label());
761 log->SetTouched(Label());
767 //=======================================================================
768 //function : MultiTransformate1D
770 //=======================================================================
771 void GEOMImpl_BlockDriver::MultiTransformate1D (const TopoDS_Shape& theBlock,
772 const TopoDS_Shape& theFace1,
773 const TopoDS_Shape& theFace2,
774 const Standard_Integer theNbIter,
775 TopoDS_Shape& theResult) const
777 // Construct Tool, where <theFace1> will be the first face,
778 // and a face, opposite to <theFace1>, will be the second face
779 GEOMImpl_Block6Explorer aBlockTool;
780 aBlockTool.InitByBlockAndFace(theBlock, theFace1);
782 // Find IDs of the faces
783 Standard_Integer dir_face1 = 1, dir_face2 = 2;
784 if (!theFace2.IsNull()) {
785 dir_face2 = aBlockTool.GetFaceID(theFace2);
788 // Find three pairs of points
789 Standard_Integer v11_id = 0, v12_id = 0, v13_id = 0; // vertices of the first face
790 Standard_Integer v21_id = 0, v22_id = 0, v23_id = 0; // vertices of the second face
792 if (dir_face2 == 2) { // <theFace2> is opposite to <theFace1>
794 // We will take vertices with equal local numbers on the faces,
795 // as the Block6Explorer gives equal local numbers
796 // to the linked vertices on the opposite faces,
797 // i.e. v1* is linked with the v2* by an edge:
801 // / | / | dir_face1 - bottom
803 // /________ / | dir_face2 - top
812 v11_id = aBlockTool.GetVertexID(dir_face1, 1);
813 v12_id = aBlockTool.GetVertexID(dir_face1, 2);
814 v13_id = aBlockTool.GetVertexID(dir_face1, 4);
816 v21_id = aBlockTool.GetVertexID(dir_face2, 1);
817 v22_id = aBlockTool.GetVertexID(dir_face2, 2);
818 v23_id = aBlockTool.GetVertexID(dir_face2, 4);
824 // / | / | dir_face1 - bottom
826 // /________ / | dir_face2 - right (for example)
829 // | / | /v12 = v22 (common_vertex2)
833 // v13 v11 = v21 (common_vertex1)
835 Standard_Integer common_edge_id = aBlockTool.FindCommonEdgeID(dir_face1, dir_face2);
836 Standard_Integer common_vertex1 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 1);
837 Standard_Integer common_vertex2 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 2);
839 Standard_Integer not_common_v1 = 0;
840 Standard_Integer vid = 1;
841 Standard_Boolean isFound = Standard_False;
842 while (!isFound && vid <= 4) {
843 not_common_v1 = aBlockTool.GetVertexID(dir_face1, vid);
844 isFound = (not_common_v1 != common_vertex2 &&
845 aBlockTool.FindEdgeID(not_common_v1, common_vertex1) != 0);
849 Standard_Integer not_common_v2 = 0;
851 isFound = Standard_False;
852 while (!isFound && vid <= 4) {
853 not_common_v2 = aBlockTool.GetVertexID(dir_face2, vid);
854 isFound = (not_common_v2 != common_vertex2 &&
855 aBlockTool.FindEdgeID(not_common_v2, common_vertex1) != 0);
859 v11_id = common_vertex1;
860 v12_id = common_vertex2;
861 v13_id = not_common_v1;
863 v21_id = common_vertex1;
864 v22_id = common_vertex2;
865 v23_id = not_common_v2;
868 // Construct a transformation operator
869 TopoDS_Vertex V11 = TopoDS::Vertex(aBlockTool.GetVertex(v11_id));
870 TopoDS_Vertex V12 = TopoDS::Vertex(aBlockTool.GetVertex(v12_id));
871 TopoDS_Vertex V13 = TopoDS::Vertex(aBlockTool.GetVertex(v13_id));
873 TopoDS_Vertex V21 = TopoDS::Vertex(aBlockTool.GetVertex(v21_id));
874 TopoDS_Vertex V22 = TopoDS::Vertex(aBlockTool.GetVertex(v22_id));
875 TopoDS_Vertex V23 = TopoDS::Vertex(aBlockTool.GetVertex(v23_id));
877 // Axes of the first direction face
878 gp_Pnt P1 = BRep_Tool::Pnt(V11);
879 gp_Vec VecN1 (P1, BRep_Tool::Pnt(V12));
880 gp_Vec VecX1 (P1, BRep_Tool::Pnt(V13));
881 gp_Ax3 Ax1 (P1, VecN1, VecX1);
883 // Axes of the second direction face
884 gp_Pnt P2 = BRep_Tool::Pnt(V21);
885 gp_Vec VecN2 (P2, BRep_Tool::Pnt(V22));
886 gp_Vec VecX2 (P2, BRep_Tool::Pnt(V23));
887 gp_Ax3 Ax2 (P2, VecN2, VecX2);
890 aTrsf.SetDisplacement(Ax1, Ax2);
892 // Check, that <theFace2> is similar to <theFace1>.
893 // Actually, we need only to check right position of one
894 // vertex, not involved into the transformation construction.
895 if (!aBlockTool.IsSimilarFaces(dir_face1, dir_face2, aTrsf)) {
896 Standard_ConstructionError::Raise("The direction faces are not similar");
899 // Perform multi-transformation
900 TopoDS_Compound aCompound;
902 B.MakeCompound(aCompound);
904 TopoDS_Shape aPrevShape = theBlock;
905 for (Standard_Integer i = 0; i < theNbIter; i++) {
906 B.Add(aCompound, aPrevShape);
907 BRepBuilderAPI_Transform aTransformation (aPrevShape, aTrsf, Standard_False);
908 aPrevShape = aTransformation.Shape();
910 theResult = aCompound;
913 //=======================================================================
914 //function : MultiTransformate2D
916 //=======================================================================
917 void GEOMImpl_BlockDriver::MultiTransformate2D (const TopoDS_Shape& theBlock,
918 const TopoDS_Shape& theFace1U,
919 const TopoDS_Shape& theFace2U,
920 const Standard_Integer theNbIterU,
921 const TopoDS_Shape& theFace1V,
922 const TopoDS_Shape& theFace2V,
923 const Standard_Integer theNbIterV,
924 TopoDS_Shape& theResult) const
926 // Construct Tool, where <theFace1U> will be the first face,
927 // and a face, opposite to <theFace1U>, will be the second face
928 GEOMImpl_Block6Explorer aBlockTool;
929 aBlockTool.InitByBlockAndFace(theBlock, theFace1U);
931 gp_Trsf aTrsfU, aTrsfV;
933 for (Standard_Integer uv = 1; uv <= 2; uv++) {
935 TopoDS_Shape theFace1 = theFace1U;
936 TopoDS_Shape theFace2 = theFace2U;
939 theFace1 = theFace1V;
940 theFace2 = theFace2V;
943 // Find IDs of the faces
944 Standard_Integer dir_face1 = aBlockTool.GetFaceID(theFace1);
945 Standard_Integer dir_face2 = 0;
946 Standard_Integer opp_face1 = aBlockTool.GetOppositeFaceID(dir_face1);
947 if (theFace2.IsNull()) {
948 dir_face2 = opp_face1;
950 dir_face2 = aBlockTool.GetFaceID(theFace2);
953 // Find three pairs of points
954 Standard_Integer v11_id = 0, v12_id = 0, v13_id = 0; // vertices of the first face
955 Standard_Integer v21_id = 0, v22_id = 0, v23_id = 0; // vertices of the second face
957 if (dir_face2 == opp_face1) { // <theFace2> is opposite to <theFace1>
959 // We will take vertices with equal local numbers on the faces,
960 // as the Block6Explorer gives equal local numbers
961 // to the linked vertices on the opposite faces,
962 // i.e. v1* is linked with the v2* by an edge:
964 v11_id = aBlockTool.GetVertexID(dir_face1, 1);
965 v12_id = aBlockTool.GetVertexID(dir_face1, 2);
966 v13_id = aBlockTool.GetVertexID(dir_face1, 4);
968 v21_id = aBlockTool.GetVertexID(dir_face2, 1);
969 v22_id = aBlockTool.GetVertexID(dir_face2, 2);
970 v23_id = aBlockTool.GetVertexID(dir_face2, 4);
974 Standard_Integer common_edge_id = aBlockTool.FindCommonEdgeID(dir_face1, dir_face2);
975 Standard_Integer common_vertex1 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 1);
976 Standard_Integer common_vertex2 = aBlockTool.GetVertexOnEdgeID(common_edge_id, 2);
978 Standard_Integer not_common_v1 = 0;
979 Standard_Integer vid = 1;
980 Standard_Boolean isFound = Standard_False;
981 while (!isFound && vid <= 4) {
982 not_common_v1 = aBlockTool.GetVertexID(dir_face1, vid);
983 isFound = (not_common_v1 != common_vertex2 &&
984 aBlockTool.FindEdgeID(not_common_v1, common_vertex1) != 0);
988 Standard_Integer not_common_v2 = 0;
990 isFound = Standard_False;
991 while (!isFound && vid <= 4) {
992 not_common_v2 = aBlockTool.GetVertexID(dir_face2, vid);
993 isFound = (not_common_v2 != common_vertex2 &&
994 aBlockTool.FindEdgeID(not_common_v2, common_vertex1) != 0);
998 v11_id = common_vertex1;
999 v12_id = common_vertex2;
1000 v13_id = not_common_v1;
1002 v21_id = common_vertex1;
1003 v22_id = common_vertex2;
1004 v23_id = not_common_v2;
1007 // Construct a transformation operator
1008 TopoDS_Vertex V11 = TopoDS::Vertex(aBlockTool.GetVertex(v11_id));
1009 TopoDS_Vertex V12 = TopoDS::Vertex(aBlockTool.GetVertex(v12_id));
1010 TopoDS_Vertex V13 = TopoDS::Vertex(aBlockTool.GetVertex(v13_id));
1012 TopoDS_Vertex V21 = TopoDS::Vertex(aBlockTool.GetVertex(v21_id));
1013 TopoDS_Vertex V22 = TopoDS::Vertex(aBlockTool.GetVertex(v22_id));
1014 TopoDS_Vertex V23 = TopoDS::Vertex(aBlockTool.GetVertex(v23_id));
1016 // Axes of the first direction face
1017 gp_Pnt P1 = BRep_Tool::Pnt(V11);
1018 gp_Vec VecN1 (P1, BRep_Tool::Pnt(V12));
1019 gp_Vec VecX1 (P1, BRep_Tool::Pnt(V13));
1020 gp_Ax3 Ax1 (P1, VecN1, VecX1);
1022 // Axes of the second direction face
1023 gp_Pnt P2 = BRep_Tool::Pnt(V21);
1024 gp_Vec VecN2 (P2, BRep_Tool::Pnt(V22));
1025 gp_Vec VecX2 (P2, BRep_Tool::Pnt(V23));
1026 gp_Ax3 Ax2 (P2, VecN2, VecX2);
1029 aTrsf.SetDisplacement(Ax1, Ax2);
1038 // Check, that <theFace2> is similar to <theFace1>.
1039 // Actually, we need only to check right position of one
1040 // vertex, not involved into the transformation construction.
1041 if (!aBlockTool.IsSimilarFaces(dir_face1, dir_face2, aTrsf)) {
1042 Standard_ConstructionError::Raise("The direction faces are not similar");
1046 // Perform multi-transformation
1047 TopoDS_Compound aCompound;
1049 B.MakeCompound(aCompound);
1051 TopoDS_Shape aPrevShapeU = theBlock;
1052 for (int i = 0; i < theNbIterU; i++) {
1053 TopoDS_Shape aPrevShapeV = aPrevShapeU;
1054 for (int j = 0; j < theNbIterV; j++) {
1055 B.Add(aCompound, aPrevShapeV);
1056 BRepBuilderAPI_Transform aTransformationV (aPrevShapeV, aTrsfV, Standard_False);
1057 aPrevShapeV = aTransformationV.Shape();
1059 BRepBuilderAPI_Transform aTransformationU (aPrevShapeU, aTrsfU, Standard_False);
1060 aPrevShapeU = aTransformationU.Shape();
1061 // Correction of the second transformation according to the first transformation
1062 Ax1V.Transform(aTrsfU);
1063 Ax2V.Transform(aTrsfU);
1064 aTrsfV.SetDisplacement(Ax1V, Ax2V);
1067 theResult = aCompound;
1070 //================================================================================
1072 * \brief Returns a name of creation operation and names and values of creation parameters
1074 //================================================================================
1076 bool GEOMImpl_BlockDriver::
1077 GetCreationInformation(std::string& theOperationName,
1078 std::vector<GEOM_Param>& theParams)
1080 if (Label().IsNull()) return 0;
1081 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
1083 Standard_Integer aType = function->GetType();
1084 GEOMImpl_IBlocks aCI1 (function);
1085 GEOMImpl_IBlockTrsf aCI2 (function);
1086 Handle(TColStd_HSequenceOfTransient) aShapes = aCI1.GetShapes();
1089 case BLOCK_FACE_FOUR_EDGES:
1090 theOperationName = "Q_FACE";
1091 AddParam( theParams, "Edge 1", aShapes->Value(1) );
1092 AddParam( theParams, "Edge 2", aShapes->Value(2) );
1093 AddParam( theParams, "Edge 3", aShapes->Value(3) );
1094 AddParam( theParams, "Edge 4", aShapes->Value(4) );
1096 case BLOCK_FACE_TWO_EDGES:
1097 theOperationName = "Q_FACE";
1098 AddParam( theParams, "Edge 1", aShapes->Value(1) );
1099 AddParam( theParams, "Edge 2", aShapes->Value(2) );
1101 case BLOCK_FACE_FOUR_PNT:
1102 theOperationName = "Q_FACE";
1103 AddParam( theParams, "Point 1", aShapes->Value(1) );
1104 AddParam( theParams, "Point 2", aShapes->Value(2) );
1105 AddParam( theParams, "Point 3", aShapes->Value(3) );
1106 AddParam( theParams, "Point 4", aShapes->Value(4) );
1108 case BLOCK_SIX_FACES:
1109 theOperationName = "HEX_SOLID";
1110 AddParam( theParams, "Face 1", aShapes->Value(1) );
1111 AddParam( theParams, "Face 2", aShapes->Value(2) );
1112 AddParam( theParams, "Face 3", aShapes->Value(3) );
1113 AddParam( theParams, "Face 4", aShapes->Value(4) );
1114 AddParam( theParams, "Face 5", aShapes->Value(5) );
1115 AddParam( theParams, "Face 6", aShapes->Value(6) );
1117 case BLOCK_TWO_FACES:
1118 theOperationName = "HEX_SOLID";
1119 AddParam( theParams, "Face 1", aShapes->Value(1) );
1120 AddParam( theParams, "Face 2", aShapes->Value(2) );
1122 case BLOCK_COMPOUND_GLUE:
1123 theOperationName = "MakeBlockCompound";
1124 AddParam( theParams, "Compound", aShapes->Value(1) );
1126 case BLOCK_REMOVE_EXTRA:
1127 theOperationName = "REMOVE_EXTRA_EDGES";
1128 AddParam( theParams, "Selected shape", aCI2.GetOriginal() );
1129 AddParam( theParams, "Union faces", aCI2.GetOptimumNbFaces() == 0);
1131 case BLOCK_COMPOUND_IMPROVE:
1132 theOperationName = "CHECK_COMPOUND";
1133 AddParam( theParams, "Selected shape", aCI2.GetOriginal() );
1135 case BLOCK_MULTI_TRANSFORM_1D:
1136 theOperationName = "MUL_TRANSFORM";
1137 AddParam( theParams, "Main Object", aCI2.GetOriginal() );
1138 AddParam( theParams, "Face 1", aCI2.GetFace1U() );
1139 AddParam( theParams, "Face 2", aCI2.GetFace2U() );
1140 AddParam( theParams, "Nb. Times", aCI2.GetNbIterU() );
1142 case BLOCK_MULTI_TRANSFORM_2D:
1143 theOperationName = "MUL_TRANSFORM";
1144 AddParam( theParams, "Main Object", aCI2.GetOriginal() );
1145 AddParam( theParams, "Face 1 U", aCI2.GetFace1U() );
1146 AddParam( theParams, "Face 2 U", aCI2.GetFace2U() );
1147 AddParam( theParams, "Nb. Times V", aCI2.GetNbIterV() );
1148 AddParam( theParams, "Face 1 V", aCI2.GetFace1V() );
1149 AddParam( theParams, "Face 2 V", aCI2.GetFace2V() );
1150 AddParam( theParams, "Nb. Times V", aCI2.GetNbIterV() );
1152 case BLOCK_UNION_FACES:
1153 theOperationName = "UNION_FACES";
1154 AddParam( theParams, "Selected shape", aCI2.GetOriginal() );
1163 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_BlockDriver,GEOM_BaseDriver);