0031652: Visualization - crash on highlighting HLR Computed presentation displayed...
[occt.git] / src / Graphic3d / Graphic3d_Structure.cxx
index 4f03321..3492a3f 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-#include <Graphic3d_Structure.ixx>
-#include <Graphic3d_Structure.pxx>
+#include <Graphic3d_Structure.hxx>
 
+#include <Bnd_Box.hxx>
+#include <gp_Pnt.hxx>
+#include <Graphic3d_DataStructureManager.hxx>
 #include <Graphic3d_GraphicDriver.hxx>
-#include <Graphic3d_MaterialAspect.hxx>
-
-#include <Graphic3d_MapOfStructure.hxx>
+#include <Graphic3d_Group.hxx>
 #include <Graphic3d_MapIteratorOfMapOfStructure.hxx>
-
+#include <Graphic3d_MapOfStructure.hxx>
+#include <Graphic3d_PriorityDefinitionError.hxx>
+#include <Graphic3d_StructureDefinitionError.hxx>
+#include <Graphic3d_StructureManager.hxx>
+#include <Graphic3d_TransformError.hxx>
 #include <Quantity_Color.hxx>
-#include <TColStd_Array2OfReal.hxx>
-#include <Graphic3d_TextureMap.hxx>
 
-#include <Aspect_PolygonOffsetMode.hxx>
+#include "Graphic3d_Structure.pxx"
+
+#include <Standard_Dump.hxx>
 
 #include <stdio.h>
 
-//=============================================================================
-//function : Graphic3d_Structure
-//purpose  :
-//=============================================================================
-Graphic3d_Structure::Graphic3d_Structure (const Handle(Graphic3d_StructureManager)& theManager)
-: myStructureManager      (theManager.operator->()),
-  myFirstStructureManager (theManager.operator->()),
-  myComputeVisual         (Graphic3d_TOS_ALL),
-  myHighlightColor        (Quantity_NOC_WHITE),
-  myHighlightMethod       (Aspect_TOHM_COLOR),
-  myOwner                 (NULL),
-  myVisual                (Graphic3d_TOS_ALL)
-{
-  myCStructure = theManager->GraphicDriver()->Structure (theManager);
-
-  // default aspects
-  Handle(Graphic3d_AspectLine3d)     aAspectLine3d     = new Graphic3d_AspectLine3d();
-  Handle(Graphic3d_AspectText3d)     aAspectText3d     = new Graphic3d_AspectText3d();
-  Handle(Graphic3d_AspectMarker3d)   aAspectMarker3d   = new Graphic3d_AspectMarker3d();
-  Handle(Graphic3d_AspectFillArea3d) aAspectFillArea3d = new Graphic3d_AspectFillArea3d();
-  theManager->PrimitivesAspect (aAspectLine3d, aAspectText3d, aAspectMarker3d, aAspectFillArea3d);
-  aAspectFillArea3d->SetPolygonOffsets (Aspect_POM_Fill, 1.0, 0.0);
-
-  // update the associated CStructure
-  UpdateStructure (aAspectLine3d, aAspectText3d, aAspectMarker3d, aAspectFillArea3d);
-}
+IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_Structure,Standard_Transient)
 
 //=============================================================================
 //function : Graphic3d_Structure
 //purpose  :
 //=============================================================================
 Graphic3d_Structure::Graphic3d_Structure (const Handle(Graphic3d_StructureManager)& theManager,
-                                          const Handle(Graphic3d_Structure)&        thePrs)
-: myStructureManager      (theManager.operator->()),
-  myFirstStructureManager (theManager.operator->()),
-  myComputeVisual         (thePrs->myComputeVisual),
-  myHighlightColor        (thePrs->myHighlightColor),
-  myHighlightMethod       (thePrs->myHighlightMethod),
-  myOwner                 (thePrs->myOwner),
-  myVisual                (thePrs->myVisual)
+                                          const Handle(Graphic3d_Structure)&        theLinkPrs)
+: myStructureManager(theManager.get()),
+  myOwner           (NULL),
+  myVisual          (Graphic3d_TOS_ALL),
+  myComputeVisual   (Graphic3d_TOS_ALL)
 {
-  myCStructure = thePrs->myCStructure->ShadowLink (theManager);
-
-  // default aspects
-  Handle(Graphic3d_AspectLine3d)     aAspectLine3d     = new Graphic3d_AspectLine3d();
-  Handle(Graphic3d_AspectText3d)     aAspectText3d     = new Graphic3d_AspectText3d();
-  Handle(Graphic3d_AspectMarker3d)   aAspectMarker3d   = new Graphic3d_AspectMarker3d();
-  Handle(Graphic3d_AspectFillArea3d) aAspectFillArea3d = new Graphic3d_AspectFillArea3d();
-  theManager->PrimitivesAspect (aAspectLine3d, aAspectText3d, aAspectMarker3d, aAspectFillArea3d);
-  aAspectFillArea3d->SetPolygonOffsets (Aspect_POM_Fill, 1.0, 0.0);
-
-  // update the associated CStructure
-  UpdateStructure (aAspectLine3d, aAspectText3d, aAspectMarker3d, aAspectFillArea3d);
+  if (!theLinkPrs.IsNull())
+  {
+    myOwner = theLinkPrs->myOwner;
+    if (theLinkPrs->myVisual != Graphic3d_TOS_COMPUTED)
+    {
+      myVisual = theLinkPrs->myVisual;
+    }
+    myComputeVisual = theLinkPrs->myComputeVisual;
+    myCStructure = theLinkPrs->myCStructure->ShadowLink (theManager);
+  }
+  else
+  {
+    myCStructure = theManager->GraphicDriver()->CreateStructure (theManager);
+  }
 }
 
 //=============================================================================
-//function : Destroy
+//function : ~Graphic3d_Structure
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::Destroy()
+Graphic3d_Structure::~Graphic3d_Structure()
 {
-  // as myFirstStructureManager can be already destroyed,
+  // as myStructureManager can be already destroyed,
   // avoid attempts to access it
-  myFirstStructureManager = NULL;
+  myStructureManager = NULL;
   Remove();
 }
 
 //=============================================================================
-//function : Clear
+//function : clear
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::Clear (const Standard_Boolean theWithDestruction)
+void Graphic3d_Structure::clear (const Standard_Boolean theWithDestruction)
 {
   if (IsDeleted()) return;
 
-  myCStructure->ContainsFacet = 0;
-
   // clean groups in graphics driver at first
   GraphicClear (theWithDestruction);
 
-  // only then remove group references
-  if (theWithDestruction)
-    myGroups.Clear();
-
+  myCStructure->ContainsFacet = 0;
   myStructureManager->Clear (this, theWithDestruction);
 
-  Update();
+  Update (true);
+}
+
+//=======================================================================
+//function : CalculateBoundBox
+//purpose  : Calculates AABB of a structure.
+//=======================================================================
+void Graphic3d_Structure::CalculateBoundBox()
+{
+  Graphic3d_BndBox3d aBox;
+  addTransformed (aBox, Standard_True);
+  myCStructure->ChangeBoundingBox() = aBox;
 }
 
 //=============================================================================
@@ -132,42 +116,28 @@ void Graphic3d_Structure::Remove()
   // Pass Standard_False to Clear(..) method to avoid updating in
   // structure manager, it isn't necessary, besides of it structure manager
   // could be already destroyed and invalid pointers used in structure;
-  Standard_Integer Length = myGroups.Length();
-  for (Standard_Integer aGrId = 1; aGrId <= Length; ++aGrId)
-    myGroups.ChangeValue (aGrId)->Clear (Standard_False);
-
-  //        Standard_Address APtr = (void *) This ().operator->();
-  Standard_Address APtr = (void *) this;
-  // It is necessary to remove the eventual pointer on the structure
-  // that can be destroyed, in the list of descendants
-  // of ancesters of this structure and in the list of ancesters
-  // of descendants of the same structure.
-
-  Length = myDescendants.Length();
-  for (Standard_Integer i = 1; i <= Length; ++i)
+  for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (myCStructure->Groups()); aGroupIter.More(); aGroupIter.Next())
   {
-    ((Graphic3d_Structure *)(myDescendants.Value (i)))->Remove (APtr, Graphic3d_TOC_ANCESTOR);
+    aGroupIter.ChangeValue()->Clear (Standard_False);
   }
 
-  Length = myAncestors.Length();
-  for (Standard_Integer i = 1; i <= Length; ++i)
+  // It is necessary to remove the eventual pointer on the structure that can be destroyed, in the list of descendants
+  // of ancestors of this structure and in the list of ancestors of descendants of the same structure.
+  for (Standard_Integer aStructIdx = 1, aNbDesc = myDescendants.Size(); aStructIdx <= aNbDesc; ++aStructIdx)
   {
-    ((Graphic3d_Structure *)(myAncestors.Value (i)))->Remove (APtr, Graphic3d_TOC_DESCENDANT);
+    myDescendants.FindKey (aStructIdx)->Remove (this, Graphic3d_TOC_ANCESTOR);
   }
 
-  myCStructure->ContainsFacet = 0;
+  for (Standard_Integer aStructIdx = 1, aNbAnces = myAncestors.Size(); aStructIdx <= aNbAnces; ++aStructIdx)
+  {
+    myAncestors.FindKey (aStructIdx)->Remove (this, Graphic3d_TOC_DESCENDANT);
+  }
 
   // Destruction of me in the graphic library
   const Standard_Integer aStructId = myCStructure->Id;
+  myCStructure->GraphicDriver()->RemoveIdentification(aStructId);
   myCStructure->GraphicDriver()->RemoveStructure (myCStructure);
   myCStructure.Nullify();
-
-  // Liberation of the identification if the destroyed structure
-  // in the first manager that performs creation of the structure.
-  if (myFirstStructureManager != NULL)
-  {
-    myFirstStructureManager->Remove (aStructId);
-  }
 }
 
 //=============================================================================
@@ -184,26 +154,11 @@ void Graphic3d_Structure::Display()
     myStructureManager->Display (this);
   }
 
-  myCStructure->visible = 1;
-}
-
-//=============================================================================
-//function : Display
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::Display (const Standard_Integer thePriority)
-{
-  if (IsDeleted()) return;
-
-  SetDisplayPriority (thePriority);
-
-  if (!myCStructure->stick)
+  if (myCStructure->visible != 1)
   {
-    myCStructure->stick = 1;
-    myStructureManager->Display (this);
+    myCStructure->visible = 1;
+    myCStructure->OnVisibilityChanged();
   }
-
-  myCStructure->visible = 1;
 }
 
 //=============================================================================
@@ -254,19 +209,10 @@ void Graphic3d_Structure::ResetDisplayPriority()
 }
 
 //=============================================================================
-//function : DisplayPriority
+//function : erase
 //purpose  :
 //=============================================================================
-Standard_Integer Graphic3d_Structure::DisplayPriority() const
-{
-  return myCStructure->Priority;
-}
-
-//=============================================================================
-//function : Erase
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::Erase()
+void Graphic3d_Structure::erase()
 {
   if (IsDeleted())
   {
@@ -284,75 +230,29 @@ void Graphic3d_Structure::Erase()
 //function : Highlight
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::Highlight (const Aspect_TypeOfHighlightMethod theMethod)
+void Graphic3d_Structure::Highlight (const Handle(Graphic3d_PresentationAttributes)& theStyle,
+                                     const Standard_Boolean theToUpdateMgr)
 {
   if (IsDeleted())
   {
     return;
   }
 
-  // Highlight on already Highlighted structure.
-  if (myCStructure->highlight)
-  {
-    Aspect_TypeOfUpdate anUpdateMode = myStructureManager->UpdateMode();
-    if (anUpdateMode == Aspect_TOU_WAIT)
-    {
-      UnHighlight();
-    }
-    else
-    {
-      // To avoid call of method : Update()
-      // Not useful and can be costly.
-      myStructureManager->SetUpdateMode (Aspect_TOU_WAIT);
-      UnHighlight();
-      myStructureManager->SetUpdateMode (anUpdateMode);
-    }
-  }
-
   SetDisplayPriority (Structure_MAX_PRIORITY - 1);
 
-  GraphicHighlight (theMethod);
-  if (myCStructure->stick)
-  {
-    myStructureManager->Highlight (this, theMethod);
-  }
-
-  Update();
-}
+  myCStructure->GraphicHighlight (theStyle);
 
-//=============================================================================
-//function : SetHighlightColor
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::SetHighlightColor (const Quantity_Color& theColor)
-{
-  if (IsDeleted())
+  if (!theToUpdateMgr)
   {
     return;
   }
 
-  if (!myCStructure->highlight)
+  if (myCStructure->stick)
   {
-    myHighlightColor = theColor;
-    return;
+    myStructureManager->Highlight (this);
   }
 
-  // Change highlight color on already Highlighted structure.
-  Aspect_TypeOfUpdate anUpdateMode  = myStructureManager->UpdateMode();
-  if (anUpdateMode == Aspect_TOU_WAIT)
-  {
-    UnHighlight();
-  }
-  else
-  {
-    // To avoid call of method : Update()
-    // Not useful and can be costly.
-    myStructureManager->SetUpdateMode (Aspect_TOU_WAIT);
-    UnHighlight();
-    myStructureManager->SetUpdateMode (anUpdateMode);
-  }
-  myHighlightColor = theColor;
-  Highlight (myHighlightMethod);
+  Update();
 }
 
 //=============================================================================
@@ -363,31 +263,15 @@ void Graphic3d_Structure::SetVisible (const Standard_Boolean theValue)
 {
   if (IsDeleted()) return;
 
-  myCStructure->visible = theValue ? 1 : 0;
-  myCStructure->UpdateNamedStatus();
-  Update();
-}
-
-//=============================================================================
-//function : SetPick
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::SetPick (const Standard_Boolean theValue)
-{
-  if (IsDeleted ()) return;
-
-  myCStructure->pick = theValue ? 1 : 0;
-  myCStructure->UpdateNamedStatus();
-
-  if (theValue)
-  {
-    myStructureManager->Detectable (this);
-  }
-  else
+  const unsigned isVisible = theValue ? 1 : 0;
+  if (myCStructure->visible == isVisible)
   {
-    myStructureManager->Undetectable (this);
+    return;
   }
-  Update();
+
+  myCStructure->visible = isVisible;
+  myCStructure->OnVisibilityChanged();
+  Update (true);
 }
 
 //=============================================================================
@@ -402,7 +286,7 @@ void Graphic3d_Structure::UnHighlight()
   {
     myCStructure->highlight = 0;
 
-    GraphicUnHighlight();
+    myCStructure->GraphicUnhighlight();
     myStructureManager->UnHighlight (this);
 
     ResetDisplayPriority();
@@ -410,95 +294,6 @@ void Graphic3d_Structure::UnHighlight()
   }
 }
 
-//=============================================================================
-//function : HighlightColor
-//purpose  :
-//=============================================================================
-const Quantity_Color& Graphic3d_Structure::HighlightColor() const
-{
-  return myHighlightColor;
-}
-
-//=============================================================================
-//function : IsDisplayed
-//purpose  :
-//=============================================================================
-Standard_Boolean Graphic3d_Structure::IsDisplayed() const
-{
-  return myCStructure->stick ? Standard_True : Standard_False;
-}
-
-//=============================================================================
-//function : IsDeleted
-//purpose  :
-//=============================================================================
-Standard_Boolean Graphic3d_Structure::IsDeleted() const
-{
-  return myCStructure.IsNull();
-}
-
-//=============================================================================
-//function : IsHighlighted
-//purpose  :
-//=============================================================================
-Standard_Boolean Graphic3d_Structure::IsHighlighted() const
-{
-  return myCStructure->highlight ? Standard_True : Standard_False;
-}
-
-//=============================================================================
-//function : IsSelectable
-//purpose  :
-//=============================================================================
-Standard_Boolean Graphic3d_Structure::IsSelectable() const
-{
-  return myCStructure->pick ? Standard_True : Standard_False;
-}
-
-//=============================================================================
-//function : IsVisible
-//purpose  :
-//=============================================================================
-Standard_Boolean Graphic3d_Structure::IsVisible() const
-{
-  return myCStructure->visible ? Standard_True : Standard_False;
-}
-
-//=============================================================================
-//function : IsRotated
-//purpose  :
-//=============================================================================
-Standard_Boolean Graphic3d_Structure::IsRotated() const
-{
-  // A somewhat light test !
-  return myCStructure->Transformation[0][1] != 0.0
-      || myCStructure->Transformation[0][2] != 0.0
-      || myCStructure->Transformation[1][0] != 0.0
-      || myCStructure->Transformation[1][2] != 0.0
-      || myCStructure->Transformation[2][0] != 0.0
-      || myCStructure->Transformation[2][1] != 0.0;
-}
-
-//=============================================================================
-//function : IsTransformed
-//purpose  :
-//=============================================================================
-Standard_Boolean Graphic3d_Structure::IsTransformed() const
-{
-  Standard_Boolean aResult = Standard_False;
-  for (Standard_Integer i = 0; i <= 3 && !aResult; ++i)
-  {
-    for (Standard_Integer j = 0; j <= 3 && !aResult; ++j)
-    {
-      if (i == j)
-        aResult = myCStructure->Transformation[i][j] != 1.0;
-      else
-        aResult = myCStructure->Transformation[i][j] != 0.0;
-    }
-  }
-  return aResult;
-}
-
 //=============================================================================
 //function : ContainsFacet
 //purpose  :
@@ -516,10 +311,9 @@ Standard_Boolean Graphic3d_Structure::ContainsFacet() const
   }
 
   // stop at the first descendant containing at least one facet
-  const Standard_Integer aNbDesc = myDescendants.Length();
-  for (Standard_Integer aStructIter = 1; aStructIter <= aNbDesc; ++aStructIter)
+  for (NCollection_IndexedMap<Graphic3d_Structure*>::Iterator anIter (myDescendants); anIter.More(); anIter.Next())
   {
-    if (((Graphic3d_Structure *)(myDescendants.Value (aStructIter)))->ContainsFacet())
+    if (anIter.Value()->ContainsFacet())
     {
       return Standard_True;
     }
@@ -542,20 +336,18 @@ Standard_Boolean Graphic3d_Structure::IsEmpty() const
   // - if all these groups are empty
   // - or if all groups are empty and all their descendants are empty
   // - or if all its descendants are empty
-  const Standard_Integer aNbGroups = myGroups.Length();
-  for (Standard_Integer aGrpIter = 1; aGrpIter <= aNbGroups; ++aGrpIter)
+  for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (myCStructure->Groups()); aGroupIter.More(); aGroupIter.Next())
   {
-    if (!myGroups.Value (aGrpIter)->IsEmpty())
+    if (!aGroupIter.Value()->IsEmpty())
     {
       return Standard_False;
     }
   }
 
   // stop at the first non-empty descendant
-  const Standard_Integer aNbDesc = myDescendants.Length();
-  for (Standard_Integer aDescIter = 1; aDescIter <= aNbDesc; ++aDescIter)
+  for (NCollection_IndexedMap<Graphic3d_Structure*>::Iterator anIter (myDescendants); anIter.More(); anIter.Next())
   {
-    if (!((Graphic3d_Structure* )(myDescendants.Value (aDescIter)))->IsEmpty())
+    if (!anIter.Value()->IsEmpty())
     {
       return Standard_False;
     }
@@ -563,21 +355,6 @@ Standard_Boolean Graphic3d_Structure::IsEmpty() const
   return Standard_True;
 }
 
-//=============================================================================
-//function : PrimitivesAspect
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::PrimitivesAspect (Handle(Graphic3d_AspectLine3d)&     theAspLine,
-                                            Handle(Graphic3d_AspectText3d)&     theAspText,
-                                            Handle(Graphic3d_AspectMarker3d)&   theAspMarker,
-                                            Handle(Graphic3d_AspectFillArea3d)& theAspFill) const
-{
-  theAspLine   = Line3dAspect();
-  theAspText   = Text3dAspect();
-  theAspMarker = Marker3dAspect();
-  theAspFill   = FillArea3dAspect();
-}
-
 //=============================================================================
 //function : GroupsWithFacet
 //purpose  :
@@ -592,814 +369,235 @@ void Graphic3d_Structure::GroupsWithFacet (const Standard_Integer theDelta)
 }
 
 //=============================================================================
-//function : Compute
+//function : ReCompute
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::Compute()
+void Graphic3d_Structure::ReCompute()
 {
-  // Implemented by Presentation
+  myStructureManager->ReCompute (this);
 }
 
 //=============================================================================
-//function : Compute
+//function : ReCompute
 //purpose  :
 //=============================================================================
-Handle(Graphic3d_Structure) Graphic3d_Structure::Compute (const Handle(Graphic3d_DataStructureManager)& )
+void Graphic3d_Structure::ReCompute (const Handle(Graphic3d_DataStructureManager)& theProjector)
 {
-  // Implemented by Presentation
-  return this;
+  myStructureManager->ReCompute (this, theProjector);
 }
 
 //=============================================================================
-//function : Compute
+//function : GraphicClear
 //purpose  :
 //=============================================================================
-Handle(Graphic3d_Structure) Graphic3d_Structure::Compute (const Handle(Graphic3d_DataStructureManager)& ,
-                                                          const TColStd_Array2OfReal& )
+void Graphic3d_Structure::GraphicClear (const Standard_Boolean theWithDestruction)
 {
-  // Implemented by Presentation
-  return this;
-}
+  if (myCStructure.IsNull())
+  {
+    return;
+  }
 
-//=============================================================================
-//function : Compute
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::Compute (const Handle(Graphic3d_DataStructureManager)& ,
-                                   Handle(Graphic3d_Structure)& )
-{
-  // Implemented by Presentation
-}
+  // clean and empty each group
+  for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (myCStructure->Groups()); aGroupIter.More(); aGroupIter.Next())
+  {
+    aGroupIter.ChangeValue()->Clear();
+  }
+  if (!theWithDestruction)
+  {
+    return;
+  }
 
-//=============================================================================
-//function : Compute
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::Compute (const Handle(Graphic3d_DataStructureManager)& ,
-                                   const TColStd_Array2OfReal& ,
-                                   Handle(Graphic3d_Structure)& )
-{
-  // Implemented by Presentation
+  while (!myCStructure->Groups().IsEmpty())
+  {
+    Handle(Graphic3d_Group) aGroup = myCStructure->Groups().First();
+    aGroup->Remove();
+  }
+  myCStructure->Clear();
 }
 
 //=============================================================================
-//function : ReCompute
+//function : SetVisual
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::ReCompute()
+void Graphic3d_Structure::SetVisual (const Graphic3d_TypeOfStructure theVisual)
 {
-  myStructureManager->ReCompute (this);
+  if (IsDeleted()
+   || myVisual == theVisual)
+  {
+    return;
+  }
+
+  if (!myCStructure->stick)
+  {
+    myVisual = theVisual;
+    SetComputeVisual (theVisual);
+  }
+  else
+  {
+    erase();
+    myVisual = theVisual;
+    SetComputeVisual (theVisual);
+    Display();
+  }
 }
 
 //=============================================================================
-//function : ReCompute
+//function : SetZoomLimit
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::ReCompute (const Handle(Graphic3d_DataStructureManager)& theProjector)
+void Graphic3d_Structure::SetZoomLimit (const Standard_Real theLimitInf,
+                                        const Standard_Real theLimitSup)
 {
-  myStructureManager->ReCompute (this, theProjector);
+  (void )theLimitInf;
+  (void )theLimitSup;
+  Graphic3d_StructureDefinitionError_Raise_if (theLimitInf <= 0.0,
+                                               "Bad value for ZoomLimit inf");
+  Graphic3d_StructureDefinitionError_Raise_if (theLimitSup <= 0.0,
+                                               "Bad value for ZoomLimit sup");
+  Graphic3d_StructureDefinitionError_Raise_if (theLimitSup < theLimitInf,
+                                               "ZoomLimit sup < ZoomLimit inf");
 }
 
 //=============================================================================
-//function : SetInfiniteState
+//function : AcceptConnection
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::SetInfiniteState (const Standard_Boolean theToSet)
+Standard_Boolean Graphic3d_Structure::AcceptConnection (Graphic3d_Structure* theStructure1,
+                                                        Graphic3d_Structure* theStructure2,
+                                                        Graphic3d_TypeOfConnection theType)
 {
-  myCStructure->IsInfinite = theToSet ? 1 : 0;
+  // cycle detection
+  NCollection_Map<Graphic3d_Structure*> aSet;
+  Graphic3d_Structure::Network (theStructure2, theType, aSet);
+  return !aSet.Contains (theStructure1);
 }
 
 //=============================================================================
-//function : IsInfinite
+//function : Ancestors
 //purpose  :
 //=============================================================================
-Standard_Boolean Graphic3d_Structure::IsInfinite() const
+void Graphic3d_Structure::Ancestors (Graphic3d_MapOfStructure& theSet) const
 {
-  return IsDeleted()
-       || myCStructure->IsInfinite;
+  for (NCollection_IndexedMap<Graphic3d_Structure*>::Iterator anIter (myAncestors); anIter.More(); anIter.Next())
+  {
+    theSet.Add (anIter.Value());
+  }
 }
 
 //=============================================================================
-//function : GraphicClear
+//function : Descendants
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::GraphicClear (const Standard_Boolean theWithDestruction)
+void Graphic3d_Structure::Descendants (Graphic3d_MapOfStructure& theSet) const
 {
-  // clean and empty each group
-  const Standard_Integer aLength = myGroups.Length();
-  for (Standard_Integer aGrId = 1; aGrId <= aLength; ++aGrId)
-  {
-    myGroups.ChangeValue (aGrId)->Clear();
-  }
-
-  if (theWithDestruction)
+  for (NCollection_IndexedMap<Graphic3d_Structure*>::Iterator anIter (myDescendants); anIter.More(); anIter.Next())
   {
-    while (!myGroups.IsEmpty())
-    {
-      Handle(Graphic3d_Group) aGroup = myGroups.First();
-      aGroup->Remove();
-    }
-
-    myCStructure->Clear();
+    theSet.Add (anIter.Value());
   }
 }
 
 //=============================================================================
-//function : GraphicConnect
+//function : AppendAncestor
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::GraphicConnect (const Handle(Graphic3d_Structure)& theDaughter)
+Standard_Boolean Graphic3d_Structure::AppendAncestor (Graphic3d_Structure* theAncestor)
 {
-  myCStructure->Connect (*theDaughter->myCStructure);
+  const Standard_Integer aSize = myAncestors.Size();
+
+  return myAncestors.Add (theAncestor) > aSize; // new object
 }
 
 //=============================================================================
-//function : GraphicDisconnect
+//function : AppendDescendant
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::GraphicDisconnect (const Handle(Graphic3d_Structure)& theDaughter)
+Standard_Boolean Graphic3d_Structure::AppendDescendant (Graphic3d_Structure* theDescendant)
 {
-  myCStructure->Disconnect (*theDaughter->myCStructure);
+  const Standard_Integer aSize = myDescendants.Size();
+
+  return myDescendants.Add (theDescendant) > aSize; // new object
 }
 
 //=============================================================================
-//function : Line3dAspect
+//function : RemoveAncestor
 //purpose  :
 //=============================================================================
-Handle(Graphic3d_AspectLine3d) Graphic3d_Structure::Line3dAspect() const
+Standard_Boolean Graphic3d_Structure::RemoveAncestor (Graphic3d_Structure* theAncestor)
 {
-  const Standard_Real anRGB[3] =
+  const Standard_Integer anIndex = myAncestors.FindIndex (theAncestor);
+
+  if (anIndex != 0)
   {
-    Standard_Real (myCStructure->ContextLine.Color.r),
-    Standard_Real (myCStructure->ContextLine.Color.g),
-    Standard_Real (myCStructure->ContextLine.Color.b)
-  };
-  Quantity_Color aColor;
-  aColor.SetValues (anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB);
-  Aspect_TypeOfLine aLType = Aspect_TypeOfLine (myCStructure->ContextLine.LineType);
-  Standard_Real     aWidth = Standard_Real     (myCStructure->ContextLine.Width);
-
-  Handle(Graphic3d_AspectLine3d) anAspLine = new Graphic3d_AspectLine3d (aColor, aLType, aWidth);
-  anAspLine->SetShaderProgram (myCStructure->ContextLine.ShaderProgram);
-  return anAspLine;
+    myAncestors.Swap (anIndex, myAncestors.Size());
+    myAncestors.RemoveLast();
+  }
+
+  return anIndex != 0; // object was found
 }
 
 //=============================================================================
-//function : Text3dAspect
+//function : RemoveDescendant
 //purpose  :
 //=============================================================================
-Handle(Graphic3d_AspectText3d) Graphic3d_Structure::Text3dAspect() const
+Standard_Boolean Graphic3d_Structure::RemoveDescendant (Graphic3d_Structure* theDescendant)
 {
-  const Standard_Real anRGB[3] =
+  const Standard_Integer anIndex = myDescendants.FindIndex (theDescendant);
+
+  if (anIndex != 0)
   {
-    Standard_Real (myCStructure->ContextText.Color.r),
-    Standard_Real (myCStructure->ContextText.Color.g),
-    Standard_Real (myCStructure->ContextText.Color.b)
-  };
-  Quantity_Color aColor;
-  aColor.SetValues (anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB);
-  Standard_CString         aFont       = Standard_CString         (myCStructure->ContextText.Font);
-  Standard_Real            anExpansion = Standard_Real            (myCStructure->ContextText.Expan);
-  Standard_Real            aSpace      = Standard_Real            (myCStructure->ContextText.Space);
-  Aspect_TypeOfStyleText   aStyle      = Aspect_TypeOfStyleText   (myCStructure->ContextText.Style);
-  Aspect_TypeOfDisplayText aDispType   = Aspect_TypeOfDisplayText (myCStructure->ContextText.DisplayType);
-
-  Handle(Graphic3d_AspectText3d) anAspText = new Graphic3d_AspectText3d (aColor, aFont, anExpansion, aSpace, aStyle, aDispType);
-  anAspText->SetShaderProgram (myCStructure->ContextText.ShaderProgram);
-  return anAspText;
+    myDescendants.Swap (anIndex, myDescendants.Size());
+    myDescendants.RemoveLast();
+  }
+
+  return anIndex != 0; // object was found
 }
 
 //=============================================================================
-//function : Marker3dAspect
+//function : Connect
 //purpose  :
 //=============================================================================
-Handle(Graphic3d_AspectMarker3d) Graphic3d_Structure::Marker3dAspect() const
+void Graphic3d_Structure::Connect (Graphic3d_Structure* theStructure,
+                                   Graphic3d_TypeOfConnection theType,
+                                   Standard_Boolean theWithCheck)
 {
-  const Standard_Real anRGB[3] =
+  if (IsDeleted())
   {
-    Standard_Real (myCStructure->ContextMarker.Color.r),
-    Standard_Real (myCStructure->ContextMarker.Color.g),
-    Standard_Real (myCStructure->ContextMarker.Color.b)
-  };
-  Quantity_Color aColor;
-  aColor.SetValues (anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB);
-  Aspect_TypeOfMarker aMType = myCStructure->ContextMarker.MarkerType;
-  Standard_Real       aScale = Standard_Real (myCStructure->ContextMarker.Scale);
-
-  Handle(Graphic3d_AspectMarker3d) anAspMarker = new Graphic3d_AspectMarker3d (aMType, aColor, aScale);
-  anAspMarker->SetShaderProgram (myCStructure->ContextMarker.ShaderProgram);
-  return anAspMarker;
-}
+    return;
+  }
 
