0023634: Eliminate Polyline and Polygon usage in drawers
[occt.git] / src / MeshVS / MeshVS_TextPrsBuilder.cxx
CommitLineData
b311480e 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
7fd59977 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
aff395a3 43#include <Font_NameOfFont.hxx>
7fd59977 44
45//================================================================
46// Function : Constructor MeshVS_TextPrsBuilder
47// Purpose :
48//================================================================
49MeshVS_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//================================================================
70const 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//================================================================
82void 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//================================================================
95Standard_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//================================================================
107Standard_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//================================================================
126void 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//================================================================
145void 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;
7fd59977 180 // Bold font is used by default for better text readability
eeaaaefb 181 Font_FontAspect AFontAspectType = Font_FA_Bold;
7fd59977 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 ) )
eeaaaefb 196 AFontAspectType = (Font_FontAspect)AAspect;
7fd59977 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
7fd59977 206 MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real));
207 TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*aMaxFaceNodes);
208 Standard_Integer NbNodes;
209 TCollection_AsciiString aStr;
210 MeshVS_EntityType aType;
211 TColStd_PackedMapOfInteger aCustomElements;
212
213 Standard_Real X, Y, Z;
214
215 // subtract the hidden elements and ids to exclude (to minimise allocated memory)
216 TColStd_PackedMapOfInteger anIDs;
217 anIDs.Assign( IDs );
218 if ( IsElement )
219 {
220 Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
221 if ( !aHiddenElems.IsNull() )
222 anIDs.Subtract( aHiddenElems->Map() );
223 }
224 anIDs.Subtract( IDsToExclude );
225
226 TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
227 for( ; it.More(); it.Next() )
228 {
229 Standard_Integer aKey = it.Key();
230 if( GetText ( IsElement, aKey, aStr ) )
231 {
232 if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
233 {
234 if( aType == MeshVS_ET_Node )
235 {
236 X = aCoords(1);
237 Y = aCoords(2);
238 Z = aCoords(3);
239 }
240 else if( aType == MeshVS_ET_Link ||
241 aType == MeshVS_ET_Face ||
242 aType == MeshVS_ET_Volume )
243 {
244 if( IsElement && IsExcludingOn() )
245 IDsToExclude.Add( aKey );
246 X = Y = Z = 0;
247 for ( Standard_Integer i=1; i<=NbNodes; i++ )
248 {
249 X += aCoords (3*i-2);
250 Y += aCoords (3*i-1);
251 Z += aCoords (3*i);
252 }
253 X /= Standard_Real ( NbNodes );
254 Y /= Standard_Real ( NbNodes );
255 Z /= Standard_Real ( NbNodes );
256 }
257 else
258 {
259 aCustomElements.Add( aKey );
260 continue;
261 }
262
263 Graphic3d_Vertex aPoint( X, Y, Z );
264
265 aTextGroup->Marker ( aPoint );
266 aTextGroup->Text ( aStr.ToCString(), aPoint, aHeight );
267 }
268 }
269 }
270
7fd59977 271 if (!aCustomElements.IsEmpty())
272 CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode );
273}