0024166: Unable to create file with "Save" menu of voxeldemo Qt sample
[occt.git] / src / MeshVS / MeshVS_TextPrsBuilder.cxx
1 // Created on: 2003-09-22
2 // Created by: Alexander SOLOVYOV
3 // Copyright (c) 2003-2012 OPEN CASCADE SAS
4 //
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.
9 //
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.
12 //
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.
19
20
21 #include <MeshVS_TextPrsBuilder.ixx>
22
23 #include <MeshVS_DisplayModeFlags.hxx>
24
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>
37
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>
43
44 #include <Font_NameOfFont.hxx>
45
46 //================================================================
47 // Function : Constructor MeshVS_TextPrsBuilder
48 // Purpose  :
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 )
58 {
59   Handle ( MeshVS_Drawer ) aDrawer = GetDrawer();
60   if ( !aDrawer.IsNull() )
61   {
62     aDrawer->SetDouble ( MeshVS_DA_TextHeight, Height );
63     aDrawer->SetColor  ( MeshVS_DA_TextColor, Color );
64   }
65 }
66
67 //================================================================
68 // Function : GetTexts
69 // Purpose  :
70 //================================================================
71 const MeshVS_DataMapOfIntegerAsciiString& MeshVS_TextPrsBuilder::GetTexts ( const Standard_Boolean IsElements ) const
72 {
73   if ( IsElements )
74     return myElemTextMap;
75   else
76     return myNodeTextMap;
77 }
78
79 //================================================================
80 // Function : SetTexts
81 // Purpose  :
82 //================================================================
83 void MeshVS_TextPrsBuilder::SetTexts ( const Standard_Boolean IsElements,
84                                        const MeshVS_DataMapOfIntegerAsciiString& Map )
85 {
86   if ( IsElements )
87     myElemTextMap = Map;
88   else
89     myNodeTextMap = Map;
90 }
91
92 //================================================================
93 // Function : HasTexts
94 // Purpose  :
95 //================================================================
96 Standard_Boolean MeshVS_TextPrsBuilder::HasTexts ( const Standard_Boolean IsElement ) const
97 {
98   Standard_Boolean aRes = (myNodeTextMap.Extent()>0);
99   if ( IsElement )
100     aRes = (myElemTextMap.Extent()>0);
101   return aRes;
102 }
103
104 //================================================================
105 // Function : GetText
106 // Purpose  :
107 //================================================================
108 Standard_Boolean MeshVS_TextPrsBuilder::GetText ( const Standard_Boolean IsElement,
109                                                   const Standard_Integer theID,
110                                                   TCollection_AsciiString& theStr ) const
111 {
112   const MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
113   if ( IsElement )
114     aMap = &myElemTextMap;
115
116   Standard_Boolean aRes = aMap->IsBound ( theID );
117   if ( aRes )
118     theStr = aMap->Find ( theID );
119
120   return aRes;
121 }
122
123 //================================================================
124 // Function : SetText
125 // Purpose  :
126 //================================================================
127 void MeshVS_TextPrsBuilder::SetText ( const Standard_Boolean IsElement,
128                                       const Standard_Integer ID,
129                                       const TCollection_AsciiString& Text )
130 {
131   MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
132   if ( IsElement )
133     aMap = &myElemTextMap;
134
135   Standard_Boolean aRes = aMap->IsBound ( ID );
136   if ( aRes )
137     aMap->ChangeFind ( ID ) = Text;
138   else
139     aMap->Bind ( ID, Text );
140 }
141
142 //================================================================
143 // Function : Build
144 // Purpose  :
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
151 {
152   Handle (MeshVS_DataSource) aSource = GetDataSource();
153   Handle (MeshVS_Drawer) aDrawer = GetDrawer();
154   if ( aSource.IsNull() || aDrawer.IsNull() || !HasTexts( IsElement ) ||
155     ( theDisplayMode & GetFlags() ) == 0 )
156     return;
157
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 )    )
163     return;
164
165   Prs3d_Root::NewGroup ( Prs );
166   Handle (Graphic3d_Group) aTextGroup = Prs3d_Root::CurrentGroup ( Prs );
167
168   Quantity_Color           AColor           = Quantity_NOC_YELLOW;
169 #ifdef WNT  
170   Standard_CString         AFont            = "Courier New";
171 #else
172   Standard_CString         AFont            = "Courier";
173 #endif
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; 
184   
185
186   aDrawer->GetColor  ( MeshVS_DA_TextColor, AColor );
187   aDrawer->GetDouble ( MeshVS_DA_TextExpansionFactor, AExpansionFactor );
188   aDrawer->GetDouble ( MeshVS_DA_TextSpace, ASpace );
189
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;         
198
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 );
206
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;
213
214   Standard_Real X, Y, Z;
215
216   // subtract the hidden elements and ids to exclude (to minimise allocated memory)
217   TColStd_PackedMapOfInteger anIDs;
218   anIDs.Assign( IDs );
219   if ( IsElement )
220   {
221     Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
222     if ( !aHiddenElems.IsNull() )
223       anIDs.Subtract( aHiddenElems->Map() );
224   }
225   anIDs.Subtract( IDsToExclude );
226
227   TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
228   for( ; it.More(); it.Next() )
229   {
230     Standard_Integer aKey = it.Key();
231     if( GetText ( IsElement, aKey, aStr ) )
232     {
233       if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
234       {
235         if( aType == MeshVS_ET_Node )
236         {
237           X = aCoords(1);
238           Y = aCoords(2);
239           Z = aCoords(3);
240         }
241         else if( aType == MeshVS_ET_Link || 
242           aType == MeshVS_ET_Face || 
243           aType == MeshVS_ET_Volume )
244         {
245           if( IsElement && IsExcludingOn() )
246             IDsToExclude.Add( aKey );
247           X = Y = Z = 0;
248           for ( Standard_Integer i=1; i<=NbNodes; i++ )
249           {
250             X += aCoords (3*i-2);
251             Y += aCoords (3*i-1);
252             Z += aCoords (3*i);
253           }
254           X /= Standard_Real ( NbNodes );
255           Y /= Standard_Real ( NbNodes );
256           Z /= Standard_Real ( NbNodes );
257         }
258         else 
259         {
260           aCustomElements.Add( aKey );
261           continue;
262         }
263
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);
269       }
270     }
271   }
272
273   if (!aCustomElements.IsEmpty())
274     CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode );
275 }