-//=============================================================================
-//function : FillArea3dAspect
-//purpose  :
-//=============================================================================
-Handle(Graphic3d_AspectFillArea3d) Graphic3d_Structure::FillArea3dAspect() const
-{
-  // Back Material
-  Graphic3d_MaterialAspect aBack;
-  aBack.SetShininess    (Standard_Real (myCStructure->ContextFillArea.Back.Shininess));
-  aBack.SetAmbient      (Standard_Real (myCStructure->ContextFillArea.Back.Ambient));
-  aBack.SetDiffuse      (Standard_Real (myCStructure->ContextFillArea.Back.Diffuse));
-  aBack.SetSpecular     (Standard_Real (myCStructure->ContextFillArea.Back.Specular));
-  aBack.SetTransparency (Standard_Real (myCStructure->ContextFillArea.Back.Transparency));
-  aBack.SetEmissive     (Standard_Real (myCStructure->ContextFillArea.Back.Emission));
-  if (myCStructure->ContextFillArea.Back.IsAmbient == 1)
-    aBack.SetReflectionModeOn  (Graphic3d_TOR_AMBIENT);
-  else
-    aBack.SetReflectionModeOff (Graphic3d_TOR_AMBIENT);
-  if (myCStructure->ContextFillArea.Back.IsDiffuse == 1)
-    aBack.SetReflectionModeOn  (Graphic3d_TOR_DIFFUSE);
-  else
-    aBack.SetReflectionModeOff (Graphic3d_TOR_DIFFUSE);
-  if (myCStructure->ContextFillArea.Back.IsSpecular == 1)
-    aBack.SetReflectionModeOn  (Graphic3d_TOR_SPECULAR);
-  else
-    aBack.SetReflectionModeOff (Graphic3d_TOR_SPECULAR);
-  if (myCStructure->ContextFillArea.Back.IsEmission == 1)
-    aBack.SetReflectionModeOn  (Graphic3d_TOR_EMISSION);
-  else
-    aBack.SetReflectionModeOff (Graphic3d_TOR_EMISSION);
-
-  Quantity_Color aColor (Standard_Real (myCStructure->ContextFillArea.Back.ColorSpec.r),
-                         Standard_Real (myCStructure->ContextFillArea.Back.ColorSpec.g),
-                         Standard_Real (myCStructure->ContextFillArea.Back.ColorSpec.b), Quantity_TOC_RGB);
-  aBack.SetSpecularColor (aColor);
-
-  aColor.SetValues (Standard_Real (myCStructure->ContextFillArea.Back.ColorAmb.r),
-                    Standard_Real (myCStructure->ContextFillArea.Back.ColorAmb.g),
-                    Standard_Real (myCStructure->ContextFillArea.Back.ColorAmb.b), Quantity_TOC_RGB);
-  aBack.SetAmbientColor (aColor);
-
-  aColor.SetValues (Standard_Real (myCStructure->ContextFillArea.Back.ColorDif.r),
-                    Standard_Real (myCStructure->ContextFillArea.Back.ColorDif.g),
-                    Standard_Real (myCStructure->ContextFillArea.Back.ColorDif.b), Quantity_TOC_RGB);
-  aBack.SetDiffuseColor (aColor);
-
-  aColor.SetValues (Standard_Real (myCStructure->ContextFillArea.Back.ColorEms.r),
-                    Standard_Real (myCStructure->ContextFillArea.Back.ColorEms.g),
-                    Standard_Real (myCStructure->ContextFillArea.Back.ColorEms.b), Quantity_TOC_RGB);
-  aBack.SetEmissiveColor (aColor);
-
-  aBack.SetEnvReflexion (myCStructure->ContextFillArea.Back.EnvReflexion);
-  aBack.SetMaterialType (myCStructure->ContextFillArea.Back.IsPhysic ? Graphic3d_MATERIAL_PHYSIC : Graphic3d_MATERIAL_ASPECT);
-
-  // Front Material
-  Graphic3d_MaterialAspect aFront;
-  aFront.SetShininess    (Standard_Real (myCStructure->ContextFillArea.Front.Shininess));
-  aFront.SetAmbient      (Standard_Real (myCStructure->ContextFillArea.Front.Ambient));
-  aFront.SetDiffuse      (Standard_Real (myCStructure->ContextFillArea.Front.Diffuse));
-  aFront.SetSpecular     (Standard_Real (myCStructure->ContextFillArea.Front.Specular));
-  aFront.SetTransparency (Standard_Real (myCStructure->ContextFillArea.Front.Transparency));
-  aFront.SetEmissive     (Standard_Real (myCStructure->ContextFillArea.Front.Emission));
-  if (myCStructure->ContextFillArea.Front.IsAmbient == 1)
-    aFront.SetReflectionModeOn  (Graphic3d_TOR_AMBIENT);
-  else
-    aFront.SetReflectionModeOff (Graphic3d_TOR_AMBIENT);
-  if (myCStructure->ContextFillArea.Front.IsDiffuse == 1)
-    aFront.SetReflectionModeOn  (Graphic3d_TOR_DIFFUSE);
-  else
-    aFront.SetReflectionModeOff (Graphic3d_TOR_DIFFUSE);
-  if (myCStructure->ContextFillArea.Front.IsSpecular == 1)
-    aFront.SetReflectionModeOn  (Graphic3d_TOR_SPECULAR);
-  else
-    aFront.SetReflectionModeOff (Graphic3d_TOR_SPECULAR);
-  if (myCStructure->ContextFillArea.Front.Emission == 1)
-    aFront.SetReflectionModeOn  (Graphic3d_TOR_EMISSION);
-  else
-    aFront.SetReflectionModeOff (Graphic3d_TOR_EMISSION);
-
-  aColor.SetValues (Standard_Real (myCStructure->ContextFillArea.Front.ColorSpec.r),
-                    Standard_Real (myCStructure->ContextFillArea.Front.ColorSpec.g),
-                    Standard_Real (myCStructure->ContextFillArea.Front.ColorSpec.b), Quantity_TOC_RGB);
-  aFront.SetSpecularColor (aColor);
-
-  aColor.SetValues (Standard_Real (myCStructure->ContextFillArea.Front.ColorAmb.r),
-                    Standard_Real (myCStructure->ContextFillArea.Front.ColorAmb.g),
-                    Standard_Real (myCStructure->ContextFillArea.Front.ColorAmb.b), Quantity_TOC_RGB);
-  aFront.SetAmbientColor (aColor);
-
-  aColor.SetValues (Standard_Real (myCStructure->ContextFillArea.Front.ColorDif.r),
-                    Standard_Real (myCStructure->ContextFillArea.Front.ColorDif.g),
-                    Standard_Real (myCStructure->ContextFillArea.Front.ColorDif.b), Quantity_TOC_RGB);
-  aFront.SetDiffuseColor (aColor);
-
-  aColor.SetValues (Standard_Real (myCStructure->ContextFillArea.Front.ColorEms.r),
-                    Standard_Real (myCStructure->ContextFillArea.Front.ColorEms.g),
-                    Standard_Real (myCStructure->ContextFillArea.Front.ColorEms.b), Quantity_TOC_RGB);
-  aFront.SetEmissiveColor (aColor);
-
-  aFront.SetEnvReflexion (myCStructure->ContextFillArea.Front.EnvReflexion);
-  aFront.SetMaterialType (myCStructure->ContextFillArea.Front.IsPhysic ? Graphic3d_MATERIAL_PHYSIC : Graphic3d_MATERIAL_ASPECT);
-
-  Quantity_Color anIntColor  (Standard_Real (myCStructure->ContextFillArea.IntColor.r),
-                              Standard_Real (myCStructure->ContextFillArea.IntColor.g),
-                              Standard_Real (myCStructure->ContextFillArea.IntColor.b), Quantity_TOC_RGB);
-  Quantity_Color anEdgeColor (Standard_Real (myCStructure->ContextFillArea.EdgeColor.r),
-                              Standard_Real (myCStructure->ContextFillArea.EdgeColor.g),
-                              Standard_Real (myCStructure->ContextFillArea.EdgeColor.b), Quantity_TOC_RGB);
-  Handle(Graphic3d_AspectFillArea3d) anAspFill = new Graphic3d_AspectFillArea3d (Aspect_InteriorStyle (myCStructure->ContextFillArea.Style),
-                                                                                 anIntColor, anEdgeColor,
-                                                                                 Aspect_TypeOfLine    (myCStructure->ContextFillArea.LineType),
-                                                                                 Standard_Real        (myCStructure->ContextFillArea.Width),
-                                                                                 aFront, aBack);
-
-  // Edges
-  if (myCStructure->ContextFillArea.Edge == 1)
-    anAspFill->SetEdgeOn();
-  else
-    anAspFill->SetEdgeOff();
-  // Hatch
-  anAspFill->SetHatchStyle (Aspect_HatchStyle (myCStructure->ContextFillArea.Hatch));
-  // Materials
-  // Front and Back face
-  if (myCStructure->ContextFillArea.Distinguish == 1)
-    anAspFill->SetDistinguishOn();
-  else
-    anAspFill->SetDistinguishOff();
-  if (myCStructure->ContextFillArea.BackFace == 1)
-    anAspFill->SuppressBackFace();
-  else
-    anAspFill->AllowBackFace();
-  // Texture
-  anAspFill->SetTextureMap (myCStructure->ContextFillArea.Texture.TextureMap);
-  if (myCStructure->ContextFillArea.Texture.doTextureMap == 1)
-  {
-    anAspFill->SetTextureMapOn();
-  }
-  else
-  {
-    anAspFill->SetTextureMapOff();
-  }
-  anAspFill->SetShaderProgram  (myCStructure->ContextFillArea.ShaderProgram);
-  anAspFill->SetPolygonOffsets (myCStructure->ContextFillArea.PolygonOffsetMode,
-                                myCStructure->ContextFillArea.PolygonOffsetFactor,
-                                myCStructure->ContextFillArea.PolygonOffsetUnits);
-  return anAspFill;
-}
-
-//=============================================================================
-//function : Groups
-//purpose  :
-//=============================================================================
-const Graphic3d_SequenceOfGroup& Graphic3d_Structure::Groups() const
-{
-  return myGroups;
-}
-
-//=============================================================================
-//function : NumberOfGroups
-//purpose  :
-//=============================================================================
-Standard_Integer Graphic3d_Structure::NumberOfGroups() const
-{
-  return myGroups.Length();
-}
-
-//=============================================================================
-//function : SetPrimitivesAspect
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::SetPrimitivesAspect (const Handle(Graphic3d_AspectLine3d)& theAspLine)
-{
-  if (IsDeleted()) return;
-
-  Standard_Real     aWidth;
-  Quantity_Color    aColor;
-  Aspect_TypeOfLine aLType;
-  theAspLine->Values (aColor, aLType, aWidth);
-
-  myCStructure->ContextLine.Color.r        = float (aColor.Red());
-  myCStructure->ContextLine.Color.g        = float (aColor.Green());
-  myCStructure->ContextLine.Color.b        = float (aColor.Blue());
-  myCStructure->ContextLine.LineType       = int   (aLType);
-  myCStructure->ContextLine.Width          = float (aWidth);
-  myCStructure->ContextLine.ShaderProgram  = theAspLine->ShaderProgram();
-  myCStructure->ContextLine.IsDef          = 1;
-
-  myCStructure->UpdateAspects();
-
-  // Attributes are "IsSet" during the first update of context (line, marker...)
-  myCStructure->ContextLine.IsSet     = 1;
-  myCStructure->ContextFillArea.IsSet = 1;
-  myCStructure->ContextMarker.IsSet   = 1;
-  myCStructure->ContextText.IsSet     = 1;
-
-  Update();
-}
-
-//=============================================================================
-//function : SetPrimitivesAspect
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::SetPrimitivesAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspFill)
-{
-  if (IsDeleted()) return;
-
-  Standard_Real        anRGB[3];
-  Standard_Real        aWidth;
-  Quantity_Color       anIntColor;
-  Quantity_Color       aBackIntColor;
-  Quantity_Color       anEdgeColor;
-  Aspect_TypeOfLine    aLType;
-  Aspect_InteriorStyle aStyle;
-  theAspFill->Values (aStyle, anIntColor, aBackIntColor, anEdgeColor, aLType, aWidth);
-
-  anIntColor.Values (anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB);
-  myCStructure->ContextFillArea.Style      = aStyle;
-  myCStructure->ContextFillArea.IntColor.r = float (anRGB[0]);
-  myCStructure->ContextFillArea.IntColor.g = float (anRGB[1]);
-  myCStructure->ContextFillArea.IntColor.b = float (anRGB[2]);
-
-  if (theAspFill->Distinguish())
-  {
-    aBackIntColor.Values (anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB);
-  }
-  myCStructure->ContextFillArea.BackIntColor.r = float(anRGB[0]);
-  myCStructure->ContextFillArea.BackIntColor.g = float(anRGB[1]);
-  myCStructure->ContextFillArea.BackIntColor.b = float(anRGB[2]);
-
-  // Edges
-  myCStructure->ContextFillArea.Edge        = theAspFill->Edge () ? 1 : 0;
-  myCStructure->ContextFillArea.EdgeColor.r = float (anEdgeColor.Red());
-  myCStructure->ContextFillArea.EdgeColor.g = float (anEdgeColor.Green());
-  myCStructure->ContextFillArea.EdgeColor.b = float (anEdgeColor.Blue());
-  myCStructure->ContextFillArea.LineType    = aLType;
-  myCStructure->ContextFillArea.Width       = float (aWidth);
-  myCStructure->ContextFillArea.Hatch       = theAspFill->HatchStyle();
-
-  // Front and Back face
-  myCStructure->ContextFillArea.Distinguish = theAspFill->Distinguish() ? 1 : 0;
-  myCStructure->ContextFillArea.BackFace    = theAspFill->BackFace()    ? 1 : 0;
-
-  // Back Material
-  const Graphic3d_MaterialAspect& aBack = theAspFill->BackMaterial();
-  // Light specificity
-  myCStructure->ContextFillArea.Back.Shininess    = float (aBack.Shininess());
-  myCStructure->ContextFillArea.Back.Ambient      = float (aBack.Ambient());
-  myCStructure->ContextFillArea.Back.Diffuse      = float (aBack.Diffuse());
-  myCStructure->ContextFillArea.Back.Specular     = float (aBack.Specular());
-  myCStructure->ContextFillArea.Back.Transparency = float (aBack.Transparency());
-  myCStructure->ContextFillArea.Back.Emission     = float (aBack.Emissive());
-
-  // Reflection mode
-  myCStructure->ContextFillArea.Back.IsAmbient    = (aBack.ReflectionMode (Graphic3d_TOR_AMBIENT)  ? 1 : 0);
-  myCStructure->ContextFillArea.Back.IsDiffuse    = (aBack.ReflectionMode (Graphic3d_TOR_DIFFUSE)  ? 1 : 0);
-  myCStructure->ContextFillArea.Back.IsSpecular   = (aBack.ReflectionMode (Graphic3d_TOR_SPECULAR) ? 1 : 0);
-  myCStructure->ContextFillArea.Back.IsEmission   = (aBack.ReflectionMode (Graphic3d_TOR_EMISSION) ? 1 : 0);
-
-  // Material type
-  //JR/Hp
-  myCStructure->ContextFillArea.Back.IsPhysic = (aBack.MaterialType (Graphic3d_MATERIAL_PHYSIC) ? 1 : 0 );
-
-  // Specular Color
-  myCStructure->ContextFillArea.Back.ColorSpec.r  = float (aBack.SpecularColor().Red());
-  myCStructure->ContextFillArea.Back.ColorSpec.g  = float (aBack.SpecularColor().Green());
-  myCStructure->ContextFillArea.Back.ColorSpec.b  = float (aBack.SpecularColor().Blue());
-
-  // Ambient color
-  myCStructure->ContextFillArea.Back.ColorAmb.r   = float (aBack.AmbientColor().Red());
-  myCStructure->ContextFillArea.Back.ColorAmb.g   = float (aBack.AmbientColor().Green());
-  myCStructure->ContextFillArea.Back.ColorAmb.b   = float (aBack.AmbientColor().Blue());
-
-  // Diffuse color
-  myCStructure->ContextFillArea.Back.ColorDif.r   = float (aBack.DiffuseColor().Red());
-  myCStructure->ContextFillArea.Back.ColorDif.g   = float (aBack.DiffuseColor().Green());
-  myCStructure->ContextFillArea.Back.ColorDif.b   = float (aBack.DiffuseColor().Blue());
-
-  // Emissive color
-  myCStructure->ContextFillArea.Back.ColorEms.r   = float (aBack.EmissiveColor().Red());
-  myCStructure->ContextFillArea.Back.ColorEms.g   = float (aBack.EmissiveColor().Green());
-  myCStructure->ContextFillArea.Back.ColorEms.b   = float (aBack.EmissiveColor().Blue());
-
-  myCStructure->ContextFillArea.Back.EnvReflexion =
-    float ((theAspFill->BackMaterial ()).EnvReflexion());
-
-  // Front Material
-  const Graphic3d_MaterialAspect& aFront = theAspFill->FrontMaterial();
-  // Light specificity
-  myCStructure->ContextFillArea.Front.Shininess    = float (aFront.Shininess());
-  myCStructure->ContextFillArea.Front.Ambient      = float (aFront.Ambient());
-  myCStructure->ContextFillArea.Front.Diffuse      = float (aFront.Diffuse());
-  myCStructure->ContextFillArea.Front.Specular     = float (aFront.Specular());
-  myCStructure->ContextFillArea.Front.Transparency = float (aFront.Transparency());
-  myCStructure->ContextFillArea.Front.Emission     = float (aFront.Emissive());
-
-  // Reflection mode
-  myCStructure->ContextFillArea.Front.IsAmbient    = (aFront.ReflectionMode (Graphic3d_TOR_AMBIENT)  ? 1 : 0);
-  myCStructure->ContextFillArea.Front.IsDiffuse    = (aFront.ReflectionMode (Graphic3d_TOR_DIFFUSE)  ? 1 : 0);
-  myCStructure->ContextFillArea.Front.IsSpecular   = (aFront.ReflectionMode (Graphic3d_TOR_SPECULAR) ? 1 : 0);
-  myCStructure->ContextFillArea.Front.IsEmission   = (aFront.ReflectionMode (Graphic3d_TOR_EMISSION) ? 1 : 0);
-
-  // Materail type
-  //JR/Hp
-  myCStructure->ContextFillArea.Front.IsPhysic     = (aFront.MaterialType (Graphic3d_MATERIAL_PHYSIC) ? 1 : 0);
-
-  // Specular Color
-  myCStructure->ContextFillArea.Front.ColorSpec.r  = float (aFront.SpecularColor().Red());
-  myCStructure->ContextFillArea.Front.ColorSpec.g  = float (aFront.SpecularColor().Green());
-  myCStructure->ContextFillArea.Front.ColorSpec.b  = float (aFront.SpecularColor().Blue());
-
-  // Ambient color
-  myCStructure->ContextFillArea.Front.ColorAmb.r   = float (aFront.AmbientColor().Red());
-  myCStructure->ContextFillArea.Front.ColorAmb.g   = float (aFront.AmbientColor().Green());
-  myCStructure->ContextFillArea.Front.ColorAmb.b   = float (aFront.AmbientColor().Blue());
-
-  // Diffuse color
-  myCStructure->ContextFillArea.Front.ColorDif.r   = float (aFront.DiffuseColor().Red());
-  myCStructure->ContextFillArea.Front.ColorDif.g   = float (aFront.DiffuseColor().Green());
-  myCStructure->ContextFillArea.Front.ColorDif.b   = float (aFront.DiffuseColor().Blue());
-
-  // Emissive color
-  myCStructure->ContextFillArea.Front.ColorEms.r   = float (aFront.EmissiveColor().Red());
-  myCStructure->ContextFillArea.Front.ColorEms.g   = float (aFront.EmissiveColor().Green());
-  myCStructure->ContextFillArea.Front.ColorEms.b   = float (aFront.EmissiveColor().Blue());
-
-  myCStructure->ContextFillArea.Front.EnvReflexion = float (aFront.EnvReflexion());
-
-  myCStructure->ContextFillArea.IsDef = 1; // Definition material ok
-
-  myCStructure->ContextFillArea.Texture.TextureMap   = theAspFill->TextureMap();
-  myCStructure->ContextFillArea.Texture.doTextureMap = theAspFill->TextureMapState() ? 1 : 0;
-  myCStructure->ContextFillArea.ShaderProgram        = theAspFill->ShaderProgram();
-
-  Standard_Integer   aPolyMode;
-  Standard_ShortReal aPolyFactor, aPolyUnits;
-  theAspFill->PolygonOffsets (aPolyMode, aPolyFactor, aPolyUnits);
-  myCStructure->ContextFillArea.PolygonOffsetMode   = aPolyMode;
-  myCStructure->ContextFillArea.PolygonOffsetFactor = aPolyFactor;
-  myCStructure->ContextFillArea.PolygonOffsetUnits  = aPolyUnits;
-
-  myCStructure->UpdateAspects();
-
-  // Attributes are "IsSet" during the first update of context (line, marker...)
-  myCStructure->ContextLine.IsSet     = 1;
-  myCStructure->ContextFillArea.IsSet = 1;
-  myCStructure->ContextMarker.IsSet   = 1;
-  myCStructure->ContextText.IsSet     = 1;
-
-  Update();
-}
-
-//=============================================================================
-//function : SetPrimitivesAspect
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::SetPrimitivesAspect (const Handle(Graphic3d_AspectText3d)& theAspText)
-{
-  if (IsDeleted()) return;
-
-  Standard_CString         aFont;
-  Standard_Real            aSpace, anExpansion, aTextAngle;
-  Quantity_Color           aColor, aColorSub;
-  Aspect_TypeOfStyleText   aStyle;
-  Aspect_TypeOfDisplayText aDispType;
-  Standard_Boolean         isTextZoomable;
-  Font_FontAspect          aTextFontAspect;
-  theAspText->Values (aColor, aFont, anExpansion, aSpace, aStyle, aDispType, aColorSub, isTextZoomable, aTextAngle, aTextFontAspect);
-
-  myCStructure->ContextText.Color.r         = float (aColor.Red());
-  myCStructure->ContextText.Color.g         = float (aColor.Green());
-  myCStructure->ContextText.Color.b         = float (aColor.Blue());
-  myCStructure->ContextText.Font            = aFont;
-  myCStructure->ContextText.Expan           = float (anExpansion);
-  myCStructure->ContextText.Space           = float (aSpace);
-  myCStructure->ContextText.Style           = aStyle;
-  myCStructure->ContextText.DisplayType     = aDispType;
-  myCStructure->ContextText.ColorSubTitle.r = float (aColorSub.Red());
-  myCStructure->ContextText.ColorSubTitle.g = float (aColorSub.Green());
-  myCStructure->ContextText.ColorSubTitle.b = float (aColorSub.Blue());
-  myCStructure->ContextText.TextZoomable    = isTextZoomable;
-  myCStructure->ContextText.TextAngle       = float (aTextAngle);
-  myCStructure->ContextText.TextFontAspect  = aTextFontAspect;
-  myCStructure->ContextText.ShaderProgram   = theAspText->ShaderProgram();
-
-  myCStructure->ContextText.IsDef = 1;
-
-  myCStructure->UpdateAspects();
-
-  // Attributes are "IsSet" during the first update of a context (line, marker...)
-  myCStructure->ContextLine.IsSet     = 1;
-  myCStructure->ContextFillArea.IsSet = 1;
-  myCStructure->ContextMarker.IsSet   = 1;
-  myCStructure->ContextText.IsSet     = 1;
-
-  Update();
-}
-
-//=============================================================================
-//function : SetPrimitivesAspect
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::SetPrimitivesAspect (const Handle(Graphic3d_AspectMarker3d)& theAspMarker)
-{
-  if (IsDeleted()) return;
-
-  Standard_Real       aScale;
-  Quantity_Color      aColor;
-  Aspect_TypeOfMarker aMType;
-  theAspMarker->Values (aColor, aMType, aScale);
-
-  myCStructure->ContextMarker.Color.r       = float (aColor.Red());
-  myCStructure->ContextMarker.Color.g       = float (aColor.Green());
-  myCStructure->ContextMarker.Color.b       = float (aColor.Blue());
-  myCStructure->ContextMarker.MarkerType    = aMType;
-  myCStructure->ContextMarker.Scale         = float (aScale);
-  myCStructure->ContextMarker.ShaderProgram = theAspMarker->ShaderProgram();
-  myCStructure->ContextMarker.IsDef         = 1;
-
-  myCStructure->UpdateAspects();
-
-  // Attributes are "IsSet" during the first update of a context (line, marker...)
-  myCStructure->ContextLine.IsSet     = 1;
-  myCStructure->ContextFillArea.IsSet = 1;
-  myCStructure->ContextMarker.IsSet   = 1;
-  myCStructure->ContextText.IsSet     = 1;
-
-  Update();
-}
-
-//=============================================================================
-//function : SetVisual
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::SetVisual (const Graphic3d_TypeOfStructure theVisual)
-{
-  if (IsDeleted()
-   || myVisual == theVisual)
+  // cycle detection
+  if (theWithCheck
+   && !Graphic3d_Structure::AcceptConnection (this, theStructure, theType))
   {
     return;
   }
 
-  if (!myCStructure->stick)
-  {
-    myVisual = theVisual;
-    SetComputeVisual (theVisual);
-  }
-  else
+  if (theType == Graphic3d_TOC_DESCENDANT)
   {
-    Aspect_TypeOfUpdate anUpdateMode  = myStructureManager->UpdateMode();
-    if (anUpdateMode == Aspect_TOU_WAIT)
+    if (!AppendDescendant (theStructure))
     {
-      Erase();
-      myVisual = theVisual;
-      SetComputeVisual (theVisual);
-      Display();
-    }
-    else {
-      // To avoid calling method : Update ()
-      // Not useful and can be costly.
-      myStructureManager->SetUpdateMode (Aspect_TOU_WAIT);
-      Erase();
-      myVisual = theVisual;
-      SetComputeVisual (theVisual);
-      myStructureManager->SetUpdateMode (anUpdateMode);
-      Display();
+      return;
     }
-  }
-}
-
-//=============================================================================
-//function : SetZoomLimit
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::SetZoomLimit (const Standard_Real theLimitInf,
-                                        const Standard_Real theLimitSup)
-{
-  (void )theLimitInf;
-  (void )theLimitSup;
-  Graphic3d_StructureDefinitionError_Raise_if (theLimitInf <= 0.0,
-                                               "Bad value for ZoomLimit inf");
-  Graphic3d_StructureDefinitionError_Raise_if (theLimitSup <= 0.0,
-                                               "Bad value for ZoomLimit sup");
-  Graphic3d_StructureDefinitionError_Raise_if (theLimitSup < theLimitInf,
-                                               "ZoomLimit sup < ZoomLimit inf");
-}
-
-//=============================================================================
-//function : Visual
-//purpose  :
-//=============================================================================
-Graphic3d_TypeOfStructure Graphic3d_Structure::Visual() const
-{
-  return myVisual;
-}
 
