b311480e |
1 | // Created on: 2003-09-22 |
2 | // Created by: Alexander SOLOVYOV |
973c2be1 |
3 | // Copyright (c) 2003-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
7fd59977 |
16 | |
42cf5bc1 |
17 | #include <Font_NameOfFont.hxx> |
a577aaab |
18 | #include <Graphic3d_ArrayOfPoints.hxx> |
7fd59977 |
19 | #include <Graphic3d_AspectMarker3d.hxx> |
42cf5bc1 |
20 | #include <Graphic3d_AspectText3d.hxx> |
21 | #include <Graphic3d_Group.hxx> |
22 | #include <Graphic3d_Vertex.hxx> |
23 | #include <MeshVS_Buffer.hxx> |
7fd59977 |
24 | #include <MeshVS_DataSource.hxx> |
42cf5bc1 |
25 | #include <MeshVS_DisplayModeFlags.hxx> |
7fd59977 |
26 | #include <MeshVS_Drawer.hxx> |
7fd59977 |
27 | #include <MeshVS_DrawerAttribute.hxx> |
42cf5bc1 |
28 | #include <MeshVS_Mesh.hxx> |
29 | #include <MeshVS_TextPrsBuilder.hxx> |
30 | #include <Prs3d_Presentation.hxx> |
31 | #include <Prs3d_Root.hxx> |
32 | #include <Prs3d_TextAspect.hxx> |
33 | #include <Quantity_Color.hxx> |
34 | #include <Standard_Type.hxx> |
35 | #include <TCollection_AsciiString.hxx> |
36 | #include <TColStd_Array1OfReal.hxx> |
37 | #include <TColStd_HPackedMapOfInteger.hxx> |
38 | #include <TColStd_ListIteratorOfListOfReal.hxx> |
39 | #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx> |
7fd59977 |
40 | |
41 | //================================================================ |
42 | // Function : Constructor MeshVS_TextPrsBuilder |
43 | // Purpose : |
44 | //================================================================ |
45 | MeshVS_TextPrsBuilder::MeshVS_TextPrsBuilder ( const Handle(MeshVS_Mesh)& Parent, |
46 | const Standard_Real Height, |
47 | const Quantity_Color& Color, |
48 | const MeshVS_DisplayModeFlags& Flags, |
49 | const Handle (MeshVS_DataSource)& DS, |
50 | const Standard_Integer Id, |
51 | const MeshVS_BuilderPriority& Priority ) |
52 | : MeshVS_PrsBuilder ( Parent, Flags, DS, Id, Priority ) |
53 | { |
54 | Handle ( MeshVS_Drawer ) aDrawer = GetDrawer(); |
55 | if ( !aDrawer.IsNull() ) |
56 | { |
57 | aDrawer->SetDouble ( MeshVS_DA_TextHeight, Height ); |
58 | aDrawer->SetColor ( MeshVS_DA_TextColor, Color ); |
59 | } |
60 | } |
61 | |
62 | //================================================================ |
63 | // Function : GetTexts |
64 | // Purpose : |
65 | //================================================================ |
66 | const MeshVS_DataMapOfIntegerAsciiString& MeshVS_TextPrsBuilder::GetTexts ( const Standard_Boolean IsElements ) const |
67 | { |
68 | if ( IsElements ) |
69 | return myElemTextMap; |
70 | else |
71 | return myNodeTextMap; |
72 | } |
73 | |
74 | //================================================================ |
75 | // Function : SetTexts |
76 | // Purpose : |
77 | //================================================================ |
78 | void MeshVS_TextPrsBuilder::SetTexts ( const Standard_Boolean IsElements, |
79 | const MeshVS_DataMapOfIntegerAsciiString& Map ) |
80 | { |
81 | if ( IsElements ) |
82 | myElemTextMap = Map; |
83 | else |
84 | myNodeTextMap = Map; |
85 | } |
86 | |
87 | //================================================================ |
88 | // Function : HasTexts |
89 | // Purpose : |
90 | //================================================================ |
91 | Standard_Boolean MeshVS_TextPrsBuilder::HasTexts ( const Standard_Boolean IsElement ) const |
92 | { |
93 | Standard_Boolean aRes = (myNodeTextMap.Extent()>0); |
94 | if ( IsElement ) |
95 | aRes = (myElemTextMap.Extent()>0); |
96 | return aRes; |
97 | } |
98 | |
99 | //================================================================ |
100 | // Function : GetText |
101 | // Purpose : |
102 | //================================================================ |
103 | Standard_Boolean MeshVS_TextPrsBuilder::GetText ( const Standard_Boolean IsElement, |
104 | const Standard_Integer theID, |
105 | TCollection_AsciiString& theStr ) const |
106 | { |
107 | const MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap; |
108 | if ( IsElement ) |
109 | aMap = &myElemTextMap; |
110 | |
111 | Standard_Boolean aRes = aMap->IsBound ( theID ); |
112 | if ( aRes ) |
113 | theStr = aMap->Find ( theID ); |
114 | |
115 | return aRes; |
116 | } |
117 | |
118 | //================================================================ |
119 | // Function : SetText |
120 | // Purpose : |
121 | //================================================================ |
122 | void MeshVS_TextPrsBuilder::SetText ( const Standard_Boolean IsElement, |
123 | const Standard_Integer ID, |
124 | const TCollection_AsciiString& Text ) |
125 | { |
126 | MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap; |
127 | if ( IsElement ) |
128 | aMap = &myElemTextMap; |
129 | |
130 | Standard_Boolean aRes = aMap->IsBound ( ID ); |
131 | if ( aRes ) |
132 | aMap->ChangeFind ( ID ) = Text; |
133 | else |
134 | aMap->Bind ( ID, Text ); |
135 | } |
136 | |
137 | //================================================================ |
138 | // Function : Build |
139 | // Purpose : |
140 | //================================================================ |
141 | void MeshVS_TextPrsBuilder::Build ( const Handle(Prs3d_Presentation)& Prs, |
142 | const TColStd_PackedMapOfInteger& IDs, |
143 | TColStd_PackedMapOfInteger& IDsToExclude, |
144 | const Standard_Boolean IsElement, |
145 | const Standard_Integer theDisplayMode ) const |
146 | { |
147 | Handle (MeshVS_DataSource) aSource = GetDataSource(); |
148 | Handle (MeshVS_Drawer) aDrawer = GetDrawer(); |
149 | if ( aSource.IsNull() || aDrawer.IsNull() || !HasTexts( IsElement ) || |
150 | ( theDisplayMode & GetFlags() ) == 0 ) |
151 | return; |
152 | |
153 | Standard_Integer aMaxFaceNodes; |
154 | Standard_Real aHeight; |
155 | if ( !aDrawer->GetInteger ( MeshVS_DA_MaxFaceNodes, aMaxFaceNodes ) || |
156 | aMaxFaceNodes <= 0 || |
157 | !aDrawer->GetDouble ( MeshVS_DA_TextHeight, aHeight ) ) |
158 | return; |
159 | |
160 | Prs3d_Root::NewGroup ( Prs ); |
161 | Handle (Graphic3d_Group) aTextGroup = Prs3d_Root::CurrentGroup ( Prs ); |
162 | |
163 | Quantity_Color AColor = Quantity_NOC_YELLOW; |
164 | #ifdef WNT |
165 | Standard_CString AFont = "Courier New"; |
166 | #else |
167 | Standard_CString AFont = "Courier"; |
168 | #endif |
169 | Standard_Real AExpansionFactor = 1.0; |
170 | Standard_Real ASpace = 0.0; |
171 | Aspect_TypeOfStyleText ATextStyle = Aspect_TOST_ANNOTATION; |
172 | Standard_Integer AStyleInt; |
173 | Aspect_TypeOfDisplayText ADisplayType = Aspect_TODT_NORMAL; |
174 | TCollection_AsciiString AFontString; |
175 | Standard_Integer ADispInt; |
7fd59977 |
176 | // Bold font is used by default for better text readability |
eeaaaefb |
177 | Font_FontAspect AFontAspectType = Font_FA_Bold; |
7fd59977 |
178 | Standard_Integer AAspect; |
179 | |
180 | |
181 | aDrawer->GetColor ( MeshVS_DA_TextColor, AColor ); |
182 | aDrawer->GetDouble ( MeshVS_DA_TextExpansionFactor, AExpansionFactor ); |
183 | aDrawer->GetDouble ( MeshVS_DA_TextSpace, ASpace ); |
184 | |
185 | if ( aDrawer->GetAsciiString ( MeshVS_DA_TextFont, AFontString ) ) |
186 | AFont = AFontString.ToCString(); |
187 | if ( aDrawer->GetInteger ( MeshVS_DA_TextStyle, AStyleInt ) ) |
188 | ATextStyle = (Aspect_TypeOfStyleText) AStyleInt; |
189 | if ( aDrawer->GetInteger ( MeshVS_DA_TextDisplayType, ADispInt ) ) |
190 | ADisplayType = (Aspect_TypeOfDisplayText) ADispInt; |
191 | if ( aDrawer->GetInteger ( MeshVS_DA_TextFontAspect, AAspect ) ) |
eeaaaefb |
192 | AFontAspectType = (Font_FontAspect)AAspect; |
7fd59977 |
193 | |
194 | Handle (Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d ( AColor, AFont, AExpansionFactor, ASpace, |
195 | ATextStyle, ADisplayType ); |
196 | aTextAspect->SetTextFontAspect( AFontAspectType ); |
197 | Handle (Graphic3d_AspectMarker3d) anAspectMarker3d = |
198 | new Graphic3d_AspectMarker3d( Aspect_TOM_POINT, Quantity_NOC_GRAY, 1. ); |
199 | aTextGroup->SetPrimitivesAspect( aTextAspect ); |
200 | aTextGroup->SetPrimitivesAspect( anAspectMarker3d ); |
201 | |
7fd59977 |
202 | MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real)); |
203 | TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*aMaxFaceNodes); |
204 | Standard_Integer NbNodes; |
205 | TCollection_AsciiString aStr; |
206 | MeshVS_EntityType aType; |
207 | TColStd_PackedMapOfInteger aCustomElements; |
208 | |
209 | Standard_Real X, Y, Z; |
210 | |
211 | // subtract the hidden elements and ids to exclude (to minimise allocated memory) |
212 | TColStd_PackedMapOfInteger anIDs; |
213 | anIDs.Assign( IDs ); |
214 | if ( IsElement ) |
215 | { |
216 | Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems(); |
217 | if ( !aHiddenElems.IsNull() ) |
218 | anIDs.Subtract( aHiddenElems->Map() ); |
219 | } |
220 | anIDs.Subtract( IDsToExclude ); |
221 | |
222 | TColStd_MapIteratorOfPackedMapOfInteger it (anIDs); |
223 | for( ; it.More(); it.Next() ) |
224 | { |
225 | Standard_Integer aKey = it.Key(); |
226 | if( GetText ( IsElement, aKey, aStr ) ) |
227 | { |
228 | if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) ) |
229 | { |
230 | if( aType == MeshVS_ET_Node ) |
231 | { |
232 | X = aCoords(1); |
233 | Y = aCoords(2); |
234 | Z = aCoords(3); |
235 | } |
236 | else if( aType == MeshVS_ET_Link || |
237 | aType == MeshVS_ET_Face || |
238 | aType == MeshVS_ET_Volume ) |
239 | { |
240 | if( IsElement && IsExcludingOn() ) |
241 | IDsToExclude.Add( aKey ); |
242 | X = Y = Z = 0; |
243 | for ( Standard_Integer i=1; i<=NbNodes; i++ ) |
244 | { |
245 | X += aCoords (3*i-2); |
246 | Y += aCoords (3*i-1); |
247 | Z += aCoords (3*i); |
248 | } |
249 | X /= Standard_Real ( NbNodes ); |
250 | Y /= Standard_Real ( NbNodes ); |
251 | Z /= Standard_Real ( NbNodes ); |
252 | } |
253 | else |
254 | { |
255 | aCustomElements.Add( aKey ); |
256 | continue; |
257 | } |
258 | |
a577aaab |
259 | Graphic3d_Vertex aPoint (X, Y, Z); |
260 | Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1); |
261 | anArrayOfPoints->AddVertex (X, Y, Z); |
262 | aTextGroup->AddPrimitiveArray (anArrayOfPoints); |
263 | aTextGroup->Text (aStr.ToCString(), aPoint, aHeight); |
7fd59977 |
264 | } |
265 | } |
266 | } |
267 | |
7fd59977 |
268 | if (!aCustomElements.IsEmpty()) |
269 | CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode ); |
270 | } |