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