.NET

Reply
Member
wzahner
Posts: 3
Registered: ‎01-08-2013
Message 1 of 5 (405 Views)

problem reading in dxf/dwg files

405 Views, 4 Replies
01-08-2013 06:25 AM

hello everyone,

i am developing some code to read in mass dxf OR dwg files, seperate the two, then convert each to a database to be read later. however, whenever i run it, i get this error:

System.AccessViolationException: Attempted to read or write protected memory. this is often an indication that other memory is corrupt.

     at AcDbDatabase.dxfIn(AcDbDatabase*, Char*, Char*)

     at Autodesk.AutoCAD.DatabaseServices.Database.DxfIn(String fileName, String logFilename)

     at ZahnerCommands.Commands.GetFiles()....line 72

 

here is the code that i have so far. i have been tinkering around with it for a few days and cannot seem to get it to work. the dxf file i have chosen works perfectly well, and i am using autocad 2011. thank you for all of your help

 

using

Autodesk.AutoCAD.ApplicationServices;

using

Autodesk.AutoCAD.DatabaseServices;

using

Autodesk.AutoCAD.EditorInput;

using

Autodesk.AutoCAD.Runtime;

using

System.Runtime.InteropServices;

using

Autodesk.AutoCAD.Interop;

using

Autodesk.AutoCAD.Interop.Common;

 

....

       

publicvoid GetFiles()

        {

Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

           

Editor ed = doc.Editor;

            ed.WriteMessage(

"\nChoose an input folder");

            inFolderPath = ChooseFolder();

            dwgFilePaths =

Directory.GetFiles(inFolderPath, "*.dwg");

            dxfFilePaths =

Directory.GetFiles(inFolderPath, "*.dxf");

           

// Converts to dxf files into readable databases. refer to the transaction manager to read specific info

            dxfInfo =

new Database[dxfFilePaths.Length];

           

for (int i = 0; i < dxfFilePaths.Length; i++)

            {

               

string tempFile = inFolderPath + "\\" + (i+1) + "TEMPLOG.txt";

               

                   

try

                    {

                       

using (File.CreateText(tempFile))

                        {

                            dxfInfo[i] =

new Database(false, true);

                           

using (dxfInfo[i])

                            {

                               

try

                                {

                                    dxfInfo[i].DxfIn(dxfFilePaths[i], tempFile);

//Autodesk.AutoCAD.DatabaseServices. perhaps? this is line 72

                                }

                               

catch (System.Exception wx)

                                {

                                   

MessageBox.Show("Unable to read the " + (i + 1) + " file. Check TEMPLOG.txt and\n" +wx, "Can't Read File", MessageBoxButtons.OK, MessageBoxIcon.Error);

                                   

return;

                                }

                            }

                        }

                    }

                   

catch // this was to catch for any old dxf files; i got the idea from kean's blog

                    {

                        dxfInfo[i] =

new Database(true, false);

                       

using (dxfInfo[i])

                        {

                           

try

                            {

                                dxfInfo[i].DxfIn(dxfFilePaths[i], tempFile);

                            }

                           

catch (System.Exception wx)

                            {

                               

MessageBox.Show("Unable to read the " + (i+1) + " file. Check TEMPLOG.txt, and\n" +wx, "Can't Read File", MessageBoxButtons.OK, MessageBoxIcon.Error);

                               

return;

                            }

                        }

                    }

                }

 

if there is anything else i can provide please let me know. i am at my wits end trying to figure out what is wrong.

ps: if this is the wrong section please direct me to the correct one.

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 2 of 5 (401 Views)

Re: problem reading in dxf/dwg files

01-08-2013 06:30 AM in reply to: wzahner

Try to remove line:

using (File.CreateText(tempFile))

 If you open tempFile for write then method Database.DxfIn can not open this file for write also.


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

Member
wzahner
Posts: 3
Registered: ‎01-08-2013
Message 3 of 5 (387 Views)

Re: problem reading in dxf/dwg files

01-08-2013 07:24 AM in reply to: wzahner

thank you very much! that seemed to have solved the read in problem, but now i am having a problem saving the database file as a .dwg. i feel i may have over simplified this process, but one of the main points of this program is to be a batch converter between the two file types (thus the arrays). i am getting a very similiar error, except with the saveAs command instead. here is the code i am working on:

 

(same using statements as before)

       

public void DXFtoDWG()

        {

try

{

                outFileInfo =

newDatabase[inFileInfo.Length];

                inFileInfo.CopyTo(outFileInfo, 0);

//the data from dxfInfo has been copied to inFileInfo already

               

string prefix = inFolderPath;

                newDWGFilePaths =

newString[dxfFilePaths.Length];

               

for (int i = 0; i < outFileInfo.Length; i++) // this is line 170

                {

                    newDWGFilePaths[i] = dxfFilePaths[i].Substring(0, dxfFilePaths[i].Length - 4);

                    newDWGFilePaths[i] = outFolderPath + dxfFilePaths[i].Substring(prefix.Length) +

".dwg";

                   

//using (File.Create(newDWGFilePaths[i])) i have tried it with and without this using statement

                   

//{

                   

//change the dwgVersion to newer updates of autocad (2013 would be AC1027)

                   

//using (outFileInfo[i]) i have tried it with and without this using statement

                   

//{

                        outFileInfo[i].SaveAs(newDWGFilePaths[i],

DwgVersion.AC1024);

                       

//}

                   

//

                }

            }

 

thank you again.

 

p.s.: this is the exact error:

System.AccessViolationException: Attempted to read or write protected memory. this is often an indication that other memory is corrupt.

     at AcDbDatabase.saveAs(AcDbDatabase*, Char*, Boolean, AcDbDwgVersion, SecurityParams*)

     at Autodesk.AutoCAD.DatabaseServices.Database.SaveAs(String fileName, DwgVersion version)

     at ZahnerCommands.Commands.DXFtoDWG....line 170

 

enabling the using statements moves the error to 179, which is the actual saveAs statement. thank you for all your help

Member
wzahner
Posts: 3
Registered: ‎01-08-2013
Message 4 of 5 (374 Views)

Re: problem reading in dxf/dwg files

01-08-2013 08:08 AM in reply to: wzahner

i removed the using statements and same error occurs at the saveAs place still. all other using statements in the code have been properly closed.

 

p.s. sorry for the double post but i couldnt edit it again

ADN Support Specialist
Balaji_Ram
Posts: 702
Registered: ‎03-21-2011
Message 5 of 5 (328 Views)

Re: problem reading in dxf/dwg files

01-15-2013 07:38 AM in reply to: wzahner

Hello,

 

If you still have this issue, can you please share a buildable sample project to reproduce the crash ?

I can then debug it to see what is causing it.

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.