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 <Graphic3d_ArrayOfPoints.hxx>
30 #include <TColStd_ListIteratorOfListOfReal.hxx>
31 #include <Graphic3d_Vertex.hxx>
32 #include <Graphic3d_AspectMarker3d.hxx>
33 #include <TColStd_Array1OfReal.hxx>
34 #include <TCollection_AsciiString.hxx>
35 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
36 #include <TColStd_HPackedMapOfInteger.hxx>
38 #include <MeshVS_DataSource.hxx>
39 #include <MeshVS_Drawer.hxx>
40 #include <MeshVS_Mesh.hxx>
41 #include <MeshVS_DrawerAttribute.hxx>
42 #include <MeshVS_Buffer.hxx>
44 #include <Font_NameOfFont.hxx>
46 //================================================================
47 // Function : Constructor MeshVS_TextPrsBuilder
49 //================================================================
50 MeshVS_TextPrsBuilder::MeshVS_TextPrsBuilder ( const Handle(MeshVS_Mesh)& Parent,
51 const Standard_Real Height,
52 const Quantity_Color& Color,
53 const MeshVS_DisplayModeFlags& Flags,
54 const Handle (MeshVS_DataSource)& DS,
55 const Standard_Integer Id,
56 const MeshVS_BuilderPriority& Priority )
57 : MeshVS_PrsBuilder ( Parent, Flags, DS, Id, Priority )
59 Handle ( MeshVS_Drawer ) aDrawer = GetDrawer();
60 if ( !aDrawer.IsNull() )
62 aDrawer->SetDouble ( MeshVS_DA_TextHeight, Height );
63 aDrawer->SetColor ( MeshVS_DA_TextColor, Color );
67 //================================================================
68 // Function : GetTexts
70 //================================================================
71 const MeshVS_DataMapOfIntegerAsciiString& MeshVS_TextPrsBuilder::GetTexts ( const Standard_Boolean IsElements ) const
79 //================================================================
80 // Function : SetTexts
82 //================================================================
83 void MeshVS_TextPrsBuilder::SetTexts ( const Standard_Boolean IsElements,
84 const MeshVS_DataMapOfIntegerAsciiString& Map )
92 //================================================================
93 // Function : HasTexts
95 //================================================================
96 Standard_Boolean MeshVS_TextPrsBuilder::HasTexts ( const Standard_Boolean IsElement ) const
98 Standard_Boolean aRes = (myNodeTextMap.Extent()>0);
100 aRes = (myElemTextMap.Extent()>0);
104 //================================================================
105 // Function : GetText
107 //================================================================
108 Standard_Boolean MeshVS_TextPrsBuilder::GetText ( const Standard_Boolean IsElement,
109 const Standard_Integer theID,
110 TCollection_AsciiString& theStr ) const
112 const MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
114 aMap = &myElemTextMap;
116 Standard_Boolean aRes = aMap->IsBound ( theID );
118 theStr = aMap->Find ( theID );
123 //================================================================
124 // Function : SetText
126 //================================================================
127 void MeshVS_TextPrsBuilder::SetText ( const Standard_Boolean IsElement,
128 const Standard_Integer ID,
129 const TCollection_AsciiString& Text )
131 MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
133 aMap = &myElemTextMap;
135 Standard_Boolean aRes = aMap->IsBound ( ID );
137 aMap->ChangeFind ( ID ) = Text;
139 aMap->Bind ( ID, Text );
142 //================================================================
145 //================================================================
146 void MeshVS_TextPrsBuilder::Build ( const Handle(Prs3d_Presentation)& Prs,
147 const TColStd_PackedMapOfInteger& IDs,
148 TColStd_PackedMapOfInteger& IDsToExclude,
149 const Standard_Boolean IsElement,
150 const Standard_Integer theDisplayMode ) const
152 Handle (MeshVS_DataSource) aSource = GetDataSource();
153 Handle (MeshVS_Drawer) aDrawer = GetDrawer();
154 if ( aSource.IsNull() || aDrawer.IsNull() || !HasTexts( IsElement ) ||
155 ( theDisplayMode & GetFlags() ) == 0 )
158 Standard_Integer aMaxFaceNodes;
159 Standard_Real aHeight;
160 if ( !aDrawer->GetInteger ( MeshVS_DA_MaxFaceNodes, aMaxFaceNodes ) ||
161 aMaxFaceNodes <= 0 ||
162 !aDrawer->GetDouble ( MeshVS_DA_TextHeight, aHeight ) )
165 Prs3d_Root::NewGroup ( Prs );
166 Handle (Graphic3d_Group) aTextGroup = Prs3d_Root::CurrentGroup ( Prs );
168 Quantity_Color AColor = Quantity_NOC_YELLOW;
170 Standard_CString AFont = "Courier New";
172 Standard_CString AFont = "Courier";
174 Standard_Real AExpansionFactor = 1.0;
175 Standard_Real ASpace = 0.0;
176 Aspect_TypeOfStyleText ATextStyle = Aspect_TOST_ANNOTATION;
177 Standard_Integer AStyleInt;
178 Aspect_TypeOfDisplayText ADisplayType = Aspect_TODT_NORMAL;
179 TCollection_AsciiString AFontString;
180 Standard_Integer ADispInt;
181 // Bold font is used by default for better text readability
182 Font_FontAspect AFontAspectType = Font_FA_Bold;
183 Standard_Integer AAspect;
186 aDrawer->GetColor ( MeshVS_DA_TextColor, AColor );
187 aDrawer->GetDouble ( MeshVS_DA_TextExpansionFactor, AExpansionFactor );
188 aDrawer->GetDouble ( MeshVS_DA_TextSpace, ASpace );
190 if ( aDrawer->GetAsciiString ( MeshVS_DA_TextFont, AFontString ) )
191 AFont = AFontString.ToCString();
192 if ( aDrawer->GetInteger ( MeshVS_DA_TextStyle, AStyleInt ) )
193 ATextStyle = (Aspect_TypeOfStyleText) AStyleInt;
194 if ( aDrawer->GetInteger ( MeshVS_DA_TextDisplayType, ADispInt ) )
195 ADisplayType = (Aspect_TypeOfDisplayText) ADispInt;
196 if ( aDrawer->GetInteger ( MeshVS_DA_TextFontAspect, AAspect ) )
197 AFontAspectType = (Font_FontAspect)AAspect;
199 Handle (Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d ( AColor, AFont, AExpansionFactor, ASpace,
200 ATextStyle, ADisplayType );
201 aTextAspect->SetTextFontAspect( AFontAspectType );
202 Handle (Graphic3d_AspectMarker3d) anAspectMarker3d =
203 new Graphic3d_AspectMarker3d( Aspect_TOM_POINT, Quantity_NOC_GRAY, 1. );
204 aTextGroup->SetPrimitivesAspect( aTextAspect );
205 aTextGroup->SetPrimitivesAspect( anAspectMarker3d );
207 MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real));
208 TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*aMaxFaceNodes);
209 Standard_Integer NbNodes;
210 TCollection_AsciiString aStr;
211 MeshVS_EntityType aType;
212 TColStd_PackedMapOfInteger aCustomElements;
214 Standard_Real X, Y, Z;
216 // subtract the hidden elements and ids to exclude (to minimise allocated memory)
217 TColStd_PackedMapOfInteger anIDs;
221 Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
222 if ( !aHiddenElems.IsNull() )
223 anIDs.Subtract( aHiddenElems->Map() );
225 anIDs.Subtract( IDsToExclude );
227 TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
228 for( ; it.More(); it.Next() )
230 Standard_Integer aKey = it.Key();
231 if( GetText ( IsElement, aKey, aStr ) )
233 if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
235 if( aType == MeshVS_ET_Node )
241 else if( aType == MeshVS_ET_Link ||
242 aType == MeshVS_ET_Face ||
243 aType == MeshVS_ET_Volume )
245 if( IsElement && IsExcludingOn() )
246 IDsToExclude.Add( aKey );
248 for ( Standard_Integer i=1; i<=NbNodes; i++ )
250 X += aCoords (3*i-2);
251 Y += aCoords (3*i-1);
254 X /= Standard_Real ( NbNodes );
255 Y /= Standard_Real ( NbNodes );
256 Z /= Standard_Real ( NbNodes );
260 aCustomElements.Add( aKey );
264 Graphic3d_Vertex aPoint (X, Y, Z);
265 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
266 anArrayOfPoints->AddVertex (X, Y, Z);
267 aTextGroup->AddPrimitiveArray (anArrayOfPoints);
268 aTextGroup->Text (aStr.ToCString(), aPoint, aHeight);
273 if (!aCustomElements.IsEmpty())
274 CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode );