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

cannot werit in the memorry

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
osayed3202
1051 Views, 10 Replies

cannot werit in the memorry

Hi guys,

I am working in project that purge and audit a bunch of file selected in listview control, the code shows the code generates errors cannot write in the memory and stopes in the red line.

 

 

 

DocumentCollection acdMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
string strFullPath;

 

Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;

using (Transaction Tx = db.TransactionManager.StartTransaction())
{
app.ActiveDocument.SendCommand("regen ");
app.ActiveDocument.PurgeAll();
app.ActiveDocument.PurgeAll();
app.ActiveDocument.AuditInfo(true);
app.ActiveDocument.SendCommand("regen ");

RegAppTable table = Tx.GetObject(db.RegAppTableId, OpenMode.ForRead) as RegAppTable;

ObjectIdCollection regIds = new ObjectIdCollection();
foreach (ObjectId id in table)
{
regIds.Add(id);
}

//this function will remove all
//app names which are used in the drawing file
db.Purge(regIds);


foreach (ObjectId id in regIds)
{
DBObject obj = Tx.GetObject(id, OpenMode.ForWrite);
// the program stops here, cannot write on the memory
obj.Erase();
}
Tx.Commit();
}
}

 

Best Regards
O.Sayed
CAD developer
10 REPLIES 10
Message 2 of 11

Hi,

 

I would verify if the id is valid and is not erased before open it for write.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 11

thank you very much fo your support and time.

would you please clarify what you mean.

 

 

Best Regards
O.Sayed
CAD developer
Message 4 of 11
mzakiralam
in reply to: osayed3202

What Alfred suggested you to verify the id something like below:
If id IsNot Nothing
DBObject obj = Tx.GetObject(id, OpenMode.ForWrite);
End If
Message 5 of 11

Hi,

 

>> would you please clarify what you mean.

Before opening objects for read/write I meant to verify the validity of the ObjectID:

 

if (Id.IsValid) andalso (not Id.IsErased) then

...now you can access the object in read/write mode.

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 6 of 11


@osayed3202 wrote:

Hi guys,

I am working in project that purge and audit a bunch of file selected in listview control, the code shows the code generates errors cannot write in the memory and stopes in the red line.


Do I understand correctly that you open many files sequentially? Appropriately you work in the context of the application and must lock document.

using(DocumentLock docLock = doc.LockDocument()) {
  using (Transaction Tx = db.TransactionManager.StartTransaction()) {
    // your's code ...
  }
}

I also moved code using app.ActiveDocument before the transaction...

Also I checked id before add it to regIds:

ObjectIdCollection regIds = new ObjectIdCollection();
foreach (ObjectId id in table) {
  if (!id.IsErased && !id.IsEffectivelyErased) 
     regIds.Add(id);
}

 

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







//string strTextFileName = SaveDialog.FileName; foreach (ListViewItem itemRow in this.lstView.Items) { ListViewItem item = lstView.Items[itemRow.Index]; string lines = item.SubItems[0].Text; string col = item.SubItems[1].Text; strFullPath = col + "\\" + lines; AcadApplication app = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication; app.Documents.Open(strFullPath); if (chkBoxUnregApp == true) { app.ActiveDocument.SendCommand("regen "); app.ActiveDocument.PurgeAll(); app.ActiveDocument.PurgeAll(); app.ActiveDocument.AuditInfo(true); app.ActiveDocument.SendCommand("regen "); using (Transaction Tx = db.TransactionManager.StartTransaction()) { RegAppTable table = Tx.GetObject(db.RegAppTableId, OpenMode.ForRead) as RegAppTable; ObjectIdCollection regIds = new ObjectIdCollection(); foreach (ObjectId id in table) { if (!id.IsErased && !id.IsEffectivelyErased) regIds.Add(id); } //this function will remove all //app names which are used in the drawing file db.Purge(regIds); if (regIds != null) { foreach (ObjectId id in regIds) { DBObject obj = Tx.GetObject(id, OpenMode.ForWrite); // the program stops here, cannot write on the memory obj.Erase(); } } Tx.Commit(); } }

Dear Alexander,

I changed the code as you see above but the error appear like the attached image shows it cannot write in the file eLockViolation.

 

Thanks for all of you for your time 

Osayed

 

Best Regards
O.Sayed
CAD developer
Message 8 of 11
mzakiralam
in reply to: osayed3202

You have to lock the document before doing any Operation. Probably you are using this Event from a non-modal form, this is why , it is occuring.
Message 9 of 11

You did not read my post carefully. I wrote that you should wrap your code in:

using(DocumentLock docLock = doc.LockDocument()) {
// your's code here!
}

 But you ignore me... Sorry... 😞 That is why exception eLockViolation occur!

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

Yeaaa it works now thanks guys for your time and support,

but unfortunatly when the current file finishes  and when openning another file from the listview it bring Fatal error message that always happen at the second dwg file.

Am I have to dispose or somthing like this

the code here and also the fatal erroe image attached.

 

thanks alot guys.

 

 //string strTextFileName = SaveDialog.FileName;
                foreach (ListViewItem itemRow in this.lstView.Items)
                {

                    ListViewItem item = lstView.Items[itemRow.Index];

                    string lines = item.SubItems[0].Text;
                    string col = item.SubItems[1].Text;
                    strFullPath = col + "\\" + lines;

                    AcadApplication app = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;

                    app.Documents.Open(strFullPath);

                    if (chkBoxUnregApp == true)
                    {
                        app.ActiveDocument.SendCommand("regen ");
                        app.ActiveDocument.PurgeAll();
                        app.ActiveDocument.PurgeAll();
                        app.ActiveDocument.AuditInfo(true);
                        app.ActiveDocument.SendCommand("regen ");

                        using (Transaction Tx = db.TransactionManager.StartTransaction())
                        {


                                RegAppTable table = Tx.GetObject(db.RegAppTableId, OpenMode.ForRead) as RegAppTable;

                                ObjectIdCollection regIds = new ObjectIdCollection();
                                foreach (ObjectId id in table)
                                {
                                    if (!id.IsErased && !id.IsEffectivelyErased)
                                        regIds.Add(id);
                                }

                                //this function will remove all
                                //app names which are used in the drawing file
                                db.Purge(regIds);

                                if (regIds != null)
                                {
                                    using (DocumentLock docLock = doc.LockDocument())
                                    {
                                    foreach (ObjectId id in regIds)
                                    {
                                        DBObject obj = Tx.GetObject(id, OpenMode.ForWrite);
                                        // the program stops here, cannot write on the memory
                                        obj.Erase();
                                    }
                                    
                                }
                                Tx.Commit();
                               
                            }
                        }
                    }

 

 

Best Regards
O.Sayed
CAD developer
Message 11 of 11
osayed3202
in reply to: osayed3202

hi Guys i checked but I found the fatal error has been produced at using transaction line but when i use the Tx.Dispose()

 

thanks alot

Best Regards
O.Sayed
CAD developer

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