.NET

Reply
Contributor
Posts: 17
Registered: ‎09-19-2013
Message 1 of 6 (224 Views)
Accepted Solution

Memory Stream and XRecords

224 Views, 5 Replies
10-07-2013 11:43 PM

Hello everybody,

 

I try to serilize a class with a binaryserialization and attach it as an xrecord to an entity using the memory stream. But I get an error occurred, when I try to run my test1 command (Cannot convert from MemoryStream into Byte[] Array). Here's the code:

 

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Colors;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using Autodesk.AutoCAD.Windows;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Drawing.Imaging;
using Autodesk.Windows;
using System.Data;
using Autodesk.AutoCAD.ApplicationServices.Core;

 

[Serializable]
public enum DogColor
{
Brown,
Black,
Mottled
}

[Serializable]
public class Dog
{
public String Name
{
get;
set;
}

public DogColor Color
{
get;
set;
}

public override String ToString()
{
return String.Format("Dog: {0}/{1}", Name, Color);
}
}

 

[CommandMethod("TEST1")]
public void Test1()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\nSelect an entity: ");
peo.AllowNone = false;
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status == PromptStatus.OK)
{
ObjectId id = per.ObjectId;

 

 

Dog myDog = new Dog();
myDog.Name = "Foo";
myDog.Color = DogColor.Brown;
ResultBuffer resbuf = new ResultBuffer();
MemoryStream memorystream = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(memorystream, myDog);
resbuf.Add(new TypedValue((int)DxfCode.BinaryChunk, memorystream));


SetXrecord(id, "test", resbuf);
}
}

 

Contributor
Posts: 25
Registered: ‎09-28-2004
Message 2 of 6 (215 Views)

Re: Memory Stream and XRecords

10-08-2013 12:30 AM in reply to: NikWach

The MemoryStream has a method ToArray which converts it to a Byte[]...

 

So change your line to

 

resbuf.Add(new TypedValue((int)DxfCode.BinaryChunk, memorystream.ToArray()));

 And the error will go away.

Moderator
Alexander.Rivilis
Posts: 1,346
Registered: ‎04-09-2008
Message 3 of 6 (208 Views)

Re: Memory Stream and XRecords

10-08-2013 12:53 AM in reply to: NikWach

As far as I remember BinaryChunk length have to be less than 255. So you have to split binary.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Contributor
Posts: 17
Registered: ‎09-19-2013
Message 4 of 6 (196 Views)

Re: Memory Stream and XRecords

10-08-2013 02:33 AM in reply to: Matti72

thx, it works

Contributor
Posts: 17
Registered: ‎09-19-2013
Message 5 of 6 (193 Views)

Re: Memory Stream and XRecords

10-08-2013 02:35 AM in reply to: Alexander.Rivilis

I dont know, if it is really so.

 

I do the following changes:

 

string Mystring = "Lorem ipsum dolor sit ametor sit amet, consectetur adipiscing elit. Pellentesque quis tristique risus. Vivamus dignissim lacinia ante, eu pretium lacus congue eu. Pellentesque in pellentesque eros. Phasellus volutpat viverra erat vel ultrices. Praesent mattis metus.Lorem ipsum dolor sit ametor sit amet, consectetur adipiscing elit. Pellentesque quis tristique risus. Vivamus dignissim lacinia ante, eu pretium lacus congue eu. Pellentesque in pellentesque eros. Phasellus volutpat viverra erat vel ultrices. Praesent mattis metus.";

 

bf.Serialize(memorystream, Mystring);

 

Than I attach it, and it works. 

 

I cannot get the byte array to be shown:

 

Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowAlertDialog("The attached data is: " + memorystream.GetBuffer());

 

this code doesn't work.

 

How I have to do the splitting?

 

 

Contributor
Posts: 25
Registered: ‎09-28-2004
Message 6 of 6 (183 Views)

Re: Memory Stream and XRecords

10-08-2013 02:59 AM in reply to: NikWach

If you serialize data you have to deserialize it, before you can use it again... 

 

memorystream.Seek(0, System.IO.SeekOrigin.Begin);
string myStr = bf.Deserialize(memorystream) as string;
Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowAlertDialog("The attached data is: " + myStr);

 edit:

In the line 

string myStr = bf.Deserialize(memorystream) as string;

you have (of course) to use the serialized class. So it is string in the example in your last post, in the example in your first post it would be 

Dog myDog = bf.Deserialize(memorystream) as Dog;

And as always it would be a good idea to test the result, if it is null

 

 

 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community