// Created on: 1998-01-20 // Created by: Yves FRICAUD // Copyright (c) 1997-1999 Matra Datavision // Copyright (c) 1999-2014 OPEN CASCADE SAS // // This file is part of Open CASCADE Technology software library. // // This library is free software; you can redistribute it and/or modify it under // the terms of the GNU Lesser General Public License version 2.1 as published // by the Free Software Foundation, with special exception defined in the file // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT // distribution for complete text of the license and disclaimer of any warranty. // // Alternatively, this file may be used under the terms of Open CASCADE // commercial license or contractual agreement. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // CopyShape //======================================================================= //function : MapShapes //purpose : TNaming //======================================================================= static void MapShapes(const TopoDS_Shape& SCible, const TopoDS_Shape& SSource, TopTools_DataMapOfShapeShape& M) { M.Bind(SCible,SSource); TopoDS_Iterator icible(SCible); TopoDS_Iterator isource(SSource); while (icible.More()) { if (!M.IsBound(icible.Value())) MapShapes(icible.Value(),isource.Value(),M); icible.Next(); isource.Next(); } } //======================================================================= //function : MapShapes //purpose : TNaming //======================================================================= static void MapShapes(const TDF_Label& LCible, const TDF_Label& LSource, TopTools_DataMapOfShapeShape& M) { TNaming_Iterator icible(LCible); TNaming_Iterator isource(LSource); while (icible.More()) { if (!icible.OldShape().IsNull()) { if (!M.IsBound(icible.OldShape())) MapShapes(icible.OldShape(),isource.OldShape(),M); } if (!icible.NewShape().IsNull()) { if (!M.IsBound(icible.NewShape())) MapShapes(icible.NewShape(),isource.NewShape(),M); } icible.Next(); isource.Next(); } TDF_ChildIterator iccible(LCible); TDF_ChildIterator icsource(LSource); while (iccible.More()) { MapShapes(iccible.Value(),icsource.Value(),M); iccible.Next(); icsource.Next(); } } //======================================================================= //function : SubstituteShape //purpose : TNaming //======================================================================= static void SubstituteShape(const TopoDS_Shape& oldShape, const TopoDS_Shape& newShape, TNaming_DataMapOfShapePtrRefShape& amap) { if (oldShape.IsSame(newShape)) { std::cout <<"import_tool::Substitute : oldShape IsSame newShape"<Shape(newShape); amap.UnBind(oldShape); amap.Bind(newShape,pos); } //======================================================================= //function : MakeShape //purpose : ANaming //======================================================================= TopoDS_Shape TNaming::MakeShape (const TopTools_MapOfShape& MS) { if (!MS.IsEmpty ()) { TopTools_MapIteratorOfMapOfShape it(MS); if (MS.Extent() == 1) { return it.Key(); } else { TopoDS_Compound C; BRep_Builder B; B.MakeCompound(C); for (; it.More(); it.Next()){ B.Add(C,it.Key()); } return C; } } return TopoDS_Shape(); } //======================================================================= //function : Substitute //purpose : TNaming //======================================================================= void TNaming::Substitute(const TDF_Label& LSource, const TDF_Label& LCible , TopTools_DataMapOfShapeShape& M) { //attention pour etre en parallele au niveau structure il faut que Lciblble corresponde au premier fils recopie MapShapes(LCible,LSource,M); Handle(TNaming_UsedShapes) US; LCible.Root().FindAttribute(TNaming_UsedShapes::GetID(),US); TNaming_DataMapOfShapePtrRefShape& amap = US->Map(); for (TopTools_DataMapIteratorOfDataMapOfShapeShape It(M);It.More();It.Next()) { SubstituteShape(It.Key(),It.Value(),amap); } } //======================================================================= //function : SubstituteSShape //purpose : //======================================================================= Standard_Boolean TNaming::SubstituteSShape(const TDF_Label& Lab, const TopoDS_Shape& From, TopoDS_Shape& To) { Handle(TNaming_UsedShapes) US; Lab.Root().FindAttribute(TNaming_UsedShapes::GetID(),US); TNaming_DataMapOfShapePtrRefShape& amap = US->Map(); if (!amap.IsBound(To)) return Standard_False; TNaming_RefShape* pos; pos = amap.ChangeFind(To); if(!amap.UnBind(To)) return Standard_False; //update shape To.Orientation(From.Orientation()); pos->Shape(To); return amap.Bind(To, pos); } //======================================================================= //function : Rebuild //purpose : TNaming //======================================================================= static Standard_Boolean Rebuild (const TopoDS_Shape& S, TopTools_DataMapOfShapeShape& M) { Standard_Boolean IsModified = Standard_False; if (M.IsBound(S)) return IsModified; BRep_Builder B; TopoDS_Iterator iteS (S.Oriented(TopAbs_FORWARD)); // Reconstruction des sous shapes si necessaire. for (; iteS.More(); iteS.Next()) { const TopoDS_Shape& SS = iteS.Value(); if (Rebuild (SS,M)) IsModified = Standard_True; } if (!IsModified) { M.Bind(S,S); return Standard_True; } // Reconstruction de S TopoDS_Shape NewS = S.Oriented(TopAbs_FORWARD); NewS.EmptyCopy(); if (NewS.ShapeType() == TopAbs_EDGE) { Standard_Real f,l; BRep_Tool::Range(TopoDS::Edge(S),f,l); B.Range(TopoDS::Edge(NewS),f,l); } iteS.Initialize(S.Oriented(TopAbs_FORWARD)); for (iteS.Initialize(S.Oriented(TopAbs_FORWARD)) ;iteS.More(); iteS.Next()) { const TopoDS_Shape& OS = iteS.Value(); const TopoDS_Shape& NS = M(OS); B.Add(NewS,NS.Oriented(OS.Orientation())); } M.Bind (S,NewS.Oriented(S.Orientation())); return IsModified; } //======================================================================= //function : Update //purpose : TNaming //======================================================================= void TNaming::Update(const TDF_Label& L, TopTools_DataMapOfShapeShape& M) { //Reconstruction des shapes de L suite aux substitutions decrites dans la map M. // ex : si une face est remplacee par une autre il faut reconstruire les shapes // qui contiennent cette face. Handle(TNaming_UsedShapes) US; L.Root().FindAttribute(TNaming_UsedShapes::GetID(),US); TNaming_DataMapOfShapePtrRefShape& amap = US->Map(); for (TNaming_Iterator it(L); it.More(); it.Next()) { if (!it.OldShape().IsNull()) { const TopoDS_Shape& S = it.OldShape(); if (!M.IsBound(S)) Rebuild (S,M); SubstituteShape(S,M(S),amap); } if (!it.NewShape().IsNull()) { const TopoDS_Shape& S = it.NewShape(); if (!M.IsBound(S)) Rebuild (S,M); SubstituteShape(S,M(S),amap); } } // SI Les shapes dans les sous-labels sont des sous shapes des shapes de L // si les shapes de L n ont pas changes les shapes des sous-labels ne seront // pas changes non plus. for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) { TNaming::Update (ciL.Value(),M); } } //======================================================================= //function : BuilCompound //purpose : TNaming //======================================================================= static void BuildCompound(TopoDS_Compound& C, const TDF_Label& L) { BRep_Builder B; for (TNaming_Iterator it(L); it.More(); it.Next()) { if (!it.OldShape().IsNull()) { B.Add(C,it.OldShape()); } if (!it.NewShape().IsNull()) { B.Add(C,it.NewShape()); } } for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) { BuildCompound (C,ciL.Value()); } } //======================================================================= //function : Update //purpose : TNaming //======================================================================= static void BuildMap(const TDF_Label& L, BRepBuilderAPI_Transform& Transformer, TopTools_DataMapOfShapeShape& M) { Handle(TNaming_UsedShapes) US; L.Root().FindAttribute(TNaming_UsedShapes::GetID(),US); for (TNaming_Iterator it(L); it.More(); it.Next()) { if (!it.OldShape().IsNull()) { const TopoDS_Shape& S = it.OldShape(); M.Bind(S,Transformer.ModifiedShape(S)); } if (!it.NewShape().IsNull()) { const TopoDS_Shape& S = it.NewShape(); M.Bind(S,Transformer.ModifiedShape(S)); } } for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) { BuildMap (ciL.Value(),Transformer,M); } } //======================================================================= //function : LoadNamedShape //purpose : TNaming //======================================================================= static void LoadNamedShape (TNaming_Builder& B, TNaming_Evolution Evol, const TopoDS_Shape& OS, const TopoDS_Shape& NS) { switch (Evol) { case TNaming_PRIMITIVE : { B.Generated(NS); break; } case TNaming_GENERATED : { B.Generated(OS,NS); break; } case TNaming_MODIFY : { B.Modify(OS,NS); break; } case TNaming_DELETE : { B.Delete (OS); break; } case TNaming_SELECTED : { B.Select(NS,OS); break; } default: break; } } //======================================================================= //function : Displace //purpose : TNaming //======================================================================= void TNaming::Displace (const TDF_Label& L, const TopLoc_Location& Loc, const Standard_Boolean WithOld) { TopTools_ListOfShape Olds; TopTools_ListOfShape News; TNaming_Evolution Evol; TNaming_Iterator it(L); if (it.More()) { // dp on continue de visiter les fils meme s'il y a pas de shape Evol = it.Evolution(); for ( ; it.More(); it.Next()) { Olds.Append(it.OldShape()); News.Append(it.NewShape()); } TopTools_ListIteratorOfListOfShape itOlds(Olds); TopTools_ListIteratorOfListOfShape itNews(News); TNaming_Builder B(L); for ( ;itOlds.More() ; itOlds.Next(),itNews.Next()) { TopoDS_Shape OS,NS; const TopoDS_Shape& SO = itOlds.Value(); const TopoDS_Shape& SN = itNews.Value(); OS = SO; if (WithOld && !SO.IsNull()) OS = SO.Moved(Loc); if (!SN.IsNull()) NS = SN.Moved(Loc); LoadNamedShape ( B, Evol, OS, NS); } } for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) { Displace (ciL.Value(), Loc, WithOld); } } //======================================================================= //function : Replace //purpose : TNaming //======================================================================= static void Replace (const TDF_Label& L, const TopTools_DataMapOfShapeShape& M) { TNaming_Evolution Evol; TNaming_Iterator it(L); if (!it.More()) return; Evol = it.Evolution(); TNaming_Builder B(L); TopoDS_Shape OS,NS; for ( ; it.More(); it.Next()) { if (!it.OldShape().IsNull()) { OS = it.OldShape(); if (M.IsBound(OS)) OS = M(OS); } if (!it.NewShape().IsNull()) { NS = it.NewShape(); if (M.IsBound(NS)) NS = M(NS); } LoadNamedShape ( B, Evol, OS, NS); } for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) { Replace (ciL.Value(),M); } } //======================================================================= //function : Update //purpose : TNaming //======================================================================= void TNaming::Transform(const TDF_Label& L, const gp_Trsf& T) { //-------------------------------------------------------------------- // Construction du compound qui contient tous les shapes sous le label // et ses fils. //-------------------------------------------------------------------- TopoDS_Compound CompShape; BRep_Builder B; B.MakeCompound(CompShape); BuildCompound (CompShape,L); //---------------------------- // Transformation du compound. //----------------------------- BRepBuilderAPI_Transform Transformer(CompShape, T); //----------------------------------------------------------- //Remplacement des shapes initiaux par les shapes transformes. //----------------------------------------------------------- TopTools_DataMapOfShapeShape M; BuildMap (L,Transformer,M); Replace (L,M); } //======================================================================= //function : IDList //purpose : TNaming //======================================================================= void TNaming::IDList(TDF_IDList& anIDList) { anIDList.Append(TNaming_NamedShape::GetID()); } //======================================================================= //function : Replicate //purpose : //======================================================================= void TNaming::Replicate(const Handle(TNaming_NamedShape)& NS, const gp_Trsf& T, const TDF_Label& L) { TopoDS_Shape SH = TNaming_Tool::CurrentShape(NS); TNaming::Replicate(SH, T, L); } //======================================================================= //function : Replicate //purpose : TNaming //======================================================================= void TNaming::Replicate (const TopoDS_Shape& SH, const gp_Trsf& T, const TDF_Label& L) { // transform BRepBuilderAPI_Transform opeTrsf(T); if (SH.ShapeType() == TopAbs_FACE || SH.ShapeType() == TopAbs_WIRE ) { opeTrsf.Perform(SH, Standard_True); // pour le pattern de prism } else { opeTrsf.Perform(SH, Standard_False); } const TopoDS_Shape& newSH = opeTrsf.Shape(); //BRepLib::UpdateTolerances(newSH, Standard_True); // principal shape TNaming_Builder Builder(L); Builder.Generated(SH, newSH); // sub shape TopAbs_ShapeEnum SST = TopAbs_FACE; if (SH.ShapeType() == TopAbs_FACE || SH.ShapeType() == TopAbs_WIRE ) SST = TopAbs_EDGE; TNaming_Builder Builder2 (L.FindChild(1,Standard_True)); for (TopExp_Explorer exp(SH, SST); exp.More(); exp.Next()) { const TopoDS_Shape& oldSubShape = exp.Current(); TopoDS_Shape newSubShape = opeTrsf.ModifiedShape(oldSubShape); Builder2.Generated(oldSubShape, newSubShape); } } //======================================================================= //function : ShapeCopy //purpose : TNaming //======================================================================= static TopoDS_Shape ShapeCopy(const TopoDS_Shape& S, TopTools_DataMapOfShapeShape& M) { if (S.IsNull()) return S; if (M.IsBound(S)) return M(S); //---------------------------- //construction de la copie. // 1- copie des sous shapes. // 2- reconstruction du TShape //---------------------------- BRep_Builder B; TopoDS_Iterator it (S.Oriented(TopAbs_FORWARD)); for ( ; it.More(); it.Next()) { const TopoDS_Shape& SS = it.Value(); TopoDS_Shape NewSS = ShapeCopy(SS,M); } TopoDS_Shape NewS = S.Oriented(TopAbs_FORWARD); NewS.EmptyCopy(); if (NewS.ShapeType() == TopAbs_EDGE) { Standard_Real f,l; BRep_Tool::Range(TopoDS::Edge(S),f,l); B.Range(TopoDS::Edge(NewS),f,l); } for (it.Initialize(S.Oriented(TopAbs_FORWARD)) ;it.More(); it.Next()) { const TopoDS_Shape& OS = it.Value(); const TopoDS_Shape& NS = M(OS); B.Add(NewS,NS.Oriented(OS.Orientation())); } NewS.Orientation(S.Orientation()); NewS.Free (S.Free()) ;NewS.Modified(S.Modified());NewS.Checked (S.Checked()); NewS.Orientable(S.Orientable());NewS.Closed (S.Closed()) ;NewS.Infinite(S.Infinite()); NewS.Convex (S.Convex()); M.Bind (S,NewS); return NewS; } //======================================================================= //function : ChangeShapes //purpose : TNaming //======================================================================= void TNaming::ChangeShapes(const TDF_Label& L, TopTools_DataMapOfShapeShape& M) { TopTools_ListOfShape Olds; TopTools_ListOfShape News; Handle(TNaming_NamedShape) NS; L.FindAttribute(TNaming_NamedShape::GetID(),NS); if (!NS.IsNull()) { TNaming_Evolution Evol = NS->Evolution(); for (TNaming_Iterator it(L); it.More(); it.Next()) { const TopoDS_Shape& S1 = it.OldShape(); const TopoDS_Shape& S2 = it.NewShape(); Olds.Append(ShapeCopy(S1,M));News.Append(ShapeCopy(S2,M)); } TopTools_ListIteratorOfListOfShape itOlds(Olds); TopTools_ListIteratorOfListOfShape itNews(News); TNaming_Builder B(L); for ( ;itOlds.More() ; itOlds.Next(),itNews.Next()) { LoadNamedShape ( B, Evol, itOlds.Value(), itNews.Value()); } } for (TDF_ChildIterator ciL(L); ciL.More(); ciL.Next()) { ChangeShapes (ciL.Value(),M); } } //======================================================================= //function : //purpose : //======================================================================= Standard_OStream& TNaming::Print (const TNaming_Evolution EVOL, Standard_OStream& s) { switch(EVOL){ case TNaming_PRIMITIVE : { s <<"PRIMITIVE"; break; } case TNaming_GENERATED : { s <<"GENERATED"; break; } case TNaming_MODIFY : { s <<"MODIFY"; break; } case TNaming_DELETE : { s <<"DELETE"; break; } case TNaming_SELECTED : { s <<"SELECTED"; break; } default : s << "UNKNOWN_Evolution"; break; } return s; } //======================================================================= //function : //purpose : //======================================================================= Standard_OStream& TNaming::Print (const TNaming_NameType NAME, Standard_OStream& s) { switch (NAME) { case TNaming_UNKNOWN : { s <<"UNKNOWN"; break; } case TNaming_IDENTITY : { s <<"IDENTITY"; break; } case TNaming_MODIFUNTIL : { s <<"MODIFUNTIL"; break; } case TNaming_GENERATION : { s <<"GENERATION"; break; } case TNaming_INTERSECTION : { s <<"INTERSECTION"; break; } case TNaming_UNION: { s <<"UNION"; break; } case TNaming_SUBSTRACTION : { s <<"SUBSTRACTION"; break; } case TNaming_CONSTSHAPE : { s <<"CONSTSHAPE"; break; } case TNaming_FILTERBYNEIGHBOURGS: { s <<"FILTERBYNEIGHBOURGS"; break; } case TNaming_ORIENTATION: { s <<"ORIENTATION"; break; } case TNaming_WIREIN: { s <<"WIREIN"; break; } case TNaming_SHELLIN: { s <<"SHELLIN"; break; } default : { s <<"UNKNOWN_NameType"; break; } } return s; } //======================================================================= //function : Print //purpose : Prints UsedShapes. //======================================================================= Standard_OStream& TNaming::Print (const TDF_Label& ACCESS, Standard_OStream& s) { Handle(TNaming_UsedShapes) US; if (!ACCESS.Root().FindAttribute(TNaming_UsedShapes::GetID(), US)) { #ifdef OCCT_DEBUG std::cout<<"TNaming::Print(US): Bad access"<Dump(s); } //======================================================================= //function : BuildMapIn //purpose : //======================================================================= static void BuildMapIn(const TopoDS_Shape& Context, const TopAbs_ShapeEnum StopType, TopTools_DataMapOfOrientedShapeShape& Map) { TopAbs_ShapeEnum aType; if((Context.ShapeType() == TopAbs_SOLID || Context.ShapeType() == TopAbs_FACE) && (StopType - Context.ShapeType()) != 1) aType = (TopAbs_ShapeEnum)(Context.ShapeType() +2); else aType = (TopAbs_ShapeEnum)(Context.ShapeType()+1); for (TopExp_Explorer exp(Context,aType); exp.More(); exp.Next()) { #ifdef OCCT_DEBUG if(!Map.Bind(exp.Current(), Context)) std::cout << "Not bind = " <