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