1 // File: MeshVS_TextPrsBuilder.cxx
2 // Created: Mon Sep 22 2003
3 // Author: Alexander SOLOVYOV
4 // Copyright: Open CASCADE 2003
6 #include <MeshVS_TextPrsBuilder.ixx>
8 #include <MeshVS_DisplayModeFlags.hxx>
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>
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>
28 #include <Graphic3d_NameOfFont.hxx>
30 //================================================================
31 // Function : Constructor MeshVS_TextPrsBuilder
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 )
43 Handle ( MeshVS_Drawer ) aDrawer = GetDrawer();
44 if ( !aDrawer.IsNull() )
46 aDrawer->SetDouble ( MeshVS_DA_TextHeight, Height );
47 aDrawer->SetColor ( MeshVS_DA_TextColor, Color );
51 //================================================================
52 // Function : GetTexts
54 //================================================================
55 const MeshVS_DataMapOfIntegerAsciiString& MeshVS_TextPrsBuilder::GetTexts ( const Standard_Boolean IsElements ) const
63 //================================================================
64 // Function : SetTexts
66 //================================================================
67 void MeshVS_TextPrsBuilder::SetTexts ( const Standard_Boolean IsElements,
68 const MeshVS_DataMapOfIntegerAsciiString& Map )
76 //================================================================
77 // Function : HasTexts
79 //================================================================
80 Standard_Boolean MeshVS_TextPrsBuilder::HasTexts ( const Standard_Boolean IsElement ) const
82 Standard_Boolean aRes = (myNodeTextMap.Extent()>0);
84 aRes = (myElemTextMap.Extent()>0);
88 //================================================================
91 //================================================================
92 Standard_Boolean MeshVS_TextPrsBuilder::GetText ( const Standard_Boolean IsElement,
93 const Standard_Integer theID,
94 TCollection_AsciiString& theStr ) const
96 const MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
98 aMap = &myElemTextMap;
100 Standard_Boolean aRes = aMap->IsBound ( theID );
102 theStr = aMap->Find ( theID );
107 //================================================================
108 // Function : SetText
110 //================================================================
111 void MeshVS_TextPrsBuilder::SetText ( const Standard_Boolean IsElement,
112 const Standard_Integer ID,
113 const TCollection_AsciiString& Text )
115 MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
117 aMap = &myElemTextMap;
119 Standard_Boolean aRes = aMap->IsBound ( ID );
121 aMap->ChangeFind ( ID ) = Text;
123 aMap->Bind ( ID, Text );
126 //================================================================
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
136 Handle (MeshVS_DataSource) aSource = GetDataSource();
137 Handle (MeshVS_Drawer) aDrawer = GetDrawer();
138 if ( aSource.IsNull() || aDrawer.IsNull() || !HasTexts( IsElement ) ||
139 ( theDisplayMode & GetFlags() ) == 0 )
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 ) )
149 Prs3d_Root::NewGroup ( Prs );
150 Handle (Graphic3d_Group) aTextGroup = Prs3d_Root::CurrentGroup ( Prs );
152 Quantity_Color AColor = Quantity_NOC_YELLOW;
154 Standard_CString AFont = "Courier New";
156 Standard_CString AFont = "Courier";
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;
171 aDrawer->GetColor ( MeshVS_DA_TextColor, AColor );
172 aDrawer->GetDouble ( MeshVS_DA_TextExpansionFactor, AExpansionFactor );
173 aDrawer->GetDouble ( MeshVS_DA_TextSpace, ASpace );
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;
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 );
192 aTextGroup->BeginPrimitives();
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;
201 Standard_Real X, Y, Z;
203 // subtract the hidden elements and ids to exclude (to minimise allocated memory)
204 TColStd_PackedMapOfInteger anIDs;
208 Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
209 if ( !aHiddenElems.IsNull() )
210 anIDs.Subtract( aHiddenElems->Map() );
212 anIDs.Subtract( IDsToExclude );
214 TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
215 for( ; it.More(); it.Next() )
217 Standard_Integer aKey = it.Key();
218 if( GetText ( IsElement, aKey, aStr ) )
220 if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
222 if( aType == MeshVS_ET_Node )
228 else if( aType == MeshVS_ET_Link ||
229 aType == MeshVS_ET_Face ||
230 aType == MeshVS_ET_Volume )
232 if( IsElement && IsExcludingOn() )
233 IDsToExclude.Add( aKey );
235 for ( Standard_Integer i=1; i<=NbNodes; i++ )
237 X += aCoords (3*i-2);
238 Y += aCoords (3*i-1);
241 X /= Standard_Real ( NbNodes );
242 Y /= Standard_Real ( NbNodes );
243 Z /= Standard_Real ( NbNodes );
247 aCustomElements.Add( aKey );
251 Graphic3d_Vertex aPoint( X, Y, Z );
253 aTextGroup->Marker ( aPoint );
254 aTextGroup->Text ( aStr.ToCString(), aPoint, aHeight );
259 aTextGroup->EndPrimitives();
261 if (!aCustomElements.IsEmpty())
262 CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode );