Page Index Toggle Pages: [1] 2  Send TopicPrint
Hot Topic (More than 10 Replies) Copy/Paste multiple compositeNodes (Read 12256 times)
Rich Cassell
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Sep 19th, 2012
Copy/Paste multiple compositeNodes
Aug 20th, 2014 at 1:41pm
Print Post  
Hi Stoyan,

I am using my own node class which inherits CompositeNode and i'm wondering if it's possible to use the DiagramView's CopyToClipboard functionality on it?

What i'm trying to do is allow the user to select a single node and copy it and all of it's children (nodes & links) and then be able to paste it onto another node in the diagram as a subordinate - basically, to save them having to do everything twice!

I currently have the following code which I was hoping would allow me to copy the nodes but it's crashing on the scView.CopyToClipboard(true) line if the selectedNode has any children...

Code
Select All
    'Handles mnuCopy.Click
    Private Sub mnuCopy_Click(sender As System.Object, e As System.EventArgs) Handles mnuCopy.Click
        If Not IsNothing(selectedNode) Then
            copyWithChildren(selectedNode)
        End If
    End Sub

    'Handles mnuPaste.Click
    Private Sub mnuPaste_Click(sender As System.Object, e As System.EventArgs) Handles mnuPaste.Click
        If Not IsNothing(selectedNode) Then
            scView.PasteFromClipboard(10, 10)
        End If
    End Sub

    Private Sub copyWithChildren(ByVal node As SmartScore_NodeDesign)
        selectWithChildren(node)
        scView.CopyToClipboard(True)
        doc.Selection.Change(node)
    End Sub

    Private Sub selectWithChildren(ByVal node As SmartScore_NodeDesign)
        node.Selected = True
        For Each link As DiagramLink In node.OutgoingLinks
            link.Selected = True
            selectWithChildren(link.Destination)
        Next
    End Sub 




Even if I just select a single node, it doesn't paste (although it doesn't crash - it doesn't seem to do anything)... Plus, how would I go about taking the first node of the copied group and making it a child of the node used to paste onto - would i use AttachTo? How would i select the first node of the copied group?

Any help would be really appreciated.

Thanks!

Rich Smiley
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Copy/Paste multiple compositeNodes
Reply #1 - Aug 20th, 2014 at 2:45pm
Print Post  
Hi Rich,

In order to enable clipboard support you will have to implement a copy constructor and binary serialization methods for the custom class. You could attach the pasted node from NodePasted event handler. When copying groups, attach only the node that does not have a MasterGroup set.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Rich Cassell
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Sep 19th, 2012
Re: Copy/Paste multiple compositeNodes
Reply #2 - Aug 20th, 2014 at 2:51pm
Print Post  
Hi Stoyan,

My custom class is Serializable and I override the SaveTo and LoadFrom methods too. Not sure what you mean by a copy constructor? I happen to use a couple of constructors to allow different numbers of arguments, i assume that wouldn't cause an issue? Any guidance in the copy constructor area would be great Smiley

I'll use the NodePasted group to attach, thanks.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Copy/Paste multiple compositeNodes
Reply #3 - Aug 20th, 2014 at 3:02pm
Print Post  
Hi Rich,

The copy constructor takes a single argument of same type and copies its properties, e.g

Code
Select All
Public Class OrgChartNode
    Inherits ShapeNode

    Public Sub New(ByVal prototype As OrgChartNode)
        MyBase.new(prototype)
        Title = prototype.Title
        FullName = prototype.FullName
    End Sub
... 



Alternatively you could override the Clone(bool) method to implement clipboard support.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Rich Cassell
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Sep 19th, 2012
Re: Copy/Paste multiple compositeNodes
Reply #4 - Aug 21st, 2014 at 9:16am
Print Post  
Hi Stoyan,

The Clone(bool) method works a treat!

My only query now is that when I do the copy operation it appears to actually do more of a "cut" operation in that the nodes being copied are removed from their current location... Is that something i've coded wrong or is that just the way it works? I suppose i could cheat and force an extra paste back to where it started...

Cheers,

Rich
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Copy/Paste multiple compositeNodes
Reply #5 - Aug 21st, 2014 at 12:43pm
Print Post  
Hi,

CopyToClipboard does not change anything in the diagram. Since you also select items when copying, check if they aren't hidden or moved from selection-change event handlers. Also make sure your Clone method does not move the original components to the new node, instead of cloning them too. I.e. you should implement a deep copy operation.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Rich Cassell
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Sep 19th, 2012
Re: Copy/Paste multiple compositeNodes
Reply #6 - Aug 21st, 2014 at 1:16pm
Print Post  
Hi Stoyan!

I don't use either the selection moved or change handlers so i think it's likely to be the way i've done my Clone method... Can i be a pain and ask you to put what it should look like? I'm returning "Me" at the end and i think that's probably the problem!  Embarrassed

Rich Smiley
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Copy/Paste multiple compositeNodes
Reply #7 - Aug 21st, 2014 at 2:38pm
Print Post  
Hi Rich,

It should not return Me but a copy of the object. E.g. this Clone override should be equivalent to the copy constructor above:

