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