Prod.: Engine, ver.: 6, ID: 60000414, HowTo : How can insert a drawing as block that was stored in a database with ToStream method ?

HowTo : How can insert a drawing as block that was stored in a database with ToStream method ?

Article60000414
TypeHowTo
ProductEngine
Version6
Date Added2/6/2008
Submitted byPablo Linares
Keywords

Subject

How can insert a drawing as block that was stored in a database with ToStream method ?

Summary

How can insert a drawing as block that was stored in a database with ToStream method ?

Solution

You can save/read a drawing (document) in/from a database using the ToStream method of vdDocument to save the drawing in a System.IO.MemoryStream and LoadFromMemory to read it, like :
object ByteArray = null;
System.IO.MemoryStream memorystream = Doc.ToStream();
//doc can be the BaseControl.ActiveDocument or vdDoc1.Document or vdFrame.BaseControl.ActiveDocument
if (memorystream == null) return 0;
ByteArray = memorystream.ToArray();
int size = (int)memorystream.Length;
memorystream.Close();
// then SAVE the ByteArray to your database
and
// Read the Bytes from database and store it in the ByteArray and then :
System.IO.MemoryStream memorystream = new System.IO.MemoryStream((byte[])ByteArray);
memorystream.Position = 0;
Doc.LoadFromMemory(memorystream);
memorystream.Close();
You can try the above code in a new project and see that is working. You can also use compression, see the article : http://www.vdraw.com/Export6/60000177.htm using the ToStream(true) and LoadFromMemory(stream, true).
 
If you like to insert as a block a document that is already stored in a database then try this :
Create a new C# 2005 project and in the new form add a vdScrollable control (named vdSC_From), a vdDocument control named vdDocComp_Temp, a vdFramedControl named vdFC_To and 2 buttons (button1 and button2).

The sample has no DATABASE code inside and for this a vdScrollable control ( vdSC_from control) is used to create and save the document in a ByteArray so it will be used to simulate the fill of the ByteArray from a database record/field. Then this ByteArray is "transformed" back in a Document using a temporary vdDocument control (vdDocComp_Temp), and then the AddFromDocument method of vdBlocks object is used to produce the vdBlock object that is inserted in the drawing (as vdInsert) of the vdFramed Control. See also the remarks bellow.

The code should be :

public partial class Form1 : Form
{
   
public
Form1()
    {
        InitializeComponent();
    }

//Instead of a database, a VectorDraw Scrollable control (named vdSC_From) is used to create a drawing to a bytearray
//Also a vdDocument component (named vdDocComp_Temp) is used so it can be filled with the drawing that is stored in the database (named DocumentToBlock)
//after that in the Target's Document (the vdFC_To control) a block is created with AddFromDocument and inserted

    private void button1_Click(object sender, EventArgs e)
    {
   
//instead of a database, a VectorDraw Scrollable control (vdSC_From) is used to create a drawing to a bytearray
   
    vdSC_From.BaseControl.ActiveDocument.New();
        vdSC_From.BaseControl.ActiveDocument.CommandAction.CmdCircle(
new VectorDraw.Geometry.gPoint(0, 0, 0), (double)2.0);
        vdSC_From.BaseControl.ActiveDocument.ActivePenColor.ColorIndex = 1;
        vdSC_From.BaseControl.ActiveDocument.CommandAction.CmdEllipse(
new VectorDraw.Geometry.gPoint(0, 0, 0), new VectorDraw.Geometry.gPoint(1, 1, 0), (double)3.0);
       
object ByteArray = null;
        System.IO.
MemoryStream memorystream = vdSC_From.BaseControl.ActiveDocument.ToStream(true); //save to memory compressed
       
if (memorystream == null) return ;
        ByteArray = memorystream.ToArray();
       
int size = (int)memorystream.Length;
        memorystream.Close();
        // the drawing/document is created and saved to a public ByteArray

        // now we will read this to the temporary document
       
VectorDraw.Professional.vdObjects.vdDocument DocumentToBlock = vdDocComp_Temp.Document;
        DocumentToBlock.New();
        memorystream =
new System.IO.MemoryStream((byte[])ByteArray);
        memorystream.Position = 0;
        DocumentToBlock.LoadFromMemory(memorystream,
true);//Read from memory compressed
       
memorystream.Close();

        //and add the entities of this document to a block
        //We create a block object and initialize it's default properties.
       
VectorDraw.Professional.vdCollections.vdBlocks blks = vdFC_To.BaseControl.ActiveDocument.Blocks;
        VectorDraw.Professional.vdPrimaries.
vdBlock blk = blks.AddFromDocument("CustomBlock1",DocumentToBlock,true);
        blk.Update();
       
//add the block to the blocks collection
       
vdFC_To.BaseControl.ActiveDocument.Blocks.AddItem(blk);
       
//and now insert it;
       
vdFC_To.BaseControl.ActiveDocument.CommandAction.CmdInsert("CustomBlock1", new VectorDraw.Geometry.gPoint(0.0d, 3.0d, 0.0d), 1d, 2d, 2d);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //instead of a database, a VectorDraw Scrollable control (vdSC_From) is used to create a drawing to a bytearray
       
vdSC_From.BaseControl.ActiveDocument.New();
        vdSC_From.BaseControl.ActiveDocument.CommandAction.CmdRect(
new VectorDraw.Geometry.gPoint(2.0d, 1d, 0d), new VectorDraw.Geometry.gPoint(3.0d, 4d, 0d));
        vdSC_From.BaseControl.ActiveDocument.ActivePenColor.ColorIndex = 3;
        vdSC_From.BaseControl.ActiveDocument.CommandAction.CmdArc(
new VectorDraw.Geometry.gPoint(4.0d, 0d, 0d),2d, 1.0d,2.0d);
       
object ByteArray = null;
        System.IO.
MemoryStream memorystream = vdSC_From.BaseControl.ActiveDocument.ToStream(true);
       
if (memorystream == null) return;
        ByteArray = memorystream.ToArray();
       
int size = (int)memorystream.Length;
        memorystream.Close();
       
// the drawing/document is created and saved to a public ByteArray

        // now we will read this to the temporary document
   
    VectorDraw.Professional.vdObjects.vdDocument DocumentToBlock = vdDocComp_Temp.Document;
        DocumentToBlock.New();
        memorystream =
new System.IO.MemoryStream((byte[])ByteArray);
        memorystream.Position = 0;
        DocumentToBlock.LoadFromMemory(memorystream,
true);
        memorystream.Close();

        //and add the entities of this document to a block
        //We create a block object and initialize it's default properties.
       
VectorDraw.Professional.vdCollections.vdBlocks blks = vdFC_To.BaseControl.ActiveDocument.Blocks;
        VectorDraw.Professional.vdPrimaries.
vdBlock blk = blks.AddFromDocument("CustomBlock2", DocumentToBlock, true);
        blk.Update();
       
//add the block to the blocks collection
       
vdFC_To.BaseControl.ActiveDocument.Blocks.AddItem(blk);
       
//and now insert it;
       
vdFC_To.BaseControl.ActiveDocument.CommandAction.CmdInsert("CustomBlock2", new VectorDraw.Geometry.gPoint(0.0d, 3.0d, 0.0d), 1d, 2d, 2d);
    }
}