ADN Open CIS
Сообщество программистов Autodesk в СНГ

18/06/2013

Как установить путь к двум и более каталогам конфигурации принтеров (PrinterConfigPath) при помощи AutoCAD .NET API?

Вопрос: Я пытаюсь установить два пути к каталогам конфигурации принтеров, но AutoCAD завершается с FATAL Error:

 

Код - VBA: [Выделить]
  1. Dim pref As AcadPreferences = Autodesk.AutoCAD.ApplicationServices.Application.Preferences
  2. pref.Files.PrinterConfigPath = "T:\wPlotters;T:\Plotters"

 

Если указан только один каталог, то всё работает нормально, а при двух и больше – аварийное завершение AutoCAD.

 

Ответ: Предыдущие версии AutoCAD не позволяли использовать более одного каталога в качестве пути доступа к файлам конфигурации принтеров. Поэтому метод, использующий AcadPreferences, не работает. Вместо него можно использовать метод чтения/установки переменной окружения PrinterConfigDir при помощи функций acedGetEnv/acedSetEnv (из состава ObjectARX). Для того, чтобы воспользоваться ими в AutoCAD .NET API используем P/Invoke. Ниже приведён код на C# и VB.NET:

 

Код - C#: [Выделить]
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using Autodesk.AutoCAD.Runtime;
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.Geometry;
  9. using Autodesk.AutoCAD.EditorInput;
  10. using AcRx = Autodesk.AutoCAD.Runtime;
  11. using AcEd = Autodesk.AutoCAD.EditorInput;
  12. using AcDb = Autodesk.AutoCAD.DatabaseServices;
  13. using AcAp = Autodesk.AutoCAD.ApplicationServices;
  14. using AcWin = Autodesk.AutoCAD.Windows;
  15.  
  16. [assembly: CommandClass(typeof(SetEnvInNet.MyCommands))]
  17.  
  18. namespace SetEnvInNet
  19. {
  20.  
  21.     public static class Env
  22.     {
  23.         static int Ver = AcAp.Application.Version.Major;
  24.         // AutoCAD 2007...2012
  25.         [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "acedGetEnv")]
  26.         extern static private Int32 acedGetEnv12(string var, StringBuilder val);
  27.         // AutoCAD 2013...
  28.         [DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "acedGetEnv")]
  29.         extern static private Int32 acedGetEnv13(string var, StringBuilder val);
  30.         static public string GetEnv(string var)
  31.         {
  32.             StringBuilder val = new StringBuilder(16536);
  33.             if (Ver <= 18) acedGetEnv12(var, val); else acedGetEnv13(var, val);
  34.             return val.ToString();
  35.         }
  36.         // AutoCAD 2007...2012
  37.         [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "acedSetEnv")]
  38.         extern static private Int32 acedSetEnv12(string var, string val);
  39.         // AutoCAD 2013...
  40.         [DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "acedSetEnv")]
  41.         extern static private Int32 acedSetEnv13(string var, string val);
  42.         static public void SetEnv(string var, string val)
  43.         {
  44.             if (Ver <= 18) acedSetEnv12(var, val); else acedSetEnv13(var, val);
  45.         }
  46.  
  47.     }
  48.     public class MyCommands
  49.     {
  50.         static public void AddPath(string var, string path)
  51.         {
  52.             StringBuilder oldpath = new StringBuilder(Env.GetEnv(var));
  53.             oldpath.Append(";"); oldpath.Append(path);
  54.             Env.SetEnv(var, oldpath.ToString());
  55.         }
  56.  
  57.         [CommandMethod("SetPath", CommandFlags.Modal)]
  58.         public void TestAddPath()
  59.         {
  60.             AcWin.OpenFileDialog dlg = new AcWin.OpenFileDialog("Select Directory:", "", "", "",
  61.                 AcWin.OpenFileDialog.OpenFileDialogFlags.AllowFoldersOnly);
  62.             DialogResult ds = dlg.ShowDialog();
  63.             if (ds == DialogResult.OK)
  64.             {
  65.                 AddPath("PrinterConfigDir",dlg.Filename);
  66.             }
  67.         }
  68.     }
  69. }

 

