0022777: Visualization - Unsafe way to get attribute values from MeshVS_Drawer
[occt.git] / src / MeshVS / MeshVS_TextPrsBuilder.cxx
CommitLineData
b311480e 1// Created on: 2003-09-22
2// Created by: Alexander SOLOVYOV
973c2be1 3// Copyright (c) 2003-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
7fd59977 16
42cf5bc1 17#include <Font_NameOfFont.hxx>
a577aaab 18#include <Graphic3d_ArrayOfPoints.hxx>
7fd59977 19#include <Graphic3d_AspectMarker3d.hxx>
42cf5bc1 20#include <Graphic3d_AspectText3d.hxx>
8ed07085 21#include <Graphic3d_Text.hxx>
42cf5bc1 22#include <Graphic3d_Group.hxx>
23#include <Graphic3d_Vertex.hxx>
24#include <MeshVS_Buffer.hxx>
7fd59977 25#include <MeshVS_DataSource.hxx>
42cf5bc1 26#include <MeshVS_DisplayModeFlags.hxx>
7fd59977 27#include <MeshVS_Drawer.hxx>
7fd59977 28#include <MeshVS_DrawerAttribute.hxx>
42cf5bc1 29#include <MeshVS_Mesh.hxx>
30#include <MeshVS_TextPrsBuilder.hxx>
31#include <Prs3d_Presentation.hxx>
32#include <Prs3d_Root.hxx>
33#include <Prs3d_TextAspect.hxx>
34#include <Quantity_Color.hxx>
35#include <Standard_Type.hxx>
36#include <TCollection_AsciiString.hxx>
37#include <TColStd_Array1OfReal.hxx>
38#include <TColStd_HPackedMapOfInteger.hxx>
39#include <TColStd_ListIteratorOfListOfReal.hxx>
40#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
7fd59977 41
92efcf78 42IMPLEMENT_STANDARD_RTTIEXT(MeshVS_TextPrsBuilder,MeshVS_PrsBuilder)
43
7fd59977 44//================================================================
45// Function : Constructor MeshVS_TextPrsBuilder
46// Purpose :
47//================================================================
48MeshVS_TextPrsBuilder::MeshVS_TextPrsBuilder ( const Handle(MeshVS_Mesh)& Parent,
49 const Standard_Real Height,
50 const Quantity_Color& Color,
51 const MeshVS_DisplayModeFlags& Flags,
52 const Handle (MeshVS_DataSource)& DS,
53 const Standard_Integer Id,
54 const MeshVS_BuilderPriority& Priority )
55 : MeshVS_PrsBuilder ( Parent, Flags, DS, Id, Priority )
56{
57 Handle ( MeshVS_Drawer ) aDrawer = GetDrawer();
58 if ( !aDrawer.IsNull() )
59 {
60 aDrawer->SetDouble ( MeshVS_DA_TextHeight, Height );
61 aDrawer->SetColor ( MeshVS_DA_TextColor, Color );
62 }
63}
64
65//================================================================
66// Function : GetTexts
67// Purpose :
68//================================================================
69const MeshVS_DataMapOfIntegerAsciiString& MeshVS_TextPrsBuilder::GetTexts ( const Standard_Boolean IsElements ) const
70{
71 if ( IsElements )
72 return myElemTextMap;
73 else
74 return myNodeTextMap;
75}
76
77//================================================================
78// Function : SetTexts
79// Purpose :
80//================================================================
81void MeshVS_TextPrsBuilder::SetTexts ( const Standard_Boolean IsElements,
82 const MeshVS_DataMapOfIntegerAsciiString& Map )
83{
84 if ( IsElements )
85 myElemTextMap = Map;
86 else
87 myNodeTextMap = Map;
88}
89
90//================================================================
91// Function : HasTexts
92// Purpose :
93//================================================================
94Standard_Boolean MeshVS_TextPrsBuilder::HasTexts ( const Standard_Boolean IsElement ) const
95{
96 Standard_Boolean aRes = (myNodeTextMap.Extent()>0);
97 if ( IsElement )
98 aRes = (myElemTextMap.Extent()>0);
99 return aRes;
100}
101
102//================================================================
103// Function : GetText
104// Purpose :
105//================================================================
106Standard_Boolean MeshVS_TextPrsBuilder::GetText ( const Standard_Boolean IsElement,
107 const Standard_Integer theID,
108 TCollection_AsciiString& theStr ) const
109{
110 const MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
111 if ( IsElement )
112 aMap = &myElemTextMap;
113
114 Standard_Boolean aRes = aMap->IsBound ( theID );
115 if ( aRes )
116 theStr = aMap->Find ( theID );
117
118 return aRes;
119}
120
121//================================================================
122// Function : SetText
123// Purpose :
124//================================================================
125void MeshVS_TextPrsBuilder::SetText ( const Standard_Boolean IsElement,
126 const Standard_Integer ID,
127 const TCollection_AsciiString& Text )
128{
129 MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
130 if ( IsElement )
131 aMap = &myElemTextMap;
132
133 Standard_Boolean aRes = aMap->IsBound ( ID );
134 if ( aRes )
135 aMap->ChangeFind ( ID ) = Text;
136 else
137 aMap->Bind ( ID, Text );
138}
139
140//================================================================
141// Function : Build
142// Purpose :
143//================================================================
144void MeshVS_TextPrsBuilder::Build ( const Handle(Prs3d_Presentation)& Prs,
145 const TColStd_PackedMapOfInteger& IDs,
146 TColStd_PackedMapOfInteger& IDsToExclude,
147 const Standard_Boolean IsElement,
148 const Standard_Integer theDisplayMode ) const
149{
150 Handle (MeshVS_DataSource) aSource = GetDataSource();
151 Handle (MeshVS_Drawer) aDrawer = GetDrawer();
152 if ( aSource.IsNull() || aDrawer.IsNull() || !HasTexts( IsElement ) ||
153 ( theDisplayMode & GetFlags() ) == 0 )
154 return;
155
156 Standard_Integer aMaxFaceNodes;
157 Standard_Real aHeight;
158 if ( !aDrawer->GetInteger ( MeshVS_DA_MaxFaceNodes, aMaxFaceNodes ) ||
159 aMaxFaceNodes <= 0 ||
160 !aDrawer->GetDouble ( MeshVS_DA_TextHeight, aHeight ) )
161 return;
162
bf5f0ca2 163 Handle(Graphic3d_Group) aTextGroup = Prs->NewGroup();
7fd59977 164
165 Quantity_Color AColor = Quantity_NOC_YELLOW;
bf5f0ca2 166 Standard_CString AFont = Font_NOF_ASCII_MONO;
7fd59977 167 Standard_Real AExpansionFactor = 1.0;
168 Standard_Real ASpace = 0.0;
169 Aspect_TypeOfStyleText ATextStyle = Aspect_TOST_ANNOTATION;
7fd59977 170 Aspect_TypeOfDisplayText ADisplayType = Aspect_TODT_NORMAL;
7fd59977 171 // Bold font is used by default for better text readability
eeaaaefb 172 Font_FontAspect AFontAspectType = Font_FA_Bold;
7fd59977 173
174 aDrawer->GetColor ( MeshVS_DA_TextColor, AColor );
175 aDrawer->GetDouble ( MeshVS_DA_TextExpansionFactor, AExpansionFactor );
176 aDrawer->GetDouble ( MeshVS_DA_TextSpace, ASpace );
177
b1492cb3 178 TCollection_AsciiString AFontString = Font_NOF_ASCII_MONO;
7fd59977 179 if ( aDrawer->GetAsciiString ( MeshVS_DA_TextFont, AFontString ) )
180 AFont = AFontString.ToCString();
b1492cb3 181
182 Standard_Integer AStyleInt = Aspect_TOST_ANNOTATION;
7fd59977 183 if ( aDrawer->GetInteger ( MeshVS_DA_TextStyle, AStyleInt ) )
184 ATextStyle = (Aspect_TypeOfStyleText) AStyleInt;
b1492cb3 185
186 Standard_Integer ADispInt = Aspect_TODT_NORMAL;
7fd59977 187 if ( aDrawer->GetInteger ( MeshVS_DA_TextDisplayType, ADispInt ) )
188 ADisplayType = (Aspect_TypeOfDisplayText) ADispInt;
b1492cb3 189
190 Standard_Integer AAspect = Font_FA_Bold;
7fd59977 191 if ( aDrawer->GetInteger ( MeshVS_DA_TextFontAspect, AAspect ) )
b1492cb3 192 AFontAspectType = (Font_FontAspect)AAspect;
7fd59977 193
194 Handle (Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d ( AColor, AFont, AExpansionFactor, ASpace,
195 ATextStyle, ADisplayType );
196 aTextAspect->SetTextFontAspect( AFontAspectType );
bf5f0ca2 197 aTextGroup->SetGroupPrimitivesAspect( aTextAspect );
7fd59977 198
7fd59977 199 MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real));
200 TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*aMaxFaceNodes);
201 Standard_Integer NbNodes;
202 TCollection_AsciiString aStr;
203 MeshVS_EntityType aType;
204 TColStd_PackedMapOfInteger aCustomElements;
205
206 Standard_Real X, Y, Z;
207
208 // subtract the hidden elements and ids to exclude (to minimise allocated memory)
209 TColStd_PackedMapOfInteger anIDs;
210 anIDs.Assign( IDs );
211 if ( IsElement )
212 {
213 Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
214 if ( !aHiddenElems.IsNull() )
215 anIDs.Subtract( aHiddenElems->Map() );
216 }
217 anIDs.Subtract( IDsToExclude );
218
bf5f0ca2 219 NCollection_Sequence<Graphic3d_Vec3> aPnts;
220 for (TColStd_MapIteratorOfPackedMapOfInteger it (anIDs); it.More(); it.Next())
7fd59977 221 {
222 Standard_Integer aKey = it.Key();
223 if( GetText ( IsElement, aKey, aStr ) )
224 {
225 if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
226 {
227 if( aType == MeshVS_ET_Node )
228 {
229 X = aCoords(1);
230 Y = aCoords(2);
231 Z = aCoords(3);
232 }
233 else if( aType == MeshVS_ET_Link ||
234 aType == MeshVS_ET_Face ||
235 aType == MeshVS_ET_Volume )
236 {
237 if( IsElement && IsExcludingOn() )
238 IDsToExclude.Add( aKey );
239 X = Y = Z = 0;
240 for ( Standard_Integer i=1; i<=NbNodes; i++ )
241 {
242 X += aCoords (3*i-2);
243 Y += aCoords (3*i-1);
244 Z += aCoords (3*i);
245 }
246 X /= Standard_Real ( NbNodes );
247 Y /= Standard_Real ( NbNodes );
248 Z /= Standard_Real ( NbNodes );
249 }
250 else
251 {
252 aCustomElements.Add( aKey );
253 continue;
254 }
255
bf5f0ca2 256 aPnts.Append (Graphic3d_Vec3 ((float )X, (float )Y, (float )Z));
8ed07085 257
258 Handle(Graphic3d_Text) aText = new Graphic3d_Text ((Standard_ShortReal)aHeight);
259 aText->SetText (aStr);
260 aText->SetPosition (gp_Pnt (X, Y, Z));
261 aTextGroup->AddText(aText);
7fd59977 262 }
263 }
264 }
265
bf5f0ca2 266 if (!aPnts.IsEmpty())
267 {
268 Handle(Graphic3d_Group) aMarkerGroup = Prs->NewGroup();
269 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (aPnts.Size());
270 for (NCollection_Sequence<Graphic3d_Vec3>::Iterator aPntIter (aPnts); aPntIter.More(); aPntIter.Next())
271 {
272 const Graphic3d_Vec3& aPnt = aPntIter.Value();
273 anArrayOfPoints->AddVertex (aPnt.x(), aPnt.y(), aPnt.z());
274 }
275 Handle (Graphic3d_AspectMarker3d) anAspectMarker3d = new Graphic3d_AspectMarker3d (Aspect_TOM_POINT, Quantity_NOC_GRAY, 1.0);
276 aMarkerGroup->SetGroupPrimitivesAspect (anAspectMarker3d);
277 aMarkerGroup->AddPrimitiveArray (anArrayOfPoints);
278 }
279
7fd59977 280 if (!aCustomElements.IsEmpty())
281 CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode );
282}