Hey guys,
I solved the previous problem.
Now I register also the CurrentWorkspaceChanged event and then inside that event I register then the NodeAdded.
It works well.
Still 2 questions for you experts:
Question 1:
Check my Dispose method below, I am disposing the CurrentWorkspaceChanged and then the multiple NodeAdded events to all the workspace models.
Is htis correct?
Do I even need to dispose of those? Doesnt Dynamo handle that for me?
Question 2:
In the Loaded function I need to call by hand the CurrentWorkspaceChanged event because it seems that at that time the first workspace is already loaded so if I dont do this hack it wouldnt work fine.
Is there a place where the workspace is “still loading” where I could register the CurrentWorkspaceChanged event so that i dont need this ugly extra codeline
public class SwecoNodeUsageStatisticsExtension : IViewExtension
{
private ReadyParams _readyParams;
private string _dynamoVersion;
public void Dispose()
{
_readyParams.CurrentWorkspaceChanged -= CurrentWorkspaceChanged;
foreach (var workspaceModel in _readyParams.WorkspaceModels)
{
workspaceModel.NodeAdded -= CurrentWorkspaceModel_NodesChanged;
}
}
public void Startup(ViewStartupParams p)
{
var version = p.DynamoVersion;
_dynamoVersion = version.Major + "." + version.Minor + "."+ version.Build;
}
public void Loaded(ViewLoadedParams p)
{
_readyParams = p;
_readyParams.CurrentWorkspaceChanged += CurrentWorkspaceChanged;
// If this is not here if I create a new model in beggining events dont trigger
CurrentWorkspaceChanged(_readyParams.CurrentWorkspaceModel);
}
private void CurrentWorkspaceChanged(IWorkspaceModel iWorkspaceModel)
{
iWorkspaceModel.NodeAdded += CurrentWorkspaceModel_NodesChanged;
}
private void CurrentWorkspaceModel_NodesChanged(NodeModel obj)
{
var nodesUsageCollector = new StatisticsCollector();
nodesUsageCollector.Start(
"Dynamo-DevTools",
obj.NickName,
obj.Category,
obj.Category,
obj.Description,
"",
"",
_dynamoVersion,
_readyParams.CurrentWorkspaceModel.Name.Replace("*", ""),
_readyParams.CurrentWorkspaceModel.FileName
);
nodesUsageCollector.End();
}
public void Shutdown()
{
}
public string UniqueId
{
get { return Guid.NewGuid().ToString(); }
}
public string Name
{
get { return "SwecoNodeUsageStatisticsExtension"; }
}
}