-//=============================================================================
-//function : AcceptConnection
-//purpose  :
-//=============================================================================
-Standard_Boolean Graphic3d_Structure::AcceptConnection (const Handle(Graphic3d_Structure)& theStructure1,
-                                                        const Handle(Graphic3d_Structure)& theStructure2,
-                                                        const Graphic3d_TypeOfConnection   theType)
-{
-  // cycle detection
-  Graphic3d_MapOfStructure aSet;
-  Graphic3d_Structure::Network (theStructure2, theType, aSet);
-  return !aSet.Contains (theStructure1);
-}
+    CalculateBoundBox();
+    theStructure->Connect (this, Graphic3d_TOC_ANCESTOR);
 
-//=============================================================================
-//function : Ancestors
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::Ancestors (Graphic3d_MapOfStructure& theSet) const
-{
-  const Standard_Integer aNbAnces = myAncestors.Length ();
-  for (Standard_Integer anIter = 1; anIter <= aNbAnces; ++anIter)
-  {
-    theSet.Add ((Graphic3d_Structure* )(myAncestors.Value (anIter)));
-  }
-}
-
-//=============================================================================
-//function : SetOwner
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::SetOwner (const Standard_Address theOwner)
-{
-  myOwner = theOwner;
-}
-
-//=============================================================================
-//function : Owner
-//purpose  :
-//=============================================================================
-Standard_Address Graphic3d_Structure::Owner() const
-{
-  return myOwner;
-}
+    GraphicConnect (theStructure);
+    myStructureManager->Connect (this, theStructure);
 
-//=============================================================================
-//function : Descendants
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::Descendants (Graphic3d_MapOfStructure& theSet) const
-{
-  const Standard_Integer aNbDesc = myDescendants.Length ();
-  for (Standard_Integer anIter = 1; anIter <= aNbDesc; ++anIter)
-  {
-    theSet.Add ((Graphic3d_Structure* )(myDescendants.Value (anIter)));
-  }
-}
-
-//=============================================================================
-//function : Connect
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::Connect (const Handle(Graphic3d_Structure)& theStructure,
-                                   const Graphic3d_TypeOfConnection   theType,
-                                   const Standard_Boolean             theWithCheck)
-{
-  if (IsDeleted()) return;
-
-  // cycle detection
-  if (theWithCheck
-   && !Graphic3d_Structure::AcceptConnection (this, theStructure, theType))
-  {
-    return;
+    Update (true);
   }
-
-  switch (theType)
+  else // Graphic3d_TOC_ANCESTOR
   {
-    case Graphic3d_TOC_DESCENDANT:
+    if (!AppendAncestor (theStructure))
     {
-      const Standard_Integer aNbDesc = myDescendants.Length();
-      for (Standard_Integer anIter = 1; anIter <= aNbDesc; ++anIter)
-      {
-        if (myDescendants.Value (anIter) == theStructure.operator->())
-        {
-          return;
-        }
-      }
-
-      myDescendants.Append (theStructure.operator->());
-      theStructure->Connect (this, Graphic3d_TOC_ANCESTOR);
-
-      GraphicConnect (theStructure);
-      myStructureManager->Connect (this, theStructure);
-
-      Update();
       return;
     }
-    case Graphic3d_TOC_ANCESTOR:
-    {
-      const Standard_Integer aNbAnces = myAncestors.Length();
-      for (Standard_Integer anIter = 1; anIter <= aNbAnces; ++anIter)
-      {
-        if (myAncestors.Value (anIter) == theStructure.operator->())
-        {
-          return;
-        }
-      }
 
-      myAncestors.Append (theStructure.operator->());
-      theStructure->Connect (this, Graphic3d_TOC_DESCENDANT);
+    CalculateBoundBox();
+    theStructure->Connect (this, Graphic3d_TOC_DESCENDANT);
 
-      // myGraphicDriver->Connect is called in case if connection between parent and child
-      return;
-    }
+    // myStructureManager->Connect is called in case if connection between parent and child
   }
 }
 
@@ -1407,36 +605,29 @@ void Graphic3d_Structure::Connect (const Handle(Graphic3d_Structure)& theStructu
 //function : Disconnect
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::Disconnect (const Handle(Graphic3d_Structure)& theStructure)
+void Graphic3d_Structure::Disconnect (Graphic3d_Structure* theStructure)
 {
-  if (IsDeleted()) return;
+  if (IsDeleted())
+  {
+    return;
+  }
 
-  const Standard_Integer aNbDesc = myDescendants.Length();
-  for (Standard_Integer anIter = 1; anIter <= aNbDesc; ++anIter)
+  if (RemoveDescendant (theStructure))
   {
-    if (myDescendants.Value (anIter) == theStructure.operator->())
-    {
-      myDescendants.Remove (anIter);
-      theStructure->Disconnect (this);
+    theStructure->Disconnect (this);
 
-      GraphicDisconnect (theStructure);
-      myStructureManager->Disconnect (this, theStructure);
+    GraphicDisconnect (theStructure);
+    myStructureManager->Disconnect (this, theStructure);
 
-      Update();
-      return;
-    }
+    CalculateBoundBox();
+    Update (true);
   }
-
-  const Standard_Integer aNbAnces = myAncestors.Length();
-  for (Standard_Integer anIter = 1; anIter <= aNbAnces; ++anIter)
+  else if (RemoveAncestor (theStructure))
   {
-    if (myAncestors.Value (anIter) == theStructure.operator->())
-    {
-      myAncestors.Remove (anIter);
-      theStructure->Disconnect (this);
-      // no call of myGraphicDriver->Disconnect in case of an ancestor
-      return;
-    }
+    theStructure->Disconnect (this);
+    CalculateBoundBox();
+
+    // no call of myStructureManager->Disconnect in case of an ancestor
   }
 }
 
@@ -1452,634 +643,272 @@ void Graphic3d_Structure::DisconnectAll (const Graphic3d_TypeOfConnection theTyp
   {
     case Graphic3d_TOC_DESCENDANT:
     {
-      const Standard_Integer aLength = myDescendants.Length();
-      for (Standard_Integer anIter = 1; anIter <= aLength; ++anIter)
+      for (Standard_Integer anIdx = 1, aLength = myDescendants.Size(); anIdx <= aLength; ++anIdx)
       {
         // Value (1) instead of Value (i) as myDescendants
         // is modified by :
         // Graphic3d_Structure::Disconnect (AStructure)
         // that takes AStructure from myDescendants
-        ((Graphic3d_Structure* )(myDescendants.Value (1)))->Disconnect (this);
+        myDescendants.FindKey (1)->Disconnect (this);
       }
       break;
     }
     case Graphic3d_TOC_ANCESTOR:
     {
-      const Standard_Integer aLength = myAncestors.Length();
-      for (Standard_Integer anIter = 1; anIter <= aLength; ++anIter)
+      for (Standard_Integer anIdx = 1, aLength = myAncestors.Size(); anIdx <= aLength; ++anIdx)
       {
         // Value (1) instead of Value (i) as myAncestors
         // is modified by :
         // Graphic3d_Structure::Disconnect (AStructure)
         // that takes AStructure from myAncestors
-        ((Graphic3d_Structure* )(myAncestors.Value (1)))->Disconnect (this);
+        myAncestors.FindKey (1)->Disconnect (this);
       }
       break;
     }
   }
 }
 
-//=============================================================================
-//function : Composition
-//purpose  :
-//=============================================================================
-Graphic3d_TypeOfComposition Graphic3d_Structure::Composition() const
-{
-  return myCStructure->Composition;
-}
-
 //=============================================================================
 //function : SetTransform
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::SetTransform (const TColStd_Array2OfReal&       theMatrix,
-                                        const Graphic3d_TypeOfComposition theType)
+void Graphic3d_Structure::SetTransformation (const Handle(TopLoc_Datum3D)& theTrsf)
 {
   if (IsDeleted()) return;
 
-  Standard_Real valuetrsf;
-  Standard_Real valueoldtrsf;
-  Standard_Real valuenewtrsf;
-  TColStd_Array2OfReal aNewTrsf  (0, 3, 0, 3);
-  TColStd_Array2OfReal aMatrix44 (0, 3, 0, 3);
-
-  // Assign the new transformation in an array [0..3][0..3]
-  // Avoid problemes if the user has defined matrice [1..4][1..4]
-  //                                              or [3..6][-1..2] !!
-  Standard_Integer lr = theMatrix.LowerRow();
-  Standard_Integer ur = theMatrix.UpperRow();
-  Standard_Integer lc = theMatrix.LowerCol();
-  Standard_Integer uc = theMatrix.UpperCol();
-
-  if ((ur - lr + 1 != 4) || (uc - lc + 1 != 4))
+  const Standard_Boolean wasTransformed = IsTransformed();
+
+  if (!theTrsf.IsNull()
+    && theTrsf->Trsf().Form() == gp_Identity)
   {
-    Graphic3d_TransformError::Raise ("Transform : not a 4x4 matrix");
+    myCStructure->SetTransformation (Handle(TopLoc_Datum3D)());
   }
-
-  switch (theType)
+  else
   {
-    case Graphic3d_TOC_REPLACE:
-    {
-      myCStructure->Composition = Graphic3d_TOC_REPLACE;
-      // Update of CStructure
-      for (Standard_Integer i = 0; i <= 3; ++i)
-      {
-        for (Standard_Integer j = 0; j <= 3; ++j)
-        {
-          myCStructure->Transformation[i][j] = float (theMatrix (lr + i, lc + j));
-          aNewTrsf (i, j) = theMatrix (lr + i, lc + j);
-        }
-      }
-      break;
-    }
-    case Graphic3d_TOC_POSTCONCATENATE:
-    {
-      myCStructure->Composition = Graphic3d_TOC_POSTCONCATENATE;
-      // To simplify management of indices
-      for (Standard_Integer i = 0; i <= 3; ++i)
-      {
-        for (Standard_Integer j = 0; j <= 3; ++j)
-        {
-          aMatrix44 (i, j) = theMatrix (lr + i, lc + j);
-        }
-      }
-
-      // Calculation of the product of matrices
-      for (Standard_Integer i = 0; i <= 3; ++i)
-      {
-        for (Standard_Integer j = 0; j <= 3; ++j)
-        {
-          aNewTrsf (i, j) = 0.0;
-          for (Standard_Integer k = 0; k <= 3; ++k)
-          {
-            valueoldtrsf = myCStructure->Transformation[i][k];
-            valuetrsf    = aMatrix44 (k, j);
-            valuenewtrsf = aNewTrsf (i, j) + valueoldtrsf * valuetrsf;
-            aNewTrsf (i, j) = valuenewtrsf;
-          }
-        }
-      }
-
-      // Update of CStructure
-      for (Standard_Integer i = 0; i <= 3; ++i)
-      {
-        for (Standard_Integer j = 0; j <= 3; ++j)
-        {
-          myCStructure->Transformation[i][j] = float (aNewTrsf (i, j));
-        }
-      }
-      break;
-    }
+    myCStructure->SetTransformation (theTrsf);
   }
 
   // If transformation, no validation of hidden already calculated parts
-  if (IsRotated())
+  if (IsTransformed() || (!IsTransformed() && wasTransformed))
   {
     ReCompute();
   }
 
-  GraphicTransform (aNewTrsf);
-  myStructureManager->SetTransform (this, aNewTrsf);
+  myStructureManager->SetTransform (this, theTrsf);
 
-  Update();
+  Update (true);
 }
 
 //=============================================================================
-//function : Transform
+//function : MinMaxValues
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::Transform (TColStd_Array2OfReal& theMatrix) const
+Bnd_Box Graphic3d_Structure::MinMaxValues (const Standard_Boolean theToIgnoreInfiniteFlag) const
 {
+  Graphic3d_BndBox3d aBox;
+  addTransformed (aBox, theToIgnoreInfiniteFlag);
+  if (!aBox.IsValid())
+  {
+    return Bnd_Box();
+  }
 
-  Standard_Integer lr = theMatrix.LowerRow ();
-  Standard_Integer ur = theMatrix.UpperRow ();
-  Standard_Integer lc = theMatrix.LowerCol ();
-  Standard_Integer uc = theMatrix.UpperCol ();
-
-  if ((ur - lr + 1 != 4) || (uc - lc + 1 != 4))
-    Graphic3d_TransformError::Raise ("Transform : not a 4x4 matrix");
+  Bnd_Box aResult;
+  aResult.Update (aBox.CornerMin().x(), aBox.CornerMin().y(), aBox.CornerMin().z(),
+                  aBox.CornerMax().x(), aBox.CornerMax().y(), aBox.CornerMax().z());
 
-  for (Standard_Integer i = 0; i <= 3; ++i)
+  Standard_Real aLimMin = ShortRealFirst() + 1.0;
+  Standard_Real aLimMax = ShortRealLast()  - 1.0;
+  gp_Pnt aMin = aResult.CornerMin();
+  gp_Pnt aMax = aResult.CornerMax();
+  if (aMin.X() < aLimMin && aMin.Y() < aLimMin && aMin.Z() < aLimMin
+   && aMax.X() > aLimMax && aMax.Y() > aLimMax && aMax.Z() > aLimMax)
   {
-    for (Standard_Integer j = 0; j <= 3; ++j)
-    {
-      theMatrix (lr + i, lc + j) = myCStructure->Transformation[i][j];
-    }
+    //For structure which infinite in all three dimensions the Whole bounding box will be returned
+    aResult.SetWhole();
   }
+  return aResult;
 }
 
 //=============================================================================
-//function : MinMaxValues
+//function : SetTransformPersistence
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::MinMaxValues (Standard_Real& theXMin,
-                                        Standard_Real& theYMin,
-                                        Standard_Real& theZMin,
-                                        Standard_Real& theXMax,
-                                        Standard_Real& theYMax,
-                                        Standard_Real& theZMax,
-                                        const Standard_Boolean theToIgnoreInfiniteFlag) const
+void Graphic3d_Structure::SetTransformPersistence (const Handle(Graphic3d_TransformPers)& theTrsfPers)
 {
-  if (IsEmpty())
-  {
-    return;
-  }
-
-  Standard_Real aXMin, aYMin, aZMin, aXMax, aYMax, aZMax;
-  MinMaxCoord (aXMin, aYMin, aZMin, aXMax, aYMax, aZMax);
-
-  // Infinite boundaries corresponding to empty structure or
-  // non-empty structure, without any primitives specified
-  if (aXMin == RealFirst() && aYMin == RealFirst() && aZMin == RealFirst() &&
-      aXMax == RealLast()  && aYMax == RealLast()  && aZMax == RealLast())
-  {
-    theXMin = RealFirst();
-    theYMin = RealFirst();
-    theZMin = RealFirst();
-    theXMax = RealLast();
-    theYMax = RealLast();
-    theZMax = RealLast();
-    return;
-  }
-
-  // Handle flag, which specifies that structure should be considered as infinite
-  if (IsInfinite() && !theToIgnoreInfiniteFlag)
-  {
-    Graphic3d_Vertex aVertexMin (aXMin, aYMin, aZMin);
-    Graphic3d_Vertex aVertexMax (aXMax, aYMax, aZMax);
-    const Standard_Real aDistance = aVertexMin.Distance (aVertexMax);
-
-    // Special case for infinite line:
-    // Bounding borders of infinite line has been
-    // calculated as own point in center of this line
-    if (aDistance >= 500000.0)
-    {
-      theXMin = theXMax = 0.5 * (aXMin + aXMax);
-      theYMin = theYMax = 0.5 * (aYMin + aYMax);
-      theZMin = theZMax = 0.5 * (aZMin + aZMax);
-      return;
-    }
-
-    theXMin = RealFirst();
-    theYMin = RealFirst();
-    theZMin = RealFirst();
-    theXMax = RealLast();
-    theYMax = RealLast();
-    theZMax = RealLast();
-    return;
-  }
-
-  // Min-Max values of the descendant structures
-  Standard_Real aDescXMin = RealLast();
-  Standard_Real aDescYMin = RealLast();
-  Standard_Real aDescZMin = RealLast();
-  Standard_Real aDescXMax = RealFirst();
-  Standard_Real aDescYMax = RealFirst();
-  Standard_Real aDescZMax = RealFirst();
-  for (Standard_Integer aStructIt = 1; aStructIt <= myDescendants.Length(); aStructIt++)
-  {
-    Graphic3d_Structure* aStructure = (Graphic3d_Structure*) myDescendants.Value (aStructIt);
-    aStructure->MinMaxValues (aXMin, aYMin, aZMin, aXMax, aYMax, aZMax);
-    aDescXMin = Min (aXMin, aDescXMin);
-    aDescYMin = Min (aYMin, aDescYMin);
-    aDescZMin = Min (aZMin, aDescZMin);
-    aDescXMax = Max (aXMax, aDescXMax);
-    aDescYMax = Max (aYMax, aDescYMax);
-    aDescZMax = Max (aZMax, aDescZMax);
-  }
-
-  if (aDescXMin != RealLast()  || aDescYMin != RealLast()  ||
-      aDescZMin != RealLast()  || aDescXMax != RealFirst() ||
-      aDescYMax != RealFirst() || aDescZMax != RealFirst())
-  {
-    aXMin = Min (aDescXMin, aXMin);
-    aYMin = Min (aDescYMin, aYMin);
-    aZMin = Min (aDescZMin, aZMin);
-    aXMax = Max (aDescXMax, aXMax);
-    aYMax = Max (aDescYMax, aYMax);
-    aZMax = Max (aDescZMax, aZMax);
-  }
-
-  // Case impossible as it would mean that the structure is empty or infinite
-  if (aXMin == RealFirst() && aYMin == RealFirst() && aZMin == RealFirst() &&
-      aXMax == RealLast()  && aYMax == RealLast()  && aZMax == RealLast())
+  if (IsDeleted())
   {
-    theXMin = RealFirst();
-    theYMin = RealFirst();
-    theZMin = RealFirst();
-    theXMax = RealLast();
-    theYMax = RealLast();
-    theZMax = RealLast();
     return;
   }
 
-  TColStd_Array2OfReal aTrsf(0, 3, 0, 3);
-  Transform (aTrsf);
-  TransformBoundaries (aTrsf, aXMin, aYMin, aZMin, aXMax, aYMax, aZMax);
-  theXMin = aXMin;
-  theYMin = aYMin;
-  theZMin = aZMin;
-  theXMax = aXMax;
-  theYMax = aYMax;
-  theZMax = aZMax;
+  myCStructure->SetTransformPersistence (theTrsfPers);
 }
 
 //=============================================================================
-//function : Identification
+//function : Remove
 //purpose  :
 //=============================================================================
-Standard_Integer Graphic3d_Structure::Identification() const
+void Graphic3d_Structure::Remove (Graphic3d_Structure* thePtr,
+                                  const Graphic3d_TypeOfConnection theType)
 {
-  return myCStructure->Id;
+  if (theType == Graphic3d_TOC_DESCENDANT)
+  {
+    RemoveDescendant (thePtr);
+  }
+  else
+  {
+    RemoveAncestor (thePtr);
+  }
 }
 
 //=============================================================================
-//function : SetTransformPersistence
+//function : NewGroup
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::SetTransformPersistence (const Graphic3d_TransModeFlags& theFlag)
+Handle(Graphic3d_Group) Graphic3d_Structure::NewGroup()
 {
-  SetTransformPersistence (theFlag, gp_Pnt (0.0, 0.0, 0.0));
+  return myCStructure->NewGroup (this);
 }
 
 //=============================================================================
-//function : SetTransformPersistence
+//function : Remove
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::SetTransformPersistence (const Graphic3d_TransModeFlags& theFlag,
-                                                   const gp_Pnt&                   thePoint)
+void Graphic3d_Structure::Remove (const Handle(Graphic3d_Group)& theGroup)
 {
-  if (IsDeleted()) return;
-
-  myCStructure->TransformPersistence.Flag    = theFlag;
-  myCStructure->TransformPersistence.Point.x = float (thePoint.X());
-  myCStructure->TransformPersistence.Point.y = float (thePoint.Y());
-  myCStructure->TransformPersistence.Point.z = float (thePoint.Z());
-  myCStructure->UpdateAspects();
-
-  myCStructure->TransformPersistence.IsSet = 1;
-}
+  if (theGroup.IsNull()
+   || theGroup->myStructure != this)
+  {
+    return;
+  }
 
-//=============================================================================
-//function : TransformPersistenceMode
-//purpose  :
-//=============================================================================
-Graphic3d_TransModeFlags Graphic3d_Structure::TransformPersistenceMode() const
-{
-  return myCStructure->TransformPersistence.Flag;
+  myCStructure->RemoveGroup (theGroup);
+  theGroup->myStructure = NULL;
 }
 
 //=============================================================================
-//function : TransformPersistencePoint
+//function : StructureManager
 //purpose  :
 //=============================================================================
-gp_Pnt Graphic3d_Structure::TransformPersistencePoint() const
+Handle(Graphic3d_StructureManager) Graphic3d_Structure::StructureManager() const
 {
-  gp_Pnt aPnt (0.0, 0.0, 0.0);
-  aPnt.SetX (myCStructure->TransformPersistence.Point.x);
-  aPnt.SetY (myCStructure->TransformPersistence.Point.y);
-  aPnt.SetZ (myCStructure->TransformPersistence.Point.z);
-  return aPnt;
+  return myStructureManager;
 }
 
 //=============================================================================
-//function : Add
+//function : minMaxCoord
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::Add (const Handle(Graphic3d_Group)& theGroup)
+Graphic3d_BndBox4f Graphic3d_Structure::minMaxCoord() const
 {
-  // Method called only by the constructor of Graphic3d_Group
-  // It is easy to check presence of <theGroup> in sequence myGroups.
-  myGroups.Append (theGroup);
+  Graphic3d_BndBox4f aBnd;
+  for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (myCStructure->Groups()); aGroupIter.More(); aGroupIter.Next())
+  {
+    aBnd.Combine (aGroupIter.Value()->BoundingBox());
+  }
+  return aBnd;
 }
 
 //=============================================================================
