Minimum testing with AutoCAD 2013.
C#:
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcWin = Autodesk.AutoCAD.Windows;
[assembly: CommandClass(typeof(SetEnvInNet.MyCommands))]
namespace SetEnvInNet
{
public static class Env
{
static int Ver = AcAp.Application.Version.Major;
// AutoCAD 2007...2012
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "acedGetEnv")]
extern static private Int32 acedGetEnv12(string var, StringBuilder val);
// AutoCAD 2013...
[DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "acedGetEnv")]
extern static private Int32 acedGetEnv13(string var, StringBuilder val);
static public string GetEnv(string var)
{
StringBuilder val = new StringBuilder(16536);
if (Ver <= 12) acedGetEnv12(var, val); else acedGetEnv13(var, val);
return val.ToString();
}
// AutoCAD 2007...2012
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "acedSetEnv")]
extern static private Int32 acedSetEnv12(string var, string val);
// AutoCAD 2013...
[DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "acedSetEnv")]
extern static private Int32 acedSetEnv13(string var, string val);
static public void SetEnv(string var, string val)
{
if (Ver <= 12) acedSetEnv12(var, val); else acedSetEnv13(var, val);
}
}
public class MyCommands
{
static public void AddPath(string var, string path)
{
StringBuilder oldpath = new StringBuilder(Env.GetEnv(var));
oldpath.Append(";"); oldpath.Append(path);
Env.SetEnv(var, oldpath.ToString());
}
[CommandMethod("SetPath", CommandFlags.Modal)]
public void TestAddPath()
{
AcWin.OpenFileDialog dlg = new AcWin.OpenFileDialog("Select Directory:", "", "", "",
AcWin.OpenFileDialog.OpenFileDialogFlags.AllowFoldersOnly);
DialogResult ds = dlg.ShowDialog();
if (ds == DialogResult.OK)
{
AddPath("PrinterConfigDir",dlg.Filename);
}
}
}
}
VB.NET (converted from C# with http://www.developerfusion.com/tools/convert/csharp-to-vb/)
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Windows.Forms
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports AcRx = Autodesk.AutoCAD.Runtime
Imports AcEd = Autodesk.AutoCAD.EditorInput
Imports AcDb = Autodesk.AutoCAD.DatabaseServices
Imports AcAp = Autodesk.AutoCAD.ApplicationServices
Imports AcWin = Autodesk.AutoCAD.Windows
<Assembly: CommandClass(GetType(SetEnvInNet.MyCommands))>
Namespace SetEnvInNet
Public NotInheritable Class Env
Private Sub New()
End Sub
Shared Ver As Integer = AcAp.Application.Version.Major
' AutoCAD 2007...2012
<DllImport("acad.exe", CallingConvention := CallingConvention.Cdecl, CharSet := CharSet.Unicode, EntryPoint := "acedGetEnv")> _
Private Shared Function acedGetEnv12(var As String, val As StringBuilder) As Int32
End Function
' AutoCAD 2013...
<DllImport("accore.dll", CallingConvention := CallingConvention.Cdecl, CharSet := CharSet.Unicode, EntryPoint := "acedGetEnv")> _
Private Shared Function acedGetEnv13(var As String, val As StringBuilder) As Int32
End Function
Public Shared Function GetEnv(var As String) As String
Dim val As New StringBuilder(16536)
If Ver <= 12 Then
acedGetEnv12(var, val)
Else
acedGetEnv13(var, val)
End If
Return val.ToString()
End Function
' AutoCAD 2007...2012
<DllImport("acad.exe", CallingConvention := CallingConvention.Cdecl, CharSet := CharSet.Unicode, EntryPoint := "acedSetEnv")> _
Private Shared Function acedSetEnv12(var As String, val As String) As Int32
End Function
' AutoCAD 2013...
<DllImport("accore.dll", CallingConvention := CallingConvention.Cdecl, CharSet := CharSet.Unicode, EntryPoint := "acedSetEnv")> _
Private Shared Function acedSetEnv13(var As String, val As String) As Int32
End Function
Public Shared Sub SetEnv(var As String, val As String)
If Ver <= 12 Then
acedSetEnv12(var, val)
Else
acedSetEnv13(var, val)
End If
End Sub
End Class
Public Class MyCommands
Public Shared Sub AddPath(var As String, path As String)
Dim oldpath As New StringBuilder(Env.GetEnv(var))
oldpath.Append(";")
oldpath.Append(path)
Env.SetEnv(var, oldpath.ToString())
End Sub
<CommandMethod("SetPath", CommandFlags.Modal)> _
Public Sub TestAddPath()
Dim dlg As New AcWin.OpenFileDialog("Select Directory:", "", "", "", AcWin.OpenFileDialog.OpenFileDialogFlags.AllowFoldersOnly)
Dim ds As DialogResult = dlg.ShowDialog()
If ds = DialogResult.OK Then
AddPath("PrinterConfigDir", dlg.Filename)
End If
End Sub
End Class
End Namespace
Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | 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
