1 // Created on: 2003-09-22
2 // Created by: Alexander SOLOVYOV
3 // Copyright (c) 2003-2012 OPEN CASCADE SAS
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.
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.
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.
21 #include <MeshVS_TextPrsBuilder.ixx>
23 #include <MeshVS_DisplayModeFlags.hxx>
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>
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>
43 #include <Graphic3d_NameOfFont.hxx>
45 //================================================================
46 // Function : Constructor MeshVS_TextPrsBuilder
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 )
58 Handle ( MeshVS_Drawer ) aDrawer = GetDrawer();
59 if ( !aDrawer.IsNull() )
61 aDrawer->SetDouble ( MeshVS_DA_TextHeight, Height );
62 aDrawer->SetColor ( MeshVS_DA_TextColor, Color );
66 //================================================================
67 // Function : GetTexts
69 //================================================================
70 const MeshVS_DataMapOfIntegerAsciiString& MeshVS_TextPrsBuilder::GetTexts ( const Standard_Boolean IsElements ) const
78 //================================================================
79 // Function : SetTexts
81 //================================================================
82 void MeshVS_TextPrsBuilder::SetTexts ( const Standard_Boolean IsElements,
83 const MeshVS_DataMapOfIntegerAsciiString& Map )
91 //================================================================
92 // Function : HasTexts
94 //================================================================
95 Standard_Boolean MeshVS_TextPrsBuilder::HasTexts ( const Standard_Boolean IsElement ) const
97 Standard_Boolean aRes = (myNodeTextMap.Extent()>0);
99 aRes = (myElemTextMap.Extent()>0);
103 //================================================================
104 // Function : GetText
106 //================================================================
107 Standard_Boolean MeshVS_TextPrsBuilder::GetText ( const Standard_Boolean IsElement,
108 const Standard_Integer theID,
109 TCollection_AsciiString& theStr ) const
111 const MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
113 aMap = &myElemTextMap;
115 Standard_Boolean aRes = aMap->IsBound ( theID );
117 theStr = aMap->Find ( theID );
122 //================================================================
123 // Function : SetText
125 //================================================================
126 void MeshVS_TextPrsBuilder::SetText ( const Standard_Boolean IsElement,
127 const Standard_Integer ID,
128 const TCollection_AsciiString& Text )
130 MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
132 aMap = &myElemTextMap;
134 Standard_Boolean aRes = aMap->IsBound ( ID );
136 aMap->ChangeFind ( ID ) = Text;
138 aMap->Bind ( ID, Text );
141 //================================================================
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
151 Handle (MeshVS_DataSource) aSource = GetDataSource();
152 Handle (MeshVS_Drawer) aDrawer = GetDrawer();
153 if ( aSource.IsNull() || aDrawer.IsNull() || !HasTexts( IsElement ) ||
154 ( theDisplayMode & GetFlags() ) == 0 )
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 ) )
164 Prs3d_Root::NewGroup ( Prs );
165 Handle (Graphic3d_Group) aTextGroup = Prs3d_Root::CurrentGroup ( Prs );
167 Quantity_Color AColor = Quantity_NOC_YELLOW;
169 Standard_CString AFont = "Courier New";
171 Standard_CString AFont = "Courier";
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;
185 aDrawer->GetColor ( MeshVS_DA_TextColor, AColor );
186 aDrawer->GetDouble ( MeshVS_DA_TextExpansionFactor, AExpansionFactor );
187 aDrawer->GetDouble ( MeshVS_DA_TextSpace, ASpace );
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;
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 );
206 aTextGroup->BeginPrimitives();
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;
215 Standard_Real X, Y, Z;
217 // subtract the hidden elements and ids to exclude (to minimise allocated memory)
218 TColStd_PackedMapOfInteger anIDs;
222 Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
223 if ( !aHiddenElems.IsNull() )
224 anIDs.Subtract( aHiddenElems->Map() );
226 anIDs.Subtract( IDsToExclude );
228 TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
229 for( ; it.More(); it.Next() )
231 Standard_Integer aKey = it.Key();
232 if( GetText ( IsElement, aKey, aStr ) )
234 if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
236 if( aType == MeshVS_ET_Node )
242 else if( aType == MeshVS_ET_Link ||
243 aType == MeshVS_ET_Face ||
244 aType == MeshVS_ET_Volume )
246 if( IsElement && IsExcludingOn() )
247 IDsToExclude.Add( aKey );
249 for ( Standard_Integer i=1; i<=NbNodes; i++ )
251 X += aCoords (3*i-2);
252 Y += aCoords (3*i-1);
255 X /= Standard_Real ( NbNodes );
256 Y /= Standard_Real ( NbNodes );
257 Z /= Standard_Real ( NbNodes );
261 aCustomElements.Add( aKey );
265 Graphic3d_Vertex aPoint( X, Y, Z );
267 aTextGroup->Marker ( aPoint );
268 aTextGroup->Text ( aStr.ToCString(), aPoint, aHeight );
273 aTextGroup->EndPrimitives();
275 if (!aCustomElements.IsEmpty())
276 CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode );