AcGraph.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Copyright 2015 Autodesk, Inc. All rights reserved.
  5. //
  6. // Use of this software is subject to the terms of the Autodesk license
  7. // agreement provided at the time of installation or download, or which
  8. // otherwise accompanies this software in either electronic or hard copy form.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. //
  12. // CREATED BY: Pei Zhan March 2011
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. #pragma once
  16. #pragma pack (push, 8)
  17. #include "AcGraphNode.h"
  18. #include <map>
  19. class AcImpGraph;
  20. class AcDbObject;
  21. typedef std::map<AcGraphNode::Id, AcGraphNode*> NodeIdToPtrMap;
  22. /// <summary>
  23. /// This class represents a graph which manages a set of AcGraphNode with a unique id.
  24. /// </summary>
  25. ///
  26. class ACDB_PORT AcGraph : public AcRxObject
  27. {
  28. public:
  29. ACRX_DECLARE_MEMBERS(AcGraph);
  30. enum PartialUndoCode
  31. {
  32. /// <summary>
  33. /// Event of adding a node to the graph.
  34. /// </summary>
  35. ///
  36. kAddNodePartialUndoCode = 0,
  37. /// <summary>
  38. /// Event of removing a node from the graph.
  39. /// </summary>
  40. ///
  41. kRemoveNodePartialUndoCode,
  42. /// <summary>
  43. /// Event of modifying a node in the graph.
  44. /// </summary>
  45. ///
  46. kModifyNodePartialUndoCode,
  47. /// <summary>
  48. /// Event of changing the value of node Id for the next new node.
  49. /// </summary>
  50. ///
  51. kModifyNextNewNodeIdPartialUndoCode,
  52. };
  53. typedef NodeIdToPtrMap::iterator iterator;
  54. typedef NodeIdToPtrMap::const_iterator const_iterator;
  55. /// <summary> Default constructor. </summary>
  56. ///
  57. explicit AcGraph(bool bCreateImp = true);
  58. /// <summary> Default destructor. </summary>
  59. ///
  60. virtual ~AcGraph();
  61. /// <summary>
  62. /// This function returns the owning object of the graph
  63. /// </summary>
  64. /// <returns> AcDbObject*. </returns>
  65. AcDbObject* owningObject() const;
  66. /// <summary>
  67. /// This function set the owning object of the graph. Client code needs to
  68. /// ensure the validity of AcDbObject* throughout the whole lifetime of AcGraph. In the
  69. /// AcGraph class the owning object is mainly used for partial undo purpose
  70. /// </summary>
  71. /// <returns> None. </returns>
  72. void setOwningObject(AcDbObject* pObj);
  73. /// <summary>
  74. /// This function restores state of AcGraph during partial undo. Normally it is called
  75. /// from the applyPartialUndo method in owning object. In AcGraph, partial undo information
  76. /// is automatically recorded when a change is made to an AcGraph, the partial undo info
  77. /// that is written out includes (with order): address of class descriptor object of owingObject(), undo code
  78. /// and corresponding data for each specific undo operation. In client's code, applyPartialUndo needs to
  79. /// be implement for owningObject, which has to read the data in the same order, and then call
  80. /// AcGraph::applyPartialUndo with the PartialUndoCode info
  81. /// </summary>
  82. /// <param name="pUndoFiler"> Input undo filer, normally from the object that owns this graph. </param>
  83. /// <param name="code"> code for undo operation. </param>
  84. /// <param name="nodeId"> the id of the AcGraphNode that was affected during the undo operation,
  85. /// For following undo codes:
  86. /// kAddNodePartialUndoCode: the id of the node that was added, note the returned nodeId is no longer valid as it has been removed from the graph.
  87. /// kRemoveNodePartialUndoCode: the id of the node that was removed
  88. /// kModifyNodePartialUndoCode: the id of the node that was modified
  89. /// kModifyNextNewNodeIdPartialUndoCode: the new node id that was generated
  90. /// </param>
  91. /// <returns> Acad::ErrorStatus </returns>
  92. virtual Acad::ErrorStatus applyPartialUndo(AcDbDwgFiler* pUndoFiler,
  93. PartialUndoCode code,
  94. AcGraphNode::Id &nodeId);
  95. /// <summary>
  96. /// This function adds a node to the graph, a new node id is automatically assigned to the new node.
  97. /// It returns an error if the new node already has an owner graph that is different from this one,
  98. /// or the new node already has a non-null node Id in the same graph
  99. /// </summary>
  100. /// <param name="pNewNode"> new node to be added to this graph. </param>
  101. /// <returns> Acad::ErrorStatus </returns>
  102. Acad::ErrorStatus addGraphNode(AcGraphNode* pNewNode);
  103. /// <summary>
  104. /// This function adds a node to the graph, it uses input nodeId for the new node.
  105. /// It's caller's responsibility to ensure the nodeId is unused, other wise an error will
  106. /// be returned.
  107. /// It returns an error if the new node already has an owner graph that is different from this one,
  108. /// or nodeId has already been used in the this AcGraph
  109. /// </summary>
  110. /// <param name="pNewNode"> new node to be added to this graph. </param>
  111. /// <param name="nodeId"> nodeId to be assigned to the new node. </param>
  112. /// <returns> Acad::ErrorStatus </returns>
  113. Acad::ErrorStatus addGraphNode(AcGraphNode* pNewNode, AcGraphNode::Id nodeId);
  114. /// <summary>
  115. /// This function removes a node from the graph, it returns an error if the node is still connected
  116. /// to other nodes. This function will also delete the node pointer if bDelete is true.
  117. /// </summary>
  118. /// <param name="pNode"> node to be removed from this graph. </param>
  119. /// <param name="bDelete"> if the node is to be deleted. </param>
  120. /// <returns> Acad::ErrorStatus </returns>
  121. Acad::ErrorStatus removeGraphNode(AcGraphNode* pNode, bool bDelete = true);
  122. /// <summary>
  123. /// Given a node id, this function returns a pointer to the AcGraphNode.
  124. /// Returns NULL if no match is found for the id
  125. /// </summary>
  126. /// <param name="nodeId"> the id of the returned node pointer. </param>
  127. /// <returns> pointer to the AcGraphNode </returns>
  128. AcGraphNode* getGraphNodePtr(AcGraphNode::Id nodeId) const;
  129. /// <summary>
  130. /// Disconnect an array of AcGraphNodes from their original owning graph and move them to this graph.
  131. /// All other nodes that are directly or indirectly connected to these nodes will be
  132. /// moved as well. A new node id will be assigned to each of the node after it is moved.
  133. /// During the process, when each of the nodes is moved, method AcGraphNode::nodeToBeMovedNotification
  134. /// will be called to notify this event, which can be overriden to customize the behavior
  135. /// (see AcGraphNode::nodeToBeMoved for more details)
  136. /// </summary>
  137. /// <param name="nodesToMove"> input array of nodes to be moved to this graph. </param>
  138. /// <returns> Acad::ErrorStatus </returns>
  139. Acad::ErrorStatus moveAndMerge(const AcArray<AcGraphNode*> & nodesToMove);
  140. /// <summary>
  141. /// Merge a graph into this graph, all the nodes will be unconditionally moved to this graph.
  142. /// A new node id will be assigned to each of the node after it is moved.
  143. /// </summary>
  144. /// <param name="graph"> input graph that is to be merged to this graph. </param>
  145. /// <returns> Acad::ErrorStatus </returns>
  146. Acad::ErrorStatus merge(const AcGraph &graph);
  147. /// <summary>
  148. /// Given an array of nodes, return all the related nodes that are directly or indirectly connected
  149. /// to them.
  150. /// </summary>
  151. /// <param name="nodes"> input nodes. </param>
  152. /// <param name="nodes"> output related nodes. </param>
  153. /// <returns> Acad::ErrorStatus </returns>
  154. Acad::ErrorStatus getRelatedNodes(const AcArray<AcGraphNode *> &nodes, AcArray<AcGraphNode *> &relatedNodes) const;
  155. /// <summary>
  156. /// This function returns an iterator at the beginning of the iterated range
  157. /// </summary>
  158. /// <returns> AcGraph::iterator </returns>
  159. iterator begin();
  160. /// <summary>
  161. /// This function returns a cons_iterator at the beginning of the iterated range
  162. /// </summary>
  163. /// <returns> AcGraph::const_iterator </returns>
  164. const_iterator begin() const;
  165. /// <summary>
  166. /// This function returns an iterator at the end of the iterated range
  167. /// </summary>
  168. /// <returns> AcGraph::iterator </returns>
  169. iterator end();
  170. /// <summary>
  171. /// This function returns a cons_iterator at the end of the iterated range
  172. /// </summary>
  173. /// <returns> AcGraph::const_iterator </returns>
  174. const_iterator end() const;
  175. /// <summary>
  176. /// This function returns the size(number) of nodes in the graph
  177. /// </summary>
  178. /// <returns> size_t </returns>
  179. size_t size() const;
  180. /// <summary>
  181. /// This function removes all the nodes in the graph, it also optionally deletes the pointer
  182. /// of each AcGraphNode in the AcGraph
  183. /// <param name="bDelete"> if the nodes are to be deleted. </param>
  184. /// </summary>
  185. /// <returns> none </returns>
  186. void clear(bool bDelete);
  187. /// <summary>
  188. /// This function is called by dwgOut(). Its purpose is to allow the object to write out its data.
  189. /// </summary>
  190. /// <returns> Acad::ErrorStatus </returns>
  191. virtual Acad::ErrorStatus dwgOutFields(AcDbDwgFiler *pFiler) const;
  192. /// <summary>
  193. /// This function is called by dwgIn(). Its purpose is to allow the object to read in its data.
  194. /// </summary>
  195. /// <returns> Acad::ErrorStatus </returns>
  196. virtual Acad::ErrorStatus dwgInFields (AcDbDwgFiler *pFiler);
  197. /// <summary>
  198. /// This function is called by dxfOut(). Its purpose is to allow the object to write out its data.
  199. /// </summary>
  200. /// <returns> Acad::ErrorStatus </returns>
  201. virtual Acad::ErrorStatus dxfOutFields(AcDbDxfFiler *pFiler) const;
  202. /// <summary>
  203. /// This function is called by dxfIn(). Its purpose is to allow the object to read in its data.
  204. /// </summary>
  205. /// <returns> Acad::ErrorStatus </returns>
  206. virtual Acad::ErrorStatus dxfInFields (AcDbDxfFiler *pFiler);
  207. /// <summary>
  208. /// Returns id of the graph, by default it is 0 if setGraphId was not called to assign an id previously
  209. /// </summary>
  210. /// <returns> Acad::UInt32 </returns>
  211. virtual Adesk::UInt32 graphId() const;
  212. /// <summary>
  213. /// Sets id of the graph. When the owningObject has more than one graphs, it is important to assign a
  214. /// unique id to each of the graphs
  215. /// </summary>
  216. /// <returns> none </returns>
  217. virtual void setGraphId(Adesk::UInt32 id);
  218. protected:
  219. AcImpGraph* mpImpGraph;
  220. private:
  221. /// <summary>
  222. /// Disabled copy constructor
  223. /// </summary>
  224. AcGraph(const AcGraph &graph);
  225. /// <summary>
  226. /// Disabled assignment operator
  227. /// </summary>
  228. AcGraph &operator = (const AcGraph &graph);
  229. friend class AcGraphNode;
  230. friend class AcImpGraphNode;
  231. friend class AcImpGraph;
  232. };
  233. #pragma pack (pop)