Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:19:37

0001 // Copyright (c) 2026 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _BRepGraph_ShapesView_HeaderFile
0015 #define _BRepGraph_ShapesView_HeaderFile
0016 
0017 #include <BRepGraph.hxx>
0018 #include <BRepGraphInc_Populate.hxx>
0019 #include <NCollection_Array1.hxx>
0020 #include <NCollection_DataMap.hxx>
0021 #include <NCollection_LinearVector.hxx>
0022 #include <Standard_Handle.hxx>
0023 #include <TopAbs_ShapeEnum.hxx>
0024 #include <TopTools_ShapeMapHasher.hxx>
0025 
0026 class BRepTools_History;
0027 class TCollection_AsciiString;
0028 
0029 //! @brief View for TopoDS_Shape ingestion, reconstruction and lookup.
0030 //!
0031 //! Reconstructs TopoDS shapes from graph nodes on demand, with caching
0032 //! for repeated access. Topology nodes are delegated to the incidence-table
0033 //! reconstruction backend, while Product / Occurrence nodes are assembled at
0034 //! the facade level using product-local roots and occurrence placement chains.
0035 //! Provides lookup from construction-time shapes back to their graph NodeIds
0036 //! using OCCT shape identity (TShape + Location, orientation ignored).
0037 //! Shape() is the stable cached public
0038 //! route for repeated access; Reconstruct() forces a fresh rebuild with the
0039 //! same node-kind semantics and bypasses the persistent reconstructed-shape cache.
0040 //! Add() and Compact() clear the persistent reconstructed-shape cache.
0041 //! Obtained via BRepGraph::Shapes().
0042 class BRepGraph::ShapesView
0043 {
0044 public:
0045   //! Shape-ingestion options.
0046   struct Options
0047   {
0048     BRepGraphInc_Populate::Options Populate;
0049     bool CreateAutoProduct = true;  //!< wrap topology root in a Product (unparented Add only)
0050     bool Flatten           = false; //!< drop hierarchy containers, append faces as roots
0051     bool Parallel          = false; //!< run face-level construction in parallel
0052     //! Capture every input subshape's NodeId in Result::AddedNodes.  Off by
0053     //! default so the hot path pays nothing.  Used by algorithm wrappers
0054     //! (Booleans, fillets, ...) that need to translate TopoDS_Shape objects
0055     //! returned by an OCCT algorithm into graph NodeIds for history harvest.
0056     bool TrackAddedNodes = false;
0057   };
0058 
0059   //! Status of a single Add() call.
0060   enum class AddStatus
0061   {
0062     Success,             //!< All faces built successfully.
0063     SuccessWithWarnings, //!< Build completed with diagnostics, e.g. unbounded natural faces.
0064     Failed               //!< Build failed (e.g., null shape).
0065   };
0066 
0067   //! Outcome of a single Add() call.
0068   struct Result
0069   {
0070     BRepGraph_NodeId       TopologyRoot;
0071     BRepGraph_ProductId    Product;
0072     BRepGraph_OccurrenceId Occurrence;
0073     BRepGraph_RefId        InsertedRef;
0074     AddStatus              Status = AddStatus::Failed;
0075 
0076     //! True if the build succeeded (with or without warnings).
0077     [[nodiscard]] bool IsOk() const { return Status != AddStatus::Failed; }
0078 
0079     //! Populated only when Options::TrackAddedNodes is true.  Maps every
0080     //! subshape of the input @c theShape (including the root) to the
0081     //! BRepGraph_NodeId it resolves to after the Add.  Multiple input
0082     //! shapes that share identity collapse to one entry, as in OCCT's
0083     //! map types.
0084     NCollection_DataMap<TopoDS_Shape, BRepGraph_NodeId, TopTools_ShapeMapHasher> AddedNodes;
0085   };
0086 
0087   //! Ingest a TopoDS_Shape as a new root subgraph, wrapping the topology root in a Product.
0088   //! @param[in] theShape shape to ingest
0089   //! @return Result with TopologyRoot, Product and Occurrence set on success.
0090   [[nodiscard]] Standard_EXPORT Result Add(const TopoDS_Shape& theShape);
0091 
0092   //! Ingest a TopoDS_Shape as a new root subgraph with explicit options.
0093   //! @param[in] theShape   shape to ingest
0094   //! @param[in] theOptions shape-ingestion options
0095   //! @return Result with TopologyRoot set on success; Product/Occurrence set
0096   //!         when theOptions.CreateAutoProduct is true.
0097   [[nodiscard]] Standard_EXPORT Result Add(const TopoDS_Shape& theShape, const Options& theOptions);
0098 
0099   //! Ingest a TopoDS_Shape under an existing parent.
0100   //!
0101   //! Parent kind dispatch:
0102   //!   - Product:   creates a child part-product, links via Occurrence with shape.Location().
0103   //!   - Compound:  appends topology root as a child reference.
0104   //!   - Shell:     appends a Face as a FaceRef; other shapes via AddChild.
0105   //!   - Solid:     appends a Shell as a ShellRef; other shapes via AddChild.
0106   //!   - CompSolid: appends a Solid as a SolidRef.
0107   //!   Other parent kinds (Wire, Edge, Vertex, Occurrence) are not supported and yield
0108   //!   an invalid Result (Result::Ok == false) without modification to the graph.
0109   //! @param[in] theShape  shape to ingest
0110   //! @param[in] theParent parent node receiving the topology
0111   //! @return Result with TopologyRoot set, plus (Product, Occurrence, InsertedRef) for Product
0112   //!         parents or InsertedRef for topology container parents.
0113   [[nodiscard]] Standard_EXPORT Result Add(const TopoDS_Shape&    theShape,
0114                                            const BRepGraph_NodeId theParent);
0115 
0116   //! Ingest a shape under an existing parent with explicit options.
0117   //! Options::CreateAutoProduct is ignored.
0118   [[nodiscard]] Standard_EXPORT Result Add(const TopoDS_Shape&    theShape,
0119                                            const BRepGraph_NodeId theParent,
0120                                            const Options&         theOptions);
0121 
0122   //! Collect a TopoDS_Shape -> NodeId map for graph roots and all subshapes
0123   //! resolvable through FindNode().  This is intended for algorithms that
0124   //! reconstruct selected graph roots to TopoDS, run OCCT, and then need to
0125   //! translate BRepTools_History back to graph NodeIds.
0126   Standard_EXPORT void CollectHistoryInputs(
0127     const NCollection_Array1<BRepGraph_NodeId>&                                   theRoots,
0128     NCollection_DataMap<TopoDS_Shape, BRepGraph_NodeId, TopTools_ShapeMapHasher>& theOutInputs)
0129     const;
0130 
0131   //! Add an OCCT algorithm result and absorb BRepTools_History into the
0132   //! registered BRepGraph_LayerHistory layer using explicit input shape mapping.
0133   [[nodiscard]] Standard_EXPORT Result AddWithHistory(
0134     const TopoDS_Shape& theResultShape,
0135     const NCollection_DataMap<TopoDS_Shape, BRepGraph_NodeId, TopTools_ShapeMapHasher>& theInputs,
0136     const occ::handle<BRepTools_History>&                                               theHistory,
0137     const TCollection_AsciiString&                                                      theOpLabel);
0138 
0139   //! Add an OCCT algorithm result and absorb BRepTools_History with explicit options.
0140   [[nodiscard]] Standard_EXPORT Result AddWithHistory(
0141     const TopoDS_Shape& theResultShape,
0142     const NCollection_DataMap<TopoDS_Shape, BRepGraph_NodeId, TopTools_ShapeMapHasher>& theInputs,
0143     const occ::handle<BRepTools_History>&                                               theHistory,
0144     const TCollection_AsciiString&                                                      theOpLabel,
0145     const Options&                                                                      theOptions);
0146 
0147   //! Convenience overload that collects the history input map from selected roots.
0148   [[nodiscard]] Standard_EXPORT Result
0149     AddWithHistory(const TopoDS_Shape&                         theResultShape,
0150                    const NCollection_Array1<BRepGraph_NodeId>& theInputRoots,
0151                    const occ::handle<BRepTools_History>&       theHistory,
0152                    const TCollection_AsciiString&              theOpLabel);
0153 
0154   //! Convenience overload that collects the history input map from selected roots
0155   //! and uses explicit options.
0156   [[nodiscard]] Standard_EXPORT Result
0157     AddWithHistory(const TopoDS_Shape&                         theResultShape,
0158                    const NCollection_Array1<BRepGraph_NodeId>& theInputRoots,
0159                    const occ::handle<BRepTools_History>&       theHistory,
0160                    const TCollection_AsciiString&              theOpLabel,
0161                    const Options&                              theOptions);
0162 
0163   //! Return or reconstruct a TopoDS_Shape for a node.
0164   //! Prefer this route for repeated public queries.
0165   //! Returns a cached shape when available and valid; otherwise reconstructs.
0166   //! Topology definition nodes (Vertex..CompSolid) reconstruct their topology
0167   //! directly, without assembly wrappers.
0168   //! Product nodes are reconstructed in product-local coordinates.
0169   //! Occurrence nodes are reconstructed with cumulative occurrence placement.
0170   //! @param[in] theNode node identifier
0171   //! @return corresponding TopoDS_Shape, or null shape for invalid/removed nodes
0172   [[nodiscard]] Standard_EXPORT TopoDS_Shape Shape(const BRepGraph_NodeId theNode) const;
0173 
0174   //! Check if the node has an original shape from graph construction.
0175   //! Editor-created and mutation-derived nodes have no original.
0176   //! @param[in] theNode node identifier
0177   //! @return true if an original shape exists
0178   [[nodiscard]] Standard_EXPORT bool HasOriginal(const BRepGraph_NodeId theNode) const;
0179 
0180   //! Return the original TopoDS_Shape stored during graph construction.
0181   //! @param[in] theNode node identifier
0182   //! @return original shape for an active node, or null shape when absent/invalid/removed
0183   [[nodiscard]] Standard_EXPORT TopoDS_Shape Original(const BRepGraph_NodeId theNode) const;
0184 
0185   //! Reconstruct a TopoDS_Shape from a graph node without using the persistent cache.
0186   //! Use this when the caller explicitly needs a fresh rebuild instead of the
0187   //! shared cached shape returned by Shape(). This method does not populate the
0188   //! persistent reconstructed-shape cache.
0189   //! Topology definition nodes reconstruct topology directly.
0190   //! Product nodes are reconstructed in product-local coordinates.
0191   //! Occurrence nodes are reconstructed with cumulative occurrence placement.
0192   //! @param[in] theRoot definition node identifier
0193   //! @return reconstructed shape, or null shape for invalid/removed nodes
0194   [[nodiscard]] Standard_EXPORT TopoDS_Shape Reconstruct(const BRepGraph_NodeId theRoot) const;
0195 
0196   //! Remove the cached reconstructed shape for one node.
0197   //! Does not change graph generation counters and does not rebuild the shape.
0198   //! Invalid or removed nodes are ignored.
0199   Standard_EXPORT void ClearCached(const BRepGraph_NodeId theNode);
0200 
0201   //! Remove the cached reconstructed shape for the node referenced by one reference.
0202   //! Does not change graph generation counters and does not rebuild the shape.
0203   //! Invalid or removed references are ignored.
0204   Standard_EXPORT void ClearCached(const BRepGraph_RefId theRef);
0205 
0206   //! Look up the definition NodeId for a shape from graph construction input.
0207   //! Uses OCCT IsSame() semantics (TShape + Location, orientation ignored).
0208   //! Synthetic Product / Occurrence reconstructions are not given dedicated
0209   //! TShape bindings, so lookup is only guaranteed for construction-time topology.
0210   //! Programmatically created Editor().Add*() nodes can still be located by
0211   //! UID or by direct iteration over Topo() definitions.
0212   //! @param[in] theShape shape to look up
0213   //! @return active node identifier, or invalid NodeId if the shape is absent or removed
0214   [[nodiscard]] Standard_EXPORT BRepGraph_NodeId FindNode(const TopoDS_Shape& theShape) const;
0215 
0216   //! Check if a shape is known to the graph (was part of construction input).
0217   //! Uses OCCT IsSame() semantics (TShape + Location, orientation ignored).
0218   //! Synthetic Product / Occurrence reconstructions are not given dedicated
0219   //! TShape bindings, so this is only guaranteed for construction-time topology.
0220   //! Programmatically created Editor().Add*() nodes can still be located by
0221   //! UID or by direct iteration over Topo() definitions.
0222   //! @param[in] theShape shape to check
0223   //! @return true if the shape has a corresponding active definition node
0224   [[nodiscard]] Standard_EXPORT bool HasNode(const TopoDS_Shape& theShape) const;
0225 
0226   //! Remove the active graph node corresponding to a construction-time shape.
0227   //! This is the convenience equivalent of FindNode(theShape) followed by
0228   //! Editor().Gen().RemoveNode(node).
0229   //! @param[in] theShape shape to remove
0230   //! @return true when an active node was found and removed
0231   Standard_EXPORT bool RemoveShape(const TopoDS_Shape& theShape);
0232 
0233 private:
0234   friend class BRepGraph;
0235   friend struct BRepGraph_Data;
0236 
0237   explicit ShapesView(BRepGraph* theGraph)
0238       : myGraph(theGraph)
0239   {
0240   }
0241 
0242   [[nodiscard]] static AddStatus appendImpl(
0243     BRepGraph&                                  theGraph,
0244     const TopoDS_Shape&                         theShape,
0245     const Options&                              theOptions,
0246     NCollection_LinearVector<BRepGraph_NodeId>* theOutFlatRoots = nullptr);
0247 
0248   //! Walk @p theShape and populate @p theMap with (subshape -> NodeId)
0249   //! entries for every subshape resolvable through @c theGraph.Shapes().
0250   //! Used by the Add() overloads when Options::TrackAddedNodes is true.
0251   //! Shape identity follows TopTools_ShapeMapHasher (TShape pointer +
0252   //! Location), matching OCCT's standard shape-keyed maps.
0253   static void collectAddedNodes(
0254     const BRepGraph&                                                              theGraph,
0255     const TopoDS_Shape&                                                           theShape,
0256     NCollection_DataMap<TopoDS_Shape, BRepGraph_NodeId, TopTools_ShapeMapHasher>& theMap);
0257 
0258   //! Bind source shape keys to nodes populated from a location-stripped input shape.
0259   //! This keeps ShapesView::FindNode() usable with the original TopoDS subshapes
0260   //! when root placement is stored on a Product occurrence or Compound child ref.
0261   static void bindSourceShapeAliases(BRepGraph&          theGraph,
0262                                      const TopoDS_Shape& theSourceShape,
0263                                      const TopoDS_Shape& thePopulatedShape);
0264 
0265   static BRepGraph_NodeId detectTopologyRoot(const BRepGraph&       theGraph,
0266                                              const TopAbs_ShapeEnum theShapeType,
0267                                              const uint32_t         theOldCountOfShapeKind);
0268 
0269   static uint32_t snapshotCountForKind(const BRepGraph&       theGraph,
0270                                        const TopAbs_ShapeEnum theShapeType);
0271 
0272   static void populateUIDsIncremental(BRepGraph&     theGraph,
0273                                       const uint32_t theOldVtx,
0274                                       const uint32_t theOldEdge,
0275                                       const uint32_t theOldCoEdge,
0276                                       const uint32_t theOldWire,
0277                                       const uint32_t theOldFace,
0278                                       const uint32_t theOldShell,
0279                                       const uint32_t theOldSolid,
0280                                       const uint32_t theOldComp,
0281                                       const uint32_t theOldCS,
0282                                       const uint32_t theOldProduct,
0283                                       const uint32_t theOldOccurrence,
0284                                       const uint32_t theOldShellRef,
0285                                       const uint32_t theOldFaceRef,
0286                                       const uint32_t theOldWireRef,
0287                                       const uint32_t theOldVertexRef,
0288                                       const uint32_t theOldSolidRef,
0289                                       const uint32_t theOldChildRef);
0290 
0291   BRepGraph* myGraph;
0292 };
0293 
0294 #endif // _BRepGraph_ShapesView_HeaderFile