1 // Created on: 1999-10-25
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
21 #include <QANewBRepNaming_Loader.ixx>
23 #include <TNaming.hxx>
24 #include <TDF_Label.hxx>
25 #include <TNaming_Builder.hxx>
26 #include <TopLoc_Location.hxx>
27 #include <TopExp_Explorer.hxx>
28 #include <TopTools_MapOfShape.hxx>
29 #include <TopTools_DataMapOfShapeShape.hxx>
30 #include <TopTools_ListOfShape.hxx>
31 #include <TopTools_ListIteratorOfListOfShape.hxx>
32 #include <BRepBuilderAPI_MakeShape.hxx>
33 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
35 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
36 #include <TopoDS_Iterator.hxx>
37 #include <TopTools_MapIteratorOfMapOfShape.hxx>
38 #include <BRepAlgoAPI_BooleanOperation.hxx>
39 #include <TDataStd_Name.hxx>
40 #include <TNaming_NamedShape.hxx>
42 //=======================================================================
43 //function : LoadGeneratedShapes
44 //purpose : Load in the naming data-structure the shape
45 // generated from FACE, EDGE, VERTEX,..., after the
46 // MakeShape operation. <ShapeIn> is the initial
47 // shape. <GeneratedFrom> defines the kind of
48 // shape generation to record in the naming
49 // data-structure. The <builder> is used to store the
50 // set of evolutions in the data-framework of TDF.
51 //=======================================================================
53 void QANewBRepNaming_Loader::LoadGeneratedShapes (BRepBuilderAPI_MakeShape& MS,
54 const TopoDS_Shape& ShapeIn,
55 const TopAbs_ShapeEnum KindOfShape,
56 TNaming_Builder& Builder)
58 TopTools_MapOfShape View;
59 TopExp_Explorer ShapeExplorer (ShapeIn, KindOfShape);
60 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
61 const TopoDS_Shape& Root = ShapeExplorer.Current ();
62 if (!View.Add(Root)) continue;
63 const TopTools_ListOfShape& Shapes = MS.Generated (Root);
64 TopTools_ListIteratorOfListOfShape ShapesIterator (Shapes);
65 for (;ShapesIterator.More (); ShapesIterator.Next ()) {
66 const TopoDS_Shape& newShape = ShapesIterator.Value ();
67 if (!Root.IsSame (newShape)) Builder.Generated (Root,newShape );
74 //=======================================================================
75 //function : LoadAndOrientGeneratedShapes
76 //purpose : The same as LoadGeneratedShapes plus performs orientation of
77 // loaded shapes according orientation of SubShapes
78 //=======================================================================
80 void QANewBRepNaming_Loader::LoadAndOrientGeneratedShapes (BRepBuilderAPI_MakeShape& MS,
81 const TopoDS_Shape& ShapeIn,
82 const TopAbs_ShapeEnum KindOfShape,
83 TNaming_Builder& Builder,
84 const TopTools_DataMapOfShapeShape& SubShapes)
86 TopTools_MapOfShape View;
87 TopExp_Explorer ShapeExplorer (ShapeIn, KindOfShape);
88 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
89 const TopoDS_Shape& Root = ShapeExplorer.Current ();
90 if (!View.Add(Root)) continue;
91 const TopTools_ListOfShape& Shapes = MS.Generated (Root);
92 TopTools_ListIteratorOfListOfShape ShapesIterator (Shapes);
93 for (;ShapesIterator.More (); ShapesIterator.Next ()) {
94 TopoDS_Shape newShape = ShapesIterator.Value ();
95 if (SubShapes.IsBound(newShape)) {
96 newShape.Orientation((SubShapes(newShape)).Orientation());
98 if (!Root.IsSame (newShape)) Builder.Generated (Root,newShape );
103 //=======================================================================
104 //function : LoadModifiedShapes
105 //purpose : Load in the naming data-structure the shape
106 // modified from FACE, EDGE, VERTEX,..., after the
107 // MakeShape operation. <ShapeIn> is the initial
108 // shape. <ModifiedFrom> defines the kind of shape
109 // modification to record in the naming
110 // data-structure. The <builder> is used to store the
111 // set of evolutions in the data-framework of TDF.
112 //=======================================================================
114 void QANewBRepNaming_Loader::LoadModifiedShapes (BRepBuilderAPI_MakeShape& MS,
115 const TopoDS_Shape& ShapeIn,
116 const TopAbs_ShapeEnum KindOfShape,
117 TNaming_Builder& Builder,
118 const Standard_Boolean theBool)
120 TopTools_MapOfShape View;
121 TopExp_Explorer ShapeExplorer (ShapeIn, KindOfShape);
122 TopTools_ListOfShape Shapes;
123 BRepAlgoAPI_BooleanOperation* pMS;
125 pMS = (reinterpret_cast<BRepAlgoAPI_BooleanOperation *>(&MS));
126 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
127 const TopoDS_Shape& Root = ShapeExplorer.Current ();
128 if (!View.Add(Root)) continue;
129 // const TopTools_ListOfShape& Shapes = MS.Modified (Root);
131 Shapes = pMS->Modified (Root);
133 Shapes = MS.Modified (Root);
134 TopTools_ListIteratorOfListOfShape ShapesIterator (Shapes);
135 for (;ShapesIterator.More (); ShapesIterator.Next ()) {
136 const TopoDS_Shape& newShape = ShapesIterator.Value ();
137 if (!Root.IsSame (newShape)) {
138 Builder.Modify (Root,newShape );
145 //=======================================================================
146 //function : LoadAndOrientModifiedShapes
147 //purpose : The same as LoadModifiedShapes plus performs orientation of
148 // loaded shapes according orientation of SubShapes
149 //=======================================================================
151 void QANewBRepNaming_Loader::LoadAndOrientModifiedShapes (BRepBuilderAPI_MakeShape& MS,
152 const TopoDS_Shape& ShapeIn,
153 const TopAbs_ShapeEnum KindOfShape,
154 TNaming_Builder& Builder,
155 const TopTools_DataMapOfShapeShape& SubShapes)
157 TopTools_MapOfShape View;
158 TopExp_Explorer ShapeExplorer (ShapeIn, KindOfShape);
159 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
160 const TopoDS_Shape& Root = ShapeExplorer.Current ();
161 if (!View.Add(Root)) continue;
162 const TopTools_ListOfShape& Shapes = MS.Modified(Root);
163 TopTools_ListIteratorOfListOfShape ShapesIterator (Shapes);
164 for (;ShapesIterator.More (); ShapesIterator.Next ()) {
165 TopoDS_Shape newShape = ShapesIterator.Value ();
166 if (SubShapes.IsBound(newShape)) {
167 newShape.Orientation((SubShapes(newShape)).Orientation());
169 if (!Root.IsSame (newShape)) Builder.Modify (Root,newShape );
175 //=======================================================================
176 //function : LoadDeletedShapes
177 //purpose : Load in the naming data-structure the shape
178 // deleted after the MakeShape operation.
179 // <ShapeIn> is the initial shape.
180 // <KindOfDeletedShape> defines the kind of
181 // deletion to record in the naming data-structure.
182 // The <builder> is used to store the set of evolutions
183 // in the data-framework of TDF.
184 //=======================================================================
186 void QANewBRepNaming_Loader::LoadDeletedShapes (BRepBuilderAPI_MakeShape& MS,
187 const TopoDS_Shape& ShapeIn,
188 const TopAbs_ShapeEnum KindOfShape,
189 TNaming_Builder& Builder)
191 TopTools_MapOfShape View;
192 TopExp_Explorer ShapeExplorer (ShapeIn, KindOfShape);
193 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
194 const TopoDS_Shape& Root = ShapeExplorer.Current ();
195 if (!View.Add(Root)) continue;
196 if (MS.IsDeleted (Root)) Builder.Delete (Root);
201 //=======================================================================
202 //function : ModifyPart
203 //purpose : Internal Tool
204 //=======================================================================
206 void QANewBRepNaming_Loader::ModifyPart (const TopoDS_Shape& PartShape,
207 const TopoDS_Shape& Primitive,
208 const TDF_Label& Label)
211 TNaming_Builder Builder (Label);
213 TopLoc_Location PartLocation = PartShape.Location ();
214 if (!PartLocation.IsIdentity ()) {
215 TopLoc_Location Identity;
216 Builder.Modify (PartShape.Located(Identity), Primitive);
217 TNaming::Displace (Label, PartLocation);
219 else Builder.Modify (PartShape, Primitive);
222 //=======================================================================
223 //function : HasDangleShapes
225 //=======================================================================
227 Standard_Boolean QANewBRepNaming_Loader::HasDangleShapes(const TopoDS_Shape& ShapeIn) {
228 if (ShapeIn.ShapeType() == TopAbs_COMPOUND) {
229 TopoDS_Iterator itr(ShapeIn);
230 for (; itr.More(); itr.Next())
231 if (itr.Value().ShapeType() != TopAbs_SOLID) return Standard_True;
232 return Standard_False;
233 } else if (ShapeIn.ShapeType() == TopAbs_COMPSOLID ||
234 ShapeIn.ShapeType() == TopAbs_SOLID) {
235 return Standard_False;
236 } else if (ShapeIn.ShapeType() == TopAbs_SHELL ||
237 ShapeIn.ShapeType() == TopAbs_FACE ||
238 ShapeIn.ShapeType() == TopAbs_WIRE ||
239 ShapeIn.ShapeType() == TopAbs_EDGE ||
240 ShapeIn.ShapeType() == TopAbs_VERTEX)
241 return Standard_True;
242 return Standard_False;
245 //=======================================================================
246 //function : GetDangleShapes
247 //purpose : Returns dangle sub shapes Generator - Dangle.
248 //=======================================================================
250 Standard_Boolean QANewBRepNaming_Loader::GetDangleShapes(const TopoDS_Shape& ShapeIn,
251 const TopAbs_ShapeEnum GeneratedFrom,
252 TopTools_DataMapOfShapeShape& Dangles)
255 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
256 TopAbs_ShapeEnum GeneratedTo;
257 if (GeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
258 else if (GeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
259 else return Standard_False;
260 TopExp::MapShapesAndAncestors(ShapeIn, GeneratedTo, GeneratedFrom, subShapeAndAncestors);
261 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
262 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
263 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
264 if (ancestors.Extent() == 1) Dangles.Bind(ancestors.First(), mayBeDangle);
266 return Dangles.Extent();
269 //=======================================================================
270 //function : GetDangleShapes
271 //purpose : Returns dangle sub shapes.
272 //=======================================================================
273 Standard_Boolean QANewBRepNaming_Loader::GetDangleShapes(const TopoDS_Shape& ShapeIn,
274 const TopAbs_ShapeEnum GeneratedFrom,
275 TopTools_MapOfShape& Dangles)
278 TopTools_IndexedDataMapOfShapeListOfShape subShapeAndAncestors;
279 TopAbs_ShapeEnum GeneratedTo;
280 if (GeneratedFrom == TopAbs_FACE) GeneratedTo = TopAbs_EDGE;
281 else if (GeneratedFrom == TopAbs_EDGE) GeneratedTo = TopAbs_VERTEX;
282 else return Standard_False;
283 TopExp::MapShapesAndAncestors(ShapeIn, GeneratedTo, GeneratedFrom, subShapeAndAncestors);
284 for (Standard_Integer i = 1; i <= subShapeAndAncestors.Extent(); i++) {
285 const TopoDS_Shape& mayBeDangle = subShapeAndAncestors.FindKey(i);
286 const TopTools_ListOfShape& ancestors = subShapeAndAncestors.FindFromIndex(i);
287 if (ancestors.Extent() == 1) Dangles.Add(mayBeDangle);
289 return Dangles.Extent();
292 //=======================================================================
293 //function : LoadGeneratedDangleShapes
295 //=======================================================================
297 void QANewBRepNaming_Loader::LoadGeneratedDangleShapes(const TopoDS_Shape& ShapeIn,
298 const TopAbs_ShapeEnum GeneratedFrom,
299 TNaming_Builder& Builder)
301 TopTools_DataMapOfShapeShape dangles;
302 if (!QANewBRepNaming_Loader::GetDangleShapes(ShapeIn, GeneratedFrom, dangles)) return;
303 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
304 for (; itr.More(); itr.Next()) Builder.Generated(itr.Key(), itr.Value());
307 //=======================================================================
308 //function : LoadGeneratedDangleShapes
310 //=======================================================================
312 void QANewBRepNaming_Loader::LoadGeneratedDangleShapes(const TopoDS_Shape& ShapeIn,
313 const TopAbs_ShapeEnum GeneratedFrom,
314 const TopTools_MapOfShape& OnlyThese,
315 TNaming_Builder& Builder)
317 TopTools_DataMapOfShapeShape dangles;
318 if (!QANewBRepNaming_Loader::GetDangleShapes(ShapeIn, GeneratedFrom, dangles)) return;
319 TopTools_DataMapIteratorOfDataMapOfShapeShape itr(dangles);
320 for (; itr.More(); itr.Next()) {
321 if (!OnlyThese.Contains(itr.Value())) continue;
322 Builder.Generated(itr.Key(), itr.Value());
326 //=======================================================================
327 //function : LoadModifiedDangleShapes
329 //=======================================================================
331 void QANewBRepNaming_Loader::LoadModifiedDangleShapes (BRepBuilderAPI_MakeShape& MS,
332 const TopoDS_Shape& ShapeIn,
333 const TopAbs_ShapeEnum KindOfShape,
334 TNaming_Builder& Builder)
336 TopTools_MapOfShape OnlyThese;
337 TopAbs_ShapeEnum neighbour = TopAbs_EDGE;
338 if (KindOfShape == TopAbs_EDGE) neighbour = TopAbs_FACE;
339 if (!QANewBRepNaming_Loader::GetDangleShapes(ShapeIn, neighbour, OnlyThese)) return;
341 TopTools_MapOfShape View;
342 TopExp_Explorer ShapeExplorer (ShapeIn, KindOfShape);
343 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
344 const TopoDS_Shape& Root = ShapeExplorer.Current ();
345 if (!View.Add(Root) || !OnlyThese.Contains(Root)) continue;
346 const TopTools_ListOfShape& Shapes = MS.Modified (Root);
347 TopTools_ListIteratorOfListOfShape ShapesIterator (Shapes);
348 for (;ShapesIterator.More (); ShapesIterator.Next ()) {
349 const TopoDS_Shape& newShape = ShapesIterator.Value ();
350 if (!Root.IsSame (newShape)) {
351 Builder.Modify (Root,newShape );
357 //=======================================================================
358 //function : IsDangle
359 //purpose : Don't use this method inside an iteration process!
360 //=======================================================================
361 Standard_Boolean QANewBRepNaming_Loader::IsDangle (const TopoDS_Shape& theDangle,
362 const TopoDS_Shape& theShape) {
363 TopTools_MapOfShape dangles;
364 TopAbs_ShapeEnum neighbour = TopAbs_EDGE;
365 if (theDangle.ShapeType() == TopAbs_EDGE) neighbour = TopAbs_FACE;
366 if (!QANewBRepNaming_Loader::GetDangleShapes(theShape, neighbour, dangles)) return Standard_False;
367 return dangles.Contains(theDangle);
370 //=======================================================================
371 //function : LoadDeletedDangleShapes
373 //=======================================================================
374 void QANewBRepNaming_Loader::LoadDeletedDangleShapes (BRepBuilderAPI_MakeShape& MS,
375 const TopoDS_Shape& ShapeIn,
376 const TopAbs_ShapeEnum KindOfShape,
377 TNaming_Builder& Builder)
379 if (KindOfShape != TopAbs_EDGE && KindOfShape != TopAbs_VERTEX) return; // not implemented ...
380 TopTools_MapOfShape View;
381 TopExp_Explorer ShapeExplorer (ShapeIn, KindOfShape);
382 for (; ShapeExplorer.More(); ShapeExplorer.Next ()) {
383 const TopoDS_Shape& Root = ShapeExplorer.Current ();
384 if (!View.Add(Root)) continue;
385 if (!QANewBRepNaming_Loader::IsDangle(Root, ShapeIn)) continue;
386 if (MS.IsDeleted (Root)) Builder.Delete (Root);
390 //=======================================================================
391 //function : LoadDangleShapes
393 //=======================================================================
394 void QANewBRepNaming_Loader::LoadDangleShapes(const TopoDS_Shape& theShape,const TDF_Label& theLabelGenerator) {
395 QANewBRepNaming_Loader::LoadDangleShapes(theShape, TopoDS_Shape(), theLabelGenerator);
398 //=======================================================================
399 //function : LoadDangleShapes
401 //=======================================================================
402 void QANewBRepNaming_Loader::LoadDangleShapes(const TopoDS_Shape& theShape,
403 const TopoDS_Shape& theIgnoredShape,
404 const TDF_Label& theLabelGenerator)
406 TopTools_MapOfShape dangles, ignored;
407 TopAbs_ShapeEnum GeneratedFrom = TopAbs_EDGE; // theShape.ShapeType() == TopAbs_WIRE or TopAbs_EDGE
408 if (theShape.ShapeType() == TopAbs_SHELL || theShape.ShapeType() == TopAbs_FACE)
409 GeneratedFrom = TopAbs_FACE;
410 if (!QANewBRepNaming_Loader::GetDangleShapes(theShape, GeneratedFrom, dangles)) return;
411 if (!theIgnoredShape.IsNull()) {
412 TopoDS_Iterator itrI(theIgnoredShape);
413 for (; itrI.More(); itrI.Next()) {
414 TopoDS_Shape ignoredShape = itrI.Value();
415 ignored.Add(ignoredShape);
418 TopTools_MapIteratorOfMapOfShape itr (dangles);
419 for (; itr.More(); itr.Next()) {
420 const TopoDS_Shape& aDangle = itr.Key();
421 if (ignored.Contains(aDangle)) continue;
422 TNaming_Builder aBuilder(theLabelGenerator.NewChild());
424 TDataStd_Name::Set(aBuilder.NamedShape()->Label(), "NewShapes");
426 aBuilder.Generated(aDangle);