Getting/setting paths and filenames listed in options with c#

Getting/setting paths and filenames listed in options with c#

Anonymous
Not applicable
3,348 Views
3 Replies
Message 1 of 4

Getting/setting paths and filenames listed in options with c#

Anonymous
Not applicable

Hi

 

Is it possible to get and set the various paths and files found in the options dialogue of the UI by using c# code and .NET APIs only?

 

I have done some browsing about this and the only code that I've come across that has enabled me to set (only) the "Support File Search Path", is found here: http://adndevblog.typepad.com/autocad/2012/05/modifying-autocad-support-path-through-net.html

 

As the comments indicate this code has some issues. I know this is easier in lisp and VB but I want to stay in c#.

 

Regards

Sigurjón

0 Likes
3,349 Views
3 Replies
Replies (3)
Message 2 of 4

SENL1362
Advisor
Advisor

A few samples to get you started

 

 

                //Methode 1:
                UserConfigurationManager userConfigurationManager = AcadApp.UserConfigurationManager;
                IConfigurationSection acadCurrentProfile = userConfigurationManager.OpenCurrentProfile();

                //HKCU\Software\Autodesk\AutoCAD\R19.1\ACAD-D002:409\aecProfile\Default\<<Unnamed Profile>>\General
                //%USERPROFILE%\appdata\roaming\autodesk\autocad 2014\r19.1\enu\support;c:\program files\autodesk\autocad 2014\support;c:\program files\autodesk\autocad 2014\support\en-us;c:\program files\autodesk\autocad 2014\fonts;c:\program files\autodesk\autocad 2014\help;c:\program files\autodesk\autocad 2014\express;c:\program files\autodesk\autocad 2014\support\color;c:\programdata\autodesk\applicationplugins\autodesk appmanager.bundle\contents\resources;c:\programdata\autodesk\applicationplugins\autodesk appmanager.bundle\contents\windows\2014;c:\programdata\autodesk\applicationplugins\autodesk featuredapps.bundle\contents\windows\2014;c:\programdata\autodesk\applicationplugins\autodesk importskp.bundle\contents\resources;
                //d:\apldata\autocad\acadconfig_infra.net\menu;d:\apldata\autocad\acadconfig_infra.net\kaders;d:\apldata\autocad\acadconfig_infra.net\symbolen;d:\apldata\autocad\acadconfig_infra.net\fonts;%USERPROFILE%\appdata\local\autodesk\autocad map 3d 2012\r18.2\enu\support;c:\program files\autodesk\autocad map 3d 2012\support;c:\program files\autodesk\autocad map 3d 2012\fonts;c:\program files\autodesk\autocad map 3d 2012\help;c:\program files\autodesk\autocad map 3d 2012\express;c:\program files\autodesk\autocad map 3d 2012\support\color;c:\program files\autodesk\autocad map 3d 2012\bin\fdo;c:\programdata\autodesk\applicationplugins\autodesk importskp.bundle\contents\resources;c:\programdata\autodesk\applicationplugins\autodesk featuredapps.bundle\contents\windows\2014;c:\programdata\autodesk\applicationplugins\autodesk appmanager.bundle\contents\windows\2014;c:\programdata\autodesk\applicationplugins\autodesk appmanager.bundle\contents\resources;
                IConfigurationSection generalProfileSection = acadCurrentProfile.OpenSubsection("General");
                string acadSearchPath = (string)generalProfileSection.ReadProperty("ACAD", string.Empty);
                if (string.IsNullOrWhiteSpace(acadSearchPath))
                    throw new System.Exception("AcadSearchPath not found");
                string[] tmpSearchPath = acadSearchPath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                if (ed != null)
                {
                    for (int i = 0; i < tmpSearchPath.Length; i++)
                        ed.WriteMessage("\n {0}" , tmpSearchPath[i]);
                }



                //Methode 2:
                dynamic acadObject2 = Application.AcadApplication;
                dynamic acadPrefFiles2 = acadObject2.Preferences.Files;
                string path2 = acadPrefFiles2.SupportPath;

                dynamic acadPreferences = Application.Preferences;
                string printerFolder = acadPreferences.PrinterStyleSheetPath;
                string[] ctbFiles = System.IO.Directory.GetFiles(printerFolder, "*.ctb");


                //Methode 3:
                //using System.Reflection;
                object acadObject3 = Application.AcadApplication;
                object acadPrefs3 = acadObject3.GetType().InvokeMember("Preferences", BindingFlags.GetProperty, null, acadObject2, null);
                object acadFiles = acadPrefs3.GetType().InvokeMember("Files", BindingFlags.GetProperty, null, acadPrefs3, null);
                string supportPath = (string)acadFiles.GetType().InvokeMember("SupportPath", BindingFlags.GetProperty, null, acadFiles, null);

 

 

 

 

 

 

0 Likes
Message 3 of 4

Norman_Yuan
Mentor
Mentor

You might want to study a bit more on the differences among C#/VB.NET/AutoCAD COM API/AutoCAD.NET API/late binding...No offence, if you do have clear understanding on these terms

 

AutoCAD ,NET APIs (or, managed APIs) has no direct access to the said paths (or other options available in the "Options" dialog box, while AutoCAD COM APIs provides access to those options (most, but not all of then) via AcadPreferences class.

 

Fortunately, AutoCAD .NET API offers a shortcut to access this COM objects via Autodesk.AutoCAD.ApplicationServices.Application.Preferences. So, you can use it to get all the paths the AcadPreferences offers.

 

You use the COM object in .NET codeing with either late binding (as the article you referred to did), or early binding, by which, you need to set references to AutoCAD COM type library.

 

This is the same with C# or VB.NET, only using late binding with C# code is a bit tedious, as shownin that article, but that was only if you still use .NET 3.5 or earlier. With .NET 4.0, you can use "dynamic" in C#, so that the late binding code is as simple as late binding in VB.NET (without turning on "Strict On" flag.

 

The bottom line: you use COM class AcadPreferences to access the said paths/options in your .NET code, regardless the .NET language being C# or VB.NET. You set references to Acad COM type library, if you want to have intelli-sense prompt during coding, thus, the early binding. You can use late binding in both C# and VB.NET. C# is not a "second class" thing in this regard, as you might have thought.

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 4 of 4

SENL1362
Advisor
Advisor

Hi Norman,

 

Thank you for pointing that out. I'm indeed still studying and will probably do the rest of my life :).

I provided all these quick samples because the OP didn't specify exactly the what and how. So he can pick what he needs.

 

I am glad you added some more comments and hope you will do in the future. W'll all benefit from that.

Thanks.

 

 

 

0 Likes