Track changes modification: Retrieve original element state

Track changes modification: Retrieve original element state

vietcuong.dao
Explorer Explorer
365 Views
4 Replies
Message 1 of 5

Track changes modification: Retrieve original element state

vietcuong.dao
Explorer
Explorer

Hello everyone,

 

In my project, I have utilized the ChangesMonitor.cs from the "DocumentChanged" example in Revit SDK to track the changes of elements. However when I extract the location of the changed element with e.Location, I received only the final location. In my case, the original location of the changed element is needed. I then look around and find the Track changes modification by The Building Coder (https://thebuildingcoder.typepad.com/blog/2016/01/tracking-element-modification.html). In this implementation, a snapshot of the element initial state can be captured. However, the code for that is missing.

 /// <summary>
  /// Compare the start and end states and report the 
  /// differences found. In this implementation, we
  /// just store a hash code of the element state.
  /// If you choose to store the full string 
  /// representation, you can use that for comparison,
  /// and then report exactly what changed and the
  /// original values as well.
  /// </summary>
  static void ReportDifferences(
    Document doc,
    Dictionary<int, string> start_state,
    Dictionary<int, string> end_state )
  {
    int n1 = start_state.Keys.Count;
    int n2 = end_state.Keys.Count;
 
    List<int> keys = new List<int>( start_state.Keys );
 
    foreach( int id in end_state.Keys )
    {
      if( !keys.Contains( id ) )
      {
        keys.Add( id );
      }
    }
 
    keys.Sort();
 
    int n = keys.Count;
 
    Debug.Print(
      "{0} elements before, {1} elements after, {2} total",
      n1, n2, n );
 
    int nAdded = 0;
    int nDeleted = 0;
    int nModified = 0;
    int nIdentical = 0;
    List<string> report = new List<string>();
 
    foreach( int id in keys )
    {
      if( !start_state.ContainsKey( id ) )
      {
        ++nAdded;
        report.Add( id.ToString() + " added "
          + ElementDescription( doc, id ) );
      }
      else if( !end_state.ContainsKey( id ) )
      {
        ++nDeleted;
        report.Add( id.ToString() + " deleted" );
      }
      else if( start_state[id] != end_state[id] )
      {
        ++nModified;
        report.Add( id.ToString() + " modified "
          + ElementDescription( doc, id ) );
      }
      else
      {
        ++nIdentical;
      }
    }
 
    string msg = string.Format(
      "Stopped tracking changes now.\r\n"
      + "{0} deleted, {1} added, {2} modified, "
      + "{3} identical elements:",
      nDeleted, nAdded, nModified, nIdentical );
 
    string s = string.Join( "\r\n", report );
 
    Debug.Print( msg + "\r\n" + s );
    TaskDialog dlg = new TaskDialog( "Track Changes" );
    dlg.MainInstruction = msg;
    dlg.MainContent = s;
    dlg.Show();
  }

 

/// <summary>
  /// Current snapshot of database state.
  /// You could also store the entire element state 
  /// strings here, not just their hash code, to
  /// report their complete original and modified 
  /// values.
  /// </summary>
  static Dictionary<int, string> _start_state = null;

 

  public Result Execute(
    ExternalCommandData commandData,
    ref string message,
    ElementSet elements )
  {
    UIApplication uiapp = commandData.Application;
    UIDocument uidoc = uiapp.ActiveUIDocument;
    Application app = uiapp.Application;
    Document doc = uidoc.Document;
 
    IEnumerable<Element> a = GetTrackedElements( doc );
 
    if( null == _start_state )
    {
      _start_state = GetSnapshot( a );
      TaskDialog.Show( "Track Changes",
        "Started tracking changes now." );
    }
    else
    {
      Dictionary<int, string> end_state = GetSnapshot( a );
      ReportDifferences( doc, _start_state, end_state );
      _start_state = null;
    }
    return Result.Succeeded;
  }

 Could you guys give me some hints on what should I do to retrieve the initial location of the element with the provided Track changes modification?

Thanks

0 Likes
366 Views
4 Replies
Replies (4)
Message 2 of 5

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @vietcuong.dao ,

 

Could you please take a look at this below link?

https://github.com/jeremytammik/TrackChanges/blob/master/TrackChanges/Command.cs 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 3 of 5

vietcuong.dao
Explorer
Explorer

Hi naveen.kumar.t,

 

This Command.cs is exactly what I am working on. Could you help me on how to retrieve the original location of an element with this command?

 

Thanks

0 Likes
Message 4 of 5

vietcuong.dao
Explorer
Explorer

#region Report differences
/// <summary>
/// Compare the start and end states and report the
/// differences found. In this implementation, we
/// just store a hash code of the element state.
/// If you choose to store the full string
/// representation, you can use that for comparison,
/// and then report exactly what changed and the
/// original values as well.
/// </summary>

As it is said here, I wonder how can I store the full string representation, then report what changed?

Thanks

Message 5 of 5

RPTHOMAS108
Mentor
Mentor

I wonder if you are thinking this sample can be used to do something it wan't written for?

 

Seems to me from reading it that you store the information you are interested in at some fixed point in time. Then subsequently you compare that snapshot with the current situation.

 

"/// It is up to you to ensure that all data you are
/// interested in really is included in this snapshot."

 

So if you want to know about changes in the location, I assume that you would have had to store that information at some past point in time.

 

I think these kinds of scenarios are best achieved by comparisons between models (the one you archived at the time of the last issue and the one you are about to issue).