ReadDwgFile from stream

ReadDwgFile from stream

Ahmadi_rad
Enthusiast Enthusiast
1,222 Views
5 Replies
Message 1 of 6

ReadDwgFile from stream

Ahmadi_rad
Enthusiast
Enthusiast

Hello

I have a drawing file containing some standard blocks. Using this snippet to read drawing from a memory stream:

                using (Stream fs = Assembly.GetExecutingAssembly().GetManifestResourceStream("Resources.StandardBlocks.dwg"))
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        fs.CopyTo(ms);
                        byte[] bts = ms.ToArray();

                        GCHandle pinnedArray = GCHandle.Alloc(bts, GCHandleType.Pinned);
                        IntPtr pointer = pinnedArray.AddrOfPinnedObject();
                        Database sourceDb = new Database(false, true);
                        try
                        {
                            sourceDb.ReadDwgFile(pointer, false, "");
                        }
                        catch(System.Exception e)
                        {

                        }
// Some code to work with sourceDb
                        sourceDb.Dispose();
                        pinnedArray.Free();

                    }
                }

The point is that: when code reaches ReadDwgFile, autocad crashes without  even reaching to catch statement.

if it helps, standardBlocks.dwg contains a block of an OLE excel sheet.

 

Could you please help

0 Likes
Accepted solutions (1)
1,223 Views
5 Replies
Replies (5)
Message 2 of 6

artc2
Autodesk
Autodesk
Accepted solution
The ReadDwgFile() method that takes an IntPtr as the first argument expects the IntPtr to be a pointer to an object of one of our internal C++ classes used for reading dwg files. The code static_casts the pointer to that type, so if the object isn't that type, then there will be problems such as what you've experienced.
Message 3 of 6

Ahmadi_rad
Enthusiast
Enthusiast

thanks for reply

So, How can I use a memory stream for that purpose? I don't directly have a file, whatever it is, it is stored in database or embedded resource in project.

 

 

0 Likes
Message 4 of 6

norman.yuan
Mentor
Mentor

You would save the Stream as DWG file in the local disk, and then read it into the new Database. You would delete it afterwards, if necessary.

 

Since your code is AutoCAD add-in and AutoCAD itself uses temporary files a lot. The best practice would be to obtain AutoCAD's temporary file location from current running profile (accessing it via Application.Preferences property), so that your code would have the permission to save/delete temporary file.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 6

Ahmadi_rad
Enthusiast
Enthusiast

Thanks

I found this post about using preferences object. 

But in my visual studio, I can't find class of AcadPreferences as shown in sample. Which dll shall I add to references?

 

 

0 Likes
Message 6 of 6

Alexander.Rivilis
Mentor
Mentor

I think you can easy use Path.GetTempFileName()

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | 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

0 Likes