0025266: Debug statements in the source are getting flushed on to the console
[occt.git] / src / XCAFPrs / XCAFPrs.cxx
1 // Created on: 2000-08-15
2 // Created by: Andrey BETENEV
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 #include <XCAFPrs.ixx>
17 #include <XCAFDoc_ShapeTool.hxx>
18 #include <XCAFDoc_ColorTool.hxx>
19 #include <TDF_LabelSequence.hxx>
20 #include <TopoDS_Iterator.hxx>
21 #include <BRep_Builder.hxx>
22 #include <TopoDS.hxx>
23 #include <TopoDS_Compound.hxx>
24 #include <XCAFDoc_DocumentTool.hxx>
25 #include <TColStd_HSequenceOfExtendedString.hxx>
26 #include <XCAFDoc_LayerTool.hxx>
27 #include <TopTools_SequenceOfShape.hxx>
28 #include <TopLoc_IndexedMapOfLocation.hxx>
29 #include <TDF_AttributeSequence.hxx>
30 #include <XCAFDoc_GraphNode.hxx>
31
32 static Standard_Boolean viewnameMode = Standard_False;
33
34 static Standard_Boolean getShapesOfSHUO (TopLoc_IndexedMapOfLocation& theaPrevLocMap,
35                                          const Handle(XCAFDoc_ShapeTool)& theSTool,
36                                          const TDF_Label& theSHUOlab,
37                                          TopTools_SequenceOfShape& theSHUOShapeSeq)
38 {
39   Handle(XCAFDoc_GraphNode) SHUO;
40   TDF_LabelSequence aLabSeq;
41   theSTool->GetSHUONextUsage( theSHUOlab, aLabSeq );
42   if (aLabSeq.Length() >= 1)
43     for (Standard_Integer i = 1; i <= aLabSeq.Length(); i++) {
44       TDF_Label aSubCompL = aLabSeq.Value( i );
45       TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSubCompL.Father() );
46       // create new map of laocation (to not merge locations from different shapes)
47       TopLoc_IndexedMapOfLocation aNewPrevLocMap;
48       for (Standard_Integer m = 1; m <= theaPrevLocMap.Extent(); m++)
49         aNewPrevLocMap.Add( theaPrevLocMap.FindKey( m ) );
50       aNewPrevLocMap.Add( compLoc );
51       // got for the new sublocations and corresponding shape
52       getShapesOfSHUO( aNewPrevLocMap, theSTool, aSubCompL, theSHUOShapeSeq );
53     }
54   else {
55     TopoDS_Shape aSHUO_NUSh = theSTool->GetShape ( theSHUOlab.Father() );
56     if ( aSHUO_NUSh.IsNull() ) return Standard_False;
57     // cause got shape with location already.
58     TopLoc_Location nullLoc;
59     aSHUO_NUSh.Location ( nullLoc );
60     // multiply the locations
61     Standard_Integer intMapLenght = theaPrevLocMap.Extent();
62     if ( intMapLenght < 1 )
63       return Standard_False; // should not be, but to avoid exception...?
64     TopLoc_Location SupcompLoc;
65     SupcompLoc = theaPrevLocMap.FindKey( intMapLenght );
66     if (intMapLenght > 1) {
67       Standard_Integer l = intMapLenght - 1;
68       while (l >= 1) {
69         SupcompLoc = theaPrevLocMap.FindKey( l ).Multiplied( SupcompLoc );
70         l--;
71       }
72     }
73     aSHUO_NUSh.Location( SupcompLoc );
74     theSHUOShapeSeq.Append( aSHUO_NUSh );
75   }
76   return (theSHUOShapeSeq.Length() > 0);
77 }
78
79 //=======================================================================
80 //function : CollectStyleSettings
81 //purpose  : 
82 //=======================================================================
83
84 void XCAFPrs::CollectStyleSettings (const TDF_Label &L, 
85                                     const TopLoc_Location &loc, 
86                                     XCAFPrs_DataMapOfShapeStyle &settings)
87 {
88   Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( L );
89
90   // for references, first collect colors of referred shape
91   TDF_Label Lref;
92   if ( XCAFDoc_ShapeTool::GetReferredShape ( L, Lref ) ) {
93     TopLoc_Location locSub = loc.Multiplied ( XCAFDoc_ShapeTool::GetLocation ( L ) );
94     CollectStyleSettings ( Lref, locSub, settings );
95   }
96   
97   // for assemblies, first collect colors defined in components
98   TDF_LabelSequence seq;
99   if ( XCAFDoc_ShapeTool::GetComponents ( L, seq ) && seq.Length() >0 ) {
100     for ( Standard_Integer i = 1; i <= seq.Length(); i++ ) {
101       CollectStyleSettings ( seq.Value(i), loc, settings );
102     }
103   }
104
105   // collect settings on subshapes and the shape itself
106   seq.Clear();
107   XCAFDoc_ShapeTool::GetSubShapes ( L, seq );
108   seq.Append ( L );
109   for ( Standard_Integer i = 1; i <= seq.Length(); i++ ) {
110     TDF_Label lab = seq.Value(i);
111     XCAFPrs_Style style;
112     Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( lab );
113     Handle(TColStd_HSequenceOfExtendedString) LayNames = new TColStd_HSequenceOfExtendedString;
114
115     LTool->GetLayers(lab, LayNames);
116     Standard_Integer InVisCount = 0;
117     for ( Standard_Integer iL = 1; iL <= LayNames->Length(); iL++) {
118       if ( !LTool->IsVisible( LTool->FindLayer(LayNames->Value(iL)) ) ) InVisCount++;
119     }
120     if ( (InVisCount >0 && InVisCount == LayNames->Length()) ||
121          !CTool->IsVisible(lab) ) {
122       style.SetVisibility(Standard_False);
123     }
124     else {
125       Quantity_Color C;
126       if ( CTool->GetColor ( lab, XCAFDoc_ColorGen, C ) ) {
127         style.SetColorCurv ( C );
128         style.SetColorSurf ( C );
129       }
130       if ( CTool->GetColor ( lab, XCAFDoc_ColorSurf, C ) )
131         style.SetColorSurf ( C );
132       if ( CTool->GetColor ( lab, XCAFDoc_ColorCurv, C ) )
133         style.SetColorCurv ( C );
134     }
135     // PTV try to set color from SHUO structure
136     Handle(XCAFDoc_ShapeTool) STool = CTool->ShapeTool();
137     Handle(XCAFDoc_GraphNode) SHUO;
138     TDF_AttributeSequence theSHUOAttrs;
139     if (STool->IsComponent( lab ) ) {
140       STool->GetAllComponentSHUO( lab, theSHUOAttrs );
141       for (Standard_Integer shuoIndx = 1; shuoIndx <= theSHUOAttrs.Length(); shuoIndx++) {
142         SHUO = Handle(XCAFDoc_GraphNode)::DownCast(theSHUOAttrs.Value(shuoIndx));
143         if ( SHUO.IsNull() )
144           continue;
145         TDF_Label aSHUOlab = SHUO->Label();
146
147         TDF_LabelSequence aLabSeq;
148         STool->GetSHUONextUsage( aSHUOlab, aLabSeq );
149         if (aLabSeq.Length() < 1 )
150           continue;
151       
152         Quantity_Color C;
153         XCAFPrs_Style SHUOstyle;
154         if (!CTool->IsVisible( aSHUOlab ) )
155           SHUOstyle.SetVisibility(Standard_False);
156         else {
157           if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorGen, C ) ) {
158             SHUOstyle.SetColorCurv ( C );
159             SHUOstyle.SetColorSurf ( C );
160           }
161           if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorSurf, C ) )
162             SHUOstyle.SetColorSurf ( C );
163           if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorCurv, C ) )
164             SHUOstyle.SetColorCurv ( C );
165         }
166         if ( !SHUOstyle.IsSetColorCurv() && 
167             !SHUOstyle.IsSetColorSurf() &&
168             SHUOstyle.IsVisible() )
169           continue;
170       
171       // set style for all component from Next Usage Occurrence.
172 #ifdef XCAFPRS_DEB
173         cout << "Set the style for SHUO next_usage-occurrance" << endl;
174 #endif
175         /* 
176         // may be work, but static it returns excess shapes. It is more faster to use OLD version.
177         // PTV 14.02.2003 NEW version using API of ShapeTool
178         TopTools_SequenceOfShape aSHUOShapeSeq;
179         STool->GetAllStyledComponents( SHUO, aSHUOShapeSeq );
180         for (Standard_Integer si= 1; si <= aSHUOShapeSeq.Length(); si++) {
181           TopoDS_Shape aSHUOSh = aSHUOShapeSeq.Value(si);
182           if (!aSHUOSh.IsNull())
183             settings.Bind ( aSHUOSh, SHUOstyle );
184         }
185         */
186         // OLD version that was written before ShapeTool API, and ti FASTER for presentation
187         // get TOP location of SHUO component
188         TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( lab );
189         TopLoc_IndexedMapOfLocation aPrevLocMap;
190         // get previous setted location 
191         if ( !loc.IsIdentity() )
192           aPrevLocMap.Add( loc );
193       
194         aPrevLocMap.Add( compLoc );
195         TopTools_SequenceOfShape aSHUOShapeSeq;
196         // get shapes of SHUO Next_Usage components
197         getShapesOfSHUO( aPrevLocMap, STool, aSHUOlab, aSHUOShapeSeq );
198         for (Standard_Integer n = 1; n <= aSHUOShapeSeq.Length(); n++ ) {
199           TopoDS_Shape aSHUOSh = aSHUOShapeSeq.Value( n );
200           settings.Bind ( aSHUOSh, SHUOstyle );
201         }
202         continue;
203       }
204     }
205     if ( !style.IsSetColorCurv() && 
206          !style.IsSetColorSurf() &&
207           style.IsVisible() )
208       continue;
209     TopoDS_Shape sub = XCAFDoc_ShapeTool::GetShape ( lab );
210     sub.Move ( loc );
211     settings.Bind ( sub, style );
212   }
213 }
214
215 //=======================================================================
216 //function : DispatchStyles
217 //purpose  : fill items map (style - shape)
218 //=======================================================================
219
220 // merge style with father (to reflect inheritance)
221 static Standard_Boolean MergeStyles (XCAFPrs_Style &style, const XCAFPrs_Style &father)
222 {
223   if ( ! style.IsSetColorCurv() && father.IsSetColorCurv() ) 
224     style.SetColorCurv ( father.GetColorCurv() );
225   if ( ! style.IsSetColorSurf() && father.IsSetColorSurf() ) 
226     style.SetColorSurf ( father.GetColorSurf() );
227   return style == father;
228 }
229
230 Standard_Boolean XCAFPrs::DispatchStyles (const TopoDS_Shape &shape, 
231                                           const XCAFPrs_DataMapOfShapeStyle &settings, 
232                                           XCAFPrs_DataMapOfStyleShape &items, 
233                                           const XCAFPrs_Style &DefStyle,
234                                           const Standard_Boolean force,
235                                           const TopAbs_ShapeEnum context)
236 {
237   const XCAFPrs_Style *style = &DefStyle;
238   XCAFPrs_Style ownstyle;
239
240   // check own setting of current shape
241   Standard_Boolean overriden = Standard_False;
242   if ( settings.IsBound ( shape ) ) {
243     ownstyle = settings.Find ( shape );
244     if ( ! MergeStyles ( ownstyle, DefStyle ) ) {
245       overriden = Standard_True;
246       style = &ownstyle;
247     }
248   }
249
250   // iterate on subshapes
251   BRep_Builder B;
252   TopoDS_Shape copy = shape.EmptyCopied();
253   copy.Closed (shape.Closed());
254   Standard_Boolean suboverride = Standard_False;
255   Standard_Integer nbDef = 0;
256   for ( TopoDS_Iterator it(shape); it.More(); it.Next() ) {
257     if ( DispatchStyles ( it.Value(), settings, items, *style, Standard_False, shape.ShapeType() ) ) {
258       suboverride = Standard_True;
259     }
260     else {
261       B.Add ( copy, it.Value() );
262       nbDef++;
263     }
264   }
265   if ( shape.ShapeType() == TopAbs_FACE || ! suboverride ) 
266     copy = shape;
267   else if ( ! nbDef ) return overriden || suboverride; // avoid empty compounds
268
269   // if any of styles is overriden regarding to default one, add rest to map
270   if ( overriden || force || 
271        ( suboverride && context != TopAbs_FACE ) ) { // avoid drawing edges of the same color as face
272     TopoDS_Compound C;
273     if ( items.IsBound ( *style ) ) 
274       C = TopoDS::Compound ( items.Find ( *style ) );
275     else {
276       B.MakeCompound ( C );
277       items.Bind ( *style, C );
278     }
279     B.Add ( C, copy );
280   }
281   return overriden || suboverride;
282 }
283
284 //=======================================================================
285 //function : SetViewNameMode
286 //purpose  : 
287 //=======================================================================
288
289 void XCAFPrs::SetViewNameMode(const Standard_Boolean aNameMode )
290 {
291   viewnameMode = aNameMode;
292 }
293
294 //=======================================================================
295 //function : GetViewNameMode
296 //purpose  : 
297 //=======================================================================
298
299 Standard_Boolean XCAFPrs::GetViewNameMode()
300 {
301   return viewnameMode;
302 }