CR24212: Moving OCAF samples from 'DRAW' component to OCCT samples.
[occt.git] / samples / ocafsamples / TNaming_Sample.cxx
CommitLineData
bc228f77 1// Created on: 1999-12-29
2// Created by: Sergey RUIN
3// Copyright (c) 1999-1999 Matra Datavision
4// Copyright (c) 1999-2012 OPEN CASCADE SAS
5//
6// The content of this file is subject to the Open CASCADE Technology Public
7// License Version 6.5 (the "License"). You may not use the content of this file
8// except in compliance with the License. Please obtain a copy of the License
9// at http://www.opencascade.org and read it completely before using this file.
10//
11// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13//
14// The Original Code and all software distributed under the License is
15// distributed on an "AS IS" basis, without warranty of any kind, and the
16// Initial Developer hereby disclaims all such warranties, including without
17// limitation, any warranties of merchantability, fitness for a particular
18// purpose or non-infringement. Please see the License for the specific terms
19// and conditions governing the rights and limitations under the License.
20
21
22#include <gp_Vec.hxx>
23#include <gp_Trsf.hxx>
24#include <gp_Pnt.hxx>
25
26#include <TopoDS.hxx>
27#include <TopoDS_Shape.hxx>
28#include <TopoDS_Face.hxx>
29#include <TopTools_ListOfShape.hxx>
30#include <TopTools_MapOfShape.hxx>
31#include <TopTools_ListIteratorOfListOfShape.hxx>
32#include <TopExp_Explorer.hxx>
33
34#include <TopOpeBRepBuild_HBuilder.hxx>
35
36#include <BRepPrimAPI_MakeBox.hxx>
37#include <BRepFilletAPI_MakeFillet.hxx>
38
39#include <BRepAlgo_Cut.hxx>
40#include <BRepAlgo.hxx>
41
42#include <TDF_Data.hxx>
43#include <TDF_Label.hxx>
44#include <TDF_LabelMap.hxx>
45#include <TDF_ChildIterator.hxx>
46#include <TDF_MapIteratorOfLabelMap.hxx>
47
48#include <TNaming_NamedShape.hxx>
49#include <TNaming_Selector.hxx>
50#include <TNaming_Tool.hxx>
51#include <TNaming_Builder.hxx>
52#include <TNaming.hxx>
53
54// =======================================================================================
55// This sample contains template for typical actions with OCAF Topologigal Naming services
56// =======================================================================================
57
58#ifdef DEB
59
60#define Box1POS 1
61#define Box2POS 2
62#define SelectedEdgesPOS 3
63#define FilletPOS 4
64#define CutPOS 5
65
66void Sample()
67{
68 // Starting with data framework
69 Handle(TDF_Data) DF = new TDF_Data();
70 TDF_Label aLabel = DF->Root();
71
72 TopoDS_Shape Shape, Context;
73
74 // ======================================================
75 // Creating NamedShapes with different type of Evolution
76 // Scenario:
77 // 1.Create Box1 and push it as PRIMITIVE in DF
78 // 2.Create Box2 and push it as PRIMITIVE in DF
79 // 3.Move Box2 (applying a transformation)
80 // 4.Push a selected edges of top face of Box1 in DF,
81 // create Fillet (using selected edges) and push result as modification of Box1
82 // 5.Create a Cut (Box1, Box2) as modification of Box1 and push it in DF
83 // 6.Recover result from DF
84 // ======================================================
85
86 // =====================================
87 // 1.Box1, TNaming_Evolution == PRIMITIVE
88 // =====================================
89 BRepPrimAPI_MakeBox MKBOX1( 100, 100, 100); // creating Box1
90
91 //Load the faces of the box in DF
92 TDF_Label Box1Label = aLabel.FindChild(Box1POS);
93 TDF_Label Top1 = Box1Label.FindChild(1);
94 TDF_Label Bottom1 = Box1Label.FindChild(2);
95 TDF_Label Right1 = Box1Label.FindChild(3);
96 TDF_Label Left1 = Box1Label.FindChild(4);
97 TDF_Label Front1 = Box1Label.FindChild(5);
98 TDF_Label Back1 = Box1Label.FindChild(6);
99
100 TNaming_Builder Box1Ins (Box1Label);
101 Box1Ins.Generated (MKBOX1.Shape());
102
103 TNaming_Builder Top1FaceIns (Top1);
104 TopoDS_Face Top1Face = MKBOX1.TopFace ();
105 Top1FaceIns.Generated (Top1Face);
106
107 TopoDS_Face Bottom1Face = MKBOX1.BottomFace ();
108 TNaming_Builder Bottom1FaceIns (Bottom1);
109 Bottom1FaceIns.Generated (Bottom1Face);
110
111 TopoDS_Face Right1Face = MKBOX1.RightFace ();
112 TNaming_Builder Right1FaceIns (Right1);
113 Right1FaceIns.Generated (Right1Face);
114
115 TopoDS_Face Left1Face = MKBOX1.LeftFace ();
116 TNaming_Builder Left1FaceIns (Left1);
117 Left1FaceIns.Generated (Left1Face);
118
119 TopoDS_Face Front1Face = MKBOX1.FrontFace ();
120 TNaming_Builder Front1FaceIns (Front1);
121 Front1FaceIns.Generated (Front1Face);
122
123 TopoDS_Face Back1Face = MKBOX1.BackFace ();
124 TNaming_Builder Back1FaceIns (Back1);
125 Back1FaceIns.Generated (Back1Face);
126
127 // =====================================
128 // 2.Box2, TNaming_Evolution == PRIMITIVE
129 // =====================================
130 BRepPrimAPI_MakeBox MKBOX2( 150, 150, 150); // creating Box2
131
132 //Load the faces of the box2 in DF
133 TDF_Label Box2Label = aLabel.FindChild(Box2POS);
134 TDF_Label Top2 = Box2Label.FindChild(1);
135 TDF_Label Bottom2 = Box2Label.FindChild(2);
136 TDF_Label Right2 = Box2Label.FindChild(3);
137 TDF_Label Left2 = Box2Label.FindChild(4);
138 TDF_Label Front2 = Box2Label.FindChild(5);
139 TDF_Label Back2 = Box2Label.FindChild(6);
140
141 TNaming_Builder Box2Ins (Box2Label);
142 Box2Ins.Generated (MKBOX2.Shape());
143
144 TNaming_Builder Top2FaceIns (Top2);
145 TopoDS_Face Top2Face = MKBOX2.TopFace ();
146 Top2FaceIns.Generated (Top2Face);
147
148 TopoDS_Face Bottom2Face = MKBOX2.BottomFace ();
149 TNaming_Builder Bottom2FaceIns (Bottom2);
150 Bottom2FaceIns.Generated (Bottom2Face);
151
152 TopoDS_Face Right2Face = MKBOX2.RightFace ();
153 TNaming_Builder Right2FaceIns (Right2);
154 Right2FaceIns.Generated (Right2Face);
155
156 TopoDS_Face Left2Face = MKBOX2.LeftFace ();
157 TNaming_Builder Left2FaceIns (Left2);
158 Left2FaceIns.Generated (Left2Face);
159
160 TopoDS_Face Front2Face = MKBOX2.FrontFace ();
161 TNaming_Builder Front2FaceIns (Front2);
162 Front2FaceIns.Generated (Front2Face);
163
164 TopoDS_Face Back2Face = MKBOX2.BackFace ();
165 TNaming_Builder Back2FaceIns (Back2);
166 Back2FaceIns.Generated (Back2Face);
167
168 // ====================================
169 // 3.Applying a transformation to Box2
170 // ====================================
171 gp_Vec vec1(gp_Pnt(0.,0.,0.),gp_Pnt(50.,50.,20.));
172 gp_Trsf TRSF;
173 TRSF.SetTranslation(vec1);
174 TopLoc_Location loc(TRSF);
175 TDF_LabelMap scope;
176 TDF_ChildIterator itchild;
177 for (itchild.Initialize(Box2Label,Standard_True); itchild.More();itchild.Next()) {
178 if (itchild.Value().IsAttribute(TNaming_NamedShape::GetID())) scope.Add(itchild.Value());
179 }
180 if (Box2Label.IsAttribute(TNaming_NamedShape::GetID())) scope.Add(Box2Label);
181 TDF_MapIteratorOfLabelMap it(scope);
182 for (;it.More();it.Next())
183 TNaming::Displace(it.Key(), loc, Standard_True);//with oldshapes
184
185
186 //============================================================================
187 // 4.Push a selected edges of top face of Box1 in DF,
188 // create Fillet (using selected edges) and push result as modification of Box1
189 //=============================================================================
190 Handle(TNaming_NamedShape) B1NS;
191 Box1Label.FindAttribute(TNaming_NamedShape::GetID(), B1NS);
192 const TopoDS_Shape& box1 = TNaming_Tool::GetShape(B1NS);
193 Handle(TNaming_NamedShape) Top1NS;
194 Top1.FindAttribute(TNaming_NamedShape::GetID(), Top1NS);
195 const TopoDS_Shape& top1face = TNaming_Tool::GetShape(Top1NS);
196
197 BRepFilletAPI_MakeFillet MKFILLET(box1);// fillet's algo
198 TDF_Label SelectedEdgesLabel = aLabel.FindChild(SelectedEdgesPOS); //Label for selected edges
199 TopExp_Explorer exp(top1face, TopAbs_EDGE);
200 Standard_Integer i=1;
201 for(;exp.More();exp.Next(),i++) {
202 const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
203 const TDF_Label& SelEdge = SelectedEdgesLabel.FindChild(i);
204 // Creating TNaming_Selector on label
205 TNaming_Selector Selector(SelEdge);
206
207 // Inserting shape into data framework, we need the context to find neighbourhood of shape
208 // For example the context for a lateral face of cone is cone itself
209 // If a shape is standalone the context will be the shape itself
210
211 // Selector.Select(Shape, Context);
212 // TNaming_Evolution == SELECTED
213 Selector.Select(E, box1);
214 // Recover selected edge from DF, only for example
215 const TopoDS_Edge& FE = TopoDS::Edge(Selector.NamedShape()->Get());
216 MKFILLET.Add(5., FE);
217 }
218
219 MKFILLET.Build();
220 if(!MKFILLET.IsDone()) return; //Algorithm failed
221
222 // ...put fillet in the DataFramework as modification of Box1
223 TDF_Label FilletLabel = aLabel.FindChild(FilletPOS);
224 TDF_Label DeletedFaces = FilletLabel.FindChild(i++);
225 TDF_Label ModifiedFaces = FilletLabel.FindChild(i++);
226 TDF_Label FacesFromEdges = FilletLabel.FindChild(i++);
227 TDF_Label FacesFromVertices = FilletLabel.FindChild(i);
228
229 // TNaming_Evolution == MODIFY
230 TNaming_Builder bFillet(FilletLabel);
231 bFillet.Modify(box1, MKFILLET.Shape());
232
233 //New faces generated from edges
234 TopTools_MapOfShape View;
235 TNaming_Builder FaceFromEdgeBuilder(FacesFromEdges);
236 TopExp_Explorer ShapeExplorer (box1, TopAbs_EDGE);
237 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
238 const TopoDS_Shape& Root = ShapeExplorer.Current ();
239 if (!View.Add(Root)) continue;
240 const TopTools_ListOfShape& Shapes = MKFILLET.Generated (Root);
241 TopTools_ListIteratorOfListOfShape ShapesIterator (Shapes);
242 for (;ShapesIterator.More (); ShapesIterator.Next ()) {
243 const TopoDS_Shape& newShape = ShapesIterator.Value ();
244 // TNaming_Evolution == GENERATED
245 if (!Root.IsSame (newShape)) FaceFromEdgeBuilder.Generated (Root,newShape );
246 }
247 }
248
249 //Faces of the initial shape modified by MKFILLET
250 View.Clear();
251 TNaming_Builder ModFacesBuilder(ModifiedFaces);
252 ShapeExplorer.Init(box1,TopAbs_FACE);
253 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
254 const TopoDS_Shape& Root = ShapeExplorer.Current ();
255 if (!View.Add(Root)) continue;
256 const TopTools_ListOfShape& Shapes = MKFILLET.Modified (Root);
257 TopTools_ListIteratorOfListOfShape ShapesIterator (Shapes);
258 for (;ShapesIterator.More (); ShapesIterator.Next ()) {
259 const TopoDS_Shape& newShape = ShapesIterator.Value ();
260 // TNaming_Evolution == MODIFY
261 if (!Root.IsSame (newShape)) ModFacesBuilder.Modify (Root,newShape );
262 }
263 }
264
265 //Deleted faces of the initial shape
266 View.Clear();
267 TNaming_Builder DelFacesBuilder(DeletedFaces);
268 ShapeExplorer.Init(box1, TopAbs_FACE);
269 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
270 const TopoDS_Shape& Root = ShapeExplorer.Current ();
271 if (!View.Add(Root)) continue;
272 // TNaming_Evolution == DELETE
273 if (MKFILLET.IsDeleted (Root)) DelFacesBuilder.Delete (Root);
274 }
275
276 //New faces generated from vertices
277 View.Clear();
278 TNaming_Builder FaceFromVertexBuilder(FacesFromVertices);
279 ShapeExplorer.Init(box1, TopAbs_VERTEX);
280 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
281 const TopoDS_Shape& Root = ShapeExplorer.Current ();
282 if (!View.Add(Root)) continue;
283 const TopTools_ListOfShape& Shapes = MKFILLET.Generated (Root);
284 TopTools_ListIteratorOfListOfShape ShapesIterator (Shapes);
285 for (;ShapesIterator.More (); ShapesIterator.Next ()) {
286 const TopoDS_Shape& newShape = ShapesIterator.Value ();
287 // TNaming_Evolution == GENERATED
288 if (!Root.IsSame (newShape)) FaceFromVertexBuilder.Generated (Root,newShape );
289 }
290 }
291 // =====================================================================
292 // 5.Create a Cut (Box1, Box2) as modification of Box1 and push it in DF
293 // Boolean operation - CUT Object=Box1, Tool=Box2
294 // =====================================================================
295
296 TDF_Label CutLabel = aLabel.FindChild(CutPOS);
297
298 // recover Object
299 Handle(TNaming_NamedShape) ObjectNS;
300 FilletLabel.FindAttribute(TNaming_NamedShape::GetID(), ObjectNS);
301 TopoDS_Shape OBJECT = ObjectNS->Get();
302
303 // Select Tool
304 TDF_Label ToolLabel = CutLabel.FindChild(1);
305 TNaming_Selector ToolSelector(ToolLabel);
306 Handle(TNaming_NamedShape) ToolNS;
307 Box2Label.FindAttribute(TNaming_NamedShape::GetID(), ToolNS);
308 const TopoDS_Shape& Tool = ToolNS->Get();
309 //TNaming_Evolution == SELECTED
310 ToolSelector.Select(Tool, Tool);
311 const TopoDS_Shape& TOOL = ToolSelector.NamedShape()->Get();
312
313 BRepAlgo_Cut mkCUT (OBJECT, TOOL);
314
315 if (!mkCUT.IsDone()) {
316 cout << "CUT: Algorithm failed" << endl;
317 return;
318 } else
319 {
320 TopTools_ListOfShape Larg;
321 Larg.Append(OBJECT);
322 Larg.Append(TOOL);
323
324 if (!BRepAlgo::IsValid(Larg, mkCUT.Shape(), Standard_True, Standard_False)) {
325
326 cout << "CUT: Result is not valid" << endl;
327 return;
328 } else
329 {
330 // push CUT results in DF as modification of Box1
331 TDF_Label Modified = CutLabel.FindChild(2);
332 TDF_Label Deleted = CutLabel.FindChild(3);
333 TDF_Label Intersections = CutLabel.FindChild(4);
334 TDF_Label NewFaces = CutLabel.FindChild(5);
335
336 TopoDS_Shape newS1 = mkCUT.Shape();
337 const TopoDS_Shape& ObjSh = mkCUT.Shape1();
338
339 //push in the DF result of CUT
340 TNaming_Builder CutBuilder (CutLabel);
341 // TNaming_Evolution == MODIFY
342 CutBuilder.Modify (ObjSh, newS1);
343
344 //push in the DF modified faces
345 View.Clear();
346 TNaming_Builder ModBuilder(Modified);
347 ShapeExplorer.Init(ObjSh, TopAbs_FACE);
348 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
349 const TopoDS_Shape& Root = ShapeExplorer.Current ();
350 if (!View.Add(Root)) continue;
351 const TopTools_ListOfShape& Shapes = mkCUT.Modified (Root);
352 TopTools_ListIteratorOfListOfShape ShapesIterator (Shapes);
353 for (;ShapesIterator.More (); ShapesIterator.Next ()) {
354 const TopoDS_Shape& newShape = ShapesIterator.Value ();
355 // TNaming_Evolution == MODIFY
356 if (!Root.IsSame (newShape)) ModBuilder.Modify (Root,newShape );
357 }
358 }
359
360 //push in the DF deleted faces
361 View.Clear();
362 TNaming_Builder DelBuilder(Deleted);
363 ShapeExplorer.Init (ObjSh,TopAbs_FACE);
364 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
365 const TopoDS_Shape& Root = ShapeExplorer.Current ();
366 if (!View.Add(Root)) continue;
367 // TNaming_Evolution == DELETE
368 if (mkCUT.IsDeleted (Root)) DelBuilder.Delete (Root);
369 }
370
371 // push in the DF section edges
372 TNaming_Builder IntersBuilder(Intersections);
373 Handle(TopOpeBRepBuild_HBuilder) build = mkCUT.Builder();
374 TopTools_ListIteratorOfListOfShape its = build->Section();
375 for (; its.More(); its.Next()) {
376 // TNaming_Evolution == SELECTED
377 IntersBuilder.Select(its.Value(),its.Value());
378 }
379
380 // push in the DF new faces added to the object:
381 const TopoDS_Shape& ToolSh = mkCUT.Shape2();
382 TNaming_Builder newBuilder (NewFaces);
383 ShapeExplorer.Init(ToolSh, TopAbs_FACE);
384 for (; ShapeExplorer.More(); ShapeExplorer.Next()) {
385 const TopoDS_Shape& F = ShapeExplorer.Current();
386 const TopTools_ListOfShape& modified = mkCUT.Modified(F);
387 if (!modified.IsEmpty()) {
388 TopTools_ListIteratorOfListOfShape itr(modified);
389 for (; itr.More (); itr.Next ()) {
390 const TopoDS_Shape& newShape = itr.Value();
391 Handle(TNaming_NamedShape) NS = TNaming_Tool::NamedShape(newShape, NewFaces);
392 if (NS.IsNull() || NS->Evolution() != TNaming_MODIFY) {
393 // TNaming_Evolution == GENERATED
394 newBuilder.Generated(F, newShape);
395 }
396 }
397 }
398 }
399 }
400 }
401 // end of CUT
402
403 // =================================================
404 // 6.Recover result from DF
405 // get final result - Box1 shape after CUT operation
406 // =================================================
407 Handle(TNaming_NamedShape) ResultNS;
408 CutLabel.FindAttribute(TNaming_NamedShape::GetID(), ResultNS);
409 const TopoDS_Shape& Result_1 = ResultNS->Get(); // here is result of cut operation
410 ResultNS.Nullify();
411 Box1Label.FindAttribute(TNaming_NamedShape::GetID(), ResultNS);
412 const TopoDS_Shape& Result_2 = TNaming_Tool::CurrentShape(ResultNS);//here is also result of cut operation
413
414 //
415 //Result_1 and Result_2 are the same shapes
416 //=========================================
417}
418#endif