summaryrefslogtreecommitdiff
path: root/src/XCAFPrs/XCAFPrs.cxx
blob: f200f2168b111fff3787d796864ed8dd72bb25cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// File:	XCAFPrs.cxx
// Created:	Tue Aug 15 19:37:35 2000
// Author:	Andrey BETENEV
//		<abv@doomox.nnov.matra-dtv.fr>

#include <XCAFPrs.ixx>
#include <XCAFDoc_ShapeTool.hxx>
#include <XCAFDoc_ColorTool.hxx>
#include <TDF_LabelSequence.hxx>
#include <TopoDS_Iterator.hxx>
#include <BRep_Builder.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Compound.hxx>
#include <XCAFDoc_DocumentTool.hxx>
#include <TColStd_HSequenceOfExtendedString.hxx>
#include <XCAFDoc_LayerTool.hxx>
#include <TopTools_SequenceOfShape.hxx>
#include <TopLoc_IndexedMapOfLocation.hxx>
#include <TDF_AttributeSequence.hxx>
#include <XCAFDoc_GraphNode.hxx>

static Standard_Boolean viewnameMode = Standard_False;

static Standard_Boolean getShapesOfSHUO (TopLoc_IndexedMapOfLocation& theaPrevLocMap,
                                         const Handle(XCAFDoc_ShapeTool)& theSTool,
                                         const TDF_Label& theSHUOlab,
                                         TopTools_SequenceOfShape& theSHUOShapeSeq)
{
  Handle(XCAFDoc_GraphNode) SHUO;
  TDF_LabelSequence aLabSeq;
  theSTool->GetSHUONextUsage( theSHUOlab, aLabSeq );
  if (aLabSeq.Length() >= 1)
    for (Standard_Integer i = 1; i <= aLabSeq.Length(); i++) {
      TDF_Label aSubCompL = aLabSeq.Value( i );
      TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( aSubCompL.Father() );
      // create new map of laocation (to not merge locations from different shapes)
      TopLoc_IndexedMapOfLocation aNewPrevLocMap;
      for (Standard_Integer m = 1; m <= theaPrevLocMap.Extent(); m++)
        aNewPrevLocMap.Add( theaPrevLocMap.FindKey( m ) );
      aNewPrevLocMap.Add( compLoc );
      // got for the new sublocations and corresponding shape
      getShapesOfSHUO( aNewPrevLocMap, theSTool, aSubCompL, theSHUOShapeSeq );
    }
  else {
    TopoDS_Shape aSHUO_NUSh = theSTool->GetShape ( theSHUOlab.Father() );
    if ( aSHUO_NUSh.IsNull() ) return Standard_False;
    // cause got shape with location already.
    TopLoc_Location nullLoc;
    aSHUO_NUSh.Location ( nullLoc );
    // multiply the locations
    Standard_Integer intMapLenght = theaPrevLocMap.Extent();
    if ( intMapLenght < 1 )
      return Standard_False; // should not be, but to avoid exception...?
    TopLoc_Location SupcompLoc;
    SupcompLoc = theaPrevLocMap.FindKey( intMapLenght );
    if (intMapLenght > 1) {
      Standard_Integer l = intMapLenght - 1;
      while (l >= 1) {
        SupcompLoc = theaPrevLocMap.FindKey( l ).Multiplied( SupcompLoc );
        l--;
      }
    }
    aSHUO_NUSh.Location( SupcompLoc );
    theSHUOShapeSeq.Append( aSHUO_NUSh );
  }
  return (theSHUOShapeSeq.Length() > 0);
}

//=======================================================================
//function : CollectStyleSettings
//purpose  : 
//=======================================================================

