Accessing Options/Preferences

Accessing Options/Preferences

dennis
Advisor Advisor
1,587 Views
6 Replies
Message 1 of 7

Accessing Options/Preferences

dennis
Advisor
Advisor
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Customization;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Runtime;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace MyClass.AutoCAD.EST
{
    public class PreferenceClass
    {
        public void SupportDriveFolder()
        {
            Document acDoc = acApp.DocumentManager.MdiActiveDocument;
            Database acDb = acDoc.Database;
            Editor acEd = acDoc.Editor;
            //object AcadApp = acApp.AcadApplication;
            //object acPref = AcadApp.GetType().InvokeMember("Preferences", BindingFlags.GetProperty, null, AcadApp, null);
            //object tabFiles = acPref.GetType().InvokeMember("Files", BindingFlags.GetProperty, null, acPref, null);
            //string searchpaths = (string)tabFiles.GetType().InvokeMember("SupportPath", BindingFlags.GetProperty, null, tabFiles, null);
            //var tempdwt = (string)tabFiles.GetType().InvokeMember("QNewTemplateFile", BindingFlags.GetProperty, null, tabFiles, null);
            AcadPreferences ap = (AcadPreferences)Application.Preferences;
            var files = ap.Files;
            var dwtfile = files.QNewTemplateFile;            
        }
    }
}

I get the following error when my code hits the line:

AcadPreferences ap = (AcadPreferences)Application.Preferences;

 

System.InvalidCastException
HResult=0x80004002
Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'Autodesk.AutoCAD.Interop.AcadPreferences'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{E208B0FB-C9E4-4EEC-83A5-9BE18F2526FA}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

I have been working on accessing the Options, first off just to read, but then to make changes as well.  You can see in the posted code I have the "InvokeMemeber" part and it tests out fine.  So I then opted to try out "the other way" as shown at the page:  https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-NET/files/GUI...

 

My entry code is as follows:

[CommandMethod("PREFS")]
public static void prefs()
{
var pc = new PreferenceClass();
pc.SupportDriveFolder();
}

 

Any help will be greatly appreciated.

0 Likes
Accepted solutions (1)
1,588 Views
6 Replies
Replies (6)
Message 2 of 7

Alexander.Rivilis
Mentor
Mentor
Accepted solution
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace MyClass.AutoCAD.EST
{
  public class PreferenceClass
  {
    public string QNewTemplateFile()
    {
      dynamic app = acApp.AcadApplication;
      dynamic pref = app.Preferences;
      dynamic files = pref.Files;
      dynamic dwtfile = files.QNewTemplateFile;
      return dwtfile;
    }
  }
  public class Command
  {
    [CommandMethod("PREFS")]
    public static void prefs()
    {
      Document acDoc = acApp.DocumentManager.MdiActiveDocument;
      Database acDb = acDoc.Database;
      Editor acEd = acDoc.Editor;
      var pc = new PreferenceClass();
      var dwtfile = pc.QNewTemplateFile();
      acEd.WriteMessage($"\nQNewTemplateFile={dwtfile}");
    }
  }
}

Also remove references to Autodesk.AutoCAD.Interop.dll and Autodesk.AutoCAD.Interop.Common.dll as far as it is look like you use wrong files (version or x86/x64)

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 3 of 7

Alexander.Rivilis
Mentor
Mentor

Other way:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace MyClass.AutoCAD.EST
{
  public class PreferenceClass
  {
    public string TemplateFile()
    {
      string dwtfile = HostApplicationServices.Current
        .GetEnvironmentVariable("QnewTemplate");
      if (dwtfile.Length == 0)
      {
        dwtfile = HostApplicationServices.Current
        .GetEnvironmentVariable("LastTemplate");
      }
      return dwtfile;
    }
  }
  public class Command
  {
    [CommandMethod("PREFS")]
    public static void prefs()
    {
      Document acDoc = acApp.DocumentManager.MdiActiveDocument;
      Database acDb = acDoc.Database;
      Editor acEd = acDoc.Editor;
      var pc = new PreferenceClass();
      var dwtfile = pc.TemplateFile();
      acEd.WriteMessage($"\nTemplateFile={dwtfile}");
    }
  }
}

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 4 of 7

dennis
Advisor
Advisor

This is one of those "OMG that is so much easier".  Thanks.

0 Likes
Message 5 of 7

dennis
Advisor
Advisor

I just discovered, by using Dynamic rather than defining the object AcadPreferences, I don't have the ability to "find" the name of the property i am after, like needing QNewTemplateFile rather than TemplateForQNEW.  Any ideas on that?

And haven't tried this yet, but can I set the value using dynamic?  Example: 

files.QNewTemplateFile = @"C:\Templates\SomeTemplateName.dwt";
0 Likes
Message 6 of 7

Alexander.Rivilis
Mentor
Mentor

@dennis wrote:

I just discovered, by using Dynamic rather than defining the object AcadPreferences, I don't have the ability to "find" the name of the property i am after, like needing QNewTemplateFile rather than TemplateForQNEW.  Any ideas on that?


What about reading ActiveX Developer's Guide and Reference Guide (ActiveX/VBA): http://help.autodesk.com/view/OARX/2018/ENU/?guid=GUID-5D302758-ED3F-4062-A254-FB57BAB01C44 ?

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 7 of 7

Alexander.Rivilis
Mentor
Mentor

@dennis wrote:

...And haven't tried this yet, but can I set the value using dynamic?  Example: 

files.QNewTemplateFile = @"C:\Templates\SomeTemplateName.dwt";

Try it! Smiley Happy

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes