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