summaryrefslogtreecommitdiff
path: root/src/MeshVS/MeshVS_TextPrsBuilder.cxx
blob: e97bf1762fe90a0715eeb9ebde371a83d19dbc35 (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
// File:  MeshVS_TextPrsBuilder.cxx
// Created: Mon Sep 22 2003
// Author:  Alexander SOLOVYOV
// Copyright:  Open CASCADE 2003

#include <MeshVS_TextPrsBuilder.ixx>

#include <MeshVS_DisplayModeFlags.hxx>

#include <Graphic3d_Group.hxx>
#include <Prs3d_Root.hxx>
#include <Prs3d_TextAspect.hxx>
#include <Graphic3d_AspectText3d.hxx>
#include <TColStd_ListIteratorOfListOfReal.hxx>
#include <Graphic3d_Vertex.hxx>
#include <Graphic3d_AspectMarker3d.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TCollection_AsciiString.hxx>
#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
#include <TColStd_HPackedMapOfInteger.hxx>

#include <MeshVS_DataSource.hxx>
#include <MeshVS_Drawer.hxx>
#include <MeshVS_Mesh.hxx>
#include <MeshVS_DrawerAttribute.hxx>
#include <MeshVS_Buffer.hxx>

#include <Graphic3d_NameOfFont.hxx>

//================================================================
// Function : Constructor MeshVS_TextPrsBuilder
// Purpose  :
//================================================================
MeshVS_TextPrsBuilder::MeshVS_TextPrsBuilder (  const Handle(MeshVS_Mesh)& Parent,
                                                const Standard_Real Height,
                                                const Quantity_Color& Color,
                                                const MeshVS_DisplayModeFlags& Flags,
                                                const Handle (MeshVS_DataSource)& DS,
                                                const Standard_Integer Id,
                                                const MeshVS_BuilderPriority& Priority )
                                              : MeshVS_PrsBuilder ( Parent, Flags, DS, Id, Priority )
{
  Handle ( MeshVS_Drawer ) aDrawer = GetDrawer();
  if ( !aDrawer.IsNull() )
  {
    aDrawer->SetDouble ( MeshVS_DA_TextHeight, Height );
    aDrawer->SetColor  ( MeshVS_DA_TextColor, Color );
  }
}

//================================================================
// Function : GetTexts
// Purpose  :
//================================================================
const MeshVS_DataMapOfIntegerAsciiString& MeshVS_TextPrsBuilder::GetTexts ( const Standard_Boolean IsElements ) const
{
  if ( IsElements )
    return myElemTextMap;
  else
    return myNodeTextMap;
}

//================================================================
// Function : SetTexts
// Purpose  :
//================================================================
void MeshVS_TextPrsBuilder::SetTexts ( const Standard_Boolean IsElements,
                                       const MeshVS_DataMapOfIntegerAsciiString& Map )
{
  if ( IsElements )
    myElemTextMap = Map;
  else
    myNodeTextMap = Map;
}

//================================================================
// Function : HasTexts
// Purpose  :
//================================================================
Standard_Boolean MeshVS_TextPrsBuilder::HasTexts ( const Standard_Boolean IsElement ) const
{
  Standard_Boolean aRes = (myNodeTextMap.Extent()>0);
  if ( IsElement )
    aRes = (myElemTextMap.Extent()>0);
  return aRes;
}

//================================================================
// Function : GetText
// Purpose  :
//================================================================
Standard_Boolean MeshVS_TextPrsBuilder::GetText ( const Standard_Boolean IsElement,
                                                  const Standard_Integer theID,
                                                  TCollection_AsciiString& theStr ) const
{
  const MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
  if ( IsElement )
    aMap = &myElemTextMap;

  Standard_Boolean aRes = aMap->IsBound ( theID );
  if ( aRes )
    theStr = aMap->Find ( theID );

  return aRes;
}

//================================================================
// Function : SetText
// Purpose  :
//================================================================
void MeshVS_TextPrsBuilder::SetText ( const Standard_Boolean IsElement,
                                      const Standard_Integer ID,
                                      const TCollection_AsciiString& Text )
{
  MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
  if ( IsElement )
    aMap = &myElemTextMap;

  Standard_Boolean aRes = aMap->IsBound ( ID );
  if ( aRes )
    aMap->ChangeFind ( ID ) = Text;
  else
    aMap->Bind ( ID, Text );
}

//================================================================
// Function : Build
// Purpose  :
//================================================================
void MeshVS_TextPrsBuilder::Build ( const Handle(Prs3d_Presentation)& Prs,
                                    const TColStd_PackedMapOfInteger& IDs,
                                    TColStd_PackedMapOfInteger& IDsToExclude,
                                    const Standard_Boolean IsElement,
                                    const Standard_Integer theDisplayMode ) const
{
  Handle (MeshVS_DataSource) aSource = GetDataSource();
  Handle (MeshVS_Drawer) aDrawer = GetDrawer();
  if ( aSource.IsNull() || aDrawer.IsNull() || !HasTexts( IsElement ) ||
    ( theDisplayMode & GetFlags() ) == 0 )
    return;

  Standard_Integer aMaxFaceNodes;
  Standard_Real aHeight;
  if ( !aDrawer->GetInteger ( MeshVS_DA_MaxFaceNodes, aMaxFaceNodes ) ||
    aMaxFaceNodes <= 0 ||
    !aDrawer->GetDouble  ( MeshVS_DA_TextHeight, aHeight )    )
    return;

  Prs3d_Root::NewGroup ( Prs );
  Handle (Graphic3d_Group) aTextGroup = Prs3d_Root::CurrentGroup ( Prs );

  Quantity_Color           AColor           = Quantity_NOC_YELLOW;
#ifdef WNT  
  Standard_CString         AFont            = "Courier New";
#else
  Standard_CString         AFont            = "Courier";
#endif
  Standard_Real            AExpansionFactor = 1.0;
  Standard_Real            ASpace           = 0.0;
  Aspect_TypeOfStyleText   ATextStyle       = Aspect_TOST_ANNOTATION;
  Standard_Integer         AStyleInt;
  Aspect_TypeOfDisplayText ADisplayType     = Aspect_TODT_NORMAL;
  TCollection_AsciiString  AFontString;
  Standard_Integer         ADispInt;
  // Bold font is used by default for better text readability
  OSD_FontAspect           AFontAspectType  = OSD_FA_Bold;
  Standard_Integer         AAspect; 
  

  aDrawer->GetColor  ( MeshVS_DA_TextColor, AColor );
  aDrawer->GetDouble ( MeshVS_DA_TextExpansionFactor, AExpansionFactor );
  aDrawer->GetDouble ( MeshVS_DA_TextSpace, ASpace );

  if ( aDrawer->GetAsciiString ( MeshVS_DA_TextFont, AFontString ) )
    AFont = AFontString.ToCString();
  if ( aDrawer->GetInteger ( MeshVS_DA_TextStyle, AStyleInt ) )
    ATextStyle = (Aspect_TypeOfStyleText) AStyleInt;
  if ( aDrawer->GetInteger ( MeshVS_DA_TextDisplayType, ADispInt ) )
    ADisplayType = (Aspect_TypeOfDisplayText) ADispInt;
  if ( aDrawer->GetInteger ( MeshVS_DA_TextFontAspect, AAspect ) )
    AFontAspectType = (OSD_FontAspect)AAspect;         

  Handle (Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d ( AColor, AFont, AExpansionFactor, ASpace,
    ATextStyle, ADisplayType );
  aTextAspect->SetTextFontAspect( AFontAspectType );
  Handle (Graphic3d_AspectMarker3d) anAspectMarker3d =
    new Graphic3d_AspectMarker3d( Aspect_TOM_POINT, Quantity_NOC_GRAY, 1. );
  aTextGroup->SetPrimitivesAspect( aTextAspect );
  aTextGroup->SetPrimitivesAspect( anAspectMarker3d );

  aTextGroup->BeginPrimitives();

  MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real));
  TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*aMaxFaceNodes);
  Standard_Integer NbNodes;
  TCollection_AsciiString aStr;
  MeshVS_EntityType aType;
  TColStd_PackedMapOfInteger aCustomElements;

  Standard_Real X, Y, Z;

  // subtract the hidden elements and ids to exclude (to minimise allocated memory)
  TColStd_PackedMapOfInteger anIDs;
  anIDs.Assign( IDs );
  if ( IsElement )
  {
    Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
    if ( !aHiddenElems.IsNull() )
      anIDs.Subtract( aHiddenElems->Map() );
  }
  anIDs.Subtract( IDsToExclude );

  TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
  for( ; it.More(); it.Next() )
  {
    Standard_Integer aKey = it.Key();
    if( GetText ( IsElement, aKey, aStr ) )
    {
      if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
      {
        if( aType == MeshVS_ET_Node )
        {
          X = aCoords(1);
          Y = aCoords(2);
          Z = aCoords(3);
        }
        else if( aType == MeshVS_ET_Link || 
          aType == MeshVS_ET_Face || 
          aType == MeshVS_ET_Volume )
        {
          if( IsElement && IsExcludingOn() )
            IDsToExclude.Add( aKey );
          X = Y = Z = 0;
          for ( Standard_Integer i=1; i<=NbNodes; i++ )
          {
            X += aCoords (3*i-2);
            Y += aCoords (3*i-1);
            Z += aCoords (3*i);
          }
          X /= Standard_Real ( NbNodes );
          Y /= Standard_Real ( NbNodes );
          Z /= Standard_Real ( NbNodes );
        }
        else 
        {
          aCustomElements.Add( aKey );
          continue;
        }

        Graphic3d_Vertex aPoint( X, Y, Z );

        aTextGroup->Marker ( aPoint );
        aTextGroup->Text ( aStr.ToCString(), aPoint, aHeight );
      }
    }
  }

  aTextGroup->EndPrimitives();

  if (!aCustomElements.IsEmpty())
    CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode );
}