fa7629c7016abfaf29997b04f42a62d10176e48e
[occt.git] / src / MeshVS / MeshVS_TextPrsBuilder.cxx
1 // File:  MeshVS_TextPrsBuilder.cxx
2 // Created: Mon Sep 22 2003
3 // Author:  Alexander SOLOVYOV
4 // Copyright:  Open CASCADE 2003
5
6 #include <MeshVS_TextPrsBuilder.ixx>
7
8 #include <MeshVS_DisplayModeFlags.hxx>
9
10 #include <Graphic3d_Group.hxx>
11 #include <Prs3d_Root.hxx>
12 #include <Prs3d_TextAspect.hxx>
13 #include <Graphic3d_AspectText3d.hxx>
14 #include <TColStd_ListIteratorOfListOfReal.hxx>
15 #include <Graphic3d_Vertex.hxx>
16 #include <Graphic3d_AspectMarker3d.hxx>
17 #include <TColStd_Array1OfReal.hxx>
18 #include <TCollection_AsciiString.hxx>
19 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
20 #include <TColStd_HPackedMapOfInteger.hxx>
21
22 #include <MeshVS_DataSource.hxx>
23 #include <MeshVS_Drawer.hxx>
24 #include <MeshVS_Mesh.hxx>
25 #include <MeshVS_DrawerAttribute.hxx>
26 #include <MeshVS_Buffer.hxx>
27
28 #include <Graphic3d_NameOfFont.hxx>
29
30 //================================================================
31 // Function : Constructor MeshVS_TextPrsBuilder
32 // Purpose  :
33 //================================================================
34 MeshVS_TextPrsBuilder::MeshVS_TextPrsBuilder (  const Handle(MeshVS_Mesh)& Parent,
35                                                 const Standard_Real Height,
36                                                 const Quantity_Color& Color,
37                                                 const MeshVS_DisplayModeFlags& Flags,
38                                                 const Handle (MeshVS_DataSource)& DS,
39                                                 const Standard_Integer Id,
40                                                 const MeshVS_BuilderPriority& Priority )
41                                               : MeshVS_PrsBuilder ( Parent, Flags, DS, Id, Priority )
42 {
43   Handle ( MeshVS_Drawer ) aDrawer = GetDrawer();
44   if ( !aDrawer.IsNull() )
45   {
46     aDrawer->SetDouble ( MeshVS_DA_TextHeight, Height );
47     aDrawer->SetColor  ( MeshVS_DA_TextColor, Color );
48   }
49 }
50
51 //================================================================
52 // Function : GetTexts
53 // Purpose  :
54 //================================================================
55 const MeshVS_DataMapOfIntegerAsciiString& MeshVS_TextPrsBuilder::GetTexts ( const Standard_Boolean IsElements ) const
56 {
57   if ( IsElements )
58     return myElemTextMap;
59   else
60     return myNodeTextMap;
61 }
62
63 //================================================================
64 // Function : SetTexts
65 // Purpose  :
66 //================================================================
67 void MeshVS_TextPrsBuilder::SetTexts ( const Standard_Boolean IsElements,
68                                        const MeshVS_DataMapOfIntegerAsciiString& Map )
69 {
70   if ( IsElements )
71     myElemTextMap = Map;
72   else
73     myNodeTextMap = Map;
74 }
75
76 //================================================================
77 // Function : HasTexts
78 // Purpose  :
79 //================================================================
80 Standard_Boolean MeshVS_TextPrsBuilder::HasTexts ( const Standard_Boolean IsElement ) const
81 {
82   Standard_Boolean aRes = (myNodeTextMap.Extent()>0);
83   if ( IsElement )
84     aRes = (myElemTextMap.Extent()>0);
85   return aRes;
86 }
87
88 //================================================================
89 // Function : GetText
90 // Purpose  :
91 //================================================================
92 Standard_Boolean MeshVS_TextPrsBuilder::GetText ( const Standard_Boolean IsElement,
93                                                   const Standard_Integer theID,
94                                                   TCollection_AsciiString& theStr ) const
95 {
96   const MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
97   if ( IsElement )
98     aMap = &myElemTextMap;
99
100   Standard_Boolean aRes = aMap->IsBound ( theID );
101   if ( aRes )
102     theStr = aMap->Find ( theID );
103
104   return aRes;
105 }
106
107 //================================================================
108 // Function : SetText
109 // Purpose  :
110 //================================================================
111 void MeshVS_TextPrsBuilder::SetText ( const Standard_Boolean IsElement,
112                                       const Standard_Integer ID,
113                                       const TCollection_AsciiString& Text )
114 {
115   MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
116   if ( IsElement )
117     aMap = &myElemTextMap;
118
119   Standard_Boolean aRes = aMap->IsBound ( ID );
120   if ( aRes )
121     aMap->ChangeFind ( ID ) = Text;
122   else
123     aMap->Bind ( ID, Text );
124 }
125
126 //================================================================
127 // Function : Build
128 // Purpose  :
129 //================================================================
130 void MeshVS_TextPrsBuilder::Build ( const Handle(Prs3d_Presentation)& Prs,
131                                     const TColStd_PackedMapOfInteger& IDs,
132                                     TColStd_PackedMapOfInteger& IDsToExclude,
133                                     const Standard_Boolean IsElement,
134                                     const Standard_Integer theDisplayMode ) const
135 {
136   Handle (MeshVS_DataSource) aSource = GetDataSource();
137   Handle (MeshVS_Drawer) aDrawer = GetDrawer();
138   if ( aSource.IsNull() || aDrawer.IsNull() || !HasTexts( IsElement ) ||
139     ( theDisplayMode & GetFlags() ) == 0 )
140     return;
141
142   Standard_Integer aMaxFaceNodes;
143   Standard_Real aHeight;
144   if ( !aDrawer->GetInteger ( MeshVS_DA_MaxFaceNodes, aMaxFaceNodes ) ||
145     aMaxFaceNodes <= 0 ||
146     !aDrawer->GetDouble  ( MeshVS_DA_TextHeight, aHeight )    )
147     return;
148
149   Prs3d_Root::NewGroup ( Prs );
150   Handle (Graphic3d_Group) aTextGroup = Prs3d_Root::CurrentGroup ( Prs );
151
152   Quantity_Color           AColor           = Quantity_NOC_YELLOW;
153 #ifdef WNT  
154   Standard_CString         AFont            = "Courier New";
155 #else
156   Standard_CString         AFont            = "Courier";
157 #endif
158   Standard_Real            AExpansionFactor = 1.0;
159   Standard_Real            ASpace           = 0.0;
160   Aspect_TypeOfStyleText   ATextStyle       = Aspect_TOST_ANNOTATION;
161   Standard_Integer         AStyleInt;
162   Aspect_TypeOfDisplayText ADisplayType     = Aspect_TODT_NORMAL;
163   TCollection_AsciiString  AFontString;
164   Standard_Integer         ADispInt;
165   Standard_Boolean         ATexFont;
166   // Bold font is used by default for better text readability
167   OSD_FontAspect           AFontAspectType  = OSD_FA_Bold;
168   Standard_Integer         AAspect; 
169   
170
171   aDrawer->GetColor  ( MeshVS_DA_TextColor, AColor );
172   aDrawer->GetDouble ( MeshVS_DA_TextExpansionFactor, AExpansionFactor );
173   aDrawer->GetDouble ( MeshVS_DA_TextSpace, ASpace );
174
175   if ( aDrawer->GetAsciiString ( MeshVS_DA_TextFont, AFontString ) )
176     AFont = AFontString.ToCString();
177   if ( aDrawer->GetInteger ( MeshVS_DA_TextStyle, AStyleInt ) )
178     ATextStyle = (Aspect_TypeOfStyleText) AStyleInt;
179   if ( aDrawer->GetInteger ( MeshVS_DA_TextDisplayType, ADispInt ) )
180     ADisplayType = (Aspect_TypeOfDisplayText) ADispInt;
181   if ( aDrawer->GetInteger ( MeshVS_DA_TextFontAspect, AAspect ) )
182     AFontAspectType = (OSD_FontAspect)AAspect;         
183
184   Handle (Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d ( AColor, AFont, AExpansionFactor, ASpace,
185     ATextStyle, ADisplayType );
186   aTextAspect->SetTextFontAspect( AFontAspectType );
187   Handle (Graphic3d_AspectMarker3d) anAspectMarker3d =
188     new Graphic3d_AspectMarker3d( Aspect_TOM_POINT, Quantity_NOC_GRAY, 1. );
189   aTextGroup->SetPrimitivesAspect( aTextAspect );
190   aTextGroup->SetPrimitivesAspect( anAspectMarker3d );
191
192   aTextGroup->BeginPrimitives();
193
194   MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real));
195   TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*aMaxFaceNodes);
196   Standard_Integer NbNodes;
197   TCollection_AsciiString aStr;
198   MeshVS_EntityType aType;
199   TColStd_PackedMapOfInteger aCustomElements;
200
201   Standard_Real X, Y, Z;
202
203   // subtract the hidden elements and ids to exclude (to minimise allocated memory)
204   TColStd_PackedMapOfInteger anIDs;
205   anIDs.Assign( IDs );
206   if ( IsElement )
207   {
208     Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
209     if ( !aHiddenElems.IsNull() )
210       anIDs.Subtract( aHiddenElems->Map() );
211   }
212   anIDs.Subtract( IDsToExclude );
213
214   TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
215   for( ; it.More(); it.Next() )
216   {
217     Standard_Integer aKey = it.Key();
218     if( GetText ( IsElement, aKey, aStr ) )
219     {
220       if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
221       {
222         if( aType == MeshVS_ET_Node )
223         {
224           X = aCoords(1);
225           Y = aCoords(2);
226           Z = aCoords(3);
227         }
228         else if( aType == MeshVS_ET_Link || 
229           aType == MeshVS_ET_Face || 
230           aType == MeshVS_ET_Volume )
231         {
232           if( IsElement && IsExcludingOn() )
233             IDsToExclude.Add( aKey );
234           X = Y = Z = 0;
235           for ( Standard_Integer i=1; i<=NbNodes; i++ )
236           {
237             X += aCoords (3*i-2);
238             Y += aCoords (3*i-1);
239             Z += aCoords (3*i);
240           }
241           X /= Standard_Real ( NbNodes );
242           Y /= Standard_Real ( NbNodes );
243           Z /= Standard_Real ( NbNodes );
244         }
245         else 
246         {
247           aCustomElements.Add( aKey );
248           continue;
249         }
250
251         Graphic3d_Vertex aPoint( X, Y, Z );
252
253         aTextGroup->Marker ( aPoint );
254         aTextGroup->Text ( aStr.ToCString(), aPoint, aHeight );
255       }
256     }
257   }
258
259   aTextGroup->EndPrimitives();
260
261   if (!aCustomElements.IsEmpty())
262     CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode );
263 }