1 // Created on: 2003-09-22
2 // Created by: Alexander SOLOVYOV
3 // Copyright (c) 2003-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <Font_NameOfFont.hxx>
18 #include <Graphic3d_ArrayOfPoints.hxx>
19 #include <Graphic3d_AspectMarker3d.hxx>
20 #include <Graphic3d_AspectText3d.hxx>
21 #include <Graphic3d_Text.hxx>
22 #include <Graphic3d_Group.hxx>
23 #include <Graphic3d_Vertex.hxx>
24 #include <MeshVS_Buffer.hxx>
25 #include <MeshVS_DataSource.hxx>
26 #include <MeshVS_DisplayModeFlags.hxx>
27 #include <MeshVS_Drawer.hxx>
28 #include <MeshVS_DrawerAttribute.hxx>
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>
42 IMPLEMENT_STANDARD_RTTIEXT(MeshVS_TextPrsBuilder,MeshVS_PrsBuilder)
44 //================================================================
45 // Function : Constructor MeshVS_TextPrsBuilder
47 //================================================================
48 MeshVS_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 )
57 Handle ( MeshVS_Drawer ) aDrawer = GetDrawer();
58 if ( !aDrawer.IsNull() )
60 aDrawer->SetDouble ( MeshVS_DA_TextHeight, Height );
61 aDrawer->SetColor ( MeshVS_DA_TextColor, Color );
65 //================================================================
66 // Function : GetTexts
68 //================================================================
69 const MeshVS_DataMapOfIntegerAsciiString& MeshVS_TextPrsBuilder::GetTexts ( const Standard_Boolean IsElements ) const
77 //================================================================
78 // Function : SetTexts
80 //================================================================
81 void MeshVS_TextPrsBuilder::SetTexts ( const Standard_Boolean IsElements,
82 const MeshVS_DataMapOfIntegerAsciiString& Map )
90 //================================================================
91 // Function : HasTexts
93 //================================================================
94 Standard_Boolean MeshVS_TextPrsBuilder::HasTexts ( const Standard_Boolean IsElement ) const
96 Standard_Boolean aRes = (myNodeTextMap.Extent()>0);
98 aRes = (myElemTextMap.Extent()>0);
102 //================================================================
103 // Function : GetText
105 //================================================================
106 Standard_Boolean MeshVS_TextPrsBuilder::GetText ( const Standard_Boolean IsElement,
107 const Standard_Integer theID,
108 TCollection_AsciiString& theStr ) const
110 const MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
112 aMap = &myElemTextMap;
114 Standard_Boolean aRes = aMap->IsBound ( theID );
116 theStr = aMap->Find ( theID );
121 //================================================================
122 // Function : SetText
124 //================================================================
125 void MeshVS_TextPrsBuilder::SetText ( const Standard_Boolean IsElement,
126 const Standard_Integer ID,
127 const TCollection_AsciiString& Text )
129 MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
131 aMap = &myElemTextMap;
133 Standard_Boolean aRes = aMap->IsBound ( ID );
135 aMap->ChangeFind ( ID ) = Text;
137 aMap->Bind ( ID, Text );
140 //================================================================
143 //================================================================
144 void 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
150 Handle (MeshVS_DataSource) aSource = GetDataSource();
151 Handle (MeshVS_Drawer) aDrawer = GetDrawer();
152 if ( aSource.IsNull() || aDrawer.IsNull() || !HasTexts( IsElement ) ||
153 ( theDisplayMode & GetFlags() ) == 0 )
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 ) )
163 Handle(Graphic3d_Group) aTextGroup = Prs->NewGroup();
165 Quantity_Color AColor = Quantity_NOC_YELLOW;
166 Standard_CString AFont = Font_NOF_ASCII_MONO;
167 Standard_Real AExpansionFactor = 1.0;
168 Standard_Real ASpace = 0.0;
169 Aspect_TypeOfStyleText ATextStyle = Aspect_TOST_ANNOTATION;
170 Aspect_TypeOfDisplayText ADisplayType = Aspect_TODT_NORMAL;
171 // Bold font is used by default for better text readability
172 Font_FontAspect AFontAspectType = Font_FA_Bold;
174 aDrawer->GetColor ( MeshVS_DA_TextColor, AColor );
175 aDrawer->GetDouble ( MeshVS_DA_TextExpansionFactor, AExpansionFactor );
176 aDrawer->GetDouble ( MeshVS_DA_TextSpace, ASpace );
178 TCollection_AsciiString AFontString = Font_NOF_ASCII_MONO;
179 if ( aDrawer->GetAsciiString ( MeshVS_DA_TextFont, AFontString ) )
180 AFont = AFontString.ToCString();
182 Standard_Integer AStyleInt = Aspect_TOST_ANNOTATION;
183 if ( aDrawer->GetInteger ( MeshVS_DA_TextStyle, AStyleInt ) )
184 ATextStyle = (Aspect_TypeOfStyleText) AStyleInt;
186 Standard_Integer ADispInt = Aspect_TODT_NORMAL;
187 if ( aDrawer->GetInteger ( MeshVS_DA_TextDisplayType, ADispInt ) )
188 ADisplayType = (Aspect_TypeOfDisplayText) ADispInt;
190 Standard_Integer AAspect = Font_FA_Bold;
191 if ( aDrawer->GetInteger ( MeshVS_DA_TextFontAspect, AAspect ) )
192 AFontAspectType = (Font_FontAspect)AAspect;
194 Handle (Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d ( AColor, AFont, AExpansionFactor, ASpace,
195 ATextStyle, ADisplayType );
196 aTextAspect->SetTextFontAspect( AFontAspectType );
197 aTextGroup->SetGroupPrimitivesAspect( aTextAspect );
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;
206 Standard_Real X, Y, Z;
208 // subtract the hidden elements and ids to exclude (to minimise allocated memory)
209 TColStd_PackedMapOfInteger anIDs;
213 Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
214 if ( !aHiddenElems.IsNull() )
215 anIDs.Subtract( aHiddenElems->Map() );
217 anIDs.Subtract( IDsToExclude );
219 NCollection_Sequence<Graphic3d_Vec3> aPnts;
220 for (TColStd_MapIteratorOfPackedMapOfInteger it (anIDs); it.More(); it.Next())
222 Standard_Integer aKey = it.Key();
223 if( GetText ( IsElement, aKey, aStr ) )
225 if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
227 if( aType == MeshVS_ET_Node )
233 else if( aType == MeshVS_ET_Link ||
234 aType == MeshVS_ET_Face ||
235 aType == MeshVS_ET_Volume )
237 if( IsElement && IsExcludingOn() )
238 IDsToExclude.Add( aKey );
240 for ( Standard_Integer i=1; i<=NbNodes; i++ )
242 X += aCoords (3*i-2);
243 Y += aCoords (3*i-1);
246 X /= Standard_Real ( NbNodes );
247 Y /= Standard_Real ( NbNodes );
248 Z /= Standard_Real ( NbNodes );
252 aCustomElements.Add( aKey );
256 aPnts.Append (Graphic3d_Vec3 ((float )X, (float )Y, (float )Z));
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);
266 if (!aPnts.IsEmpty())
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())
272 const Graphic3d_Vec3& aPnt = aPntIter.Value();
273 anArrayOfPoints->AddVertex (aPnt.x(), aPnt.y(), aPnt.z());
275 Handle (Graphic3d_AspectMarker3d) anAspectMarker3d = new Graphic3d_AspectMarker3d (Aspect_TOM_POINT, Quantity_NOC_GRAY, 1.0);
276 aMarkerGroup->SetGroupPrimitivesAspect (anAspectMarker3d);
277 aMarkerGroup->AddPrimitiveArray (anArrayOfPoints);
280 if (!aCustomElements.IsEmpty())
281 CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode );