0029559: Samples - wrong copyright statement in FuncDemo
[occt.git] / tools / VInspector / VInspector_Tools.cxx
CommitLineData
14bbbdcb 1// Created on: 2017-06-16
2// Created by: Natalia ERMOLAEVA
3// Copyright (c) 2017 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
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.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
0cb512c0 16#include <inspector/VInspector_Tools.hxx>
14bbbdcb 17
18#include <AIS_ListIteratorOfListOfInteractive.hxx>
19#include <AIS_ListOfInteractive.hxx>
14bbbdcb 20#include <AIS_Selection.hxx>
21#include <AIS_Shape.hxx>
22#include <AIS_Trihedron.hxx>
23#include <BRep_Builder.hxx>
24#include <BRepTools.hxx>
25#include <gp_Trsf.hxx>
26#include <SelectMgr_StateOfSelection.hxx>
27#include <SelectMgr_TypeOfUpdate.hxx>
28#include <SelectMgr_TypeOfBVHUpdate.hxx>
29#include <Standard_Version.hxx>
30#include <StdSelect_BRepOwner.hxx>
31
32#include <QStringList>
33
34#include <sstream>
35
36// =======================================================================
37// function : GetShapeTypeInfo
38// purpose :
39// =======================================================================
40TCollection_AsciiString VInspector_Tools::GetShapeTypeInfo (const TopAbs_ShapeEnum& theType)
41{
42 Standard_SStream aSStream;
43 TopAbs::Print (theType, aSStream);
44 return aSStream.str().c_str();
45}
46
47// =======================================================================
48// function : GetPointerInfo
49// purpose :
50// =======================================================================
51TCollection_AsciiString VInspector_Tools::GetPointerInfo (const Handle(Standard_Transient)& thePointer, const bool isShortInfo)
52{
53 std::ostringstream aPtrStr;
54 aPtrStr << thePointer.operator->();
55 if (!isShortInfo)
56 return aPtrStr.str().c_str();
57
58 TCollection_AsciiString anInfoPtr (aPtrStr.str().c_str());
59 for (int aSymbolId = 1; aSymbolId < anInfoPtr.Length(); aSymbolId++)
60 {
61 if (anInfoPtr.Value(aSymbolId) != '0')
62 {
63 anInfoPtr = anInfoPtr.SubString(aSymbolId, anInfoPtr.Length());
64 anInfoPtr.Prepend("0x");
65 return anInfoPtr;
66 }
67 }
68 return aPtrStr.str().c_str();
69}
70
71// =======================================================================
72// function : SelectedOwners
73// purpose :
74// =======================================================================
75int VInspector_Tools::SelectedOwners (const Handle(AIS_InteractiveContext)& theContext,
76 const Handle(AIS_InteractiveObject)& theObject,
77 const bool theShapeInfoOnly)
78{
79 QStringList anObjects;
80 if (theContext.IsNull())
81 return 0;
82
83 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
84 for (theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected())
85 {
86 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
87 if (anOwner.IsNull()) // TODO: check why it is possible
88 continue;
89
90 if (!theObject.IsNull())
91 {
92 Handle(AIS_InteractiveObject) anOwnerPresentation =
93 Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
94 if (anOwnerPresentation != theObject)
95 continue;
96 }
97 Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast (anOwner);
98 if (theShapeInfoOnly && BROwnr.IsNull())
99 continue;
100
101 Standard_Transient* anOwnerPtr = anOwner.operator->();
102 if (aSelectedIds.contains ((size_t)anOwnerPtr))
103 continue;
104 aSelectedIds.append ((size_t)anOwnerPtr);
105
106 anObjects.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
107 }
108 return anObjects.size();
109}
110
111// =======================================================================
112// function : IsOwnerSelected
113// purpose :
114// =======================================================================
115bool VInspector_Tools::IsOwnerSelected (const Handle(AIS_InteractiveContext)& theContext,
116 const Handle(SelectBasics_EntityOwner)& theOwner)
117{
118 bool anIsSelected = false;
119 Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (theOwner);
120 for (theContext->InitSelected(); theContext->MoreSelected() && !anIsSelected; theContext->NextSelected())
121 anIsSelected = theContext->SelectedOwner() == anOwner;
122 return anIsSelected;
123}
124
125// =======================================================================
126// function : ContextOwners
127// purpose :
128// =======================================================================
129NCollection_List<Handle(SelectBasics_EntityOwner)> VInspector_Tools::ContextOwners (
130 const Handle(AIS_InteractiveContext)& theContext)
131{
132 NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
133 if (theContext.IsNull())
134 return aResultOwners;
135
136 AIS_ListOfInteractive aListOfIO;
137 theContext->DisplayedObjects (aListOfIO);
138 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
139 for (AIS_ListIteratorOfListOfInteractive aIt(aListOfIO); aIt.More(); aIt.Next())
140 {
141 Handle(AIS_InteractiveObject) anIO = aIt.Value();
142 if (anIO.IsNull())
143 continue;
b5cce1ab 144 for (SelectMgr_SequenceOfSelection::Iterator aSelIter (anIO->Selections()); aSelIter.More(); aSelIter.Next())
14bbbdcb 145 {
b5cce1ab 146 Handle(SelectMgr_Selection) aSelection = aSelIter.Value();
14bbbdcb 147 if (aSelection.IsNull())
148 continue;
b5cce1ab 149 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (aSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
14bbbdcb 150 {
b5cce1ab 151 Handle(SelectMgr_SensitiveEntity) anEntity = aSelEntIter.Value();
14bbbdcb 152 if (anEntity.IsNull())
153 continue;
154 const Handle(SelectBasics_SensitiveEntity)& aBase = anEntity->BaseSensitive();
155 Handle(SelectBasics_EntityOwner) anOwner = aBase->OwnerId();
156 Standard_Transient* anOwnerPtr = anOwner.operator->();
157 if (aSelectedIds.contains ((size_t)anOwnerPtr))
158 continue;
159 aSelectedIds.append ((size_t)anOwnerPtr);
160 aResultOwners.Append (anOwner);
161 }
162 }
163 }
164 return aResultOwners;
165}
166
167// =======================================================================
168// function : ActiveOwners
169// purpose :
170// =======================================================================
171NCollection_List<Handle(SelectBasics_EntityOwner)> VInspector_Tools::ActiveOwners (
172 const Handle(AIS_InteractiveContext)& theContext,
173 NCollection_List<Handle(SelectBasics_EntityOwner)>& theEmptySelectableOwners)
174{
175 NCollection_List<Handle(SelectBasics_EntityOwner)> aResultOwners;
176
177 // only local context is processed: TODO for global context
178 Handle(AIS_InteractiveContext) aContext = theContext;
179 if (aContext.IsNull())
180 return aResultOwners;
181 NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
182 // OCCT BUG:1 - equal pointer owners are appears in the list
183#if OCC_VERSION_HEX > 0x060901
184 aContext->MainSelector()->ActiveOwners (anActiveOwners);
185#else
186 anActiveOwners = aContext->MainSelector()->ActiveOwners();
187#endif
188 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
189 Handle(SelectMgr_EntityOwner) anOwner;
190 for (NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
191 anOwnersIt.More(); anOwnersIt.Next())
192 {
193 anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
194 if (anOwner.IsNull())
195 continue;
196
197 Standard_Transient* anOwnerPtr = anOwner.operator->();
198 if (aSelectedIds.contains ((size_t)anOwnerPtr))
199 continue;
200 aSelectedIds.append ((size_t)anOwnerPtr);
201
202 aResultOwners.Append (anOwner);
203 Handle(SelectMgr_SelectableObject) aSelectable = anOwner->Selectable();
204 if (aSelectable.IsNull() ||
205 !theContext->IsDisplayed(Handle(AIS_InteractiveObject)::DownCast (aSelectable)))
206 theEmptySelectableOwners.Append (anOwner);
207 }
208 return aResultOwners;
209}
210
211// =======================================================================
212// function : AddOrRemoveSelectedShapes
213// purpose :
214// =======================================================================
215void VInspector_Tools::AddOrRemoveSelectedShapes (const Handle(AIS_InteractiveContext)& theContext,
216 const NCollection_List<Handle(SelectBasics_EntityOwner)>& theOwners)
217{
218 // TODO: the next two rows are to be removed later
219 theContext->UnhilightSelected(false);
220 theContext->ClearSelected(false);
221
222 theContext->UnhilightSelected(Standard_False);
223
14bbbdcb 224 for (NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt(theOwners);
225 anOwnersIt.More(); anOwnersIt.Next())
226 {
227 Handle(SelectMgr_EntityOwner) anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
14bbbdcb 228 theContext->AddOrRemoveSelected (anOwner, Standard_False);
14bbbdcb 229 }
230 theContext->UpdateCurrentViewer();
231}
232
233// =======================================================================
234// function : AddOrRemovePresentations
235// purpose :
236// =======================================================================
237void VInspector_Tools::AddOrRemovePresentations (const Handle(AIS_InteractiveContext)& theContext,
238 const NCollection_List<Handle(AIS_InteractiveObject)>& thePresentations)
239{
240 // TODO: the next two rows are to be removed later
241 theContext->UnhilightSelected(false);
242 theContext->ClearSelected(false);
243
244 for (NCollection_List<Handle(AIS_InteractiveObject)>::Iterator anIOIt (thePresentations); anIOIt.More(); anIOIt.Next())
245 theContext->AddOrRemoveSelected (anIOIt.Value(), false);
246
247 theContext->UpdateCurrentViewer();
248}
249
250// =======================================================================
251// function : GetInfo
252// purpose :
253// =======================================================================
254QList<QVariant> VInspector_Tools::GetInfo (Handle(AIS_InteractiveObject)& theObject)
255{
256 QList<QVariant> anInfo;
257 anInfo.append (theObject->DynamicType()->Name());
258 anInfo.append (VInspector_Tools::GetPointerInfo (theObject, true).ToCString());
259
260 Handle(AIS_Shape) aShapeIO = Handle(AIS_Shape)::DownCast (theObject);
261 if (aShapeIO.IsNull())
262 return anInfo;
263
264 const TopoDS_Shape& aShape = aShapeIO->Shape();
265 if (!aShape.IsNull())
266 anInfo.append (VInspector_Tools::GetShapeTypeInfo (aShape.ShapeType()).ToCString());
267
268 return anInfo;
269}
270
271// =======================================================================
272// function : GetHighlightInfo
273// purpose :
274// =======================================================================
275QList<QVariant> VInspector_Tools::GetHighlightInfo (const Handle(AIS_InteractiveContext)& theContext)
276{
277 QList<QVariant> aValues;
278 if (theContext.IsNull())
279 return aValues;
280
281 QStringList aSelectedNames;
282 QStringList aSelectedPointers;
283 QStringList aSelectedTypes;
284 QStringList aSelectedOwners;
285 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
286 for (theContext->InitDetected(); theContext->MoreDetected(); theContext->NextDetected())
287 {
288 Handle(SelectMgr_EntityOwner) anOwner = theContext->DetectedOwner();
289 if (anOwner.IsNull())
290 continue;
291 Standard_Transient* anOwnerPtr = anOwner.operator->();
292 if (aSelectedIds.contains ((size_t)anOwnerPtr))
293 continue;
294 aSelectedIds.append ((size_t)anOwnerPtr);
295 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
296 if (anIO.IsNull())
297 continue;
298 QList<QVariant> anIOInfo = VInspector_Tools::GetInfo (anIO);
299 if (anIOInfo.size() == 3)
300 {
301 aSelectedNames.append (anIOInfo[0].toString());
302 aSelectedPointers.append (anIOInfo[1].toString());
303 aSelectedTypes.append (anIOInfo[2].toString());
304 }
305 aSelectedOwners.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
306 }
307 aValues.append (aSelectedNames.join (", "));
308 aValues.append (aSelectedPointers.join (", "));
309 aValues.append (aSelectedTypes.join (", "));
310 aValues.append (aSelectedOwners.join (", "));
311
312 return aValues;
313}
314
315// =======================================================================
316// function : GetSelectedInfo
317// purpose :
318// =======================================================================
319QList<QVariant> VInspector_Tools::GetSelectedInfo (const Handle(AIS_InteractiveContext)& theContext)
320{
321 QList<QVariant> aValues;
322 if (theContext.IsNull())
323 return aValues;
324
325 QStringList aSelectedNames;
326 QStringList aSelectedPointers;
327 QStringList aSelectedTypes;
328 QStringList aSelectedOwners;
329 QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
330 for (theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected())
331 {
332 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
333 if (anOwner.IsNull())
334 continue;
335 Standard_Transient* anOwnerPtr = anOwner.operator->();
336 if (aSelectedIds.contains ((size_t)anOwnerPtr))
337 continue;
338 aSelectedIds.append ((size_t)anOwnerPtr);
339 Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
340 if (anIO.IsNull())
341 continue;
342
343 QList<QVariant> anIOInfo = VInspector_Tools::GetInfo (anIO);
344 if (anIOInfo.size() == 3)
345 {
346 aSelectedNames.append (anIOInfo[0].toString());
347 aSelectedPointers.append (anIOInfo[1].toString());
348 aSelectedTypes.append (anIOInfo[2].toString());
349 }
350 aSelectedOwners.append (VInspector_Tools::GetPointerInfo (anOwnerPtr, true).ToCString());
351 }
352 aValues.append (aSelectedNames.join (", "));
353 aValues.append (aSelectedPointers.join (", "));
354 aValues.append (aSelectedTypes.join (", "));
355 aValues.append (aSelectedOwners.join (", "));
356 return aValues;
357}
358
359// =======================================================================
360// function : GetSelectedInfoPointers
361// purpose :
362// =======================================================================
363QString VInspector_Tools::GetSelectedInfoPointers (const Handle(AIS_InteractiveContext)& theContext)
364{
365 QList<QVariant> aSelectedInfo = VInspector_Tools::GetSelectedInfo (theContext);
366 return aSelectedInfo.size() > 2 ? aSelectedInfo[1].toString() : QString();
367}
368
369// =======================================================================
370// function : ToName
371// purpose :
372// =======================================================================
373TCollection_AsciiString VInspector_Tools::ToName (const VInspector_SelectionType theType, const int theValue)
374{
375 switch (theType)
376 {
377 case VInspector_SelectionType_TypeOfUpdate:
378 {
379 switch (theValue)
380 {
381 case SelectMgr_TOU_Full: return "Full";
382 case SelectMgr_TOU_Partial: return "Partial";
383 case SelectMgr_TOU_None: return "None";
384 default: break;
385 }
386 }
387 break;
388 case VInspector_SelectionType_StateOfSelection:
389 {
390 switch (theValue)
391 {
392 case SelectMgr_SOS_Activated: return "Activated";
393 case SelectMgr_SOS_Deactivated: return "Deactivated";
394 case SelectMgr_SOS_Sleeping: return "Sleeping";
395 case SelectMgr_SOS_Any: return "Any";
396 case SelectMgr_SOS_Unknown: return "Unknown";
397 default: break;
398 }
399 }
400 break;
401 case VInspector_SelectionType_TypeOfBVHUpdate:
402 {
403 switch (theValue)
404 {
405 case SelectMgr_TBU_Add: return "Add";
406 case SelectMgr_TBU_Remove: return "Remove";
407 case SelectMgr_TBU_Renew: return "Renew";
408 case SelectMgr_TBU_Invalidate: return "Invalidate";
409 case SelectMgr_TBU_None: return "None";
410 default: break;
411 }
412 }
413 default: break;
414 }
415 return "";
416}
417
418// =======================================================================
419// function : SelectionModeToName
420// purpose :
421// =======================================================================
422TCollection_AsciiString VInspector_Tools::SelectionModeToName (int theMode, const Handle(AIS_InteractiveObject)& thePresentation)
423{
424 // types are obtained by comment of SelectMgr_Selection class;
425 Handle(AIS_Shape) aShapePresentation = Handle(AIS_Shape)::DownCast (thePresentation);
426 if (!aShapePresentation.IsNull())
427 return VInspector_Tools::GetShapeTypeInfo (AIS_Shape::SelectionType (theMode)).ToCString();
428 else
429 {
430 Handle(AIS_Trihedron) aTrihedronPresentation = Handle(AIS_Trihedron)::DownCast (thePresentation);
431 if (!aTrihedronPresentation.IsNull())
432 {
433 switch (theMode)
434 {
435 case 0: return "Trihedron";
436 case 1: return "Trihedron Origin";
437 case 2: return "Trihedron Axes";
438 case 3: return "Trihedron Planes";
439 default: break;
440 }
441 }
442 }
443 return TCollection_AsciiString (theMode);
444}
445
446// =======================================================================
447// function : OrientationToName
448// purpose :
449// =======================================================================
450TCollection_AsciiString VInspector_Tools::OrientationToName (const TopAbs_Orientation& theOrientation)
451{
452 Standard_SStream aSStream;
453 TopAbs::Print(theOrientation, aSStream);
454 return aSStream.str().c_str();
455}
456
457// =======================================================================
458// function : LocationToName
459// purpose :
460// =======================================================================
461TCollection_AsciiString VInspector_Tools::LocationToName (const TopLoc_Location& theLocation)
462{
463 gp_Trsf aTrsf = theLocation.Transformation();
464
465 TCollection_AsciiString aValues;
466 for (int aRowId = 1; aRowId <= 3; aRowId++)
467 {
468 for (int aColId = 1; aColId <= 4; aColId++) {
469 aValues += TCollection_AsciiString (aTrsf.Value(aRowId, aColId));
470 if (aColId != 4)
471 aValues += ",";
472 }
473 if (aRowId != 3)
474 aValues += " ";
475 }
476 return aValues;
477}
478
479// =======================================================================
480// function : ReadShape
481// purpose :
482// =======================================================================
483TopoDS_Shape VInspector_Tools::ReadShape (const TCollection_AsciiString& theFileName)
484{
485 TopoDS_Shape aShape;
486
487 BRep_Builder aBuilder;
488 BRepTools::Read (aShape, theFileName.ToCString(), aBuilder);
489
490 return aShape;
491}