void XCAFPrs::CollectStyleSettings (const TDF_Label &L, 
				    const TopLoc_Location &loc, 
				    XCAFPrs_DataMapOfShapeStyle &settings)
{
  Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( L );

  // for references, first collect colors of referred shape
  TDF_Label Lref;
  if ( XCAFDoc_ShapeTool::GetReferredShape ( L, Lref ) ) {
    TopLoc_Location locSub = loc.Multiplied ( XCAFDoc_ShapeTool::GetLocation ( L ) );
    CollectStyleSettings ( Lref, locSub, settings );
  }
  
  // for assemblies, first collect colors defined in components
  TDF_LabelSequence seq;
  if ( XCAFDoc_ShapeTool::GetComponents ( L, seq ) && seq.Length() >0 ) {
    for ( Standard_Integer i = 1; i <= seq.Length(); i++ ) {
      CollectStyleSettings ( seq.Value(i), loc, settings );
    }
  }

  // collect settings on subshapes and the shape itself
  seq.Clear();
  XCAFDoc_ShapeTool::GetSubShapes ( L, seq );
  seq.Append ( L );
  for ( Standard_Integer i = 1; i <= seq.Length(); i++ ) {
    TDF_Label lab = seq.Value(i);
    XCAFPrs_Style style;
    Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( lab );
    Handle(TColStd_HSequenceOfExtendedString) LayNames = new TColStd_HSequenceOfExtendedString;

    LTool->GetLayers(lab, LayNames);
    Standard_Integer InVisCount = 0;
    for ( Standard_Integer iL = 1; iL <= LayNames->Length(); iL++) {
      if ( !LTool->IsVisible( LTool->FindLayer(LayNames->Value(iL)) ) ) InVisCount++;
    }
    if ( (InVisCount >0 && InVisCount == LayNames->Length() ) ||
         !CTool->IsVisible(lab) ) {
      style.SetVisibility(Standard_False);
    }
    else {
      Quantity_Color C;
      if ( CTool->GetColor ( lab, XCAFDoc_ColorGen, C ) ) {
	style.SetColorCurv ( C );
	style.SetColorSurf ( C );
      }
      if ( CTool->GetColor ( lab, XCAFDoc_ColorSurf, C ) )
	style.SetColorSurf ( C );
      if ( CTool->GetColor ( lab, XCAFDoc_ColorCurv, C ) )
	style.SetColorCurv ( C );
    }
    // PTV try to set color from SHUO structure
    Handle(XCAFDoc_ShapeTool) STool = CTool->ShapeTool();
    Handle(XCAFDoc_GraphNode) SHUO;
    TDF_AttributeSequence theSHUOAttrs;
    if (STool->IsComponent( lab ) ) {
      STool->GetAllComponentSHUO( lab, theSHUOAttrs );
      for (Standard_Integer shuoIndx = 1; shuoIndx <= theSHUOAttrs.Length(); shuoIndx++) {
        SHUO = Handle(XCAFDoc_GraphNode)::DownCast(theSHUOAttrs.Value(shuoIndx));
        if ( SHUO.IsNull() )
          continue;
        TDF_Label aSHUOlab = SHUO->Label();

        TDF_LabelSequence aLabSeq;
        STool->GetSHUONextUsage( aSHUOlab, aLabSeq );
        if (aLabSeq.Length() < 1 )
          continue;
      
        Quantity_Color C;
        XCAFPrs_Style SHUOstyle;
        if (!CTool->IsVisible( aSHUOlab ) )
          SHUOstyle.SetVisibility(Standard_False);
        else {
          if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorGen, C ) ) {
            SHUOstyle.SetColorCurv ( C );
            SHUOstyle.SetColorSurf ( C );
          }
          if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorSurf, C ) )
            SHUOstyle.SetColorSurf ( C );
          if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorCurv, C ) )
            SHUOstyle.SetColorCurv ( C );
        }
        if ( !SHUOstyle.IsSetColorCurv() && 
            !SHUOstyle.IsSetColorSurf() &&
            SHUOstyle.IsVisible() )
          continue;
      
      // set style for all component from Next Usage Occurrence.
#ifdef DEB
        cout << "Set the style for SHUO next_usage-occurrance" << endl;
