Track changes modification: Retrieve original element state
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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