0033041: Coding - get rid of unused headers [TopTools to Xw]
[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.hxx>
17
18 #include <TDF_Label.hxx>
19 #include <TopLoc_IndexedMapOfLocation.hxx>
20 #include <TopLoc_Location.hxx>
21 #include <TopoDS.hxx>
22 #include <TopoDS_Compound.hxx>
23 #include <XCAFDoc_ColorTool.hxx>
24 #include <XCAFDoc_DocumentTool.hxx>
25 #include <XCAFDoc_GraphNode.hxx>
26 #include <XCAFDoc_LayerTool.hxx>
27 #include <XCAFDoc_VisMaterialTool.hxx>
28 #include <XCAFDoc_ShapeTool.hxx>
29 #include <XCAFPrs_Style.hxx>
30
31 static Standard_Boolean viewnameMode = Standard_False;
32
33 //! Fill colors of XCAFPrs_Style structure.
34 static void fillStyleColors (XCAFPrs_Style& theStyle,
35                              const Handle(XCAFDoc_ColorTool)& theTool,
36                              const TDF_Label& theLabel)
37 {
38   Quantity_ColorRGBA aColor;
39   if (theTool->GetColor (theLabel, XCAFDoc_ColorGen, aColor))
40   {
41     theStyle.SetColorCurv (aColor.GetRGB());
42     theStyle.SetColorSurf (aColor);
43   }
44   if (theTool->GetColor (theLabel, XCAFDoc_ColorSurf, aColor))
45   {
46     theStyle.SetColorSurf (aColor);
47   }
48   if (theTool->GetColor (theLabel, XCAFDoc_ColorCurv, aColor))
49   {
50     theStyle.SetColorCurv (aColor.GetRGB());
51   }
52 }
53
54 static Standard_Boolean getShapesOfSHUO (TopLoc_IndexedMapOfLocation& theaPrevLocMap,
55                                          const Handle(XCAFDoc_ShapeTool)& theSTool,
56                                          const TDF_Label& theSHUOlab,
57                                          TopTools_SequenceOfShape& theSHUOShapeSeq)
58 {
59   Handle(XCAFDoc_GraphNode) SHUO;
60   TDF_LabelSequence aLabSeq;
61   theSTool->GetSHUONextUsage( theSHUOlab, aLabSeq );
62   if (aLabSeq.Length() >= 1)
63     for (Standard_Integer i = 1; i <= aLabSeq.Length(); i++) {
64       TDF_Label aSubCompL = aLabSeq.Value( i );
65       TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSubCompL.Father() );
66       // create new map of laocation (to not merge locations from different shapes)
67       TopLoc_IndexedMapOfLocation aNewPrevLocMap;
68       for (Standard_Integer m = 1; m <= theaPrevLocMap.Extent(); m++)
69         aNewPrevLocMap.Add( theaPrevLocMap.FindKey( m ) );
70       aNewPrevLocMap.Add( compLoc );
71       // got for the new sublocations and corresponding shape
72       getShapesOfSHUO( aNewPrevLocMap, theSTool, aSubCompL, theSHUOShapeSeq );
73     }
74   else {
75     TopoDS_Shape aSHUO_NUSh = theSTool->GetShape ( theSHUOlab.Father() );
76     if ( aSHUO_NUSh.IsNull() ) return Standard_False;
77     // cause got shape with location already.
78     TopLoc_Location nullLoc;
79     aSHUO_NUSh.Location ( nullLoc );
80     // multiply the locations
81     Standard_Integer intMapLenght = theaPrevLocMap.Extent();
82     if ( intMapLenght < 1 )
83       return Standard_False; // should not be, but to avoid exception...?
84     TopLoc_Location SupcompLoc;
85     SupcompLoc = theaPrevLocMap.FindKey( intMapLenght );
86     if (intMapLenght > 1) {
87       Standard_Integer l = intMapLenght - 1;
88       while (l >= 1) {
89         SupcompLoc = theaPrevLocMap.FindKey( l ).Multiplied( SupcompLoc );
90         l--;
91       }
92     }
93     aSHUO_NUSh.Location( SupcompLoc, Standard_False );
94     theSHUOShapeSeq.Append( aSHUO_NUSh );
95   }
96   return (theSHUOShapeSeq.Length() > 0);
97 }
98
99 //=======================================================================
100 //function : CollectStyleSettings
101 //purpose  : 
102 //=======================================================================
103
104 void XCAFPrs::CollectStyleSettings (const TDF_Label& theLabel,
105                                     const TopLoc_Location& theLoc,
106                                     XCAFPrs_IndexedDataMapOfShapeStyle& theSettings,
107                                     const Quantity_ColorRGBA& theLayerColor)
108 {
109   // for references, first collect colors of referred shape
110   {
111     TDF_Label aLabelRef;
112     if (XCAFDoc_ShapeTool::GetReferredShape (theLabel, aLabelRef))
113     {
114       Quantity_ColorRGBA aLayerColor = theLayerColor;
115       Handle(XCAFDoc_LayerTool) aLayerTool = XCAFDoc_DocumentTool::LayerTool (theLabel);
116       Handle(TColStd_HSequenceOfExtendedString) aLayerNames = new TColStd_HSequenceOfExtendedString();
117       aLayerTool->GetLayers (theLabel, aLayerNames);
118       if (aLayerNames->Length() == 1)
119       {
120         TDF_Label aLayer = aLayerTool->FindLayer (aLayerNames->First());
121         Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(theLabel);
122         Quantity_ColorRGBA aColor;
123         if (aColorTool->GetColor (aLayer, XCAFDoc_ColorGen, aColor))
124           aLayerColor = aColor;
125       }
126       TopLoc_Location aLocSub = theLoc.Multiplied (XCAFDoc_ShapeTool::GetLocation (theLabel));
127       CollectStyleSettings (aLabelRef, aLocSub, theSettings, aLayerColor);
128     }
129   }
130
131   // for assemblies, first collect colors defined in components
132   {
133     TDF_LabelSequence aComponentLabSeq;
134     if (XCAFDoc_ShapeTool::GetComponents (theLabel, aComponentLabSeq)
135     && !aComponentLabSeq.IsEmpty())
136     {
137       for (TDF_LabelSequence::Iterator aComponentIter (aComponentLabSeq); aComponentIter.More(); aComponentIter.Next())
138       {
139         const TDF_Label& aComponentLab = aComponentIter.Value();
140         CollectStyleSettings (aComponentLab, theLoc, theSettings, theLayerColor);
141       }
142     }
143   }
144
145   // collect settings on subshapes
146   Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(theLabel);
147   Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (theLabel);
148
149   TDF_LabelSequence aLabSeq;
150   XCAFDoc_ShapeTool::GetSubShapes (theLabel, aLabSeq);
151   // and add the shape itself
152   aLabSeq.Append (theLabel);
153   for (TDF_LabelSequence::Iterator aLabIter (aLabSeq); aLabIter.More(); aLabIter.Next())
154   {
155     const TDF_Label& aLabel = aLabIter.Value();
156     XCAFPrs_Style aStyle;
157     aStyle.SetVisibility (aColorTool->IsVisible (aLabel));
158     aStyle.SetMaterial (aMatTool->GetShapeMaterial (aLabel));
159
160     Handle(TColStd_HSequenceOfExtendedString) aLayerNames;
161     Handle(XCAFDoc_LayerTool) aLayerTool = XCAFDoc_DocumentTool::LayerTool (aLabel);
162     if (aStyle.IsVisible())
163     {
164       aLayerNames = new TColStd_HSequenceOfExtendedString();
165       aLayerTool->GetLayers (aLabel, aLayerNames);
166       Standard_Integer aNbHidden = 0;
167       for (TColStd_HSequenceOfExtendedString::Iterator aLayerIter (*aLayerNames); aLayerIter.More(); aLayerIter.Next())
168       {
169         const TCollection_ExtendedString& aLayerName = aLayerIter.Value();
170         if (!aLayerTool->IsVisible (aLayerTool->FindLayer (aLayerName)))
171         {
172           ++aNbHidden;
173         }
174       }
175       aStyle.SetVisibility (aNbHidden == 0
176                          || aNbHidden != aLayerNames->Length());
177     }
178
179     if (aColorTool->IsColorByLayer (aLabel))
180     {
181       Quantity_ColorRGBA aLayerColor = theLayerColor;
182       if (aLayerNames.IsNull())
183       {
184         aLayerNames = new TColStd_HSequenceOfExtendedString();
185         aLayerTool->GetLayers (aLabel, aLayerNames);
186       }
187       if (aLayerNames->Length() == 1)
188       {
189         TDF_Label aLayer = aLayerTool->FindLayer (aLayerNames->First());
190         Quantity_ColorRGBA aColor;
191         if (aColorTool->GetColor (aLayer, XCAFDoc_ColorGen, aColor))
192         {
193           aLayerColor = aColor;
194         }
195       }
196
197       aStyle.SetColorCurv (aLayerColor.GetRGB());
198       aStyle.SetColorSurf (aLayerColor);
199     }
200     else
201     {
202       fillStyleColors (aStyle, aColorTool, aLabel);
203     }
204
205     // PTV try to set color from SHUO structure
206     const Handle(XCAFDoc_ShapeTool)& aShapeTool = aColorTool->ShapeTool();
207     if (aShapeTool->IsComponent (aLabel))
208     {
209       TDF_AttributeSequence aShuoAttribSeq;
210       aShapeTool->GetAllComponentSHUO (aLabel, aShuoAttribSeq);
211       for (TDF_AttributeSequence::Iterator aShuoAttribIter (aShuoAttribSeq); aShuoAttribIter.More(); aShuoAttribIter.Next())
212       {
213         Handle(XCAFDoc_GraphNode) aShuoNode = Handle(XCAFDoc_GraphNode)::DownCast (aShuoAttribIter.Value());
214         if (aShuoNode.IsNull())
215         {
216           continue;
217         }
218
219         const TDF_Label aShuolab = aShuoNode->Label();
220         {
221           TDF_LabelSequence aShuoLabSeq;
222           aShapeTool->GetSHUONextUsage (aShuolab, aShuoLabSeq);
223           if (aShuoLabSeq.IsEmpty())
224           {
225             continue;
226           }
227         }
228
229         XCAFPrs_Style aShuoStyle;
230         aShuoStyle.SetMaterial  (aMatTool->GetShapeMaterial (aShuolab));
231         aShuoStyle.SetVisibility(aColorTool->IsVisible (aShuolab));
232         fillStyleColors (aShuoStyle, aColorTool, aShuolab);
233         if (aShuoStyle.IsEmpty())
234         {
235           continue;
236         }
237
238         // set style for all component from Next Usage Occurrence.
239       #ifdef OCCT_DEBUG
240         std::cout << "Set the style for SHUO next_usage-occurrence" << std::endl;
241       #endif
242         /* 
243         // may be work, but static it returns excess shapes. It is more faster to use OLD version.
244         // PTV 14.02.2003 NEW version using API of ShapeTool
245         TopTools_SequenceOfShape aShuoShapeSeq;
246         aShapeTool->GetAllStyledComponents (aShuoNode, aShuoShapeSeq);
247         for (TopTools_SequenceOfShape::Iterator aShuoShapeIter (aShuoShapeSeq); aShuoShapeIter.More(); aShuoShapeIter.Next())
248         {
249           const TopoDS_Shape& aShuoShape = aShuoShapeIter.Value();
250           if (!aShuoShape.IsNull())
251             theSettings.Bind (aShuoShape, aShuoStyle);
252         }*/
253         // OLD version that was written before ShapeTool API, and it FASTER for presentation
254         // get TOP location of SHUO component
255         TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation (aLabel);
256         TopLoc_IndexedMapOfLocation aPrevLocMap;
257         // get previous set location
258         if (!theLoc.IsIdentity())
259         {
260           aPrevLocMap.Add (theLoc);
261         }
262         aPrevLocMap.Add (compLoc);
263
264         // get shapes of SHUO Next_Usage components
265         TopTools_SequenceOfShape aShuoShapeSeq;
266         getShapesOfSHUO (aPrevLocMap, aShapeTool, aShuolab, aShuoShapeSeq);
267         for (TopTools_SequenceOfShape::Iterator aShuoShapeIter (aShuoShapeSeq); aShuoShapeIter.More(); aShuoShapeIter.Next())
268         {
269           const TopoDS_Shape& aShuoShape = aShuoShapeIter.Value();
270           XCAFPrs_Style* aMapStyle = theSettings.ChangeSeek (aShuoShape);
271           if (aMapStyle == NULL)
272             theSettings.Add (aShuoShape, aShuoStyle);
273           else
274             *aMapStyle = aShuoStyle;
275         }
276         continue;
277       }
278     }
279
280     if (aStyle.IsEmpty())
281     {
282       continue;
283     }
284
285     TopoDS_Shape aSubshape = XCAFDoc_ShapeTool::GetShape (aLabel);
286     if (aSubshape.ShapeType() == TopAbs_COMPOUND)
287     {
288       if (aSubshape.NbChildren() == 0)
289       {
290         continue;
291       }
292     }
293     aSubshape.Move (theLoc, Standard_False);
294     XCAFPrs_Style* aMapStyle = theSettings.ChangeSeek (aSubshape);
295     if (aMapStyle == NULL)
296       theSettings.Add (aSubshape, aStyle);
297     else
298       *aMapStyle = aStyle;
299   }
300 }
301
302 //=======================================================================
303 //function : SetViewNameMode
304 //purpose  : 
305 //=======================================================================
306
307 void XCAFPrs::SetViewNameMode(const Standard_Boolean aNameMode )
308 {
309   viewnameMode = aNameMode;
310 }
311
312 //=======================================================================
313 //function : GetViewNameMode
314 //purpose  : 
315 //=======================================================================
316
317 Standard_Boolean XCAFPrs::GetViewNameMode()
318 {
319   return viewnameMode;
320 }