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 Standard_Integer AStyleInt;
171 Aspect_TypeOfDisplayText ADisplayType = Aspect_TODT_NORMAL;
172 TCollection_AsciiString AFontString;
173 Standard_Integer ADispInt;
174 // Bold font is used by default for better text readability
175 Font_FontAspect AFontAspectType = Font_FA_Bold;
176 Standard_Integer AAspect;
179 aDrawer->GetColor ( MeshVS_DA_TextColor, AColor );
180 aDrawer->GetDouble ( MeshVS_DA_TextExpansionFactor, AExpansionFactor );
181 aDrawer->GetDouble ( MeshVS_DA_TextSpace, ASpace );
183 if ( aDrawer->GetAsciiString ( MeshVS_DA_TextFont, AFontString ) )
184 AFont = AFontString.ToCString();
185 if ( aDrawer->GetInteger ( MeshVS_DA_TextStyle, AStyleInt ) )
186 ATextStyle = (Aspect_TypeOfStyleText) AStyleInt;
187 if ( aDrawer->GetInteger ( MeshVS_DA_TextDisplayType, ADispInt ) )
188 ADisplayType = (Aspect_TypeOfDisplayText) ADispInt;
189 if ( aDrawer->GetInteger ( MeshVS_DA_TextFontAspect, AAspect ) )
190 AFontAspectType = (Font_FontAspect)AAspect;
192 Handle (Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d ( AColor, AFont, AExpansionFactor, ASpace,
193 ATextStyle, ADisplayType );
194 aTextAspect->SetTextFontAspect( AFontAspectType );
195 aTextGroup->SetGroupPrimitivesAspect( aTextAspect );
197 MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real));
198 TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*aMaxFaceNodes);
199 Standard_Integer NbNodes;
200 TCollection_AsciiString aStr;
201 MeshVS_EntityType aType;
202 TColStd_PackedMapOfInteger aCustomElements;
204 Standard_Real X, Y, Z;
206 // subtract the hidden elements and ids to exclude (to minimise allocated memory)
207 TColStd_PackedMapOfInteger anIDs;
211 Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
212 if ( !aHiddenElems.IsNull() )
213 anIDs.Subtract( aHiddenElems->Map() );
215 anIDs.Subtract( IDsToExclude );
217 NCollection_Sequence<Graphic3d_Vec3> aPnts;
218 for (TColStd_MapIteratorOfPackedMapOfInteger it (anIDs); it.More(); it.Next())
220 Standard_Integer aKey = it.Key();
221 if( GetText ( IsElement, aKey, aStr ) )
223 if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
225 if( aType == MeshVS_ET_Node )
231 else if( aType == MeshVS_ET_Link ||
232 aType == MeshVS_ET_Face ||
233 aType == MeshVS_ET_Volume )
235 if( IsElement && IsExcludingOn() )
236 IDsToExclude.Add( aKey );
238 for ( Standard_Integer i=1; i<=NbNodes; i++ )
240 X += aCoords (3*i-2);
241 Y += aCoords (3*i-1);
244 X /= Standard_Real ( NbNodes );
245 Y /= Standard_Real ( NbNodes );
246 Z /= Standard_Real ( NbNodes );
250 aCustomElements.Add( aKey );
254 aPnts.Append (Graphic3d_Vec3 ((float )X, (float )Y, (float )Z));
256 Handle(Graphic3d_Text) aText = new Graphic3d_Text ((Standard_ShortReal)aHeight);
257 aText->SetText (aStr);
258 aText->SetPosition (gp_Pnt (X, Y, Z));
259 aTextGroup->AddText(aText);
264 if (!aPnts.IsEmpty())
266 Handle(Graphic3d_Group) aMarkerGroup = Prs->NewGroup();
267 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (aPnts.Size());
268 for (NCollection_Sequence<Graphic3d_Vec3>::Iterator aPntIter (aPnts); aPntIter.More(); aPntIter.Next())
270 const Graphic3d_Vec3& aPnt = aPntIter.Value();
271 anArrayOfPoints->AddVertex (aPnt.x(), aPnt.y(), aPnt.z());
273 Handle (Graphic3d_AspectMarker3d) anAspectMarker3d = new Graphic3d_AspectMarker3d (Aspect_TOM_POINT, Quantity_NOC_GRAY, 1.0);
274 aMarkerGroup->SetGroupPrimitivesAspect (anAspectMarker3d);
275 aMarkerGroup->AddPrimitiveArray (anArrayOfPoints);
278 if (!aCustomElements.IsEmpty())
279 CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode );