Anybody knows if is there a way to duplicate a GH_Component (or a IGH_DocumentObject) from code?
Like copy/paste within the GH Canvas.
Thanks
Anybody knows if is there a way to duplicate a GH_Component (or a IGH_DocumentObject) from code?
Like copy/paste within the GH Canvas.
Thanks
There is no duplication as such. You have to serialise, then deserialise it. The GH_DocumentIO class has some handy methods for doing this in memory.
Thanks, working fine!
Hi @Harper can you elaborate how you solved it please?
Hi, how do you obtain it?
Thank you
Hi, I know this is an old thread but I found a way that works to achieve this and thought I could share it here:
GH_Document ghDoc = this.Component.OnPingDocument();
List<IGH_DocumentObject> selectedObjects = ghDoc.SelectedObjects();GH_Document newDoc = new GH_Document();
GH_IO.Serialization.GH_Archive archive = new GH_IO.Serialization.GH_Archive();
GH_IO.Serialization.GH_IWriter writer = archive.CreateTopLevelNode(“CopiedElements”);
newDoc.Write(writer, selectedObjects);string serializedObjects = archive.Serialize_Xml();
archive.Deserialize_Xml(serializedObjects);
archive.ExtractObject(newDoc, “CopiedElements”);
newDoc.MutateAllIds();// move copied canvas objects
RectangleF rect = newDoc.BoundingBox();
newDoc.TranslateObjects(new Size((int) rect.Width + 50, 0), false);// deselect all previous objects
ghDoc.DeselectAll();bool mergeSuccess = ghDoc.MergeDocument(newDoc);
// Get newly selected objects
List<IGH_DocumentObject> newSelectedObjects = ghDoc.SelectedObjects();// Refresh all the newly added components
newSelectedObjects.ForEach(x => x.ExpireSolution(true));