-//function : Remove
+//function : addTransformed
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::Remove (const Standard_Address           thePtr,
-                                  const Graphic3d_TypeOfConnection theType)
+void Graphic3d_Structure::getBox (Graphic3d_BndBox3d&    theBox,
+                                  const Standard_Boolean theToIgnoreInfiniteFlag) const
 {
-  switch (theType)
+  Graphic3d_BndBox4f aBoxF = minMaxCoord();
+  if (aBoxF.IsValid())
   {
-    case Graphic3d_TOC_DESCENDANT:
+    theBox = Graphic3d_BndBox3d (Graphic3d_Vec3d ((Standard_Real )aBoxF.CornerMin().x(),
+                                                  (Standard_Real )aBoxF.CornerMin().y(),
+                                                  (Standard_Real )aBoxF.CornerMin().z()),
+                                 Graphic3d_Vec3d ((Standard_Real )aBoxF.CornerMax().x(),
+                                                  (Standard_Real )aBoxF.CornerMax().y(),
+                                                  (Standard_Real )aBoxF.CornerMax().z()));
+    if (IsInfinite()
+    && !theToIgnoreInfiniteFlag)
     {
-      const Standard_Integer aNbDesc = myDescendants.Length();
-      for (Standard_Integer anIter = 1; anIter <= aNbDesc; ++anIter)
+      const Graphic3d_Vec3d aDiagVec = theBox.CornerMax() - theBox.CornerMin();
+      if (aDiagVec.SquareModulus() >= 500000.0 * 500000.0)
       {
-        if (myDescendants.Value (anIter) == thePtr)
-        {
-          myDescendants.Remove (anIter);
-          return;
-        }
+        // bounding borders of infinite line has been calculated as own point in center of this line
+        theBox = Graphic3d_BndBox3d ((theBox.CornerMin() + theBox.CornerMax()) * 0.5);
       }
-      break;
-    }
-    case Graphic3d_TOC_ANCESTOR:
-    {
-      const Standard_Integer aNbAncestors = myAncestors.Length();
-      for (Standard_Integer anIter = 1; anIter <= aNbAncestors; ++anIter)
+      else
       {
-        if (myAncestors.Value (anIter) == thePtr)
-        {
-          myAncestors.Remove (anIter);
-          return;
-        }
+        theBox = Graphic3d_BndBox3d (Graphic3d_Vec3d (RealFirst(), RealFirst(), RealFirst()),
+                                     Graphic3d_Vec3d (RealLast(),  RealLast(),  RealLast()));
+        return;
       }
-      break;
     }
   }
 }
 
 //=============================================================================
-//function : Remove
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::Remove (const Handle(Graphic3d_Group)& theGroup)
-{
-  const Standard_Integer aNbGroups = myGroups.Length();
-  for (Standard_Integer aGrpIter = 1; aGrpIter <= aNbGroups; ++aGrpIter)
-  {
-    if (myGroups.Value (aGrpIter) == theGroup)
-    {
-      myGroups.Remove (aGrpIter);
-      return;
-    }
-  }
-}
-
-//=============================================================================
-//function : StructureManager
+//function : addTransformed
 //purpose  :
 //=============================================================================
-Handle(Graphic3d_StructureManager) Graphic3d_Structure::StructureManager() const
+void Graphic3d_Structure::addTransformed (Graphic3d_BndBox3d&    theBox,
+                                          const Standard_Boolean theToIgnoreInfiniteFlag) const
 {
-  return myStructureManager;
-}
+  Graphic3d_BndBox3d aCombinedBox, aBox;
+  getBox (aCombinedBox, theToIgnoreInfiniteFlag);
 
