1 // Created on: 2000-08-15
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <XCAFPrs.hxx>
18 #include <TDF_Label.hxx>
19 #include <TopLoc_IndexedMapOfLocation.hxx>
20 #include <TopLoc_Location.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>
31 static Standard_Boolean viewnameMode = Standard_False;
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)
38 Quantity_ColorRGBA aColor;
39 if (theTool->GetColor (theLabel, XCAFDoc_ColorGen, aColor))
41 theStyle.SetColorCurv (aColor.GetRGB());
42 theStyle.SetColorSurf (aColor);
44 if (theTool->GetColor (theLabel, XCAFDoc_ColorSurf, aColor))
46 theStyle.SetColorSurf (aColor);
48 if (theTool->GetColor (theLabel, XCAFDoc_ColorCurv, aColor))
50 theStyle.SetColorCurv (aColor.GetRGB());
54 static Standard_Boolean getShapesOfSHUO (TopLoc_IndexedMapOfLocation& theaPrevLocMap,
55 const Handle(XCAFDoc_ShapeTool)& theSTool,
56 const TDF_Label& theSHUOlab,
57 TopTools_SequenceOfShape& theSHUOShapeSeq)
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 );
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;
89 SupcompLoc = theaPrevLocMap.FindKey( l ).Multiplied( SupcompLoc );
93 aSHUO_NUSh.Location( SupcompLoc, Standard_False );
94 theSHUOShapeSeq.Append( aSHUO_NUSh );
96 return (theSHUOShapeSeq.Length() > 0);
99 //=======================================================================
100 //function : CollectStyleSettings
102 //=======================================================================
104 void XCAFPrs::CollectStyleSettings (const TDF_Label& theLabel,
105 const TopLoc_Location& theLoc,
106 XCAFPrs_IndexedDataMapOfShapeStyle& theSettings,
107 const Quantity_ColorRGBA& theLayerColor)
109 // for references, first collect colors of referred shape
112 if (XCAFDoc_ShapeTool::GetReferredShape (theLabel, aLabelRef))
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)
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;
126 TopLoc_Location aLocSub = theLoc.Multiplied (XCAFDoc_ShapeTool::GetLocation (theLabel));
127 CollectStyleSettings (aLabelRef, aLocSub, theSettings, aLayerColor);
131 // for assemblies, first collect colors defined in components
133 TDF_LabelSequence aComponentLabSeq;
134 if (XCAFDoc_ShapeTool::GetComponents (theLabel, aComponentLabSeq)
135 && !aComponentLabSeq.IsEmpty())
137 for (TDF_LabelSequence::Iterator aComponentIter (aComponentLabSeq); aComponentIter.More(); aComponentIter.Next())
139 const TDF_Label& aComponentLab = aComponentIter.Value();
140 CollectStyleSettings (aComponentLab, theLoc, theSettings, theLayerColor);
145 // collect settings on subshapes
146 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(theLabel);
147 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (theLabel);
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())
155 const TDF_Label& aLabel = aLabIter.Value();
156 XCAFPrs_Style aStyle;
157 aStyle.SetVisibility (aColorTool->IsVisible (aLabel));
158 aStyle.SetMaterial (aMatTool->GetShapeMaterial (aLabel));
160 Handle(TColStd_HSequenceOfExtendedString) aLayerNames;
161 Handle(XCAFDoc_LayerTool) aLayerTool = XCAFDoc_DocumentTool::LayerTool (aLabel);
162 if (aStyle.IsVisible())
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())
169 const TCollection_ExtendedString& aLayerName = aLayerIter.Value();
170 if (!aLayerTool->IsVisible (aLayerTool->FindLayer (aLayerName)))
175 aStyle.SetVisibility (aNbHidden == 0
176 || aNbHidden != aLayerNames->Length());
179 if (aColorTool->IsColorByLayer (aLabel))
181 Quantity_ColorRGBA aLayerColor = theLayerColor;
182 if (aLayerNames.IsNull())
184 aLayerNames = new TColStd_HSequenceOfExtendedString();
185 aLayerTool->GetLayers (aLabel, aLayerNames);
187 if (aLayerNames->Length() == 1)
189 TDF_Label aLayer = aLayerTool->FindLayer (aLayerNames->First());
190 Quantity_ColorRGBA aColor;
191 if (aColorTool->GetColor (aLayer, XCAFDoc_ColorGen, aColor))
193 aLayerColor = aColor;
197 aStyle.SetColorCurv (aLayerColor.GetRGB());
198 aStyle.SetColorSurf (aLayerColor);
202 fillStyleColors (aStyle, aColorTool, aLabel);
205 // PTV try to set color from SHUO structure
206 const Handle(XCAFDoc_ShapeTool)& aShapeTool = aColorTool->ShapeTool();
207 if (aShapeTool->IsComponent (aLabel))
209 TDF_AttributeSequence aShuoAttribSeq;
210 aShapeTool->GetAllComponentSHUO (aLabel, aShuoAttribSeq);
211 for (TDF_AttributeSequence::Iterator aShuoAttribIter (aShuoAttribSeq); aShuoAttribIter.More(); aShuoAttribIter.Next())
213 Handle(XCAFDoc_GraphNode) aShuoNode = Handle(XCAFDoc_GraphNode)::DownCast (aShuoAttribIter.Value());
214 if (aShuoNode.IsNull())
219 const TDF_Label aShuolab = aShuoNode->Label();
221 TDF_LabelSequence aShuoLabSeq;
222 aShapeTool->GetSHUONextUsage (aShuolab, aShuoLabSeq);
223 if (aShuoLabSeq.IsEmpty())
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())
238 // set style for all component from Next Usage Occurrence.
240 std::cout << "Set the style for SHUO next_usage-occurrence" << std::endl;
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())
249 const TopoDS_Shape& aShuoShape = aShuoShapeIter.Value();
250 if (!aShuoShape.IsNull())
251 theSettings.Bind (aShuoShape, aShuoStyle);
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())
260 aPrevLocMap.Add (theLoc);
262 aPrevLocMap.Add (compLoc);
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())
269 const TopoDS_Shape& aShuoShape = aShuoShapeIter.Value();
270 XCAFPrs_Style* aMapStyle = theSettings.ChangeSeek (aShuoShape);
271 if (aMapStyle == NULL)
272 theSettings.Add (aShuoShape, aShuoStyle);
274 *aMapStyle = aShuoStyle;
280 if (aStyle.IsEmpty())
285 TopoDS_Shape aSubshape = XCAFDoc_ShapeTool::GetShape (aLabel);
286 if (aSubshape.ShapeType() == TopAbs_COMPOUND)
288 if (aSubshape.NbChildren() == 0)
293 aSubshape.Move (theLoc, Standard_False);
294 XCAFPrs_Style* aMapStyle = theSettings.ChangeSeek (aSubshape);
295 if (aMapStyle == NULL)
296 theSettings.Add (aSubshape, aStyle);
302 //=======================================================================
303 //function : SetViewNameMode
305 //=======================================================================
307 void XCAFPrs::SetViewNameMode(const Standard_Boolean aNameMode )
309 viewnameMode = aNameMode;
312 //=======================================================================
313 //function : GetViewNameMode
315 //=======================================================================
317 Standard_Boolean XCAFPrs::GetViewNameMode()