1 // Created on: 2000-01-19
2 // Created by: data exchange team
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <BRep_Builder.hxx>
18 #include <BRep_Tool.hxx>
19 #include <BRepBuilderAPI_MakeEdge.hxx>
20 #include <Geom2d_Curve.hxx>
21 #include <Geom_Curve.hxx>
22 #include <Geom_Surface.hxx>
24 #include <gp_Pnt2d.hxx>
25 #include <Interface_Static.hxx>
26 #include <Message_ListIteratorOfListOfMsg.hxx>
27 #include <Message_ListOfMsg.hxx>
28 #include <Message_Msg.hxx>
29 #include <Resource_Manager.hxx>
30 #include <ShapeAlgo.hxx>
31 #include <ShapeAlgo_AlgoContainer.hxx>
32 #include <ShapeAlgo_ToolContainer.hxx>
33 #include <ShapeAnalysis_Edge.hxx>
34 #include <ShapeBuild_Edge.hxx>
35 #include <ShapeBuild_ReShape.hxx>
36 #include <ShapeCustom.hxx>
37 #include <ShapeExtend_DataMapOfShapeListOfMsg.hxx>
38 #include <ShapeExtend_MsgRegistrator.hxx>
39 #include <ShapeFix_Edge.hxx>
40 #include <ShapeFix_Shape.hxx>
41 #include <ShapeProcess.hxx>
42 #include <ShapeProcess_ShapeContext.hxx>
43 #include <Standard_ErrorHandler.hxx>
44 #include <Standard_Failure.hxx>
45 #include <Standard_Transient.hxx>
46 #include <Standard_Type.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Face.hxx>
51 #include <TopoDS_Iterator.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TopoDS_Vertex.hxx>
54 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
55 #include <TopTools_DataMapOfShapeShape.hxx>
56 #include <Transfer_FinderProcess.hxx>
57 #include <Transfer_TransientListBinder.hxx>
58 #include <Transfer_TransientProcess.hxx>
59 #include <TransferBRep.hxx>
60 #include <TransferBRep_ShapeBinder.hxx>
61 #include <TransferBRep_ShapeMapper.hxx>
62 #include <UnitsMethods.hxx>
63 #include <XSAlgo_AlgoContainer.hxx>
64 #include <XSAlgo_ToolContainer.hxx>
65 #include <TopExp_Explorer.hxx>
67 IMPLEMENT_STANDARD_RTTIEXT(XSAlgo_AlgoContainer,Standard_Transient)
69 //=======================================================================
70 //function : XSAlgo_AlgoContainer
72 //=======================================================================
73 XSAlgo_AlgoContainer::XSAlgo_AlgoContainer()
75 myTC = new XSAlgo_ToolContainer;
78 //=======================================================================
79 //function : PrepareForTransfer
81 //=======================================================================
83 void XSAlgo_AlgoContainer::PrepareForTransfer() const
85 UnitsMethods::SetCasCadeLengthUnit(Interface_Static::IVal("xstep.cascade.unit"));
88 //=======================================================================
89 //function : ProcessShape
91 //=======================================================================
93 TopoDS_Shape XSAlgo_AlgoContainer::ProcessShape (const TopoDS_Shape& shape,
94 const Standard_Real Prec,
95 const Standard_Real maxTol,
96 const Standard_CString prscfile,
97 const Standard_CString pseq,
98 Handle(Standard_Transient)& info,
99 const Message_ProgressRange& theProgress,
100 const Standard_Boolean NonManifold) const
102 if ( shape.IsNull() ) return shape;
104 Handle(ShapeProcess_ShapeContext) context = Handle(ShapeProcess_ShapeContext)::DownCast(info);
105 if ( context.IsNull() )
107 Standard_CString rscfile = Interface_Static::CVal(prscfile);
110 context = new ShapeProcess_ShapeContext(shape, rscfile);
111 context->SetDetalisation(TopAbs_EDGE);
113 context->SetNonManifold(NonManifold);
116 Standard_CString seq = Interface_Static::CVal ( pseq );
117 if ( ! seq ) seq = pseq;
119 // if resource file is not loaded or does not define <seq>.exec.op,
121 Handle(Resource_Manager) rsc = context->ResourceManager();
122 TCollection_AsciiString str ( seq );
124 if ( ! rsc->Find ( str.ToCString() ) ) {
127 static Standard_Integer time = 0;
129 std::cout << "Warning: XSAlgo_AlgoContainer::ProcessShape(): Sequence " << str.ToCString() <<
130 " is not defined in " << prscfile << " resource; do default processing" << std::endl;
134 // if reading, do default ShapeFix
135 if ( ! strncmp ( pseq, "read.", 5 ) ) {
138 Handle(ShapeExtend_MsgRegistrator) msg = new ShapeExtend_MsgRegistrator;
139 Handle(ShapeFix_Shape) sfs = ShapeAlgo::AlgoContainer()->ToolContainer()->FixShape();
141 sfs->SetMsgRegistrator ( msg );
142 sfs->SetPrecision ( Prec );
143 sfs->SetMaxTolerance ( maxTol );
144 sfs->FixFaceTool()->FixWireTool()->FixSameParameterMode() = Standard_False;
145 sfs->FixSolidTool()->CreateOpenSolidMode() = Standard_False;
146 sfs->Perform(theProgress);
148 TopoDS_Shape S = sfs->Shape();
149 if ( ! S.IsNull() && S != shape ) {
150 context->RecordModification ( sfs->Context(), msg );
151 context->SetResult ( S );
154 catch (Standard_Failure const& anException) {
156 std::cout << "Error: XSAlgo_AlgoContainer::ProcessShape(): Exception in ShapeFix::Shape" << std::endl;
157 anException.Print(std::cout); std::cout << std::endl;
161 return context->Result();
163 // for writing, define default sequence of DirectFaces
164 else if ( ! strncmp ( pseq, "write.", 6 ) ) {
165 rsc->SetResource ( str.ToCString(), "DirectFaces" );
169 // Define runtime tolerances and do Shape Processing
170 rsc->SetResource ( "Runtime.Tolerance", Prec );
171 rsc->SetResource ( "Runtime.MaxTolerance", maxTol );
173 if ( !ShapeProcess::Perform(context, seq, theProgress) )
174 return shape; // return original shape
176 return context->Result();
179 //=======================================================================
180 //function : PerformFixShape
182 //=======================================================================
185 TopoDS_Shape XSAlgo_AlgoContainer::PerformFixShape(const TopoDS_Shape& S,
186 const Handle(Transfer_TransientProcess)& TP,
187 const Standard_Real Prec,
188 const Standard_Real MaxTol) const
190 if ( S.IsNull() ) return S;
192 TopoDS_Shape shape = S;
196 Handle(ShapeFix_Shape) sfs = ShapeAlgo::AlgoContainer()->ToolContainer()->FixShape();
198 sfs->SetMsgRegistrator ( new ShapeExtend_MsgRegistrator );
199 sfs->SetPrecision ( Prec );
200 sfs->SetMaxTolerance ( MaxTol );
201 sfs->FixFaceTool()->FixWireTool()->FixSameParameterMode() = Standard_False;
204 shape = sfs->Shape();
206 // to be removed when messages come
207 if ( shape == S || shape.IsNull() ) return S;
209 // update map to reflect the substitutions
210 Handle(ShapeBuild_ReShape) context = sfs->Context();
211 const ShapeExtend_DataMapOfShapeListOfMsg& msgmap =
212 Handle(ShapeExtend_MsgRegistrator)::DownCast (sfs->MsgRegistrator())->MapShape();
213 for ( Standard_Integer i=1; i <= TP->NbMapped(); i++ ) {
214 Handle(Transfer_Binder) bnd = TP->MapItem ( i );
215 Handle(TransferBRep_ShapeBinder) sb = Handle(TransferBRep_ShapeBinder)::DownCast ( bnd );
216 if ( sb.IsNull() || sb->Result().IsNull() ) continue;
218 TopoDS_Shape orig = sb->Result();
220 // update messages (messages must be taken from each level in the substitution map)
221 TopoDS_Shape cur, next = orig;
224 Message_ListOfMsg msglist;
225 if (msgmap.IsBound (cur)) {
226 msglist = msgmap.Find (cur);
227 for (Message_ListIteratorOfListOfMsg iter (msglist); iter.More(); iter.Next()) {
228 const Message_Msg& msg = iter.Value();
229 sb->AddWarning (msg.Value(), msg.Original());
232 next = context->Value (cur);
233 } while (cur != next);
237 if ( ! context->Status ( orig, res, Standard_True ) ) continue;
239 sb->SetResult ( res );
242 catch (Standard_Failure) {
244 std::cout << "Error: XSAlgo_AlgoContainer::PerformFixShape(): Exception in ShapeFix::Shape" << std::endl;
251 // ============================================================================
252 // Method : MakeEdgeOnCurve
253 // Purpose : for CheckPCurve
254 // ============================================================================
256 static TopoDS_Edge MakeEdgeOnCurve(const TopoDS_Edge edge)
259 //BRep_Builder B; // B not used - see below (skl)
260 Handle(Geom_Curve) C3d;
261 ShapeAnalysis_Edge sae;
262 Standard_Real cf, cl;
263 if (!sae.Curve3d (edge, C3d, cf, cl, Standard_False ))
265 gp_Pnt PV1 = C3d->Value(cf);
266 gp_Pnt PV2 = C3d->Value(cl);
267 BRepBuilderAPI_MakeEdge mkEdge(C3d, PV1, PV2, cf, cl);
268 //:S4136 Standard_Real tol = BRep_Tool::Tolerance (edge);
269 ShapeBuild_Edge SBE; //skl 10.07.2001
270 SBE.SetRange3d(mkEdge,cf,cl); //skl 10.07.2001
271 result = mkEdge.Edge();
272 //:S4136 B.UpdateEdge(result,tol);
276 //=======================================================================
277 //function : CheckPCurve
279 //=======================================================================
281 Standard_Boolean XSAlgo_AlgoContainer::CheckPCurve (const TopoDS_Edge& E,
282 const TopoDS_Face& face,
283 const Standard_Real preci,
284 const Standard_Boolean isSeam) const
286 Standard_Real w1, w2;
287 Handle(Geom2d_Curve) thePC;
288 ShapeAnalysis_Edge sae;
289 if ( ! sae.PCurve (E, face, thePC, w1, w2, Standard_False ) ) {
290 return Standard_False;
293 // Check for pcurve longer than surface
294 Handle(Geom_Surface) surf = BRep_Tool::Surface(face);
295 Standard_Real UF,UL,VF,VL;
296 surf->Bounds (UF,UL,VF,VL);
298 PUV1 = thePC->Value(w1);
299 PUV2 = thePC->Value(w2);
300 // Multi-periodique ? mieux vaut jeter (attention aux valeurs infinies)
301 Standard_Real DU = Abs (PUV1.X() - PUV2.X());
302 Standard_Real DV = Abs (PUV1.Y() - PUV2.Y());
303 if ( DU/8. > (UL/6. - UF/6.) || DV/8. > (VL/6. - VF/6.) ) {
304 ShapeBuild_Edge().RemovePCurve(E,face);
306 std::cout<<"Removing pcurve periodic"<<std::endl;
308 return Standard_False;
311 // Second Check : 2D and 3D consistency (if the Pcurve has not been
313 // On verifie aussi qu on ne s enroule pas trop ...
314 // ex. : UVV en DEGRES sur une surface en RADIANS, recalee = 57 tours !
316 Handle(Geom_Curve) C3d;
317 Standard_Real cf1, cl1;
318 sae.Curve3d (E, C3d, cf1, cl1, Standard_False );
320 gp_Pnt P1 = surf->Value(PUV1.X(), PUV1.Y());
321 gp_Pnt P2 = surf->Value(PUV2.X(), PUV2.Y());
322 TopoDS_Vertex V1 = TopExp::FirstVertex(E);
323 TopoDS_Vertex V2 = TopExp::LastVertex(E);
324 gp_Pnt PV1 = ( C3d.IsNull() ? BRep_Tool::Pnt(V1) : C3d->Value(cf1) );
325 gp_Pnt PV2 = ( C3d.IsNull() ? BRep_Tool::Pnt(V2) : C3d->Value(cl1) );
326 Standard_Real Dist11 = PV1.Distance(P1), Dist22 = PV2.Distance(P2);
328 if (!((Dist11 <= preci) && (Dist22 <= preci))) {
329 ShapeBuild_Edge().RemovePCurve(E,face);
331 std::cout<<"Removing pcurve points"<<std::endl;
333 return Standard_False;
337 // pdn checking deviation between pcurve and 3D curve
340 // Make temporary edge for analysis
341 if ( C3d.IsNull() ) return Standard_False;
342 TopoDS_Edge edge = MakeEdgeOnCurve(E);
344 // fill it with pcurve(s)
346 Handle(Geom2d_Curve) seamPC;
349 TopoDS_Shape REdge = E.Reversed() ;
350 if ( ! sae.PCurve ( TopoDS::Edge ( REdge ),
351 face, seamPC, f, l, Standard_False ) ||
353 seamPC = Handle(Geom2d_Curve)::DownCast ( thePC->Copy() );
354 B.UpdateEdge ( edge, thePC, seamPC, face, 0.);
356 else B.UpdateEdge ( edge, thePC, face, 0.);
357 B.Range(edge,face,w1,w2);
358 B.SameRange(edge, Standard_False );
360 Standard_Integer SPmode = Interface_Static::IVal("read.stdsameparameter.mode");
362 B.SameParameter (edge, Standard_False );
364 // call FixSP to see what it will do
365 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
366 sfe->FixSameParameter(edge);
367 Standard_Real tol = BRep_Tool::Tolerance (edge);
368 // Standard_Real tolV1 = BRep_Tool::Tolerance(TopExp::FirstVertex(edge));
369 // Standard_Real tolV2 = BRep_Tool::Tolerance(TopExp::LastVertex(edge));
370 Standard_Boolean sr = BRep_Tool::SameRange ( edge );
371 Standard_Boolean sp = BRep_Tool::SameParameter ( edge );
373 // if result is not nice, try to call projection and take the best
374 if ( tol > Min ( 1., 2.*preci ) || ! sr ) {
375 //pdn trying to recompute pcurve
376 TopoDS_Edge edgePr = MakeEdgeOnCurve(E);
377 sfe->FixAddPCurve(edgePr, face, isSeam, preci);
378 sfe->FixSameParameter(edgePr);
379 Standard_Real tolPr = BRep_Tool::Tolerance (edgePr);
380 //pdn choose the best pcurve
381 if ( tolPr < tol || ! sr ) {
382 // tolV1 = BRep_Tool::Tolerance(TopExp::FirstVertex(edgePr));
383 // tolV2 = BRep_Tool::Tolerance(TopExp::LastVertex(edgePr));
384 sr = BRep_Tool::SameRange ( edgePr );
385 sp = BRep_Tool::SameParameter ( edgePr );
391 // get corrected pcurve from the temporary edge, and put to original
392 sae.PCurve ( edge, face, thePC, w1, w2, Standard_False );
395 TopoDS_Shape REdge = edge.Reversed();
396 sae.PCurve ( TopoDS::Edge ( REdge ), face, seamPC, f, l, Standard_False );
397 if ( E.Orientation() == TopAbs_REVERSED ) //:abv 14.11.01: coneEl.sat loop
398 B.UpdateEdge ( E, seamPC, thePC, face, tol );
400 B.UpdateEdge ( E, thePC, seamPC, face, tol );
402 else B.UpdateEdge ( E, thePC, face, tol );
404 B.UpdateVertex(V1,tol);
405 B.UpdateVertex(V2,tol);
406 B.Range(E,face, w1, w2);
407 if(BRep_Tool::SameRange(E))
408 B.SameRange( E, sr );
409 if(BRep_Tool::SameParameter(E))
410 B.SameParameter ( E, sp );
412 return Standard_True;
415 //=======================================================================
416 //function : MergeTransferInfo
418 //=======================================================================
420 void XSAlgo_AlgoContainer::MergeTransferInfo(const Handle(Transfer_TransientProcess)& TP,
421 const Handle(Standard_Transient) &info,
422 const Standard_Integer startTPitem) const
424 Handle(ShapeProcess_ShapeContext) context = Handle(ShapeProcess_ShapeContext)::DownCast ( info );
425 if ( context.IsNull() ) return;
427 const TopTools_DataMapOfShapeShape &map = context->Map();
428 Handle(ShapeExtend_MsgRegistrator) msg = context->Messages();
429 if ( map.Extent() <=0 && ( msg.IsNull() || msg->MapShape().Extent() <=0 ) )
432 Standard_Integer i = ( startTPitem >0 ? startTPitem : 1 );
433 for ( ; i <= TP->NbMapped(); i++ )
435 Handle(Transfer_Binder) bnd = TP->MapItem ( i );
436 Handle(TransferBRep_ShapeBinder) sb = Handle(TransferBRep_ShapeBinder)::DownCast ( bnd );
437 if ( sb.IsNull() || sb->Result().IsNull() ) continue;
439 TopoDS_Shape orig = sb->Result();
441 if ( map.IsBound ( orig ) )
443 sb->SetResult ( map.Find ( orig ) );
445 else if ( !orig.Location().IsIdentity() )
447 TopLoc_Location aNullLoc;
448 TopoDS_Shape atmpSh = orig.Located(aNullLoc);
449 if ( map.IsBound ( atmpSh ) ) sb->SetResult ( map.Find ( atmpSh ) );
453 // Some of edges may be modified.
454 BRepTools_ReShape aReShape;
455 Standard_Boolean hasModifiedEdges = Standard_False;
456 TopExp_Explorer anExpSE(orig, TopAbs_EDGE);
458 // Remember modifications.
459 for( ; anExpSE.More() ; anExpSE.Next() )
461 if ( map.IsBound ( anExpSE.Current() ) )
463 hasModifiedEdges = Standard_True;
464 TopoDS_Shape aModifiedShape = map.Find( anExpSE.Current() );
465 aReShape.Replace(anExpSE.Current(), aModifiedShape);
469 // Apply modifications and store result in binder.
470 if (hasModifiedEdges)
472 TopoDS_Shape aRes = aReShape.Apply(orig);
473 sb->SetResult ( aRes );
479 if ( ! msg.IsNull() ) {
480 const ShapeExtend_DataMapOfShapeListOfMsg& msgmap = msg->MapShape();
481 if ( msgmap.IsBound (orig) ) {
482 const Message_ListOfMsg &msglist = msgmap.Find (orig);
483 for (Message_ListIteratorOfListOfMsg iter (msglist); iter.More(); iter.Next()) {
484 const Message_Msg& mess = iter.Value();
485 sb->AddWarning (TCollection_AsciiString(mess.Value()).ToCString(),
486 TCollection_AsciiString(mess.Original()).ToCString());
493 //=======================================================================
494 //function : MergeTransferInfo
496 //=======================================================================
498 void XSAlgo_AlgoContainer::MergeTransferInfo(const Handle(Transfer_FinderProcess)& FP,
499 const Handle(Standard_Transient) &info) const
501 Handle(ShapeProcess_ShapeContext) context = Handle(ShapeProcess_ShapeContext)::DownCast ( info );
502 if ( context.IsNull() ) return;
504 const TopTools_DataMapOfShapeShape &map = context->Map();
505 TopTools_DataMapIteratorOfDataMapOfShapeShape ShapeShapeIterator(map);
506 Handle(ShapeExtend_MsgRegistrator) msg = context->Messages();
508 for ( ; ShapeShapeIterator.More(); ShapeShapeIterator.Next() ) {
510 TopoDS_Shape orig = ShapeShapeIterator.Key(), res = ShapeShapeIterator.Value();
511 Handle(TransferBRep_ShapeMapper) resMapper = TransferBRep::ShapeMapper ( FP, res );
512 Handle(Transfer_Binder) resBinder = FP->Find ( resMapper );
514 if (resBinder.IsNull()) {
515 resBinder = new TransferBRep_ShapeBinder(res);
516 //if <orig> shape was split, put entities corresponding to new shapes
517 // into Transfer_TransientListBinder.
518 if ( orig.ShapeType() > res.ShapeType() ) {
520 Handle(Transfer_TransientListBinder) TransientListBinder = new Transfer_TransientListBinder;
521 for (TopoDS_Iterator it(res); it.More(); it.Next()) {
522 Handle(Transfer_Finder) subMapper = TransferBRep::ShapeMapper ( FP, it.Value());
523 if (subMapper.IsNull()) continue;
525 Handle(Standard_Transient) tr = FP->FindTransient ( subMapper );
526 if (tr.IsNull()) continue;
527 TransientListBinder->AddResult(tr);
530 if ( TransientListBinder->NbTransients() == 1 ) resBinder = new TransferBRep_ShapeBinder(sub);
531 else if ( TransientListBinder->NbTransients() > 1 ) {
532 resBinder->AddResult(TransientListBinder);
533 // resBinder->SetNext(TransientListBinder, Standard_True);
535 std::cout<<"Info: TransientListBinder created for split shape"<<std::endl;
538 std::cout<<"Warning: XSAlgo_AlgoContainer::MergeTransferInfo() "
539 <<"No results were found for split shape. "<<std::endl;
540 //<<"Transfer_FinderProcess->NbMapped() = "<<FP->NbMapped()<<std::endl;
546 Handle(TransferBRep_ShapeMapper) origMapper= TransferBRep::ShapeMapper ( FP, orig);
547 Handle(Transfer_Binder) origBinder = FP->Find ( origMapper );
548 if ( origBinder.IsNull() ) {
549 FP->Bind(origMapper, resBinder);
552 origBinder->AddResult ( resBinder );
556 if ( ! msg.IsNull() ) {
557 const ShapeExtend_DataMapOfShapeListOfMsg& msgmap = msg->MapShape();
558 if ( msgmap.IsBound (orig) ) {
559 const Message_ListOfMsg &msglist = msgmap.Find (orig);
560 for (Message_ListIteratorOfListOfMsg iter (msglist); iter.More(); iter.Next()) {
561 const Message_Msg& mess = iter.Value();
562 resBinder->AddWarning (TCollection_AsciiString(mess.Value()).ToCString(),
563 TCollection_AsciiString(mess.Original()).ToCString());