-//=============================================================================
-//function : MinMaxCoord
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::MinMaxCoord (Standard_Real& theXMin,
-                                       Standard_Real& theYMin,
-                                       Standard_Real& theZMin,
-                                       Standard_Real& theXMax,
-                                       Standard_Real& theYMax,
-                                       Standard_Real& theZMax) const
-{
-  if (IsEmpty())
+  for (NCollection_IndexedMap<Graphic3d_Structure*>::Iterator anIter (myDescendants); anIter.More(); anIter.Next())
   {
-    theXMin = RealFirst();
-    theYMin = RealFirst();
-    theZMin = RealFirst();
-    theXMax = RealLast();
-    theYMax = RealLast();
-    theZMax = RealLast();
-    return;
+    const Graphic3d_Structure* aStruct = anIter.Value();
+    aStruct->getBox (aBox, theToIgnoreInfiniteFlag);
+    aCombinedBox.Combine (aBox);
   }
 
-  Standard_Real aXMin = RealLast();
-  Standard_Real aYMin = RealLast();
-  Standard_Real aZMin = RealLast();
-  Standard_Real aXMax = RealFirst();
-  Standard_Real aYMax = RealFirst();
-  Standard_Real aZMax = RealFirst();
-  Standard_Real aGroupXMin, aGroupYMin, aGroupZMin, aGroupXMax, aGroupYMax, aGroupZMax;
-  for (Standard_Integer aGroupIt = 1; aGroupIt <= myGroups.Length(); aGroupIt++)
+  aBox = aCombinedBox;
+  if (aBox.IsValid())
   {
-    const Handle(Graphic3d_Group)& aGroup = myGroups.Value (aGroupIt);
-
-    if (aGroup->IsEmpty())
+    if (!myCStructure->Transformation().IsNull())
     {
-      continue;
+      TransformBoundaries (myCStructure->Transformation()->Trsf(),
+                           aBox.CornerMin().x(), aBox.CornerMin().y(), aBox.CornerMin().z(),
+                           aBox.CornerMax().x(), aBox.CornerMax().y(), aBox.CornerMax().z());
     }
 
-    aGroup->MinMaxValues (aGroupXMin, aGroupYMin, aGroupZMin, aGroupXMax, aGroupYMax, aGroupZMax);
-    aXMin = Min (aXMin, aGroupXMin);
-    aYMin = Min (aYMin, aGroupYMin);
-    aZMin = Min (aZMin, aGroupZMin);
-    aXMax = Max (aXMax, aGroupXMax);
-    aYMax = Max (aYMax, aGroupYMax);
-    aZMax = Max (aZMax, aGroupZMax);
-  }
-
-  // Case impossible as it would mean that the structure is empty
-  if (aXMin == RealLast()  && aYMin == RealLast()  && aZMin == RealLast() &&
-      aXMax == RealFirst() && aYMax == RealFirst() && aZMax == RealFirst())
-  {
-    theXMin = RealFirst();
-    theYMin = RealFirst();
-    theZMin = RealFirst();
-    theXMax = RealLast();
-    theYMax = RealLast();
-    theZMax = RealLast();
-  }
-
-  theXMin = aXMin;
-  theYMin = aYMin;
-  theZMin = aZMin;
-  theXMax = aXMax;
-  theYMax = aYMax;
-  theZMax = aZMax;
-}
-
-//=============================================================================
-//function : MinMaxCoordWithDescendants
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::MinMaxCoordWithDescendants (Standard_Real& theXMin,
-                                                      Standard_Real& theYMin,
-                                                      Standard_Real& theZMin,
-                                                      Standard_Real& theXMax,
-                                                      Standard_Real& theYMax,
-                                                      Standard_Real& theZMax) const
-{
-  if (IsEmpty())
-  {
-    theXMin = RealFirst();
-    theYMin = RealFirst();
-    theZMin = RealFirst();
-    theXMax = RealLast();
-    theYMax = RealLast();
-    theZMax = RealLast();
-    return;
-  }
-
-  Standard_Real aXMin, aYMin, aZMin, aXMax, aYMax, aZMax;
-  MinMaxCoord (aXMin, aYMin, aZMin, aXMax, aYMax, aZMax);
-
-  // Min-Max of the descendant structures
-  Standard_Real aDescXMin = RealLast();
-  Standard_Real aDescYMin = RealLast();
-  Standard_Real aDescZMin = RealLast();
-  Standard_Real aDescXMax = RealFirst();
-  Standard_Real aDescYMax = RealFirst();
-  Standard_Real aDescZMax = RealFirst();
-  for (Standard_Integer aStructIt = 1; aStructIt <= myDescendants.Length(); aStructIt++)
-  {
-    Graphic3d_Structure* aStructure = (Graphic3d_Structure*) myDescendants.Value (aStructIt);
-    if (aStructure->IsEmpty())
+    // if box is still valid after transformation
+    if (aBox.IsValid())
     {
-      continue;
+      theBox.Combine (aBox);
+    }
+    else // it was infinite, return untransformed
+    {
+      theBox.Combine (aCombinedBox);
     }
-
-    aStructure->MinMaxCoordWithDescendants (aXMin, aYMin, aZMin, aXMax, aYMax, aZMax);
-    aDescXMin = Min (aXMin, aDescXMin);
-    aDescYMin = Min (aYMin, aDescYMin);
-    aDescZMin = Min (aZMin, aDescZMin);
-    aDescXMax = Max (aXMax, aDescXMax);
-    aDescYMax = Max (aYMax, aDescYMax);
-    aDescZMax = Max (aZMax, aDescZMax);
-  }
-
-  if (aDescXMin != RealLast()  || aDescYMin != RealLast()  ||
-      aDescZMin != RealLast()  || aDescXMax != RealFirst() ||
-      aDescYMax != RealFirst() || aDescZMax != RealFirst())
-  {
-    TColStd_Array2OfReal aTrsf(0, 3, 0, 3);
-    Transform (aTrsf);
-    TransformBoundaries (aTrsf, aDescXMin, aDescYMin, aDescZMin, aDescXMax, aDescYMax, aDescZMax);
-
-    aXMin = Min (aDescXMin, aXMin);
-    aYMin = Min (aDescYMin, aYMin);
-    aZMin = Min (aDescZMin, aZMin);
-    aXMax = Max (aDescXMax, aXMax);
-    aYMax = Max (aDescYMax, aYMax);
-    aZMax = Max (aDescZMax, aZMax);
-  }
-
-  // Case impossible as it would mean that the structure is empty
-  if (aXMin == RealLast()  && aYMin == RealLast()  && aZMin == RealLast() &&
-      aXMax == RealFirst() && aYMax == RealFirst() && aZMax == RealFirst())
-  {
-    theXMin = RealFirst();
-    theYMin = RealFirst();
-    theZMin = RealFirst();
-    theXMax = RealLast();
-    theYMax = RealLast();
-    theZMax = RealLast();
   }
-
-  theXMin = aXMin;
-  theYMin = aYMin;
-  theZMin = aZMin;
-  theXMax = aXMax;
-  theYMax = aYMax;
-  theZMax = aZMax;
 }
 
 //=============================================================================
 //function : Transforms
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::Transforms (const TColStd_Array2OfReal& theTrsf,
+void Graphic3d_Structure::Transforms (const gp_Trsf& theTrsf,
                                       const Standard_Real theX,    const Standard_Real theY,    const Standard_Real theZ,
                                       Standard_Real&      theNewX, Standard_Real&      theNewY, Standard_Real&      theNewZ)
 {
   const Standard_Real aRL = RealLast();
   const Standard_Real aRF = RealFirst();
+  theNewX = theX;
+  theNewY = theY;
+  theNewZ = theZ;
   if ((theX == aRF) || (theY == aRF) || (theZ == aRF)
    || (theX == aRL) || (theY == aRL) || (theZ == aRL))
   {
-    theNewX = theX;
-    theNewY = theY;
-    theNewZ = theZ;
-  }
-  else
-  {
-    Standard_Real A, B, C, D;
-    A       = theTrsf (0, 0);
-    B       = theTrsf (0, 1);
-    C       = theTrsf (0, 2);
-    D       = theTrsf (0, 3);
-    theNewX = A * theX + B * theY + C * theZ + D;
-    A       = theTrsf (1, 0);
-    B       = theTrsf (1, 1);
-    C       = theTrsf (1, 2);
-    D       = theTrsf (1, 3);
-    theNewY = A * theX + B * theY + C * theZ + D;
-    A       = theTrsf (2, 0);
-    B       = theTrsf (2, 1);
-    C       = theTrsf (2, 2);
-    D       = theTrsf (2, 3);
-    theNewZ = A * theX + B * theY + C * theZ + D;
+    return;
   }
-}
 
-//=============================================================================
-//function : Transforms
-//purpose  :
-//=============================================================================
-Graphic3d_Vector Graphic3d_Structure::Transforms (const TColStd_Array2OfReal& theTrsf,
-                                                  const Graphic3d_Vector&     theCoord)
-{
-  Standard_Real anXYZ[3];
-  Graphic3d_Structure::Transforms (theTrsf,
-                                   theCoord.X(), theCoord.Y(), theCoord.Z(),
-                                   anXYZ[0], anXYZ[1], anXYZ[2]);
-  return Graphic3d_Vector (anXYZ[0], anXYZ[1], anXYZ[2]);
+  theTrsf.Transforms (theNewX, theNewY, theNewZ);
 }
 
 //=============================================================================
 //function : Transforms
 //purpose  :
 //=============================================================================
-Graphic3d_Vertex Graphic3d_Structure::Transforms (const TColStd_Array2OfReal& theTrsf,
-                                                  const Graphic3d_Vertex&     theCoord)
-{
-  Standard_Real anXYZ[3];
-  Graphic3d_Structure::Transforms (theTrsf,
-                                   theCoord.X(), theCoord.Y(), theCoord.Z(),
-                                   anXYZ[0], anXYZ[1], anXYZ[2]);
-  return Graphic3d_Vertex (anXYZ[0], anXYZ[1], anXYZ[2]);
-}
-
-//=============================================================================
-//function : Transforms
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::TransformBoundaries (const TColStd_Array2OfReal& theTrsf,
+void Graphic3d_Structure::TransformBoundaries (const gp_Trsf& theTrsf,
                                                Standard_Real& theXMin,
                                                Standard_Real& theYMin,
                                                Standard_Real& theZMin,
@@ -2134,28 +963,29 @@ void Graphic3d_Structure::TransformBoundaries (const TColStd_Array2OfReal& theTr
 //function : Network
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::Network (const Handle(Graphic3d_Structure)& theStructure,
-                                   const Graphic3d_TypeOfConnection   theType,
-                                   Graphic3d_MapOfStructure&          theSet)
+void Graphic3d_Structure::Network (Graphic3d_Structure* theStructure,
+                                   const Graphic3d_TypeOfConnection theType,
+                                   NCollection_Map<Graphic3d_Structure*>& theSet)
 {
-  Graphic3d_MapOfStructure aSetD, aSetA;
-  theStructure->Descendants (aSetD);
-  theStructure->Ancestors   (aSetA);
   theSet.Add (theStructure);
   switch (theType)
   {
     case Graphic3d_TOC_DESCENDANT:
-      for (Graphic3d_MapIteratorOfMapOfStructure anIter (aSetD); anIter.More(); anIter.Next())
+    {
+      for (NCollection_IndexedMap<Graphic3d_Structure*>::Iterator anIter (theStructure->myDescendants); anIter.More(); anIter.Next())
       {
-        Graphic3d_Structure::Network (anIter.Key(), theType, theSet);
+        Graphic3d_Structure::Network (anIter.Value(), theType, theSet);
       }
       break;
+    }
     case Graphic3d_TOC_ANCESTOR:
-      for (Graphic3d_MapIteratorOfMapOfStructure anIter (aSetA); anIter.More(); anIter.Next())
+    {
+      for (NCollection_IndexedMap<Graphic3d_Structure*>::Iterator anIter (theStructure->myAncestors); anIter.More(); anIter.Next())
       {
-        Graphic3d_Structure::Network (anIter.Key (), theType, theSet);
+        Graphic3d_Structure::Network (anIter.Value(), theType, theSet);
       }
       break;
+    }
   }
 }
 
@@ -2166,9 +996,9 @@ void Graphic3d_Structure::Network (const Handle(Graphic3d_Structure)& theStructu
 void Graphic3d_Structure::PrintNetwork (const Handle(Graphic3d_Structure)& theStructure,
                                         const Graphic3d_TypeOfConnection   theType)
 {
-  Graphic3d_MapOfStructure aSet;
-  Graphic3d_Structure::Network (theStructure, theType, aSet);
-  for (Graphic3d_MapIteratorOfMapOfStructure anIter (aSet); anIter.More(); anIter.Next())
+  NCollection_Map<Graphic3d_Structure*> aSet;
+  Graphic3d_Structure::Network (theStructure.get(), theType, aSet);
+  for (NCollection_Map<Graphic3d_Structure*>::Iterator anIter (aSet); anIter.More(); anIter.Next())
   {
     std::cout << "\tIdent " << (anIter.Key())->Identification () << "\n";
   }
@@ -2179,377 +1009,40 @@ void Graphic3d_Structure::PrintNetwork (const Handle(Graphic3d_Structure)& theSt
 //function : Update
 //purpose  :
 //=============================================================================
-void Graphic3d_Structure::Update() const
+void Graphic3d_Structure::Update (const bool theUpdateLayer) const
 {
   if (IsDeleted())
   {
     return;
   }
 
-  if (myStructureManager->UpdateMode() == Aspect_TOU_ASAP)
-  {
-    myStructureManager->Update();
-  }
-}
-
-//=============================================================================
-//function : UpdateStructure
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::UpdateStructure (const Handle(Graphic3d_AspectLine3d)&     theAspLine,
-                                           const Handle(Graphic3d_AspectText3d)&     theAspText,
-                                           const Handle(Graphic3d_AspectMarker3d)&   theAspMarker,
-                                           const Handle(Graphic3d_AspectFillArea3d)& theAspFill)
-{
-  Standard_CString          aFont;
-  Standard_Real             aSpace, anExpansion, aWidth, aScale;
-  Quantity_Color            aColor, anIntColor, aBackIntColor, anEdgeColor, aColorSub;
-  Aspect_TypeOfLine         aLType;
-  Aspect_TypeOfMarker       aMType;
-  Aspect_InteriorStyle      aStyle;
-  Aspect_TypeOfStyleText    aStyleT;
-  Aspect_TypeOfDisplayText  aDisplayType;
-  Standard_Boolean          aTextZoomable;
-  Standard_Real             aTextAngle;
-  Font_FontAspect           aTextFontAspect;
-
-  theAspLine->Values (aColor, aLType, aWidth);
-  myCStructure->ContextLine.Color.r        = float (aColor.Red());
-  myCStructure->ContextLine.Color.g        = float (aColor.Green());
-  myCStructure->ContextLine.Color.b        = float (aColor.Blue());
-  myCStructure->ContextLine.LineType       = aLType;
-  myCStructure->ContextLine.Width          = float (aWidth);
-  myCStructure->ContextLine.ShaderProgram  = theAspLine->ShaderProgram();
-
-  theAspMarker->Values (aColor, aMType, aScale);
-  myCStructure->ContextMarker.Color.r      = float (aColor.Red());
-  myCStructure->ContextMarker.Color.g      = float (aColor.Green());
-  myCStructure->ContextMarker.Color.b      = float (aColor.Blue());
-  myCStructure->ContextMarker.MarkerType   = aMType;
-  myCStructure->ContextMarker.Scale        = float (aScale);
-  myCStructure->ContextMarker.ShaderProgram = theAspMarker->ShaderProgram();
-
-  theAspText->Values (aColor, aFont, anExpansion, aSpace, aStyleT, aDisplayType, aColorSub, aTextZoomable, aTextAngle, aTextFontAspect);
-  myCStructure->ContextText.Color.r          = float (aColor.Red());
-  myCStructure->ContextText.Color.g          = float (aColor.Green());
-  myCStructure->ContextText.Color.b          = float (aColor.Blue());
-  myCStructure->ContextText.Font             = aFont;
-  myCStructure->ContextText.Expan            = float (anExpansion);
-  myCStructure->ContextText.Style            = aStyleT;
-  myCStructure->ContextText.DisplayType      = aDisplayType;
-  myCStructure->ContextText.Space            = float (aSpace);
-  myCStructure->ContextText.ColorSubTitle.r  = float (aColorSub.Red());
-  myCStructure->ContextText.ColorSubTitle.g  = float (aColorSub.Green());
-  myCStructure->ContextText.ColorSubTitle.b  = float (aColorSub.Blue());
-  myCStructure->ContextText.TextZoomable     = aTextZoomable;
-  myCStructure->ContextText.TextAngle        = float (aTextAngle);
-  myCStructure->ContextText.TextFontAspect   = aTextFontAspect;
-  myCStructure->ContextText.ShaderProgram    = theAspText->ShaderProgram();
-
-  Standard_Real anRGB[3];
-  theAspFill->Values (aStyle, anIntColor, aBackIntColor, anEdgeColor, aLType, aWidth);
-  anIntColor.Values (anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB);
-  myCStructure->ContextFillArea.Style      = aStyle;
-  myCStructure->ContextFillArea.IntColor.r = float (anRGB[0]);
-  myCStructure->ContextFillArea.IntColor.g = float (anRGB[1]);
-  myCStructure->ContextFillArea.IntColor.b = float (anRGB[2]);
-
-  if (theAspFill->Distinguish())
-  {
-    aBackIntColor.Values (anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB);
-  }
-  myCStructure->ContextFillArea.BackIntColor.r = float (anRGB[0]);
-  myCStructure->ContextFillArea.BackIntColor.g = float (anRGB[1]);
-  myCStructure->ContextFillArea.BackIntColor.b = float (anRGB[2]);
-
-  // Edges
-  myCStructure->ContextFillArea.Edge               = theAspFill->Edge () ? 1:0;
-  myCStructure->ContextFillArea.EdgeColor.r        = float (anEdgeColor.Red());
-  myCStructure->ContextFillArea.EdgeColor.g        = float (anEdgeColor.Green());
-  myCStructure->ContextFillArea.EdgeColor.b        = float (anEdgeColor.Blue());
-  myCStructure->ContextFillArea.LineType           = aLType;
-  myCStructure->ContextFillArea.Width              = float (aWidth);
-  myCStructure->ContextFillArea.Hatch              = theAspFill->HatchStyle();
-
-  // Front and Back face
-  myCStructure->ContextFillArea.Distinguish        = theAspFill->Distinguish() ? 1 : 0;
-  myCStructure->ContextFillArea.BackFace           = theAspFill->BackFace()    ? 1 : 0;
-  // Back Material
-  const Graphic3d_MaterialAspect& aBack = theAspFill->BackMaterial();
-  // Light specificity
-  myCStructure->ContextFillArea.Back.Shininess     = float (aBack.Shininess());
-  myCStructure->ContextFillArea.Back.Ambient       = float (aBack.Ambient());
-  myCStructure->ContextFillArea.Back.Diffuse       = float (aBack.Diffuse());
-  myCStructure->ContextFillArea.Back.Specular      = float (aBack.Specular());
-  myCStructure->ContextFillArea.Back.Transparency  = float (aBack.Transparency());
-  myCStructure->ContextFillArea.Back.Emission      = float (aBack.Emissive());
-
-  // Reflection mode
-  myCStructure->ContextFillArea.Back.IsAmbient     = (aBack.ReflectionMode (Graphic3d_TOR_AMBIENT)  ? 1 : 0);
-  myCStructure->ContextFillArea.Back.IsDiffuse     = (aBack.ReflectionMode (Graphic3d_TOR_DIFFUSE)  ? 1 : 0);
-  myCStructure->ContextFillArea.Back.IsSpecular    = (aBack.ReflectionMode (Graphic3d_TOR_SPECULAR) ? 1 : 0);
-  myCStructure->ContextFillArea.Back.IsEmission    = (aBack.ReflectionMode (Graphic3d_TOR_EMISSION) ? 1 : 0);
-
-  // Material type
-  myCStructure->ContextFillArea.Back.IsPhysic      = (aBack.MaterialType (Graphic3d_MATERIAL_PHYSIC) ? 1 : 0);
-
-  // Specular color
-  myCStructure->ContextFillArea.Back.ColorSpec.r   = float (aBack.SpecularColor().Red());
-  myCStructure->ContextFillArea.Back.ColorSpec.g   = float (aBack.SpecularColor().Green());
-  myCStructure->ContextFillArea.Back.ColorSpec.b   = float (aBack.SpecularColor().Blue());
-
-  // Ambient color
-  myCStructure->ContextFillArea.Back.ColorAmb.r    = float (aBack.AmbientColor().Red());
-  myCStructure->ContextFillArea.Back.ColorAmb.g    = float (aBack.AmbientColor().Green());
-  myCStructure->ContextFillArea.Back.ColorAmb.b    = float (aBack.AmbientColor().Blue());
-
-  // Diffuse color
-  myCStructure->ContextFillArea.Back.ColorDif.r    = float (aBack.DiffuseColor().Red());
-  myCStructure->ContextFillArea.Back.ColorDif.g    = float (aBack.DiffuseColor().Green());
-  myCStructure->ContextFillArea.Back.ColorDif.b    = float (aBack.DiffuseColor().Blue());
-
-  // Emissive color
-  myCStructure->ContextFillArea.Back.ColorEms.r    = float (aBack.EmissiveColor().Red());
-  myCStructure->ContextFillArea.Back.ColorEms.g    = float (aBack.EmissiveColor().Green());
-  myCStructure->ContextFillArea.Back.ColorEms.b    = float (aBack.EmissiveColor().Blue());
-
-  myCStructure->ContextFillArea.Back.EnvReflexion  = float (aBack.EnvReflexion());
-
-  // Front Material
-  const Graphic3d_MaterialAspect& aFront = theAspFill->FrontMaterial();
-  // Light specificity
-  myCStructure->ContextFillArea.Front.Shininess    = float (aFront.Shininess());
-  myCStructure->ContextFillArea.Front.Ambient      = float (aFront.Ambient());
-  myCStructure->ContextFillArea.Front.Diffuse      = float (aFront.Diffuse());
-  myCStructure->ContextFillArea.Front.Specular     = float (aFront.Specular());
-  myCStructure->ContextFillArea.Front.Transparency = float (aFront.Transparency());
-  myCStructure->ContextFillArea.Front.Emission     = float (aFront.Emissive());
-
-  // Reflection mode
-  myCStructure->ContextFillArea.Front.IsAmbient    = (aFront.ReflectionMode (Graphic3d_TOR_AMBIENT)  ? 1 : 0);
-  myCStructure->ContextFillArea.Front.IsDiffuse    = (aFront.ReflectionMode (Graphic3d_TOR_DIFFUSE)  ? 1 : 0);
-  myCStructure->ContextFillArea.Front.IsSpecular   = (aFront.ReflectionMode (Graphic3d_TOR_SPECULAR) ? 1 : 0);
-  myCStructure->ContextFillArea.Front.IsEmission   = (aFront.ReflectionMode (Graphic3d_TOR_EMISSION) ? 1 : 0);
-
-  // Material type
-  myCStructure->ContextFillArea.Front.IsPhysic     = (aFront.MaterialType (Graphic3d_MATERIAL_PHYSIC) ? 1 : 0);
-
-  // Specular color
-  myCStructure->ContextFillArea.Front.ColorSpec.r  = float (aFront.SpecularColor().Red());
-  myCStructure->ContextFillArea.Front.ColorSpec.g  = float (aFront.SpecularColor().Green());
-  myCStructure->ContextFillArea.Front.ColorSpec.b  = float (aFront.SpecularColor().Blue());
-
-  // Ambient color
-  myCStructure->ContextFillArea.Front.ColorAmb.r   = float (aFront.AmbientColor().Red());
-  myCStructure->ContextFillArea.Front.ColorAmb.g   = float (aFront.AmbientColor().Green());
-  myCStructure->ContextFillArea.Front.ColorAmb.b   = float (aFront.AmbientColor().Blue());
-
-  // Diffuse color
-  myCStructure->ContextFillArea.Front.ColorDif.r   = float (aFront.DiffuseColor().Red());
-  myCStructure->ContextFillArea.Front.ColorDif.g   = float (aFront.DiffuseColor().Green());
-  myCStructure->ContextFillArea.Front.ColorDif.b   = float (aFront.DiffuseColor().Blue());
-
-  // Emissive color
-  myCStructure->ContextFillArea.Front.ColorEms.r   = float (aFront.EmissiveColor().Red());
-  myCStructure->ContextFillArea.Front.ColorEms.g   = float (aFront.EmissiveColor().Green());
-  myCStructure->ContextFillArea.Front.ColorEms.b   = float (aFront.EmissiveColor().Blue());
-
-  myCStructure->ContextFillArea.Front.EnvReflexion = float (aFront.EnvReflexion());
-
-  myCStructure->ContextFillArea.Texture.TextureMap   = theAspFill->TextureMap();
-  myCStructure->ContextFillArea.Texture.doTextureMap = theAspFill->TextureMapState() ? 1 : 0;
-  myCStructure->ContextFillArea.ShaderProgram        = theAspFill->ShaderProgram();
-
-  Standard_Integer   aPolyMode;
-  Standard_ShortReal aPolyFactor, aPolyUnits;
-  theAspFill->PolygonOffsets (aPolyMode, aPolyFactor, aPolyUnits);
-  myCStructure->ContextFillArea.PolygonOffsetMode   = aPolyMode;
-  myCStructure->ContextFillArea.PolygonOffsetFactor = aPolyFactor;
-  myCStructure->ContextFillArea.PolygonOffsetUnits  = aPolyUnits;
-}
-
-//=============================================================================
-//function : GraphicHighlight
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::GraphicHighlight (const Aspect_TypeOfHighlightMethod theMethod)
-{
-  Standard_Real anRGB[3];
-  myCStructure->highlight = 1;
-  myHighlightMethod = theMethod;
-  switch (theMethod)
-  {
-    case Aspect_TOHM_COLOR:
-    {
-      myHighlightColor.Values (anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB);
-      myCStructure->HighlightWithColor (Graphic3d_Vec3 (float (anRGB[0]), float (anRGB[1]), float (anRGB[2])), Standard_True);
-      myCStructure->UpdateNamedStatus();
-      break;
-    }
-    case Aspect_TOHM_BOUNDBOX:
-    {
-      Standard_Real XMin, YMin, ZMin, XMax, YMax, ZMax;
-      if (IsEmpty() || IsInfinite())
-      {
-        // Empty or infinite structure
-        XMin = YMin = ZMin = 0.0;
-        XMax = YMax = ZMax = 0.0;
-      }
-      else
-      {
-        MinMaxCoordWithDescendants (XMin, YMin, ZMin, XMax, YMax, ZMax);
-      }
-      myCStructure->BoundBox.Pmin.x  = float (XMin);
-      myCStructure->BoundBox.Pmin.y  = float (YMin);
-      myCStructure->BoundBox.Pmin.z  = float (ZMin);
-      myCStructure->BoundBox.Pmax.x  = float (XMax);
-      myCStructure->BoundBox.Pmax.y  = float (YMax);
-      myCStructure->BoundBox.Pmax.z  = float (ZMax);
-      myHighlightColor.Values (anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB);
-      myCStructure->BoundBox.Color.r = float (anRGB[0]);
-      myCStructure->BoundBox.Color.g = float (anRGB[1]);
-      myCStructure->BoundBox.Color.b = float (anRGB[2]);
-      myCStructure->HighlightWithBndBox (Standard_True);
-      break;
-    }
-  }
-}
-
-//=============================================================================
-//function : GraphicTransform
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::GraphicTransform (const TColStd_Array2OfReal& theMatrix)
-{
-  for (Standard_Integer i = 0; i <= 3; ++i)
-  {
-    for (Standard_Integer j = 0; j <= 3; ++j)
-    {
-      myCStructure->Transformation[i][j] = float (theMatrix (i, j));
-    }
-  }
-  myCStructure->UpdateTransformation();
-}
-
-//=============================================================================
-//function : GraphicUnHighlight
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::GraphicUnHighlight()
-{
-  myCStructure->highlight = 0;
-  switch (myHighlightMethod)
-  {
-    case Aspect_TOHM_COLOR:
-      myCStructure->HighlightWithColor (Graphic3d_Vec3 (0.0f, 0.0f, 0.0f), Standard_False);
-      myCStructure->UpdateNamedStatus();
-      break;
-    case Aspect_TOHM_BOUNDBOX:
-      myCStructure->HighlightWithBndBox (Standard_False);
-      myCStructure->UpdateNamedStatus();
-      break;
-  }
-}
-
-//=============================================================================
-//function : ComputeVisual
-//purpose  :
-//=============================================================================
-Graphic3d_TypeOfStructure Graphic3d_Structure::ComputeVisual() const
-{
-  return myComputeVisual;
-}
-
-//=============================================================================
-//function : SetComputeVisual
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::SetComputeVisual (const Graphic3d_TypeOfStructure theVisual)
-{
-  // The ComputeVisual is saved only if the structure is declared TOS_ALL, TOS_WIREFRAME or TOS_SHADING.
-  // This declaration permits to calculate proper representation of the structure calculated by Compute instead of passage to TOS_COMPUTED.
-  if (theVisual != Graphic3d_TOS_COMPUTED)
-  {
-    myComputeVisual = theVisual;
-  }
-}
-
-//=============================================================================
-//function : Plot
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::Plot (const Handle(Graphic3d_Plotter)& )
-{
-  //
-}
-
-//=============================================================================
-//function : SetHLRValidation
-//purpose  :
-//=============================================================================
-void Graphic3d_Structure::SetHLRValidation (const Standard_Boolean theFlag)
-{
-  myCStructure->HLRValidation = theFlag ? 1 : 0;
-}
-
-//=============================================================================
-//function : HLRValidation
-//purpose  :
-//=============================================================================
-Standard_Boolean Graphic3d_Structure::HLRValidation() const
-{
-  // Hidden parts stored in <me> are valid if :
-  // 1/ the owner is defined.
-  // 2/ they are not invalid.
-  return myOwner != NULL
-      && myCStructure->HLRValidation != 0;
-}
-
-//=======================================================================
-//function : CStructure
-//purpose  :
-//=======================================================================
-const Handle(Graphic3d_CStructure)& Graphic3d_Structure::CStructure() const
-{
-  return myCStructure;
+  myStructureManager->Update (theUpdateLayer ? myCStructure->ZLayer() : Graphic3d_ZLayerId_UNKNOWN);
 }
 
 //=======================================================================
 //function : SetZLayer
 //purpose  :
 //=======================================================================
-void Graphic3d_Structure::SetZLayer (const Standard_Integer theLayerId)
+void Graphic3d_Structure::SetZLayer (const Graphic3d_ZLayerId theLayerId)
 {
   // if the structure is not displayed, unable to change its display layer
   if (IsDeleted ())
     return;
 
   myStructureManager->ChangeZLayer (this, theLayerId);
+  myCStructure->SetZLayer (theLayerId);
 }
 
 //=======================================================================
-//function : GetZLayer
-//purpose  :
+//function : DumpJson
+//purpose  : 
 //=======================================================================
-Standard_Integer Graphic3d_Structure::GetZLayer () const
+void Graphic3d_Structure::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
 {
-  return myStructureManager->GetZLayer (this);
-}
+  OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
 
-//=======================================================================
-//function : SetClipPlanes
-//purpose  :
-//=======================================================================
-void Graphic3d_Structure::SetClipPlanes (const Graphic3d_SequenceOfHClipPlane& thePlanes)
-{
-  myCStructure->SetClipPlanes (thePlanes);
-}
+  OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myCStructure.get())
 
-//=======================================================================
-//function : GetClipPlanes
-//purpose  :
-//=======================================================================
-const Graphic3d_SequenceOfHClipPlane& Graphic3d_Structure::GetClipPlanes() const
-{
-  return myCStructure->ClipPlanes();
+  OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myVisual)
+  OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myComputeVisual)
 }