0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / TNaming / TNaming_CopyShape.cxx
1 // Created on: 2000-02-14
2 // Created by: Denis PASCAL
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <TNaming_CopyShape.hxx>
18 #include <TNaming_TranslateTool.hxx>
19 #include <TopAbs.hxx>
20 #include <TopLoc_Datum3D.hxx>
21 #include <TopLoc_Location.hxx>
22 #include <TopoDS_Edge.hxx>
23 #include <TopoDS_Face.hxx>
24 #include <TopoDS_Iterator.hxx>
25 #include <TopoDS_Shape.hxx>
26 #include <TopoDS_Vertex.hxx>
27
28 //=======================================================================
29 //function : CopyTool
30 //purpose  : Tool to copy a set of shape(s), using the aMap
31 //=======================================================================
32 void TNaming_CopyShape::CopyTool( const TopoDS_Shape& aShape,
33                                  TColStd_IndexedDataMapOfTransientTransient&  aMap,
34                                  TopoDS_Shape&       aResult)
35 {
36
37   Handle(TNaming_TranslateTool) TrTool = new TNaming_TranslateTool();
38   TNaming_CopyShape::Translate (aShape, aMap, aResult, TrTool) ;
39 }
40
41 //=======================================================================
42 //function : Translate
43 //purpose  : TNaming
44 //=======================================================================
45
46 void TNaming_CopyShape::Translate( const TopoDS_Shape& aShape,                   
47                                   TColStd_IndexedDataMapOfTransientTransient&  aMap,
48                                   TopoDS_Shape& aResult,
49                                   const Handle(TNaming_TranslateTool)& TrTool)
50 {
51   aResult.Nullify();
52
53   if (aShape.IsNull()) return;
54
55   if (aMap.Contains(aShape.TShape())) {
56     // get the translated TShape
57     Handle(TopoDS_TShape) TS =
58        *((Handle(TopoDS_TShape)*) &aMap.FindFromKey(aShape.TShape()));
59     aResult.TShape(TS);
60   }
61   else {
62
63     // create if not translated and update
64     
65     switch (aShape.ShapeType()) {
66       
67     case TopAbs_VERTEX :
68       TrTool->MakeVertex(aResult);
69       TrTool->UpdateVertex(aShape,aResult,aMap);
70       break;
71       
72     case TopAbs_EDGE :
73       TrTool->MakeEdge(aResult);
74       TrTool->UpdateEdge(aShape,aResult,aMap);
75       break;
76       
77     case TopAbs_WIRE :
78       TrTool->MakeWire(aResult);
79       TrTool->UpdateShape(aShape,aResult);
80       break;
81       
82     case TopAbs_FACE :
83       TrTool->MakeFace(aResult);
84       TrTool->UpdateFace(aShape,aResult,aMap);
85       break;
86     
87     case TopAbs_SHELL :
88       TrTool->MakeShell(aResult);
89       TrTool->UpdateShape(aShape,aResult);
90       break;
91     
92     case TopAbs_SOLID :
93       TrTool->MakeSolid(aResult);
94       TrTool->UpdateShape(aShape,aResult);
95       break;
96       
97     case TopAbs_COMPSOLID :
98       TrTool->MakeCompSolid(aResult);
99       TrTool->UpdateShape(aShape,aResult);
100       break;
101       
102   case TopAbs_COMPOUND :
103       TrTool->MakeCompound(aResult);
104       TrTool->UpdateShape(aShape,aResult);
105       break;
106
107     default:
108       break;
109     }
110   
111     // bind and copy the sub-elements
112     aMap.Add(aShape.TShape(),aResult.TShape()); //TShapes
113     TopoDS_Shape S = aShape;
114     S.Orientation(TopAbs_FORWARD);
115     S.Location(TopLoc_Location()); //Identity
116     // copy current Shape
117     TopoDS_Iterator itr(S, Standard_False);
118     Standard_Boolean wasFree = aResult.Free();
119     aResult.Free(Standard_True);
120     // translate <sub-shapes>
121     for (;itr.More();itr.Next()) {
122       TopoDS_Shape subShape;
123       TNaming_CopyShape::Translate(itr.Value(), aMap, subShape, TrTool);
124       TrTool->Add(aResult, subShape);// add subshapes
125     }
126
127     aResult.Free(wasFree);
128   }
129   
130   aResult.Orientation(aShape.Orientation());
131   aResult.Location(TNaming_CopyShape::Translate(aShape.Location(), aMap));
132   TrTool->UpdateShape(aShape,aResult);
133 // #ifdef OCCT_DEBUG
134 //     if(fShar) {
135 //       std::cout << "=== Shareable shape ===" << std::endl;
136 //       std::cout << "aShape Type = " <<(aShape.TShape())->DynamicType() << std::endl;
137 //       if(aShape.Orientation() == aResult.Orientation())
138 //      std::cout<<"\tSource and result shapes have the same Orientation"<< std::endl;
139 //       if((aShape.Location().IsEqual(aResult.Location())))
140 //      std::cout <<"\tSource and result shapes have the same Locations" << std::endl;
141 //       if((aShape.IsSame(aResult)))
142 //       std::cout <<"\tShapes arew the same (i.e. the same TShape and the same Locations)" << std::endl;
143 //     }            
144 // #endif
145 }
146
147 //=======================================================================
148 // static TranslateDatum3D
149 //=======================================================================
150 static Handle(TopLoc_Datum3D) TranslateDatum3D(const Handle(TopLoc_Datum3D)& D,
151                                                TColStd_IndexedDataMapOfTransientTransient&  aMap)
152 {
153   Handle(TopLoc_Datum3D) TD;
154   if(aMap.Contains(D))
155        TD = Handle(TopLoc_Datum3D)::DownCast(aMap.FindFromKey(D));
156   else {
157     TD = new TopLoc_Datum3D(D->Transformation());
158     aMap.Add(D, TD);
159   }
160   return TD;
161 }
162 //=======================================================================
163 //function : Translates
164 //purpose  : Topological Location
165 //=======================================================================
166
167 TopLoc_Location TNaming_CopyShape::Translate(const TopLoc_Location& L,
168                                              TColStd_IndexedDataMapOfTransientTransient&  aMap)
169 {
170   TopLoc_Location result;
171
172  if (!L.IsIdentity()) {
173    result = Translate(L.NextLocation(),aMap) * 
174      TopLoc_Location(TranslateDatum3D(L.FirstDatum(),aMap)).Powered(L.FirstPower());
175
176  }
177   return result;
178 }
179
180