Issue
I would like to get notified when some data changed in the project database. Which event shall I subscribe for?
Solution
You will need to create an event handler for the DataLinksManager.DataLinkOperationOccurred event. This is a code sample:
static private bool _isActive = true; // To avoid recursion
// Event Handler
//---------------------------------------------------------------
public static void dlm_DataLinkOperationOccurred(object sender,
DataLinkEventArgs e)
{
if (e.Action == DataLinkAction.RowModified && _isActive)
{
try
{
DataLinksManager dlm = PlantApplication.CurrentProject.
ProjectParts["Piping"].DataLinksManager;
if (dlm.HasProperty(e.RowId, "MyProperty"))
{
StringCollection props = new StringCollection();
props.Add("Size");
props.Add("Spec");
StringCollection values = null;
values = dlm.GetProperties(e.RowId, props, true);
PpObjectIdArray ppoids = dlm.FindAcPpObjectIds(e.RowId);
StringCollection newprops = new StringCollection();
newprops.Add("MyProperty");
StringCollection newvals = new StringCollection();
newvals.Add(values[0] + values[1]);
_isActive = false;
dlm.SetProperties(ppoids.First.Value, newprops, newvals);
}
}
catch
{
System.Diagnostics.Debug.WriteLine(
"Error in the Event Handler:\n{0}", e.ToString() );
}
finally
{
_isActive = true;
}
}
}
// Subscribe to the event
//---------------------------------------------------------------
[CommandMethod("AddEventHandler", CommandFlags.Modal)]
public static void AddEventHandler()
{
try
{
DataLinksManager dlm = PlantApplication.CurrentProject.
ProjectParts["Piping"].DataLinksManager;
dlm.DataLinkOperationOccurred +=
new DataLinkEventHandler(dlm_DataLinkOperationOccurred);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString(),
"Cannot add the Event Handler");
}
}
// Remove the subscription
//---------------------------------------------------------------
[CommandMethod("RemoveEventHandler", CommandFlags.Modal)]
public static void RemoveEventHandler()
{
try
{
DataLinksManager dlm = PlantApplication.CurrentProject.
ProjectParts["Piping"].DataLinksManager;
dlm.DataLinkOperationOccurred -=
new DataLinkEventHandler(dlm_DataLinkOperationOccurred);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString(),
"Cannot remove the Event Handler");
}
}