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.
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.
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | 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
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
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
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.
Can't find what you're looking for? Ask the community or share your knowledge.