Код - VB.NET: [Выделить]
  1. Imports System.Runtime.InteropServices
  2. Imports System.Text
  3. Imports System.Windows.Forms
  4. Imports Autodesk.AutoCAD.Runtime
  5. Imports Autodesk.AutoCAD.ApplicationServices
  6. Imports Autodesk.AutoCAD.DatabaseServices
  7. Imports Autodesk.AutoCAD.Geometry
  8. Imports Autodesk.AutoCAD.EditorInput
  9. Imports AcRx = Autodesk.AutoCAD.Runtime
  10. Imports AcEd = Autodesk.AutoCAD.EditorInput
  11. Imports AcDb = Autodesk.AutoCAD.DatabaseServices
  12. Imports AcAp = Autodesk.AutoCAD.ApplicationServices
  13. Imports AcWin = Autodesk.AutoCAD.Windows
  14.  
  15. <Assembly: CommandClass(GetType(SetEnvInNet.MyCommands))>
  16.  
  17. Namespace SetEnvInNet
  18.  
  19.             Public NotInheritable Class Env
  20.                         Private Sub New()
  21.                         End Sub
  22.                         Shared Ver As Integer = AcAp.Application.Version.Major
  23.                         ' AutoCAD 2007...2012
  24.                         <DllImport("acad.exe", CallingConvention := CallingConvention.Cdecl, CharSet := CharSet.Unicode, EntryPoint := "acedGetEnv")> _
  25.                         Private Shared Function acedGetEnv12(var As String, val As StringBuilder) As Int32
  26.                         End Function
  27.                         ' AutoCAD 2013...
  28.                         <DllImport("accore.dll", CallingConvention := CallingConvention.Cdecl, CharSet := CharSet.Unicode, EntryPoint := "acedGetEnv")> _
  29.                         Private Shared Function acedGetEnv13(var As String, val As StringBuilder) As Int32
  30.                         End Function
  31.                         Public Shared Function GetEnv(var As String) As String
  32.                                    Dim val As New StringBuilder(16536)
  33.                                    If Ver <= 18 Then
  34.                                                acedGetEnv12(var, val)
  35.                                    Else
  36.                                                acedGetEnv13(var, val)
  37.                                    End If
  38.                                    Return val.ToString()
  39.                         End Function
  40.                         ' AutoCAD 2007...2012
  41.                         <DllImport("acad.exe", CallingConvention := CallingConvention.Cdecl, CharSet := CharSet.Unicode, EntryPoint := "acedSetEnv")> _
  42.                         Private Shared Function acedSetEnv12(var As String, val As String) As Int32
  43.                         End Function
  44.                         ' AutoCAD 2013...
  45.                         <DllImport("accore.dll", CallingConvention := CallingConvention.Cdecl, CharSet := CharSet.Unicode, EntryPoint := "acedSetEnv")> _
  46.                         Private Shared Function acedSetEnv13(var As String, val As String) As Int32
  47.                         End Function
  48.                         Public Shared Sub SetEnv(var As String, val As String)
  49.                                    If Ver <= 18 Then
  50.                                                acedSetEnv12(var, val)
  51.                                    Else
  52.                                                acedSetEnv13(var, val)
  53.                                    End If
  54.                         End Sub
  55.  
  56.             End Class
  57.             Public Class MyCommands
  58.                         Public Shared Sub AddPath(var As String, path As String)
  59.                                    Dim oldpath As New StringBuilder(Env.GetEnv(var))
  60.                                    oldpath.Append(";")
  61.                                    oldpath.Append(path)
  62.                                    Env.SetEnv(var, oldpath.ToString())
  63.                         End Sub
  64.  
  65.                         <CommandMethod("SetPath", CommandFlags.Modal)> _
  66.                         Public Sub TestAddPath()
  67.                                    Dim dlg As New AcWin.OpenFileDialog("Select Directory:", "", "", "", AcWin.OpenFileDialog.OpenFileDialogFlags.AllowFoldersOnly)
  68.                                    Dim ds As DialogResult = dlg.ShowDialog()
  69.                                    If ds = DialogResult.OK Then
  70.                                                AddPath("PrinterConfigDir", dlg.Filename)
  71.                                     End If
  72.                         End Sub
  73.             End Class
  74. End Namespace

 

Обсуждение: http://adn-cis.org/forum/index.php?topic=101.0

Автор: Александр Ривилис
Автор перевода: Александр Ривилис
Опубликовано 18.06.2013
Отредактировано 17.09.2015 в 19:05:38