Author Topic: eWrongDatabase on Database.Wblock since AutoCAD 2017  (Read 2966 times)

0 Members and 1 Guest are viewing this topic.

MarioR

  • Newt
  • Posts: 64
eWrongDatabase on Database.Wblock since AutoCAD 2017
« on: June 19, 2018, 08:23:28 AM »
Hello,

i have a Problem. Since today i test my code for change the autocadversion from 2014 to 2017.
On 2014 works all fine, but on 2017 trows an eWrongDatase error. What is my fail?

Code worke on 2014:
Code: [Select]
                                                    // objectIds selected by typevalue selection on editor
                                                    selectedIds = new ObjectIdCollection(acPromptSelectionResult.Value.GetObjectIds());
                                                    if (selectedIds != null)
                                                    {
                                                        if (selectedIds.Count > 0)
                                                        {
                                                            String newFilename = "";
                                                            if (item.UseCurrentDwgName)
                                                            {
                                                                newFilename = System.IO.Path.GetFileName(myTransaction.AcDocument.Name);
                                                                newFilename = newFilename.Remove(newFilename.Length - 4);
                                                                if (!ClassStringTools.IsNullOrWhiteSpace(item.Prefix))
                                                                    newFilename = item.Prefix + newFilename;
                                                                if (!ClassStringTools.IsNullOrWhiteSpace(item.Postfix))
                                                                    newFilename += item.Postfix;
                                                                newFilename += ".dwg";
                                                            }
                                                            else
                                                            {
                                                                newFilename = item.ManualSeparationName;
                                                                if (!newFilename.ToLower().EndsWith(".dwg"))
                                                                    newFilename += ".dwg";
                                                            }
                                                            if (System.IO.Directory.Exists(item.Path))
                                                            {
                                                                newFilename = ClassFileTools.addBackslashIfMissing(item.Path) + newFilename;
                                                                Database oldACDatabase = HostApplicationServices.WorkingDatabase;

                                                                using (Document newDoc = DocumentCollectionExtension.Add(Application.DocumentManager, AppSettingsSeparation.getSettings().NewDWGTemplate))
                                                                    //Application.DocumentManager.Add(AppSettingsSeparation.getSettings().NewDWGTemplate))
                                                                {
                                                                    Boolean canWrite = true;
                                                                    using (DocumentLock newDocLoc = newDoc.LockDocument())
                                                                    {
                                                                        using (Database newDb = newDoc.Database)
                                                                        {                                                                           
                                                                            try
                                                                            {
                                                                                if (System.IO.File.Exists(newFilename))
                                                                                {
                                                                                    if (!ClassFileTools.DeleteToRecycleBin(newFilename))
                                                                                        canWrite = false;
                                                                                }
                                                                                if (canWrite)
                                                                                {
                                                                                   // -------- now chrashed -----
                                                                                   myTransaction.AcDatabase.Wblock(newDb, selectedIds, Point3d.Origin, DuplicateRecordCloning.Ignore);
                                                                                   // <<< this crashed.
                                                                                }
                                                                            }
                                                                            catch (System.Exception ex)
                                                                            {
                                                                                clEditor.LogException(ex);
                                                                                Log.LogException(ex);
                                                                            }
                                                                        }
                                                                    }
                                                                    if (canWrite)
                                                                    {
                                                                        try
                                                                        {
                                                                            clTransaction newTransaction = clSingletonTransaktionsManager.Instance.getTransaction(newDoc, TRANSACTIONNAME); // is my transactionmanager
                                                                            clDatabase.purgeGeoPlaceMarker(newTransaction);
                                                                            if (iAmNotAutoCAD)
                                                                                clKillProxy.cleanCivilObjects(newTransaction);
                                                                            clSingletonTransaktionsManager.Instance.CommitTransaction(newTransaction);
                                                                            clTransaction.lockDatabase(newDoc);
                                                                            newDoc.Database.SaveAs(newFilename, DwgVersion.Newest);
                                                                            newDoc.CloseAndDiscard();
                                                                            result = true;
                                                                        }
                                                                        catch (System.Exception ex)
                                                                        {
                                                                            Log.LogException(ex);
                                                                            clEditor.LogException(ex);
                                                                            result = false;
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        newDoc.CloseAndDiscard();
                                                                        result = false;
                                                                    }
                                                                }
                                                                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = sourceDocument;
                                                                }
                                                            }
                                                        }
                                                    }

Has any body a example that works under 2017?

regards Mario

n.yuan

  • Bull Frog
  • Posts: 348