Code
Select All
Public Overrides Function Clone(ByVal clipboard As Boolean) As MindFusion.Diagramming.DiagramItem
    Dim copy As New OrgChartNode
    copy.Title = Me.title
    copy.FullName = Me.FullName
    Return copy
End Function 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Rich Cassell
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Sep 19th, 2012
Re: Copy/Paste multiple compositeNodes
Reply #8 - Aug 21st, 2014 at 2:47pm
Print Post  
Hi Stoyan,

I'm getting an error with this line "Dim copy As New OrgChartNode" because i'm not passing in a parameter... I've tried using "Me" as a parameter (because that's the only DiagramItem i have at that point)... But then i get a NullArgumentException in the constructor... At this line -MyBase.New(newNode)  Huh

Cheers,

Rich
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Copy/Paste multiple compositeNodes
Reply #9 - Aug 21st, 2014 at 2:58pm
Print Post  
I don't know much VB, but if MyBase.New returns a CompositeNode instance it won't do since Clone must return an instance of the custom class. Better create a no-arg constructor that just loads your node's XML template if you use one, or creates child components by code, and then use it in the Clone method to get the empty instance. Finally set its properties to respective values of the current instance.
  
Back to top
 
IP Logged
 
Rich Cassell
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Sep 19th, 2012
Re: Copy/Paste multiple compositeNodes
Reply #10 - Aug 22nd, 2014 at 9:10am
Print Post  
I've done a no-arg constructor and that's definitely improved things in that it now creates new nodes accordingly... The issue i have now is that the nodes that are created are appearing as solid white and doesn't include any of the details on it (The parameters all match the copied nodes perfectly, so not sure why this is happening)... To confuse things further, if i save the diagram with the blank nodes and then load it back up, they appear perfectly that time! Not sure if it's something to do with the drawing of the diagram after they're created but i've tried calling Invalidate but it's not made a difference.

I'll persevere.

Thanks for your help so far Smiley

Rich
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Copy/Paste multiple compositeNodes
Reply #11 - Aug 22nd, 2014 at 1:17pm
Print Post  
Actually it’s better to implement copy constructor instead of Clone because you will be able to call base copy constructor to copy inherited properties. Attached project shows clipboard support for the EmloyeeNode class from the demo.

I hope that helps,
Stoyan
  

clip_vb.zip (Attachment deleted)
Back to top
 
IP Logged
 
Rich Cassell
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Sep 19th, 2012
Re: Copy/Paste multiple compositeNodes
Reply #12 - Aug 22nd, 2014 at 1:59pm
Print Post  
Hi Stoyan,

Thanks for that! I can compare what i have with a working version now Smiley

I still get the error, even though it matches your example, on the MyBase.new(prototype) line at the start of the copy constructor. It's throwing an argumentNullException but i've no idea why... I think i might have to do some reading up because you clearly don't get that error and it appears to be the same code.

Thanks again,

Rich Smiley
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Copy/Paste multiple compositeNodes
Reply #13 - Aug 25th, 2014 at 7:43am
Print Post  
Could you post the exception stack trace?
  
Back to top
 
IP Logged
 
Rich Cassell
Junior Member
**
Offline


I Love MindFusion!

Posts: 63
Joined: Sep 19th, 2012
Re: Copy/Paste multiple compositeNodes
Reply #14 - Aug 26th, 2014 at 7:30am
Print Post  
Hi Stoyan,

The stack trace of the error is below:

Code
Select All
System.ArgumentNullException was unhandled by user code
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: value
  ParamName=value
  Source=mscorlib
  StackTrace:
       at System.IO.BinaryWriter.Write(String value)
       at MindFusion.Diagramming.Components.TextComponent.SaveTo(BinaryWriter writer, PersistContext context)
       at MindFusion.Diagramming.Components.ContainerComponent.SaveTo(BinaryWriter writer, PersistContext context)
       at MindFusion.Diagramming.Components.StackPanel.SaveTo(BinaryWriter writer, PersistContext context)
       at MindFusion.Diagramming.Components.ContainerComponent.SaveTo(BinaryWriter writer, PersistContext context)
       at MindFusion.Diagramming.Components.GridPanel.SaveTo(BinaryWriter writer, PersistContext context)
       at MindFusion.Diagramming.Components.ContentComponent.SaveTo(BinaryWriter writer, PersistContext context)
       at MindFusion.Diagramming.Components.BorderComponent.SaveTo(BinaryWriter writer, PersistContext context)
       at MindFusion.Diagramming.Components.ContainerComponent.SaveTo(BinaryWriter writer, PersistContext context)
       at MindFusion.Diagramming.Components.ContainerComponent.SaveTo(BinaryWriter writer, PersistContext context)
       at MindFusion.Diagramming.CompositeNode..ctor(CompositeNode prototype)
       at SmartScore_FlowDesigner.SmartScore_NodeDesign..ctor(SmartScore_NodeDesign prototype) in F:\IRP Projects\SmartScore+\SmartScore+ FlowDesigner\SmartScore+ NodeDesign.vb:line 156
  InnerException:
 




Thanks,
Rich Smiley
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send TopicPrint