示例#1
0
        public override IMoBiCommand AddNew(IMoBiProject project, IBuildingBlock buildingBlockToAddTo)
        {
            if (!project.MoleculeBlockCollection.Any() || !project.SpatialStructureCollection.Any())
            {
                throw new MoBiException(AppConstants.Exceptions.UnableToCreateStartValues);
            }

            TBuildingBlock newEntity;

            using (var createPresenter = ApplicationController.Start <ICreateStartValuesPresenter <TBuildingBlock> >())
            {
                newEntity = createPresenter.Create();
            }

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

            var macroCommand = new MoBiMacroCommand
            {
                ObjectType  = ObjectName,
                CommandType = AppConstants.Commands.AddCommand
            };

            macroCommand.Add(GetAddCommand(newEntity, project, buildingBlockToAddTo).Run(Context));

            //Icon may depend on name.
            newEntity.Icon           = InteractionTask.IconFor(newEntity);
            macroCommand.Description = AppConstants.Commands.AddToProjectDescription(ObjectName, newEntity.Name);
            _editTask.EditBuildingBlock(newEntity);
            return(macroCommand);
        }
 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));
     }
 }
        /// <summary>
        ///    Clones the specified object with the name enterd by user.
        /// </summary>
        /// <param name="buildingBlockToClone">The object to clone.</param>
        public IMoBiCommand Clone(TBuildingBlock buildingBlockToClone)
        {
            var name = DialogCreator.AskForInput(AppConstants.Dialog.AskForNewName(AppConstants.CloneName(buildingBlockToClone)),
                                                 AppConstants.Captions.NewName,
                                                 AppConstants.CloneName(buildingBlockToClone), _editTask.GetForbiddenNames(buildingBlockToClone, Context.CurrentProject.All <TBuildingBlock>()));

            if (string.IsNullOrEmpty(name))
            {
                return(new MoBiEmptyCommand());
            }

            var clone = InteractionTask.Clone(buildingBlockToClone).WithName(name);

            return(AddToProject(clone));
        }
        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);
        }