Re: eWrongDatabase on Database.Wblock since AutoCAD 2017
« Reply #1 on: June 19, 2018, 09:45:25 AM »
Hello,

i have a Problem. Since today i test my code for change the autocadversion from 2014 to 2017.
On 2014 works all fine, but on 2017 trows an eWrongDatase error. What is my fail?

Code worke on 2014:
Code: [Select]
                                                    // objectIds selected by typevalue selection on editor
                                                    selectedIds = new ObjectIdCollection(acPromptSelectionResult.Value.GetObjectIds());
                                                    if (selectedIds != null)
                                                    {
                                                        if (selectedIds.Count > 0)
                                                        {
                                                            ...
                                                            ...
                                                            if (System.IO.Directory.Exists(item.Path))
                                                            {
                                                                newFilename = ClassFileTools.addBackslashIfMissing(item.Path) + newFilename;
                                                                Database oldACDatabase = HostApplicationServices.WorkingDatabase;

                                                                using (Document newDoc = DocumentCollectionExtension.Add(Application.DocumentManager, AppSettingsSeparation.getSettings().NewDWGTemplate))
                                                                    //Application.DocumentManager.Add(AppSettingsSeparation.getSettings().NewDWGTemplate))
                                                                {
                                                                    Boolean canWrite = true;
                                                                    using (DocumentLock newDocLoc = newDoc.LockDocument())
                                                                    {
                                                                        using (Database newDb = newDoc.Database)
                                                                        {                                                                           
                                                                            try
                                                                            {
                                                                                if (System.IO.File.Exists(newFilename))
                                                                                {
                                                                                    if (!ClassFileTools.DeleteToRecycleBin(newFilename))
                                                                                        canWrite = false;
                                                                                }
                                                                                if (canWrite)
                                                                                {
                                                                                   // -------- now chrashed -----
                                                                                   [color=red][b]myTransaction.AcDatabase[/b][/color].Wblock(newDb, selectedIds, Point3d.Origin, DuplicateRecordCloning.Ignore);
                                                                                   // <<< this crashed.
                                                                                }
                                                                            }
                                                                            catch (System.Exception ex)
                                                                            {
                                                                                clEditor.LogException(ex);
                                                                                Log.LogException(ex);
                                                                            }
                                                                        }
                                                                    }
                                                                    ...
                                                                    ...
                                                                }
                                                                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = sourceDocument;
                                                                }
                                                            }
                                                        }
                                                    }

Has any body a example that works under 2017?

regards Mario

See the red-bold part in the line of code where the crash occurs. You did not show what "myTransaction" object is (I can only guess it is a custom class that may inherits or includes Transaction class), and what its property AcDatabase is (and how it is obtained). By your code logic, the AcDatabase should be the current drawing (before the code opens a new drawing for data to be WBlocked to).

The other critical thing you did not show is the context the code is executed in: application context, or document context. Again, judging by the last line of your code, where you set the MdiDocument to "sourceDocument", I guess you run the code in Application context (you set CommandFlags.Session flag).

Since the major change from Acad2014 post Acad2014 version (i.e. 2015 and later) is the removal of Fiber, which would cause issues if active document changes during execution of commands/custom application, I suspect this is the possible cause of the crash, because you claim it works with Acad2014.

From what I can see from your code logic, I do not think your code needs to run in Application context, because you open a new document for copying data to it, it does not have to be MdiActiveDocument. Yes, without CommandFlags.Sessioon flag, the code running in document context can still open document in AutoCAD, the newly opened document simply does not become MdiActiveDocument. While if you do have CommandFlas.Session, then mewly opened document automatically becomes NdiActiveDocument.

Without seeing more relevant code, I suggest you try one of the 2 things, or both:
1. remove "CommandFlags.Session" flag, if you used it. or
2. change this line
    myTransaction.AcDatabase.Wblock(....);
    to
    sourceDatabase.Wblock(...); //assume variable "sourceDatabase" was correctly pointed to the valid database where the entities were selected from.

MarioR

  • Newt
  • Posts: 64
Re: eWrongDatabase on Database.Wblock since AutoCAD 2017
« Reply #2 on: June 20, 2018, 05:28:54 AM »
Hello n.yuan,

thanks for your answer. The solution was "remove session flag".

@show into "myTransaction" ... It's my own transactionmanager. In the last 11 years i have been programming on autocad since 2007. In this time have grown many libraries ....

If there is a problem in these libraries from the new autocad version, it is tricky to blend out my own library code from post.

many thanks and regards
Mario