I'm trying to create a extender to be called from Lisp, so some of my other routines could more easily "cleanup" the infamous scales list. So after banging my head to pieces trying to do this through pure lisp, I moved over to Net. A lot of Kean Walmsley's blogs have showed me much about the subject. But one thing from
here, seems to constantly give me errors.
So far my code's as follows:
/// <summary>
/// Get attached scales of a selected entity.
/// </summary>
/// <param name="args">The lisp arguments (if any). This should contain either
/// the ename or text handle of the object.</param>
/// <returns>A list of scale names attached to the object.</returns>
[LispFunction("AnnoScalesExt_GetAttachedList")]
public ResultBuffer GetAttachedScaleList(ResultBuffer args) {
ResultBuffer res = new ResultBuffer();
if ((args != null) && (args.AsArray().Length > 0)) {
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
ObjectContextManager ocm = db.ObjectContextManager;
Array argArray = args.AsArray();
TypedValue tv = (TypedValue)argArray.GetValue(0);
ObjectId id = ObjectId.Null;
Transaction tr = doc.Database.TransactionManager.StartTransaction();
DBObject obj;
using (tr) {
if (tv.TypeCode == (int)LispDataType.ObjectId) //ename
{
id = (ObjectId)tv.Value;
}
else if (tv.TypeCode == (int)LispDataType.Text) //string, handle
{
long lg = System.Convert.ToInt64(tv.Value.ToString(), 16);
Handle hd = new Handle(lg);
id = db.GetObjectId(false, hd, -1);
}
obj = tr.GetObject(id, OpenMode.ForRead);
}
if ((obj != null) && (obj.Annotative == AnnotativeStates.True)) {
System.Collections.Generic.List<ObjectContext> ocl = ObjectContexts.GetContexts(obj, "ACDB_ANNOTATIONSCALES");
if (ocl != null){
res.Add(new TypedValue((int)LispDataType.ListBegin));
foreach (ObjectContext oc in ocl) {
res.Add(new TypedValue((int)LispDataType.ObjectId, id));
}
res.Add(new TypedValue((int)LispDataType.ListEnd));
}
else {
res.Add(new TypedValue((int)LispDataType.Nil, null));
}
}
{
res.Add(new TypedValue((int)LispDataType.Nil, null));
}
}
else {
res.Add(new TypedValue((int)LispDataType.Nil));
}
return res;
}
If I debug in Vanilla2008-SP1 (XP32-SP3), it errors out on the red highlighted line:
System.TypeLoadException: Could not load type
'Autodesk.AutoCAD.Internal.ObjectContexts' from assembly 'acmgd,
Version=17.1.0.0, Culture=neutral, PublicKeyToken=null'.
at AnnoScalesExt.LispExtendedFunctions.GetAttachedScaleList(ResultBuffer
args)
at AcMgCommandClass.InvokeWorker(AcMgCommandClass* , MethodInfo mi, Object
commandObject, Boolean bLispFunction)
at AcMgCommandClass.InvokeWorkerWithExceptionFilter(AcMgCommandClass* ,
MethodInfo mi, Object commandObject, Boolean bLispFunction)
at AcMgPerDocumentCommandClass.Invoke(AcMgPerDocumentCommandClass* ,
gcroot<System::Reflection::MethodInfo ^>* mi, Boolean bLispFunction)
at AcMgCommandClass.CommandThunk.InvokeLisp(CommandThunk* ); error: ADS
request error
If I use the very same code in 2011 (even though Keane shows a "more direct" way for that) it works perfectly fine. Am I missing something? I knew there's something "strange" when I set the reference path to the 2011 inc folder to avoid a problem with the
using Autodesk.AutoCAD.Internal;
Is it that my particular version of 2008 doesn't include this? I thought that is exactly why Keane's blog shows the 2 different methods (i.e. prior to 2009 and thereafter).
Edit: Sorry this forum's code tag doesn't seem to allow colours in the code. The erroring line is this one:
System.Collections.Generic.List<ObjectContext> ocl = ObjectContexts.GetContexts(obj, "ACDB_ANNOTATIONSCALES");