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