1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 // abv 28.04.99 S4137: ading method Apply for work on all types of shapes
15 // sln 29.11.01 Bug24: correction iteration through map in method 'Status'
16 // sln 29.11.01 Bug22: correction of methods Replace and Value for case when mode myConsiderLocation is on
18 #include <BRep_Builder.hxx>
19 #include <BRep_GCurve.hxx>
20 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
21 #include <BRep_ListOfCurveRepresentation.hxx>
22 #include <BRep_TEdge.hxx>
23 #include <BRep_Tool.hxx>
24 #include <BRepTools_ReShape.hxx>
25 #include <Geom_Surface.hxx>
26 #include <NCollection_IndexedMap.hxx>
27 #include <Standard_Type.hxx>
28 #include <TopExp_Explorer.hxx>
29 #include <TopLoc_Location.hxx>
31 #include <TopoDS_Compound.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Face.hxx>
34 #include <TopoDS_Iterator.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Shell.hxx>
37 #include <TopoDS_Solid.hxx>
39 IMPLEMENT_STANDARD_RTTIEXT(BRepTools_ReShape,Standard_Transient)
44 //! Adds the shape to the map.
45 //! If the shape is a wire, shell or solid then
46 //! adds the sub-shapes of the shape instead.
47 //! Returns 'true' if the sub-shapes were added.
48 template<typename TMap>
49 void Add(TMap& theMap, const TopoDS_Shape& theShape)
51 const TopAbs_ShapeEnum aType = theShape.ShapeType();
52 if (aType != TopAbs_WIRE && aType != TopAbs_SHELL &&
53 aType != TopAbs_COMPSOLID)
59 for (TopoDS_Iterator aIt(theShape); aIt.More(); aIt.Next())
61 theMap.Add(aIt.Value());
67 //include <ShapeExtend.hxx>
68 //#include <BRepTools_Edge.hxx>
69 static void CopyRanges (const TopoDS_Shape& toedge, const TopoDS_Shape& fromedge,
70 const Standard_Real alpha, const Standard_Real beta)
72 Handle(BRep_TEdge) aTEdgeFrom = Handle(BRep_TEdge)::DownCast(fromedge.TShape());
73 Handle(BRep_TEdge) aTEdgeTo = Handle(BRep_TEdge)::DownCast(toedge.TShape());
74 BRep_ListOfCurveRepresentation& tolist = aTEdgeTo->ChangeCurves();
75 BRep_ListIteratorOfListOfCurveRepresentation fromitcr (aTEdgeFrom->ChangeCurves());
76 for (; fromitcr.More(); fromitcr.Next()) {
77 Handle(BRep_GCurve) fromGC = Handle(BRep_GCurve)::DownCast(fromitcr.Value());
78 if ( fromGC.IsNull() ) continue;
79 Standard_Boolean isC3d = fromGC->IsCurve3D();
81 if(fromGC->Curve3D().IsNull()) continue; }
83 if(fromGC->PCurve().IsNull()) continue; }
85 if ( ! isC3d && ! fromGC->IsCurveOnSurface()) continue; // only 3d curves and pcurves are treated
87 Handle(Geom_Surface) surface;
90 surface = fromGC->Surface();
91 L = fromGC->Location();
94 Handle(BRep_GCurve) toGC;
95 for (BRep_ListIteratorOfListOfCurveRepresentation toitcr (tolist); toitcr.More(); toitcr.Next()) {
96 toGC = Handle(BRep_GCurve)::DownCast(toitcr.Value());
97 if ( toGC.IsNull() ) continue;
99 if ( ! toGC->IsCurve3D() ) continue;
101 else if ( ! toGC->IsCurveOnSurface() ||
102 surface != toGC->Surface() || L != toGC->Location() ) continue;
103 Standard_Real first = fromGC->First();
104 Standard_Real last = fromGC->Last();
105 Standard_Real len = last - first;
106 toGC->SetRange ( first+alpha*len, first+beta*len );
113 //=======================================================================
114 //function : BRepTools_ReShape
116 //=======================================================================
118 BRepTools_ReShape::BRepTools_ReShape()
120 myConsiderLocation = Standard_False;
124 //=======================================================================
127 //=======================================================================
129 void BRepTools_ReShape::Clear()
131 myShapeToReplacement.Clear();
136 //=======================================================================
139 //=======================================================================
141 void BRepTools_ReShape::Remove (const TopoDS_Shape& shape)
143 TopoDS_Shape nulshape;
144 replace(shape, nulshape, TReplacementKind_Remove);
147 //=======================================================================
150 //=======================================================================
152 void BRepTools_ReShape::replace (const TopoDS_Shape& ashape,
153 const TopoDS_Shape& anewshape,
154 const TReplacementKind theKind)
156 TopoDS_Shape shape = ashape;
157 TopoDS_Shape newshape = anewshape;
158 if ( shape.IsNull() || shape == newshape ) return;
160 if (shape.Orientation() == TopAbs_REVERSED)
165 // protect against INTERNAL or EXTERNAL shape
166 else if (shape.Orientation() == TopAbs_INTERNAL
167 || shape.Orientation() == TopAbs_EXTERNAL)
169 newshape.Orientation((newshape.Orientation() == shape.Orientation()) ?
170 TopAbs_FORWARD : TopAbs_REVERSED);
171 shape.Orientation(TopAbs_FORWARD);
174 if (myConsiderLocation) {
175 //sln 29.11.01 Bug22: Change location of 'newshape' in accordance with location of 'shape'
176 newshape.Location(newshape.Location().Multiplied(shape.Location().Inverted()));
177 TopLoc_Location nullLoc;
178 shape.Location ( nullLoc );
182 if ( IsRecorded ( shape ) && ((myConsiderLocation && ! Value ( shape ).IsPartner ( newshape )) ||
183 (!myConsiderLocation && ! Value ( shape ).IsSame ( newshape ))))
184 std::cout << "Warning: BRepTools_ReShape::Replace: shape already recorded" << std::endl;
187 myShapeToReplacement.Bind(shape, TReplacement(newshape, theKind));
188 myNewShapes.Add (newshape);
191 //=======================================================================
192 //function : IsRecorded
194 //=======================================================================
196 Standard_Boolean BRepTools_ReShape::IsRecorded (const TopoDS_Shape& ashape) const
198 TopoDS_Shape shape = ashape;
199 if (myConsiderLocation) {
200 TopLoc_Location nullLoc;
201 shape.Location ( nullLoc );
203 if (shape.IsNull()) return Standard_False;
204 return myShapeToReplacement.IsBound (shape);
208 //=======================================================================
211 //=======================================================================
213 TopoDS_Shape BRepTools_ReShape::Value (const TopoDS_Shape& ashape) const
216 if (ashape.IsNull()) return res;
217 TopoDS_Shape shape = ashape;
218 if (myConsiderLocation) {
219 TopLoc_Location nullLoc;
220 shape.Location ( nullLoc );
223 Standard_Boolean fromMap = Standard_False;
224 if (!myShapeToReplacement.IsBound(shape))
230 res = myShapeToReplacement(shape).Result();
231 if (shape.Orientation() == TopAbs_REVERSED)
235 fromMap = Standard_True;
237 // for INTERNAL/EXTERNAL, since they are not fully supported, keep orientation
238 if ( shape.Orientation() == TopAbs_INTERNAL ||
239 shape.Orientation() == TopAbs_EXTERNAL )
240 res.Orientation ( shape.Orientation() );
242 if (myConsiderLocation) {
243 //sln 29.11.01 Bug22: Recalculate location of resulting shape in accordance with
244 //whether result is from map or not
245 if(fromMap) res.Location(ashape.Location()*res.Location());
246 else res.Location(ashape.Location());
253 //=======================================================================
256 //=======================================================================
258 Standard_Integer BRepTools_ReShape::Status(const TopoDS_Shape& ashape,
260 const Standard_Boolean last)
262 Standard_Integer res = 0;
263 if (ashape.IsNull()) { newsh.Nullify(); return res; }
265 TopoDS_Shape shape = ashape;
266 TopLoc_Location aLocSh = shape.Location();
267 if (myConsiderLocation) {
268 TopLoc_Location nullLoc;
269 shape.Location ( nullLoc );
272 if (!myShapeToReplacement.IsBound(shape))
279 newsh = myShapeToReplacement(shape).Result();
283 if (newsh.IsNull()) res = -1;
284 else if (newsh.IsEqual (shape)) res = 0;
285 else if ( last && ((myConsiderLocation && ! newsh.IsPartner (shape)) ||
286 (!myConsiderLocation && ! newsh.IsSame (shape)))) {
287 //TopoDS_Shape newnewsh;
288 //Standard_Integer newres = Status (newsh, newnewsh, last);
290 //if (newres) res = newres;
291 // sln 29.11.01 Bug24: Correction iteration through maps. Way of iteration used early does not
292 // correspond to way of storing information in the maps.
293 newsh = Apply(shape, TopAbs_SHAPE);
294 if (newsh.IsNull()) res = -1;
295 if (newsh.IsEqual (shape)) res = 0;
298 if(myConsiderLocation && !newsh.IsNull())
300 TopLoc_Location aResLoc = (res >0 && !newsh.Location().IsIdentity() ?
301 aLocSh * newsh.Location() : aLocSh);
302 newsh.Location(aResLoc);
307 //=======================================================================
308 //function : EncodeStatus
310 //=======================================================================
311 static Standard_Integer EncodeStatus (const Standard_Integer status)
314 case 0 : return 0x0000; //ShapeExtend_OK
315 case 1: return 0x0001; //ShapeExtend_DONE1
316 case 2: return 0x0002; //....
317 case 3: return 0x0004;
318 case 4: return 0x0008;
319 case 5: return 0x0010;
320 case 6: return 0x0020;
321 case 7: return 0x0040;
322 case 8: return 0x0080; //....
323 case 9 : return 0x00ff; //ShapeExtend_DONE
324 case 10: return 0x0100; //ShapeExtend_FAIL1
325 case 11: return 0x0200; //...
326 case 12: return 0x0400;
327 case 13: return 0x0800;
328 case 14: return 0x1000;
329 case 15: return 0x2000;
330 case 16: return 0x4000;
331 case 17: return 0x8000; //....
332 case 18 : return 0xff00; //ShapeExtend_FAIL
338 //=======================================================================
341 //=======================================================================
343 TopoDS_Shape BRepTools_ReShape::Apply (const TopoDS_Shape& shape,
344 const TopAbs_ShapeEnum until)
346 myStatus = EncodeStatus(0); //ShapeExtend::EncodeStatus ( ShapeExtend_OK );
347 if ( shape.IsNull() ) return shape;
349 // apply direct replacement
350 TopoDS_Shape newsh = Value ( shape );
352 // if shape removed, return NULL
353 if ( newsh.IsNull() ) {
354 myStatus = EncodeStatus (2); //ShapeExtend_DONE2
358 // if shape replaced, apply modifications to the result recursively
359 if ( (myConsiderLocation && ! newsh.IsPartner (shape)) ||
360 (!myConsiderLocation &&! newsh.IsSame ( shape )) ) {
361 TopoDS_Shape res = Apply ( newsh, until );
362 myStatus |= EncodeStatus(1); //ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
366 TopAbs_ShapeEnum st = shape.ShapeType(); //, subt;
367 if ( st >= until ) return newsh; // critere d arret
368 if(st == TopAbs_VERTEX || st == TopAbs_SHAPE)
370 // define allowed types of components
371 //fix for SAMTECH bug OCC322 about abcense internal vertices after sewing.
374 case TopAbs_COMPOUND: subt = TopAbs_SHAPE; break;
375 case TopAbs_COMPSOLID: subt = TopAbs_SOLID; break;
376 case TopAbs_SOLID: subt = TopAbs_SHELL; break;
377 case TopAbs_SHELL: subt = TopAbs_FACE; break;
378 case TopAbs_FACE: subt = TopAbs_WIRE; break;
379 case TopAbs_WIRE: subt = TopAbs_EDGE; break;
380 case TopAbs_EDGE: subt = TopAbs_VERTEX; break;
383 default: return shape;
388 TopoDS_Shape result = shape.EmptyCopied();
389 TopAbs_Orientation orien = shape.Orientation();
390 result.Orientation(TopAbs_FORWARD); // protect against INTERNAL or EXTERNAL shapes
391 Standard_Boolean modif = Standard_False;
392 Standard_Integer locStatus = myStatus;
394 // apply recorded modifications to subshapes
395 Standard_Boolean isEmpty = Standard_True;
396 for ( TopoDS_Iterator it(shape,Standard_False); it.More(); it.Next() ) {
397 TopoDS_Shape sh = it.Value();
398 newsh = Apply ( sh, until );
400 if ( myStatus & EncodeStatus(4)) //ShapeExtend::DecodeStatus ( myStatus, ShapeExtend_DONE4 ) )
401 locStatus |= EncodeStatus(4); //|= ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
404 if ( newsh.IsNull() ) {
405 locStatus |= EncodeStatus(4); //ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
409 isEmpty = Standard_False;
410 locStatus |= EncodeStatus(3);//ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
411 if ( st == TopAbs_COMPOUND || newsh.ShapeType() == sh.ShapeType()) { //fix for SAMTECH bug OCC322 about abcense internal vertices after sewing.
412 B.Add ( result, newsh );
415 Standard_Integer nitems = 0;
416 for ( TopoDS_Iterator subit(newsh); subit.More(); subit.Next(), nitems++ ) {
417 TopoDS_Shape subsh = subit.Value();
418 if ( subsh.ShapeType() == sh.ShapeType() ) B.Add ( result, subsh );//fix for SAMTECH bug OCC322 about abcense internal vertices after sewing.
419 else locStatus |= EncodeStatus(10);//ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
421 if ( ! nitems ) locStatus |= EncodeStatus(10);//ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
423 if ( ! modif ) return shape;
425 // For empty topological containers (any kind of shape except vertex, edge
426 // and face) we have to produce an empty result
427 if ( isEmpty && st != TopAbs_VERTEX && st != TopAbs_EDGE && st != TopAbs_FACE )
429 result = TopoDS_Shape();
433 // restore Range on edge broken by EmptyCopied()
434 if ( st == TopAbs_EDGE ) {
435 CopyRanges (result, shape, 0, 1);
437 else if (st == TopAbs_FACE) {
438 TopoDS_Face face = TopoDS::Face ( shape );
439 if( BRep_Tool::NaturalRestriction( face ) ) {
441 aB.NaturalRestriction( TopoDS::Face ( result ), Standard_True );
444 else if (st == TopAbs_WIRE || st == TopAbs_SHELL)
445 result.Closed (BRep_Tool::IsClosed (result));
447 result.Orientation(orien);
450 replace(shape, result,
451 result.IsNull() ? TReplacementKind_Remove : TReplacementKind_Modify);
452 myStatus = locStatus;
458 //=======================================================================
461 //=======================================================================
463 /*Standard_Boolean BRepTools_ReShape::Status (const ShapeExtend_Status status) const
465 return ShapeExtend::DecodeStatus ( myStatus, status );
468 //=======================================================================
469 //function : CopyVertex
471 //=======================================================================
473 TopoDS_Vertex BRepTools_ReShape::CopyVertex(const TopoDS_Vertex& theV,
474 const Standard_Real theTol)
476 return CopyVertex(theV, BRep_Tool::Pnt(theV), theTol);
479 //=======================================================================
480 //function : CopyVertex
482 //=======================================================================
484 TopoDS_Vertex BRepTools_ReShape::CopyVertex(const TopoDS_Vertex& theV,
485 const gp_Pnt& theNewPos,
486 const Standard_Real theTol)
488 TopoDS_Vertex aVertexCopy;
489 Standard_Boolean isRecorded = IsRecorded(theV);
490 aVertexCopy = isRecorded ? TopoDS::Vertex(Apply(theV)) : TopoDS::Vertex(theV.EmptyCopied());
493 Standard_Real aNewTol = theTol > 0.0 ? theTol : BRep_Tool::Tolerance(theV);
494 B.UpdateVertex(aVertexCopy, theNewPos, aNewTol);
497 Replace(theV, aVertexCopy);
502 Standard_Boolean BRepTools_ReShape::IsNewShape(const TopoDS_Shape& theShape) const
504 return myNewShapes.Contains(theShape);
507 //=======================================================================
510 //=======================================================================
512 Handle(BRepTools_History) BRepTools_ReShape::History() const
514 Handle(BRepTools_History) aHistory = new BRepTools_History;
517 for (TShapeToReplacement::Iterator aRIt(myShapeToReplacement);
518 aRIt.More(); aRIt.Next())
520 const TopoDS_Shape& aShape = aRIt.Key();
521 if (!BRepTools_History::IsSupportedType(aShape) ||
522 myNewShapes.Contains(aShape))
527 NCollection_IndexedMap<TopoDS_Shape> aIntermediates;
528 NCollection_Map<TopoDS_Shape> aModified;
529 aIntermediates.Add(aShape);
530 for (Standard_Integer aI = 1; aI <= aIntermediates.Size(); ++aI)
532 const TopoDS_Shape& aIntermediate = aIntermediates(aI);
533 const TReplacement* aReplacement =
534 myShapeToReplacement.Seek(aIntermediate);
535 if (aReplacement == NULL)
537 Add(aModified, aIntermediate);
539 else if (aReplacement->RelationKind() !=
540 BRepTools_History::TRelationType_Removed)
542 const TopoDS_Shape aResult = aReplacement->RelationResult();
543 if (!aResult.IsNull())
545 Add(aIntermediates, aResult);
550 if (aModified.IsEmpty())
552 aHistory->Remove(aShape);
556 for (NCollection_Map<TopoDS_Shape>::Iterator aIt(aModified);
557 aIt.More(); aIt.Next())
559 aHistory->AddModified(aShape, aIt.Value());