.NET

Reply
Contributor
osayed3202
Posts: 12
Registered: ‎01-05-2014
Message 1 of 11 (249 Views)
Accepted Solution

cannot werit in the memorry

249 Views, 10 Replies
01-14-2014 06:20 AM

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
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.

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... :smileysad: That is why exception eLockViolation occur!

*Expert Elite*
Alfred.NESWADBA
Posts: 9,071
Registered: ‎06-29-2007
Message 2 of 11 (236 Views)

Re: cannot werit in the memorry

01-14-2014 08:02 AM in reply to: osayed3202

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
-------------------------------------------------------------------------
Contributor
osayed3202
Posts: 12
Registered: ‎01-05-2014
Message 3 of 11 (215 Views)

Re: cannot werit in the memorry

01-15-2014 01:31 AM in reply to: Alfred.NESWADBA

thank you very much fo your support and time.

would you please clarify what you mean.

 

 

Best Regards
O.Sayed
CAD developer
Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 4 of 11 (205 Views)

Re: cannot werit in the memorry

01-15-2014 04:02 AM 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
*Expert Elite*
Alfred.NESWADBA
Posts: 9,071
Registered: ‎06-29-2007
Message 5 of 11 (197 Views)

Re: cannot werit in the memorry

01-15-2014 11:25 AM in reply to: osayed3202

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
-------------------------------------------------------------------------
Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 6 of 11 (189 Views)

Re: cannot werit in the memorry

01-15-2014 02:06 PM in reply to: osayed3202

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

 


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

Contributor
osayed3202
Posts: 12
Registered: ‎01-05-2014
Message 7 of 11 (139 Views)

Re: cannot werit in the memorry

01-20-2014 01:03 AM in reply to: Alexander.Rivilis






//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
Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 8 of 11 (138 Views)

Re: cannot werit in the memorry

01-20-2014 01:13 AM 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.
Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 9 of 11 (122 Views)

Re: cannot werit in the memorry

01-20-2014 03:42 AM in reply to: osayed3202

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... :smileysad: That is why exception eLockViolation occur!


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

Contributor
osayed3202
Posts: 12
Registered: ‎01-05-2014
Message 10 of 11 (111 Views)

Re: cannot werit in the memorry

01-20-2014 06:08 AM in reply to: Alexander.Rivilis

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
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

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