| Article | 60000414 |
| Type | HowTo |
| Product | Engine |
| Date Added | 2/6/2008 12:00:00 AM |
| Fixed | (2/6/2008 12:00:00 AM) |
| Submitted by | Pablo Linares |
Summary
How can insert a drawing as block that was stored in a database with ToStream method ?
Solution
andobject 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
// 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();
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);
}
}
