public override IReadOnlyCollection <IEventGroupBuilder> LoadItems(string filename)
 {
     try
     {
         var sourceEventGroup = InteractionTask.LoadItems <IEventGroupBuildingBlock>(filename).First();
         return(selectTopEventGroupBuilder(sourceEventGroup).ToList());
     }
     catch (NotMatchingSerializationFileException)
     {
         //No Event Group Building Block in File so we are unable to determine the Root EventGroups in it. so we try normal Load
         return(base.LoadItems(filename));
     }
 }
        public override IMoBiCommand AddExisting(TParent parent, IBuildingBlock buildingBlockWithFormulaCache)
        {
            string filename = InteractionTask.AskForFileToOpen(AppConstants.Dialog.Load(_editTask.ObjectName), Constants.Filter.PKML_FILE_FILTER, Constants.DirectoryKey.MODEL_PART);

            if (filename.IsNullOrEmpty())
            {
                return(new MoBiEmptyCommand());
            }

            var sourceSpatialStructure = InteractionTask.LoadItems <IMoBiSpatialStructure>(filename).FirstOrDefault();

            if (sourceSpatialStructure == null)
            {
                return(new MoBiEmptyCommand());
            }

            var allAvailableContainersToImport = sourceSpatialStructure.TopContainers
                                                 .SelectMany(x => x.GetAllContainersAndSelf <IContainer>(cont => !cont.IsAnImplementationOf <IParameter>()));

            var allImportedContainers    = selectContainersToImport(allAvailableContainersToImport).ToList();
            var allImportedNeighborhoods = getConnectingNeighborhoods(allImportedContainers, sourceSpatialStructure);

            allImportedContainers.Each(registerLoadedIn);
            allImportedNeighborhoods.Each(registerLoadedIn);

            var targetSpatialStructure = GetSpatialStructure();

            var command = AddItemsToProject(allImportedContainers, parent, buildingBlockWithFormulaCache);

            if (command.IsEmpty())
            {
                return(new MoBiEmptyCommand());
            }


            var addNeighborhoodsCommand = addNeighborhoodsToProject(allImportedNeighborhoods, targetSpatialStructure);

            if (addNeighborhoodsCommand.IsEmpty())
            {
                return(command);
            }

            var macroCommand = new MoBiMacroCommand()
            {
                CommandType = command.CommandType, ObjectType = command.ObjectType, Comment = command.Comment, Description = command.Description, ExtendedDescription = command.ExtendedDescription
            };

            macroCommand.Add(command);
            macroCommand.Add(addNeighborhoodsCommand);

            if (sourceSpatialStructure.DiagramModel == null || targetSpatialStructure.DiagramModel == null)
            {
                return(macroCommand);
            }
            var lcs = new LayoutCopyService();

            foreach (var container in allImportedContainers)
            {
                var sourceContainer = sourceSpatialStructure.DiagramModel.GetNode <IContainerNode>(container.Id);
                var targetContainer = targetSpatialStructure.DiagramModel.GetNode <IContainerNode>(container.Id);
                try
                {
                    targetSpatialStructure.DiagramModel.BeginUpdate();
                    lcs.Copy(sourceContainer, targetContainer);
                }
                finally
                {
                    targetSpatialStructure.DiagramModel.EndUpdate();
                }
            }
            if (targetSpatialStructure.DiagramManager.IsInitialized)
            {
                targetSpatialStructure.DiagramManager.RefreshFromDiagramOptions();
            }

            return(macroCommand);
        }