Author Topic: setting autocad file paths in vb.net  (Read 11557 times)

0 Members and 1 Guest are viewing this topic.

sandzilla

  • Guest
setting autocad file paths in vb.net
« on: May 06, 2008, 10:07:48 AM »
Hello everyone
I'm new to vb.net and AutoCAD programming so please take it easy on me

I'm converting vba code (that I wrote) to vb.net and i cant figure out how to set and get some file paths in my code. For example the plotstyle support path or the file support path.

here is the vba example that worked flawless
Code: [Select]
plotpath = ThisDrawing.Application.Preferences.Files.PrinterStyleSheetPath
supportpath = ThisDrawing.Application.Preferences.Files.supportpath
here is what i have so far in vb.net
Code: [Select]
Dim acadprefclass As Autodesk.AutoCAD.Interop.AcadPreferencesProfilesClass
Dim acadpref As Autodesk.AutoCAD.Interop.AcadPreferencesFiles
dim plotpath as string
dim supportpath as string

plotpath = acadpref.PrinterStyleSheetPath
supportpath = acadpref.SupportPath
Here is the error I get when I compile my code to a dll and run it in autocad
Object reference not set to an instance of an object.

If someone could help I would appreciate it greatly.
« Last Edit: May 06, 2008, 10:24:11 AM by Maverick® »

Maverick®

  • Seagull
  • Posts: 14778
Re: setting autocad file paths in vb.net
« Reply #1 on: May 06, 2008, 10:24:53 AM »
I just added some code tags in your post to make the code stand out Sandzilla.  No worries.

And Welcome!!

ReneRam

  • Guest
Re: setting autocad file paths in vb.net
« Reply #2 on: May 06, 2008, 06:02:37 PM »
Take a look at:


Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("SystemVariableName")

and

Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("SystemVariableName")


I think this should work, but you have to find the exact SystemVariableName.

I've been looking for the Plot Style Table Path, since I wanted to give you a little snippet, but I can't find the variable name anymore   :?

If I type in the editor

Quote
Command: (getenv "PrinterStyleSheetDir")
"C:\\Documents and Settings\\René\\Dati applicazioni\\Autodesk\\AutoCAD
2008\\R17.1\\enu\\Plot Styles"


PrinterStyleSheetDir was the variable until AutoCAD 2005, but now I can't find it in the Acad2008 system variables  :-o
maybe some of the gurus here...

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: setting autocad file paths in vb.net
« Reply #3 on: May 06, 2008, 09:01:35 PM »
Welcome to TheSwamp sandzilla!

This is in C# , but it might give you a push in the right direction  :laugh:

Code: [Select]
   [CommandMethod("doit")]
    public void doit()
    {
      Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
      try
      {
        UserConfigurationManager userConfigurationManager = AcAp.Application.UserConfigurationManager;
        IConfigurationSection profile = userConfigurationManager.OpenCurrentProfile();

        bool GeneralExists = profile.ContainsSubsection("General");

        if (!GeneralExists)
        {
          ed.WriteMessage("\n Subsection General does not exist: ");
          return;
        }

        using (IConfigurationSection general = profile.OpenSubsection("General"))
        {
          bool ACADExists = general.Contains("ACAD");

          if (!ACADExists)
          {
            ed.WriteMessage("\n Subsection ACAD does not exist: ");
            return;
          }
          string paths = (string)general.ReadProperty("ACAD", string.Empty);
          StringBuilder sb = new StringBuilder();
          string[] strings = paths.Split(new char[] { ';' });

          foreach (string s in strings)
            sb.Append(s + "\n");

          ed.WriteMessage(sb.ToString());
        }

      }
      catch (System.Exception ex)
      {
        ed.WriteMessage(ex.Message);
        ed.WriteMessage(ex.StackTrace);
      }
    }
« Last Edit: May 06, 2008, 11:46:27 PM by Daniel »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: setting autocad file paths in vb.net
« Reply #4 on: May 06, 2008, 11:53:57 PM »
Here is another, basically all the values are stored in the registry  :-)

Code: [Select]
   [CommandMethod("doit2")]
    public void doit2()
    {
      Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
      try
      {
        UserConfigurationManager userConfigurationManager = AcAp.Application.UserConfigurationManager;
        IConfigurationSection profile = userConfigurationManager.OpenCurrentProfile();

        bool GeneralExists = profile.ContainsSubsection("General");

        if (!GeneralExists)
        {
          ed.WriteMessage("\n Subsection General does not exist: ");
          return;
        }

        using (IConfigurationSection general = profile.OpenSubsection("General"))
        {
          bool PrinterStyleSheetDirExists = general.Contains("PrinterStyleSheetDir");

          if (!PrinterStyleSheetDirExists)
          {
            ed.WriteMessage("\n Subsection ACAD does not exist: ");
            return;
          }
          string path = (string)general.ReadProperty("PrinterStyleSheetDir", string.Empty);

          ed.WriteMessage(path);
        }
      }
      catch (System.Exception ex)
      {
        ed.WriteMessage(ex.Message);
        ed.WriteMessage(ex.StackTrace);
      }
    }

Glenn R

  • Guest
Re: setting autocad file paths in vb.net
« Reply #5 on: May 07, 2008, 05:01:02 AM »
Nicely done Dan - good example of using the UserConfigurationManager and related objects.

I have noticed, that if you change settings that are stored in the profile through the AutoCAD options dialog, those changed values aren't persisted to the registry until a successful autoCAD shutdown. So, changing a value through a session and then reading the registry for that value, *might* not give you the actual value right at that moment in time.

