using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using acad = Autodesk.AutoCAD.ApplicationServices.Application;
using AcApp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcRtm = Autodesk.AutoCAD.Runtime;
[assembly
: AcRtm
.CommandClass(typeof(Testing
.Class1))]
namespace Testing {
public class Class1 {
//In this method code I try to create dictionary, which for every RXClass contains according it wrapper type.
//
//The error occurs after a while after cmd was finished:
//FATAL ERROR: Unhandled Access Violation Reading 0x0008 Exception at 63972160h
//
//QUESTION: Why I get it error?
[AcRtm.CommandMethod("cmd")]
public void Cmd() {
AcApp.Document doc = acad.DocumentManager.MdiActiveDocument;
AcDb.Database db = doc.Database;
AcEd.Editor ed = doc.Editor;
//target dictionary
Dictionary
<AcRtm
.RXClass, Type
> mngTypeDict
= new Dictionary
<AcRtm
.RXClass, Type
>();
//errors log
StringBuilder sb
= new StringBuilder
();
//It is necessary to exclude some types from processing, differently we will receive the errors:
//Error message:
//INTERNAL ERROR: !dbAnnotationScaleCollectioni.cpp@546: eNullEntityPointer
//or
//INTERNAL ERROR: !dbAnnotationScaleCollectioni.cpp@37: eNullObjectPointer
//
//QUESTION: Why I get it errors?
String[] types
= new String[] { "AcDbAnnotationScaleCollection",
"AcDbAnnotationScaleCollectionIterator" };
foreach (DictionaryEntry item in AcRtm.SystemObjects.ClassDictionary) {
AcRtm.RXClass rxClass = null;
Type type = null;
try {
if (item.Value != null) {
rxClass = (AcRtm.RXClass)item.Value;
if (!types.Contains(rxClass.Name)) {
//I use such code:
AcRtm.RXObject rxObj = rxClass.Create();
type = rxObj.GetType();
//But I can't use such code, because AutoCAD right there will finish the operation without the error message:
//using (AcRtm.RXObject rxObj = rxClass.Create()) {
// type = rxObj.GetType();
//}
//
//QUESTION: Why I get it error?
}
}
else {
sb.AppendLine(String.Format("{0} key value is null.", item.Key));
}
}
catch (Exception ex) {
if (rxClass != null)
sb.AppendLine(String.Format("Can't to create instance of {0}. Error: {1}", rxClass.Name, ex.Message));
}
}
}
//I try to find instances of "AcDbAnnotationScaleCollection" or "AcDbAnnotationScaleCollectionIterator"
//
//QUESTION: Why I can't find it?
[AcRtm.CommandMethod("cmd2")]
public void Cmd2() {
AcApp.Document doc = acad.DocumentManager.MdiActiveDocument;
AcDb.Database db = doc.Database;
AcEd.Editor ed = doc.Editor;
//I use such array, like in Cmd method:
String[] types
= new String[] { "AcDbAnnotationScaleCollection",
"AcDbAnnotationScaleCollectionIterator" };
using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) {
//I execute iteration on all database objects:
for (long i = db.BlockTableId.Handle.Value; i < db.Handseed.Value; i++) {
AcDb.ObjectId id = AcDb.ObjectId.Null;
AcDb.DBObject dbObj = null;
Type type = null;
AcDb
.Handle h
= new AcDb
.Handle(i
); bool result = db.TryGetObjectId(h, out id);
//But I can't find instances of "AcDbAnnotationScaleCollection" or "AcDbAnnotationScaleCollectionIterator":
//the condition is never executed.
if (result && id.IsValid && !id.IsNull && types.Contains(id.ObjectClass.Name)) {
dbObj = tr.GetObject(id, AcDb.OpenMode.ForRead);
type = dbObj.GetType();
}
}
tr.Abort();
}
}
}
}