#endif
        /* 
        // may be work, but static it returns excess shapes. It is more faster to use OLD version.
        // PTV 14.02.2003 NEW version using API of ShapeTool
        TopTools_SequenceOfShape aSHUOShapeSeq;
        STool->GetAllStyledComponents( SHUO, aSHUOShapeSeq );
        for (Standard_Integer si= 1; si <= aSHUOShapeSeq.Length(); si++) {
          TopoDS_Shape aSHUOSh = aSHUOShapeSeq.Value(si);
          if (!aSHUOSh.IsNull())
            settings.Bind ( aSHUOSh, SHUOstyle );
        }
        */
        // OLD version that was written before ShapeTool API, and ti FASTER for presentation
        // get TOP location of SHUO component
        TopLoc_Location compLoc = XCAFDoc_ShapeTool::GetLocation ( lab );
        TopLoc_IndexedMapOfLocation aPrevLocMap;
        // get previous setted location 
        if ( !loc.IsIdentity() )
          aPrevLocMap.Add( loc );
      
        aPrevLocMap.Add( compLoc );
        TopTools_SequenceOfShape aSHUOShapeSeq;
        // get shapes of SHUO Next_Usage components
        getShapesOfSHUO( aPrevLocMap, STool, aSHUOlab, aSHUOShapeSeq );
        for (Standard_Integer n = 1; n <= aSHUOShapeSeq.Length(); n++ ) {
          TopoDS_Shape aSHUOSh = aSHUOShapeSeq.Value( n );
          settings.Bind ( aSHUOSh, SHUOstyle );
        }
        continue;
      }
    }
    if ( !style.IsSetColorCurv() && 
         !style.IsSetColorSurf() &&
          style.IsVisible() )
      continue;
    TopoDS_Shape sub = XCAFDoc_ShapeTool::GetShape ( lab );
    sub.Move ( loc );
    settings.Bind ( sub, style );
  }
}

//=======================================================================
//function : DispatchStyles
//purpose  : fill items map (style - shape)
//=======================================================================

// merge style with father (to reflect inheritance)
static Standard_Boolean MergeStyles (XCAFPrs_Style &style, const XCAFPrs_Style &father)
{
  if ( ! style.IsSetColorCurv() && father.IsSetColorCurv() ) 
    style.SetColorCurv ( father.GetColorCurv() );
  if ( ! style.IsSetColorSurf() && father.IsSetColorSurf() ) 
    style.SetColorSurf ( father.GetColorSurf() );
  return style == father;
}

Standard_Boolean XCAFPrs::DispatchStyles (const TopoDS_Shape &shape, 
					  const XCAFPrs_DataMapOfShapeStyle &settings, 
					  XCAFPrs_DataMapOfStyleShape &items, 
					  const XCAFPrs_Style &DefStyle,
					  const Standard_Boolean force,
					  const TopAbs_ShapeEnum context)
{
  const XCAFPrs_Style *style = &DefStyle;
  XCAFPrs_Style ownstyle;

  // check own setting of current shape
  Standard_Boolean overriden = Standard_False;
  if ( settings.IsBound ( shape ) ) {
    ownstyle = settings.Find ( shape );
    if ( ! MergeStyles ( ownstyle, DefStyle ) ) {
      overriden = Standard_True;
      style = &ownstyle;
    }
  }

  // iterate on subshapes
  BRep_Builder B;
  TopoDS_Shape copy = shape.EmptyCopied();
  Standard_Boolean suboverride = Standard_False;
  Standard_Integer nbDef = 0;
  for ( TopoDS_Iterator it(shape); it.More(); it.Next() ) {
    if ( DispatchStyles ( it.Value(), settings, items, *style, Standard_False, shape.ShapeType() ) ) {
      suboverride = Standard_True;
    }
    else {
      B.Add ( copy, it.Value() );
      nbDef++;
    }
  }
  if ( shape.ShapeType() == TopAbs_FACE || ! suboverride ) 
    copy = shape;
  else if ( ! nbDef ) return overriden || suboverride; // avoid empty compounds

  // if any of styles is overriden regarding to default one, add rest to map
  if ( overriden || force || 
       ( suboverride && context != TopAbs_FACE ) ) { // avoid drawing edges of the same color as face
    TopoDS_Compound C;
    if ( items.IsBound ( *style ) ) 
      C = TopoDS::Compound ( items.Find ( *style ) );
    else {
      B.MakeCompound ( C );
      items.Bind ( *style, C );
    }
    B.Add ( C, copy );
  }
  return overriden || suboverride;
}

//=======================================================================
//function : SetViewNameMode
//purpose  : 
//=======================================================================

void XCAFPrs::SetViewNameMode(const Standard_Boolean aNameMode )
{
  viewnameMode = aNameMode;
}

//=======================================================================
//function : GetViewNameMode
//purpose  : 
//=======================================================================

Standard_Boolean XCAFPrs::GetViewNameMode()
{
  return viewnameMode;
}