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.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>
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>
32 static Standard_Boolean viewnameMode = Standard_False;
34 static Standard_Boolean getShapesOfSHUO (TopLoc_IndexedMapOfLocation& theaPrevLocMap,
35 const Handle(XCAFDoc_ShapeTool)& theSTool,
36 const TDF_Label& theSHUOlab,
37 TopTools_SequenceOfShape& theSHUOShapeSeq)
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 );
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;
69 SupcompLoc = theaPrevLocMap.FindKey( l ).Multiplied( SupcompLoc );
73 aSHUO_NUSh.Location( SupcompLoc );
74 theSHUOShapeSeq.Append( aSHUO_NUSh );
76 return (theSHUOShapeSeq.Length() > 0);
79 //=======================================================================
80 //function : CollectStyleSettings
82 //=======================================================================
84 void XCAFPrs::CollectStyleSettings (const TDF_Label &L,
85 const TopLoc_Location &loc,
86 XCAFPrs_DataMapOfShapeStyle &settings)
88 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( L );
90 // for references, first collect colors of referred shape
92 if ( XCAFDoc_ShapeTool::GetReferredShape ( L, Lref ) ) {
93 TopLoc_Location locSub = loc.Multiplied ( XCAFDoc_ShapeTool::GetLocation ( L ) );
94 CollectStyleSettings ( Lref, locSub, settings );
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 );
105 // collect settings on subshapes and the shape itself
107 XCAFDoc_ShapeTool::GetSubShapes ( L, seq );
109 for ( Standard_Integer i = 1; i <= seq.Length(); i++ ) {
110 TDF_Label lab = seq.Value(i);
112 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( lab );
113 Handle(TColStd_HSequenceOfExtendedString) LayNames = new TColStd_HSequenceOfExtendedString;
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++;
120 if ( (InVisCount >0 && InVisCount == LayNames->Length()) ||
121 !CTool->IsVisible(lab) ) {
122 style.SetVisibility(Standard_False);
126 if ( CTool->GetColor ( lab, XCAFDoc_ColorGen, C ) ) {
127 style.SetColorCurv ( C );
128 style.SetColorSurf ( C );
130 if ( CTool->GetColor ( lab, XCAFDoc_ColorSurf, C ) )
131 style.SetColorSurf ( C );
132 if ( CTool->GetColor ( lab, XCAFDoc_ColorCurv, C ) )
133 style.SetColorCurv ( C );
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));
145 TDF_Label aSHUOlab = SHUO->Label();
147 TDF_LabelSequence aLabSeq;
148 STool->GetSHUONextUsage( aSHUOlab, aLabSeq );
149 if (aLabSeq.Length() < 1 )
153 XCAFPrs_Style SHUOstyle;
154 if (!CTool->IsVisible( aSHUOlab ) )
155 SHUOstyle.SetVisibility(Standard_False);
157 if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorGen, C ) ) {
158 SHUOstyle.SetColorCurv ( C );
159 SHUOstyle.SetColorSurf ( C );
161 if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorSurf, C ) )
162 SHUOstyle.SetColorSurf ( C );
163 if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorCurv, C ) )
164 SHUOstyle.SetColorCurv ( C );
166 if ( !SHUOstyle.IsSetColorCurv() &&
167 !SHUOstyle.IsSetColorSurf() &&
168 SHUOstyle.IsVisible() )
171 // set style for all component from Next Usage Occurrence.
173 cout << "Set the style for SHUO next_usage-occurrance" << endl;
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 );
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 );
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 );
205 if ( !style.IsSetColorCurv() &&
206 !style.IsSetColorSurf() &&
209 TopoDS_Shape sub = XCAFDoc_ShapeTool::GetShape ( lab );
211 settings.Bind ( sub, style );
215 //=======================================================================
216 //function : DispatchStyles
217 //purpose : fill items map (style - shape)
218 //=======================================================================
220 // merge style with father (to reflect inheritance)
221 static Standard_Boolean MergeStyles (XCAFPrs_Style &style, const XCAFPrs_Style &father)
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;
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)
237 const XCAFPrs_Style *style = &DefStyle;
238 XCAFPrs_Style ownstyle;
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;
250 // iterate on subshapes
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;
261 B.Add ( copy, it.Value() );
265 if ( shape.ShapeType() == TopAbs_FACE || ! suboverride )
267 else if ( ! nbDef ) return overriden || suboverride; // avoid empty compounds
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
273 if ( items.IsBound ( *style ) )
274 C = TopoDS::Compound ( items.Find ( *style ) );
276 B.MakeCompound ( C );
277 items.Bind ( *style, C );
281 return overriden || suboverride;
284 //=======================================================================
285 //function : SetViewNameMode
287 //=======================================================================
289 void XCAFPrs::SetViewNameMode(const Standard_Boolean aNameMode )
291 viewnameMode = aNameMode;
294 //=======================================================================
295 //function : GetViewNameMode
297 //=======================================================================
299 Standard_Boolean XCAFPrs::GetViewNameMode()