Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-05 11:14:15

0001 // Author: Kirill Gavrilov
0002 // Copyright (c) 2017-2019 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _XCAFPrs_DocumentExplorer_HeaderFile
0016 #define _XCAFPrs_DocumentExplorer_HeaderFile
0017 
0018 #include <NCollection_DynamicArray.hxx>
0019 #include <XCAFPrs_DocumentNode.hxx>
0020 #include <TDF_Label.hxx>
0021 #include <NCollection_Sequence.hxx>
0022 #include <TopoDS_Shape.hxx>
0023 
0024 class TDocStd_Document;
0025 class XCAFDoc_ColorTool;
0026 class XCAFDoc_VisMaterialTool;
0027 
0028 typedef int XCAFPrs_DocumentExplorerFlags;
0029 
0030 //! Document explorer flags.
0031 enum
0032 {
0033   XCAFPrs_DocumentExplorerFlags_None = 0x00, //!< no flags
0034   XCAFPrs_DocumentExplorerFlags_OnlyLeafNodes =
0035     0x01,                                       //!< explore only leaf nodes (skip assembly nodes)
0036   XCAFPrs_DocumentExplorerFlags_NoStyle = 0x02, //!< do not fetch styles
0037 };
0038 
0039 //! Document iterator through shape nodes.
0040 class XCAFPrs_DocumentExplorer
0041 {
0042 public: //! @name string identification tools
0043   //! Construct a unique string identifier for the given label.
0044   //! The identifier is a concatenation of label entries (TDF_Tool::Entry() with tailing '.') of
0045   //! hierarchy from parent to child joined via '/' and looking like this:
0046   //! @code
0047   //!   0:1:1:1./0:1:1:1:9./0:1:1:5:7.
0048   //! @endcode
0049   //! This generation scheme also allows finding originating labels using TDF_Tool::Label().
0050   //! The tailing dot simplifies parent equality check.
0051   //! @param theLabel child label to define id
0052   //! @param theParentId parent string identifier defined by this method
0053   Standard_EXPORT static TCollection_AsciiString DefineChildId(
0054     const TDF_Label&               theLabel,
0055     const TCollection_AsciiString& theParentId);
0056 
0057   //! Find a shape entity based on a text identifier constructed from OCAF labels defining full
0058   //! path.
0059   //! @sa DefineChildId()
0060   Standard_EXPORT static TDF_Label FindLabelFromPathId(
0061     const occ::handle<TDocStd_Document>& theDocument,
0062     const TCollection_AsciiString&       theId,
0063     TopLoc_Location&                     theParentLocation,
0064     TopLoc_Location&                     theLocation);
0065 
0066   //! Find a shape entity based on a text identifier constructed from OCAF labels defining full
0067   //! path.
0068   //! @sa DefineChildId()
0069   static TDF_Label FindLabelFromPathId(const occ::handle<TDocStd_Document>& theDocument,
0070                                        const TCollection_AsciiString&       theId,
0071                                        TopLoc_Location&                     theLocation)
0072   {
0073     TopLoc_Location aDummy;
0074     return FindLabelFromPathId(theDocument, theId, aDummy, theLocation);
0075   }
0076 
0077   //! Find a shape entity based on a text identifier constructed from OCAF labels defining full
0078   //! path.
0079   //! @sa DefineChildId()
0080   Standard_EXPORT static TopoDS_Shape FindShapeFromPathId(
0081     const occ::handle<TDocStd_Document>& theDocument,
0082     const TCollection_AsciiString&       theId);
0083 
0084 public:
0085   //! Empty constructor.
0086   Standard_EXPORT XCAFPrs_DocumentExplorer();
0087 
0088   //! Constructor for exploring the whole document.
0089   //! @param theDocument document to explore
0090   //! @param theFlags    iteration flags
0091   //! @param theDefStyle default style for nodes with undefined style
0092   Standard_EXPORT XCAFPrs_DocumentExplorer(const occ::handle<TDocStd_Document>& theDocument,
0093                                            const XCAFPrs_DocumentExplorerFlags  theFlags,
0094                                            const XCAFPrs_Style& theDefStyle = XCAFPrs_Style());
0095 
0096   //! Constructor for exploring specified list of root shapes in the document.
0097   //! @param theDocument  document to explore
0098   //! @param theRoots     root labels to explore within specified document
0099   //! @param theFlags     iteration flags
0100   //! @param theDefStyle  default style for nodes with undefined style
0101   Standard_EXPORT XCAFPrs_DocumentExplorer(const occ::handle<TDocStd_Document>&   theDocument,
0102                                            const NCollection_Sequence<TDF_Label>& theRoots,
0103                                            const XCAFPrs_DocumentExplorerFlags    theFlags,
0104                                            const XCAFPrs_Style& theDefStyle = XCAFPrs_Style());
0105 
0106   //! Initialize the iterator from a single root shape in the document.
0107   //! @param theDocument  document to explore
0108   //! @param theRoot      single root label to explore within specified document
0109   //! @param theFlags     iteration flags
0110   //! @param theDefStyle  default style for nodes with undefined style
0111   Standard_EXPORT void Init(const occ::handle<TDocStd_Document>& theDocument,
0112                             const TDF_Label&                     theRoot,
0113                             const XCAFPrs_DocumentExplorerFlags  theFlags,
0114                             const XCAFPrs_Style&                 theDefStyle = XCAFPrs_Style());
0115 
0116   //! Initialize the iterator from the list of root shapes in the document.
0117   //! @param theDocument  document to explore
0118   //! @param theRoots     root labels to explore within specified document
0119   //! @param theFlags     iteration flags
0120   //! @param theDefStyle  default style for nodes with undefined style
0121   Standard_EXPORT void Init(const occ::handle<TDocStd_Document>&   theDocument,
0122                             const NCollection_Sequence<TDF_Label>& theRoots,
0123                             const XCAFPrs_DocumentExplorerFlags    theFlags,
0124                             const XCAFPrs_Style&                   theDefStyle = XCAFPrs_Style());
0125 
0126   //! Return TRUE if iterator points to the valid node.
0127   bool More() const { return myHasMore; }
0128 
0129   //! Return current position.
0130   const XCAFPrs_DocumentNode& Current() const { return myCurrent; }
0131 
0132   //! Return current position.
0133   XCAFPrs_DocumentNode& ChangeCurrent() { return myCurrent; }
0134 
0135   //! Return current position within specified assembly depth.
0136   const XCAFPrs_DocumentNode& Current(int theDepth) const
0137   {
0138     const int aCurrDepth = CurrentDepth();
0139     if (theDepth == aCurrDepth)
0140     {
0141       return myCurrent;
0142     }
0143 
0144     Standard_OutOfRange_Raise_if(theDepth < 0 || theDepth > myTop,
0145                                  "XCAFPrs_DocumentExplorer::Current() out of range");
0146     return myNodeStack.Value(theDepth);
0147   }
0148 
0149   //! Return depth of the current node in hierarchy, starting from 0.
0150   //! Zero means Root label.
0151   int CurrentDepth() const { return myCurrent.IsAssembly ? myTop : myTop + 1; }
0152 
0153   //! Go to the next node.
0154   Standard_EXPORT void Next();
0155 
0156   //! Return color tool.
0157   const occ::handle<XCAFDoc_ColorTool>& ColorTool() const { return myColorTool; }
0158 
0159   //! Return material tool.
0160   const occ::handle<XCAFDoc_VisMaterialTool>& VisMaterialTool() const { return myVisMatTool; }
0161 
0162 protected:
0163   //! Initialize root label.
0164   Standard_EXPORT void initRoot();
0165 
0166   //! Initialize properties for a current label.
0167   Standard_EXPORT void initCurrent(bool theIsAssembly);
0168 
0169 protected:
0170   occ::handle<XCAFDoc_ColorTool>                 myColorTool;  //!< color tool
0171   occ::handle<XCAFDoc_VisMaterialTool>           myVisMatTool; //!< visual material tool
0172   NCollection_Sequence<TDF_Label>                myRoots;      //!< sequence of root labels
0173   NCollection_Sequence<TDF_Label>::Iterator      myRootIter;   //!< current root label
0174   NCollection_DynamicArray<XCAFPrs_DocumentNode> myNodeStack;  //!< node stack
0175   int                                            myTop;        //!< top position in the node stack
0176                                                                // clang-format off
0177   bool                myHasMore;    //!< global flag indicating that iterator points to the label
0178                                                                // clang-format on
0179   XCAFPrs_Style                 myDefStyle;                    //!< default style
0180   XCAFPrs_DocumentNode          myCurrent;                     //!< current label info
0181   XCAFPrs_DocumentExplorerFlags myFlags;                       //!< iteration flags
0182 };
0183 
0184 #endif // _XCAFPrs_DocumentExplorer_HeaderFile