Author Topic: Finding support path in 2014 vs 2015  (Read 8500 times)

0 Members and 1 Guest are viewing this topic.

Keith Brown

  • Swamp Rat
  • Posts: 601
Finding support path in 2014 vs 2015
« on: July 31, 2014, 05:03:42 PM »
I have some code that works fine in 2014 but not in 2015.  I know it was probably because of the changes to the API in 2015 but its been a long day and i have been unable to find an answer on the net.  I know it is something very simple but it is just escaping me right now.  Here is the code.


Code - C#: [Select]
  1. private static void SupportPath(string path)
  2. {
  3.         var preferences = Application.Preferences as AcadPreferences;
  4.         if (preferences != null)
  5.         {
  6.                 string supportPath = preferences.Files.SupportPath;
  7.                 if (!supportPath.ToLower().Contains(path))
  8.                 {
  9.                         preferences.Files.SupportPath = preferences.Files.SupportPath + path;
  10.                 }
  11.         }
  12.         else
  13.         {
  14.                 MessageBox.Show("Unable to verify support path for drawing templates is valid.");
  15.         }
  16. }


in 2015 it always brings up the message box indicating that my preferences is equal to null.  So what changed in 2015?
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Finding support path in 2014 vs 2015
« Reply #1 on: July 31, 2014, 05:54:11 PM »
Hi Keith,
Do you have a reference to Autodesk.AutoCAD.Interop.dll ??

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Finding support path in 2014 vs 2015
« Reply #2 on: July 31, 2014, 06:17:26 PM »
I do Kerry.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

n.yuan

  • Bull Frog
  • Posts: 348
Re: Finding support path in 2014 vs 2015
« Reply #3 on: July 31, 2014, 06:28:40 PM »
Maybe, you referenced wrong version (older version, that is) of COM interop assembly?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Finding support path in 2014 vs 2015
« Reply #4 on: July 31, 2014, 06:33:38 PM »
Keith, I just tried this :

