public void CreateModel(XmlElement modelData)
{
var helper = new XmlElementHelper(modelData);
string typeName = helper.ReadString("type", String.Empty);
if (string.IsNullOrEmpty(typeName))
{
// If there wasn't a "type" attribute, then we fall-back onto
// the name of the XmlElement itself, which is usually the type
// name.
typeName = modelData.Name;
if (string.IsNullOrEmpty(typeName))
{
string guid = helper.ReadString("guid");
throw new InvalidOperationException(
string.Format("No type information: {0}", guid));
}
}
if (typeName.Contains("ConnectorModel"))
{
var connector = NodeGraph.LoadConnectorFromXml(modelData,
Nodes.ToDictionary(node => node.GUID));
// It is possible that in some cases connector can't be created,
// for example, connector connects to a custom node instance
// whose input ports have been changed, so connector can't find
// its end port owner.
if (connector == null)
{
var guidAttribute = modelData.Attributes["guid"];
if (guidAttribute == null)
{
throw new InvalidOperationException("'guid' field missing from recorded model");
}
undoRecorder.RecordModelAsOffTrack(Guid.Parse(guidAttribute.Value));
}
else
{
OnConnectorAdded(connector); // Update view-model and view.
}
}
else if (typeName.Contains("NoteModel"))
{
var noteModel = NodeGraph.LoadNoteFromXml(modelData);
AddNote(noteModel);
//check whether this note belongs to a group
foreach (var annotation in Annotations)
{
//this note "was" in a group
if (annotation.DeletedModelBases.Any(m => m.GUID == noteModel.GUID))
{
annotation.AddToSelectedModels(noteModel);
}
}
}
else if (typeName.Contains("AnnotationModel"))
{
var selectedNodes = this.Nodes == null ? null : this.Nodes.Where(s => s.IsSelected);
var selectedNotes = this.Notes == null ? null : this.Notes.Where(s => s.IsSelected);
var annotationModel = new AnnotationModel(selectedNodes, selectedNotes);
annotationModel.ModelBaseRequested += annotationModel_GetModelBase;
annotationModel.Disposed += (_) => annotationModel.ModelBaseRequested -= annotationModel_GetModelBase;
annotationModel.Deserialize(modelData, SaveContext.Undo);
AddNewAnnotation(annotationModel);
}
else if (typeName.Contains("PresetModel"))
{
var preset = new PresetModel(this.Nodes);
preset.Deserialize(modelData, SaveContext.Undo);
presets.Add(preset);
//we raise this property change here so that this event bubbles up through
//the model and to the DynamoViewModel so that presets show in the UI menu if our undo/redo
//created the first preset
RaisePropertyChanged("EnablePresetOptions");
}
else // Other node types.
{
NodeModel nodeModel = NodeFactory.CreateNodeFromXml(modelData, SaveContext.Undo, ElementResolver);
AddAndRegisterNode(nodeModel);
//check whether this node belongs to a group
foreach (var annotation in Annotations)
{
//this node "was" in a group
if (annotation.DeletedModelBases.Any(m=>m.GUID == nodeModel.GUID))
{
annotation.AddToSelectedModels(nodeModel);
}
}
}
}