Get and track the last shape inserted into a diagram

Aug 28, 2012 at 2:16 AM

Hi all,

Please, I would like to know if it is possible to track the specific shape by a property such as an id. Also, I have been wondering if it is possible to get the last shape added to the diagram, and then track its activities. Is it possible to get a collection of shapes in a sequence by which they were created or inserted to the diagram. I tried the ShapeCollection class, but currently does not return a collection of shapes according to the z-order. I realized that some shapes (like the polyline) are always on the bottom of the collection even if they were created last. Thank you. 

Coordinator
Aug 28, 2012 at 7:37 AM

Hello mymark.

mymark wrote:
Please, I would like to know if it is possible to track the specific shape by a property such as an id.

You can use the Id of a shape, but keep in mind that the Id is null until saving the repository.
In most cases, it is better to use the Shape.Tag property (attention: The contents of the Tag property will NOT be saved to the repository).
If you need to persist your tracking Id, you could combine the usage of Shape.Tag (unsaved shapes) and Shape.Id (saved shapes) but deriving your own shapes (Adding a TrackingId property and overriding the explicit omplementations of IEntity.LoadFields and IEntity.SaveFields) would be the the cleanest solution.

mymark wrote:
Also, I have been wondering if it is possible to get the last shape added to the diagram, and then track its activities.

There are many events that indicate changes to shapes and other objects:

  • The interface IDiagramPresenter has two events (ShapesInserted and ShapesRemoved) that indicate when shapes were inserted into the display.
    Attention: This is a per-display event. If you show the diagram in multiple displays, only one display will raise this event.
  • The interface IRepository a bunch of events for nearly all NShape classes. These events indicate changes that made in the repository (e.g.
    ShapesInserted, ShapesUpdated, ShapesDeleted, ConnectionInserted, ConnectionDeleted)
  • The History class (accessible through the Prject component: Project.History) raises events whenever commands are added / executed (History.CommandsAdded, History.CommandsExecuted). The commands contain the modified objects and a description of the performed changes.

mymark wrote:
Is it possible to get a collection of shapes in a sequence by which they were created or inserted to the diagram. I tried the ShapeCollection class, but currently does not return a collection of shapes according to the z-order. 

If the shape has a valid ZOrder, the ZOrder will be used when adding a shape to a ShapeCollection. If you have created a new shape manually (with program code), you have to specify the ZOrder when adding the shape to the ShapeCollection (otherwise, the ZOrder remains 0 and the sort order is undefined).
Normaly, the methods IRepository.ObtainMaxZOrder and IRepository.ObtainMinZOrder are used for this purpose. This is necessary because the NShape Framework will supports deferred loading of diagrams (the shapes will be loaded when displaying the diagram) and will support partial loading of diagrams in future versions.

mymark wrote:
I realized that some shapes (like the polyline) are always on the bottom of the collection even if they were created last.

This behavior is by design:
If you have line caps like arrows on your lines and the line thickness is >1, you do not want the arrow be drawn over the planar shapes (in most cases) and when selecting overlapping/connected shapes, the planar shapes are the ones that should be selected first.
Due to the fact that both, drawing and selecting shapes, depends on the ZOrder of the shapes, all line shapes are inserted at the bottom of of all diagram shapes whereas all planar shapes are inserted on top of all diagram shapes.