Code - C#: [Select]
  1. namespace AutoCAD_CSharp_2015_4
  2. {
  3.     public class MyCommands
  4.     {
  5.         // Modal Command with localized name
  6.         [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
  7.         public void MyCommand() // This method can have any name
  8.         {
  9.  
  10.             getSupportPath();
  11.  
  12.         }
  13.         private static void getSupportPath()
  14.         {
  15.             var preferences = Application.Preferences as AcadPreferences;
  16.             if (preferences != null)
  17.             {
  18.                 string supportPath = preferences.Files.SupportPath;
  19.                 Document doc = Application.DocumentManager.MdiActiveDocument;
  20.                 doc.Editor.WriteMessage(supportPath);
  21.             }
  22.             else
  23.             {
  24.                 MessageBox.Show("Unable to verify support path for drawing templates is valid.");
  25.             }
  26.         }
  27. }
  28.  

I got the result I expected.


Maybe, you referenced wrong version (older version, that is) of COM interop assembly?

I'd like a beer for each time I've done that :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Finding support path in 2014 vs 2015
« Reply #5 on: July 31, 2014, 06:46:01 PM »
Select the interop dll in solution explorer then in properites palette set Embed Interop Types to False

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Finding support path in 2014 vs 2015
« Reply #6 on: July 31, 2014, 06:51:07 PM »
Or you can remove the Interop references from the project and

change
Code - C#: [Select]
  1.           var preferences = Application.Preferences as AcadPreferences;
  2.  

to

Code - C#: [Select]
  1.           dynamic preferences = Application.Preferences;
  2.  

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Finding support path in 2014 vs 2015
« Reply #7 on: July 31, 2014, 06:53:26 PM »
I do Kerry.

Duh Me!
It wouldn't compile without the reference would it. I'll go back to the peanut gallery. :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Finding support path in 2014 vs 2015
« Reply #8 on: July 31, 2014, 07:02:11 PM »
Or you can remove the Interop references from the project and

change
Code - C#: [Select]
  1.           var preferences = Application.Preferences as AcadPreferences;
  2.  

to

Code - C#: [Select]
  1.           dynamic preferences = Application.Preferences;
  2.  

I like Intellisense too much :-D
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Finding support path in 2014 vs 2015
« Reply #9 on: July 31, 2014, 07:11:43 PM »
Or you can remove the Interop references from the project and

change
Code - C#: [Select]
  1.           var preferences = Application.Preferences as AcadPreferences;
  2.  

to

Code - C#: [Select]
  1.           dynamic preferences = Application.Preferences;
  2.  

I like Intellisense too much :-D

Thats why I like the recent list for references so I can add them for Intellisense then remove them for build. :-D

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Finding support path in 2014 vs 2015
« Reply #10 on: August 01, 2014, 08:53:34 AM »
Thanks for the help guys. 


At this point what I think I will do is create a separate project and just build two different dlls.  That way i can add the correct reference for each project and use the same code.  I am using the AutoLoader so it will only have to setup the PackageContents.xml file once.  Plus I will create a batch file to move my newly built dlls into my build directory for the AutoLoader.  It will take a few minutes to setup but when done will be easy to maintain.


Out of curiosity, is there any way to manipulate the support path using pure .net that does not involve using the registry?  I was really trying to avoid going down that path.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Finding support path in 2014 vs 2015
« Reply #11 on: August 01, 2014, 09:25:05 AM »
I also just figured out that I can use the AutoLoader to set a support path for each version also which would eliminate the code altogether.  DOH!
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Finding support path in 2014 vs 2015
« Reply #12 on: August 04, 2014, 04:11:32 PM »
Select the interop dll in solution explorer then in properites palette set Embed Interop Types to False




I missed this post somehow when coming up with a solution.  This is much better than creating a project for each release.  Now that I once again have a single project for 2014 and 2015 might I ask why you would set this to true?  What is the advantage to embedding vs not embedding?
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Finding support path in 2014 vs 2015
« Reply #13 on: August 07, 2014, 12:09:02 PM »
I understand the general concept or idea enough that its safe when used with AutoCAD's COM interop Assemblies but for advice on other situations these links might help

http://msdn.microsoft.com/en-us/library/dd997297(v=vs.110).aspx
http://blogs.msdn.com/b/samng/archive/2010/01/24/the-pain-of-deploying-primary-interop-assemblies.aspx

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Finding support path in 2014 vs 2015
« Reply #14 on: August 07, 2014, 01:42:18 PM »
Thanks for the info Jeff.  It does help with the overall picture of what is happening..
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

JONTHEPOPE

  • Guest
Re: Finding support path in 2014 vs 2015
« Reply #15 on: August 18, 2014, 11:42:26 PM »
I can't seem to get my  .dll to auto load .
Am I missing a line?
I'm using ACAD2015.

[HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R17.0\ACAD-5001:409\Applications\MyTestApplication]
"DESCRIPTION"="test application"
"LOADCTRLS"=dword:0000000e
"MANAGED"=dword:00000001
"LOADER"="C:\\My Path\\MyTestApp.dll"

I expect the program to run when when I open Cad and type the command???

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Finding support path in 2014 vs 2015
« Reply #16 on: August 19, 2014, 12:59:39 AM »
Hi,

Quote
[HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R17.0\ACAD-5001:409\Applications\MyTestApplication]
R17.0 stands for AutoCAD 2007. use R20.0 for AutoCAD 2015.

Quote
"LOADCTRLS" = 14
12 have to be used to load the application at a command invocation. In this case, you have to create two sub keys for the Command and the Group names.
To load the application at AutoCAD startup, use "LOADCTRLS" = 2.
See here and there.
« Last Edit: August 19, 2014, 01:17:11 AM by gile »
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
« Last Edit: August 19, 2014, 01:16:19 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Finding support path in 2014 vs 2015
« Reply #18 on: August 21, 2014, 11:56:29 AM »
Just out of curiosity.  Do you find the old way better than using the AutoLoader?  I didnt really start developing before the autoloader so i never used any of the old methods.  The autoloader is pretty easy to setup and integrating it into an install program such as Advanced Installer is very simple.  Just my 2 cents.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Finding support path in 2014 vs 2015
« Reply #19 on: August 21, 2014, 04:50:22 PM »
Just out of curiosity.  Do you find the old way better than using the AutoLoader?  I didnt really start developing before the autoloader so i never used any of the old methods.  The autoloader is pretty easy to setup and integrating it into an install program such as Advanced Installer is very simple.  Just my 2 cents.

Hi Keith,
If you mean the .bundle paradigm, I've never tried it.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Finding support path in 2014 vs 2015
« Reply #20 on: August 22, 2014, 09:00:39 AM »
Yup, that is what I meant.  Works well for me and saves me the trouble of coding alot of items that I really don't want to code.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Finding support path in 2014 vs 2015
« Reply #21 on: September 03, 2014, 09:35:05 AM »
Looks like you got it Keith but updating code for 2015 and thought I would through some different ways to modify PreferenceFiles.
 
Here is one way and as is requires .NET 4.5 but could be modified for earlier versions.
It uses the name of the properties for the variable name with CallerMemberNameAttribute  , and just add a property for each and make sure property names match environment variable names.
Code - C#: [Select]
  1.         [CommandMethod("EnviroTest1")]
  2.         public void EnviroTest1()
  3.         {
  4.             Ed.WriteLine(EnviromentVariables.QnewTemplate);
  5.         }
  6.         [CommandMethod("EnviroTest2")]
  7.         public void EnviroTest2()
  8.         {
  9.             EnviromentVariables.QnewTemplate = @"J:\CadManaged\AutoCAD\Hgce\Templates\HgceBase.dwt";
  10.             Ed.WriteLine(EnviromentVariables.QnewTemplate);
  11.         }  
  12.  

Code - C#: [Select]
  1.  public static class EnviromentVariables
  2.    {
  3.        [DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl,
  4.        CharSet = CharSet.Auto, EntryPoint = "acedSetEnv")]
  5.        extern static private Int32 acedSetEnv(string var, string val);
  6.  
  7.        private static string GetEnviromentVariable([CallerMemberName] string name = "")
  8.        {
  9.            return HostApplicationServices.Current.GetEnvironmentVariable(name);
  10.        }
  11.        private static void SetEnviromentVariable(string value, [CallerMemberName] string name = "")
  12.         {
  13.             acedSetEnv(name, value);
  14.         }
  15.        public static string ACAD
  16.        {
  17.            get { return GetEnviromentVariable(); }
  18.            set { SetEnviromentVariable(value); }
  19.        }
  20.        public static string QnewTemplate
  21.        {
  22.            get { return GetEnviromentVariable(); }
  23.            set { SetEnviromentVariable(value); }
  24.        }
  25.        
  26.     }
  27.  
  28.  

 

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Finding support path in 2014 vs 2015
« Reply #22 on: September 03, 2014, 09:40:04 AM »
This is another way going COM route but uses DLR so no need to reference interop dlls.
Code - C#: [Select]
  1.         [CommandMethod("SFTest1")]
  2.         public void SFTest1()
  3.         {
  4.            
  5.             Ed.WriteLine(Preferences.SupportPaths.GetPaths());
  6.         }
  7.         [CommandMethod("SFTest2")]
  8.         public void SFTest2()
  9.         {
  10.             SupportPath sp = Preferences.SupportPaths;
  11.             Ed.WriteLine();
  12.             Ed.WriteLine(sp.Contains(@"C:\Users\Jeff\appdata\roaming\autodesk\applicationplugins"));
  13.             Ed.WriteLine(sp.Contains(@"%AppData%\Autodesk\ApplicationPlugins"));
  14.             sp.Remove(@"C:\Users\Jeff\appdata\roaming\autodesk\applicationplugins");
  15.             sp.SaveChanges();
  16.             Ed.WriteLine(Preferences.SupportPaths.GetPaths());
  17.            
  18.         }
  19.  

Code - C#: [Select]
  1.     interface IAcadPathRepository
  2.     {
  3.         IEnumerable<string> GetPaths();
  4.         void Add(string path);
  5.         void Insert(int index, string path);
  6.         bool Remove(string path);
  7.         void SaveChanges();
  8.         bool Contains(string path);
  9.     }
  10.  

 
Code - C#: [Select]
  1.      public static class Preferences
  2.     {
  3.          static dynamic AcadPreferences { get { return Application.Preferences; } }
  4.          static dynamic AcadPreferencesFiles { get { return AcadPreferences.Files; } }
  5.         public static SupportPath SupportPaths { get { return new SupportPath(AcadPreferencesFiles); } }
  6.     }
  7.  

Code - C#: [Select]
  1.      public class SupportPath : IAcadPathRepository
  2.     {
  3.         private static char[] seperator = new char[] { ';' };
  4.         private List<string> paths = new List<string>();
  5.  
  6.         private dynamic _acadPreferencesFiles;
  7.         private dynamic PreferencesFile
  8.         {
  9.             get { return _acadPreferencesFiles.SupportPath; }
  10.             set{_acadPreferencesFiles.SupportPath = value;}
  11.         }
  12.         public SupportPath(dynamic acadPreferencesFiles)
  13.         {
  14.             _acadPreferencesFiles = acadPreferencesFiles;
  15.             paths = CreatepathList(PreferencesFile);
  16.         }
  17.         public IEnumerable<string> GetPaths()
  18.         {
  19.             return paths.AsReadOnly();
  20.         }
  21.         public void Add(string path)
  22.         {
  23.             path = Expand(path);
  24.             paths.Add(path);
  25.            
  26.            
  27.         }
  28.         public void Insert(int index, string path)
  29.         {
  30.             path = Expand(path);
  31.             paths.Insert(index, path);
  32.         }
  33.         public bool Remove(string path)
  34.         {
  35.             path = Expand(path);
  36.             return paths.Remove(path);
  37.         }
  38.        
  39.        
  40.         public bool Contains(string path)
  41.         {
  42.             return paths.Contains(Expand(path), StringComparer.OrdinalIgnoreCase);
  43.         }
  44.  
  45.         public void SaveChanges()
  46.         {
  47.             PreferencesFile = CreatePathsString(paths);
  48.             paths = CreatepathList(PreferencesFile);
  49.            
  50.         }
  51.        
  52.         private static List<string> CreatepathList(string pathString)
  53.         {
  54.             return pathString.Split(seperator).ToList();
  55.         }
  56.         private static string CreatePathsString(IEnumerable<string> pathList)
  57.         {
  58.             return String.Join(";", pathList);
  59.         }
  60.         private static string Expand(string path)
  61.         {
  62.            return path.StartsWith("%") ? Environment.ExpandEnvironmentVariables(path) : path;
  63.         }
  64.     }
  65.  

 
If your were wanting to make it more like a DirectoryInfo object than a string and quickly slapped together
Code - C#: [Select]
  1.     public class AcadDirectory : IEquatable<AcadDirectory>
  2.     {
  3.  
  4.         private readonly DirectoryInfo _directoryInfo;
  5.         public AcadDirectory(string path)
  6.         {
  7.             _directoryInfo = path.StartsWith("%") ? new DirectoryInfo(Environment.ExpandEnvironmentVariables(path)) : new DirectoryInfo(path);
  8.         }
  9.         public bool Exists { get { return _directoryInfo.Exists; } }
  10.         public string Path { get { return _directoryInfo.FullName; } }
  11.         public string Name { get { return _directoryInfo.Name; } }
  12.  
  13.         public bool Equals(AcadDirectory other)
  14.         {
  15.             if (other == null)
  16.                 return false;
  17.             return this.Name == other.Name;
  18.         }
  19.         public override bool Equals(object obj)
  20.         {
  21.             if (obj == null) return false;
  22.             var acadDir = obj as AcadDirectory;
  23.             return acadDir != null && Equals(acadDir);
  24.         }
  25.         public override int GetHashCode()
  26.         {
  27.             return (String.IsNullOrEmpty(Name) ? Name.GetHashCode() : 0);
  28.         }
  29.         public static bool operator ==(AcadDirectory acadDir1, AcadDirectory acadDir2)
  30.         {
  31.             if ((object)acadDir1 == null || ((object)acadDir2) == null)
  32.                 return Equals(acadDir1, acadDir2);
  33.             return acadDir1.Equals(acadDir2);
  34.         }
  35.         public static bool operator !=(AcadDirectory acadDir1, AcadDirectory acadDir2)
  36.         {
  37.             if (acadDir1 == null || acadDir2 == null)
  38.                 return !Equals(acadDir1, acadDir2);
  39.             return !(acadDir1.Equals(acadDir2));
  40.         }
  41.     }
  42.  

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Finding support path in 2014 vs 2015
« Reply #23 on: September 05, 2014, 09:16:32 AM »
Thanks for the information Jeff.  I will circle back around this and digest it when work lets up a little bit.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013