70002589 Insert a block from another drawing using serialization and deserialization

Article 70002589
Type Wish
Product Engine
Version 1102
Date Added 11/12/2024 12:00:00 AM
Fixed 11.3.2.0 (11/12/2024 12:00:00 AM)
Submitted by VectorDraw Team

Summary

Insert a block from an other drawing using serialization and deserialization

Solution

A new Interface VectorDraw.Professional.vdObjects.IObjectsSerialization was implemented for vdDocument.Activator object
used to manage export and import objects between deferent vdDocument objects
It contains the following methods
System.IO.MemoryStream ExportObject(object obj);
Export the passed object into a stream memory data of bytes
obj: Usually an object that implements VectorDraw.Serialize.IVDSerialise interface
return A stream memory data of bytes

object ReadObject(System.IO.MemoryStream exportedData);
Create a new object from the passed tream memory data of bytes previouly created by ExportObject method
exportedData: A memory data of bytes previouly created by ExportObject method
return A new object clone of the object previouly used by ExportObject

object CloneObject(object obj);
Create a clone of the object object by also cloning any reference serializable properties of the passed object
obj:An object to create a clone usually an object that implements VectorDraw.Serialize.IVDSerialise interface.This object can then safelly added into an other vdDocument object
returns A clone of the passed object


Example with ExportObject and ReadObject

//import a known block name from an existing drawing on disk to our open working document
//same can be done for example with entities in model space or Layers etc..
vdDocument doc;//suppose it is our working document which has previously created
String sourceFilePath = @"D:\Trampa\Trampa2\STAVROS\Support\New folder\AddBlockFromDWGFileMemoryReleaseIssue\DWG\KK-HYDRA Compact Basic-06.03.dwg";
String blockName = "KK-HYDRA Compact Basic-06.03-POL-INT-L";
//create a new temporary document to hold to open the existing drawing on disk
vdDocument tmpdoc = doc.Activator.CreateInstance("vdDocument") as vdDocument;
tmpdoc.Open(sourceFilePath);
//find the desired block name in the document
//Note : we can not set the same vdBlock object from on document to an other.This is not permited.
//Also the block reference to other objects that also needed
//Serialization will write the vdBlock object with all of its depends objets.
vdBlock blk = tmpdoc.Blocks.FindName(blockName);
if (blk == null) return;
//get the vdDocument Activator object that implements the IObjectsSerialization interface used to Export / import objects between documents
IObjectsSerialization objser = doc.Activator as IObjectsSerialization;
System.IO.MemoryStream retstream = objser.ExportObject(blk);
vdBlock block_new = objser.ReadObject(retstream) as vdBlock;
if (block_new == null) return;
//set this flag because it is very possible imported object to have same handle with existing objects in drawing
doc.Openflags |= vdDocument.OpenFlagsEnum.RecoverDublicateHandles;
//if a block with same name already exist in the drawing change the block name with a uniqe name
if (doc.Blocks.FindName(block_new.Name) != null) block_new.Name = doc.Blocks.getUniqueTableName(block_new.Name);
//select the new document for the new created block
block_new.SetUnRegisterDocument(doc);
//create a new vdInsert object that reference the new inserted block and add it to the docuemnt model entities
vdInsert ins = new vdInsert();
//needed to set all needed properties with document defaults like Layer LineType Color and other styles
ins.SetUnRegisterDocument(doc);
ins.setDocumentDefaults();
ins.Block = block_new;
doc.Model.Entities.AddItem(ins);
//redraw to see changes
doc.ZoomExtents();
doc.Redraw(false);              

Example with CloneObject
//import a known block name from an existing drawing on disk to our open working document
//same can be done for example with entities in model space or Layers etc..
vdDocument doc;//suppose it is our working document which has previously created
String sourceFilePath = @"D:\Trampa\Trampa2\STAVROS\Support\New folder\AddBlockFromDWGFileMemoryReleaseIssue\DWG\KK-HYDRA Compact Basic-06.03.dwg";
String blockName = "KK-HYDRA Compact Basic-06.03-POL-INT-L";
//create a new temporary document to hold to open the existing drawing on disk
vdDocument tmpdoc = doc.Activator.CreateInstance("vdDocument") as vdDocument;
tmpdoc.Open(sourceFilePath);
//find the desired block name in the document
//Note : we can not set the same vdBlock object from on document to an other.This is not permited.
//Also the block reference to other objects that also needed
//Serialization will write the vdBlock object with all of its depends objets.
vdBlock blk = tmpdoc.Blocks.FindName(blockName);
if (blk == null) return;
//get the vdDocument Activator object that implements the IObjectsSerialization interface used to Export / import objects between documents
IObjectsSerialization objser = doc.Activator as IObjectsSerialization;
vdBlock block_new = objser.CloneObject(blk) as vdBlock;
if (block_new == null) return;
//set this flag because it is very possible imported object to have same handle with existing objects in drawing
doc.Openflags |= vdDocument.OpenFlagsEnum.RecoverDublicateHandles;
//if a block with same name already exist in the drawing change the block name with a uniqe name
if (doc.Blocks.FindName(block_new.Name) != null) block_new.Name = doc.Blocks.getUniqueTableName(block_new.Name);
//select the new document for the new created block
block_new.SetUnRegisterDocument(doc);
//create a new vdInsert object that reference the new inserted block and add it to the docuemnt model entities
vdInsert ins = new vdInsert();
//needed to set all needed properties with document defaults like Layer LineType Color and other styles
ins.SetUnRegisterDocument(doc);
ins.setDocumentDefaults();
ins.Block = block_new;
doc.Model.Entities.AddItem(ins);
//redraw to see changes
doc.ZoomExtents();
doc.Redraw(false);

Example using the data transfer of Clipboard object data
//Copy entities of grip selection from source document of your application
//to destination document active action layout of an other instance of your application
//using the data transfer of Clipboard object data
xcopy(sourceDocument);//call for source application instance document
xpaste(DestDocument);//call for destination application instance document
void xcopy(vdDocument doc){
    vdSelection set = doc.GetGripSelection();
    if (set.Count == 0) return;
    MemoryStream stream = ((IObjectsSerialization)doc.Activator).ExportObject(set);
    System.Windows.Forms.DataObject dataobject = new DataObject();
    dataobject.SetData("xvdf", stream);
    System.Windows.Forms.Clipboard.SetDataObject(dataobject);
}
void xpaste(vdDocument doc){
    System.IO.MemoryStream data = System.Windows.Forms.Clipboard.GetData("xvdf") as MemoryStream;
    if (data == null) return;
    vdSelection set = ((IObjectsSerialization)doc.Activator).ReadObject(data) as vdSelection;
    if (set == null) return;
    doc.Openflags |= vdDocument.OpenFlagsEnum.RecoverDublicateHandles;
    foreach (vdFigure item in set) doc.ActionLayout.Entities.AddItem(item);
    doc.Redraw(false);
}

Send comments on this topic.