Example #1
0
        public EntityViewModel Duplicate()
        {
            var flags           = SubHierarchyCloneFlags.GenerateNewIdsForIdentifiableObjects;
            var clonedHierarchy = EntityHierarchyPropertyGraph.CloneSubHierarchies(Asset.Session.AssetNodeContainer, Asset.Asset, AssetSideEntity.Id.Yield(), flags, out Dictionary <Guid, Guid> idRemapping);

            AssetPartsAnalysis.GenerateNewBaseInstanceIds(clonedHierarchy);

            var addedRoot = clonedHierarchy.Parts[clonedHierarchy.RootParts.Single().Id];

            addedRoot.Folder = (Parent as EntityFolderViewModel)?.Path;

            // rename the entity to avoid having the same names
            if (Parent == null)
            {
                throw new InvalidOperationException($"{nameof(Parent)} cannot be null");
            }
            addedRoot.Entity.Name = EntityFactory.ComputeNewName(Parent, addedRoot.Entity.Name);
            Asset.AssetHierarchyPropertyGraph.AddPartToAsset(clonedHierarchy.Parts, addedRoot, (Parent.Owner as EntityViewModel)?.AssetSideEntity, Parent.Owner.IndexOfEntity(this) + 1);
            var cloneId = addedRoot.Entity.Id;

            // The view model should already exist at that point
            var partId    = new AbsoluteId(Asset.Id, cloneId);
            var viewModel = (EntityViewModel)Editor.FindPartViewModel(partId);

            // TODO: Offset a bit (by 1 scene unit horizontally?) the cloned entity so it appears distincly from the source entity
            return(viewModel);
        }
        private static string Copy([NotNull] ICopyPasteService service, [NotNull] EntityHierarchyPropertyGraph assetGraph, [NotNull] IEnumerable <EntityDesign> commonRoots)
        {
            // copy selected hierarchy
            var hierarchy = EntityHierarchyPropertyGraph.CloneSubHierarchies(assetGraph.Container.NodeContainer, assetGraph.Asset, commonRoots.Select(r => r.Entity.Id), SubHierarchyCloneFlags.None, out _);
            // visitor on this temporary asset
            var text = service.CopyFromAsset(assetGraph, assetGraph.Id, hierarchy, false);

            return(text);
        }
        private static void Paste([NotNull] ICopyPasteService service, string text, [NotNull] EntityHierarchyPropertyGraph assetGraph, EntityDesign target, string folderName)
        {
            var data = service.DeserializeCopiedData(text, assetGraph.Asset, typeof(Entity));

            Assert.NotNull(data);
            Assert.NotNull(data.Items);
            Assert.Equal(1, data.Items.Count);

            var item = data.Items[0];

            Assert.NotNull(item);
            Assert.NotNull(item.Data);
            Assert.NotNull(item.Processor);

            var targetNode = target != null?assetGraph.Container.NodeContainer.GetNode(target.Entity) : assetGraph.RootNode;

            var nodeAccessor      = new NodeAccessor(targetNode, NodeIndex.Empty);
            var propertyContainer = new PropertyContainer {
                { EntityHierarchyPasteProcessor.TargetFolderKey, folderName }
            };

            item.Processor.Paste(item, assetGraph, ref nodeAccessor, ref propertyContainer);
        }