I've done something like this before, but I don't have autocad or an ide on this machine so I can't remember the exact syntax, but it was something like this:

Code: [Select]
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

...

AcadPreferences acadPrefs = acadApp.Preferences as AcadPreferences;
if (acadPrefs == null)
return;

AcadPreferencesFiles acadPrefsFiles = acadPrefs.Files;
if (acadPrefsFiles == null)
return;

acadPrefsFiles.TemplateDwgPath = projectTemplatePath;

// Clean up COM references
Marshal.ReleaseComObject(acadPrefsFiles);
Marshal.ReleaseComObject(acadPrefs);
...


Something along those lines...

This changes the Template path as an example from some of my code files.

Cheers,
Glenn.

mcarson

  • Guest
Re: setting autocad file paths in vb.net
« Reply #6 on: May 07, 2008, 06:02:18 AM »
I have coded the following, and as tested it works - in vb.net

Code: [Select]
Public Shared Sub UpdateProfile()
            'Cast the current autocad application from .net to COM interop
            Dim oApp As Autodesk.AutoCAD.Interop.AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication
            'Get the user preferences on the current profile
            Dim profile As AcadPreferences = oApp.Preferences
            'Configure the default preferences
            Try
            Dim server As String = ReadSupportLocation()
'Printer configuration path
oApp.Preferences.Files.PrinterConfigPath = server + "\Plotters\Configuration"
'Printer description path
oApp.Preferences.Files.PrinterDescPath = server + "\Plotters\PMP Files"
'Printer style sheet path
oApp.Preferences.Files.PrinterStyleSheetPath = server + "\Plotters\Styles"
'Plot log file location
oApp.Preferences.Files.PlotLogFilePath = server + "\Plotters\Logs"
'Template settings
oApp.Preferences.Files.TemplateDwgPath = server + "\Templates"
'Support Paths
Dim supportpaths() As String = Split(oApp.Preferences.Files.SupportPath, ";")
Dim supportlist As New List(Of String)
For i As Integer = 0 To UBound(supportpaths)
supportlist.Add(supportpaths(i))
Next
'Add applications directory
If Not supportlist.Contains(server + "\Applications") Then
supportlist.Add(server + "\Applications")
End If
'Add images directories
If Not supportlist.Contains(server + "\Images\Internal") Then
supportlist.Add(server + "\Images\Internal")
End If
If Not supportlist.Contains(server + "\Images\External") Then
supportlist.Add(server + "\Images\External")
End If
'Format new support path string
Dim supportpath As String = Nothing
For Each item As String In supportlist
supportpath = supportpath + item + ";"
Next
'Add new support
oApp.Preferences.Files.SupportPath = supportpath
Catch ex As Exception

End Try

End Sub
You can access more settings using the oApp.Preferences

Glenn R

  • Guest
Re: setting autocad file paths in vb.net
« Reply #7 on: May 07, 2008, 06:18:53 AM »
Nice!  :-)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: setting autocad file paths in vb.net
« Reply #8 on: May 07, 2008, 07:58:57 AM »
Nicely done Dan - good example of using the UserConfigurationManager and related objects.

I have noticed, that if you change settings that are stored in the profile through the AutoCAD options dialog, those changed values aren't persisted to the registry until a successful autoCAD shutdown. So, changing a value through a session and then reading the registry for that value, *might* not give you the actual value right at that moment in time.

I've done something like this before, but I don't have autocad or an ide on this machine so I can't remember the exact syntax, but it was something like this:

Code: [Select]
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

...

AcadPreferences acadPrefs = acadApp.Preferences as AcadPreferences;
if (acadPrefs == null)
return;

AcadPreferencesFiles acadPrefsFiles = acadPrefs.Files;
if (acadPrefsFiles == null)
return;

acadPrefsFiles.TemplateDwgPath = projectTemplatePath;

// Clean up COM references
Marshal.ReleaseComObject(acadPrefsFiles);
Marshal.ReleaseComObject(acadPrefs);
...


Something along those lines...

This changes the Template path as an example from some of my code files.

Cheers,
Glenn.

Hehe thanks, unfortunately the WriteProperty() didn’t want to write so I couldn’t test when the update occurs … plan b  :evil:

Code: [Select]
   [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,CharSet=CharSet.Unicode)]
    private static extern int acedSetEnv(string sym, string val);

    [CommandMethod("doit2")]
    public void doit2()
    {
      Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
      try
      {
        string path = "your path";
        acedSetEnv("PrinterStyleSheetDir", path);
      }
      catch (System.Exception ex)
      {
        ed.WriteMessage(ex.Message);
        ed.WriteMessage(ex.StackTrace);
      }
    }

mcarson

  • Guest
Re: setting autocad file paths in vb.net
« Reply #9 on: May 07, 2008, 10:12:27 AM »
another note to add...
I created the function above to ensure that regardless of what profile the user had set, or wanted to create; the global support paths could be reloaded.
These changes should occur immediately, or, if you do what I have done (add a button on a custom options tab), update on close of options

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: setting autocad file paths in vb.net
« Reply #10 on: May 07, 2008, 10:31:02 AM »
another note to add...
I created the function above to ensure that regardless of what profile the user had set, or wanted to create; the global support paths could be reloaded.
These changes should occur immediately, or, if you do what I have done (add a button on a custom options tab), update on close of options

Nice work Mark  :kewl: