Author Topic: Error when obtaining ObjectContexts  (Read 1701 times)

0 Members and 1 Guest are viewing this topic.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Error when obtaining ObjectContexts
« on: June 30, 2011, 11:04:50 AM »
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:
Code: [Select]
        /// <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:
Quote
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
Code: [Select]
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:
Code: [Select]
System.Collections.Generic.List<ObjectContext> ocl = ObjectContexts.GetContexts(obj, "ACDB_ANNOTATIONSCALES");
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Error when obtaining ObjectContexts
« Reply #1 on: June 30, 2011, 11:41:52 AM »
Hi,

Prior 2010 (or 2009, I'm not sure), the Autodesk.AutoCAD.Internal namespace was defined wiyhin a separate DLL (acmgdinternal.dll). For these versions, you have to reference this DLL.
Speaking English as a French Frog

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Error when obtaining ObjectContexts
« Reply #2 on: July 01, 2011, 01:11:58 AM »
Hi gile! I posted over here since I've heard the "best" programmers are at TheSwamp :D . Looking through these posts I notice a lot of familiar names  ;)

Thanks, will give that a try. Must've missed that in Kean's blog.

BTW, How would one add such reference dynamically? I mean, I don't want to compile several DLL's for different versions - isn't that the major reason behind DotNet instead of ObjectARX? Or does it not matter, I'm assuming that adding a reference to acmgdinternal would cause an error in ACads where this no longer exists.

Doing some googling: http://www.eggheadcafe.com/software/aspnet/32217781/dynamically-adding-assembly-reference-to-executing-assembly.aspx

I'll give that a try and see if I can do something with the Assembly.Load method. Perhaps I've just answered my own question  :ugly:
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.