.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Memory Stream and XRecords

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
NikWach
726 Views, 5 Replies

Memory Stream and XRecords

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);
}
}

 

5 REPLIES 5
Message 2 of 6
Matti72
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.

Message 3 of 6
Alexander.Rivilis
in reply to: NikWach

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

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 4 of 6
NikWach
in reply to: Matti72

thx, it works

Message 5 of 6
NikWach
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?

 

 

Message 6 of 6
Matti72
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

 

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost