There are some way to make a autoloading install for .NET assembly.
1. The way to use .lsp file at startup.
2. The way to use PackageContents.xml at startup.
Now I will explain about second way.
Second way is most new way and recommanded way.
First, PackageContents.xml file has to be in ***.bundle directory in the %appdata%Autodesk/ApplicationPlugins.
And Other needed files like .dll or .png are in the ***.bundle directory or sub directory of one.
Most important things are directory name and contents of PackageContents.xml.
Directory name has to include .bundle at the last.
packageContents.xml file has to include information about command what you need.
Last, if you want to make ribbon at loading assembly(.dll), you have to observe the following rule.
Rule.
- You have to call a function to make ribbon at startup when SystemVariableChanged event is happened. And you have to delete that event handler method.
- Reference following codes.
public class InitializeKoreaModule : IExtensionApplication
{
public void Initialize()
{
Autodesk.AutoCAD.ApplicationServices.Application.SystemVariableChanged += Application_SystemVariableChanged;
}
void Application_SystemVariableChanged(object sender, Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs e)
{
if (Autodesk.Windows.ComponentManager.Ribbon != null)
{
//ok, create Ribbon
LoadRibbon();
//and remove the event handler
Autodesk.AutoCAD.ApplicationServices.Application.SystemVariableChanged -= Application_SystemVariableChanged;
}
}
public void Terminate()
{
}
public void LoadRibbon()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
RibbonTab rbTabKorea = CreateRibbon("Korea");
if (rbTabKorea == null)
{
return;
}
RibbonPanelSource rbPanelSuperelevation = CreateButtonPannel("Superelevation", rbTabKorea);
if (rbPanelSuperelevation != null)
{
rbPanelSuperelevation.Items.Add(CreateButton("Superelevation\nPlanar Figure", "SPF", "HangilIT.C3DCountryKit.Resources.product-icon.png"));
// rbPanelSuperelevation.Items.Add(new RibbonRowBreak());
}
RibbonPanelSource rbPanelEarthWork = CreateButtonPannel("Earth Work Carried Quantities", rbTabKorea);
if (rbPanelEarthWork != null)
{
rbPanelEarthWork.Items.Add(CreateButton("Earth Work\nCalculation", "ECC", "HangilIT.C3DCountryKit.Resources.product-icon.png"));
rbPanelEarthWork.Items.Add(CreateButton("Mass Curve", "MC", "HangilIT.C3DCountryKit.Resources.product-icon.png"));
rbPanelEarthWork.Items.Add(CreateButton("Mass Haul", "MH", "HangilIT.C3DCountryKit.Resources.product-icon.png"));
}
}
private RibbonTab CreateRibbon(string str)
{
// 리본 컨트롤 생성
RibbonControl rc = ComponentManager.Ribbon;
// 리본 탭 검색
RibbonTab rt = rc.FindTab(str);
if (rt == null)
{
// 리본 탭 생성
rt = new RibbonTab();
// 리본탭 정보
rt.Title = str;
rt.Id = str;
// 리본 컨트롤에 리본탭 추가
rc.Tabs.Add(rt);
}
// 리본탭 활성화
rt.IsActive = true;
return rt;
}
private RibbonPanelSource CreateButtonPannel(string str, RibbonTab rtab)
{
// 리본 패널 소스 생성
RibbonPanelSource bp = new RibbonPanelSource();
// 리본 패널 소스 정보
bp.Title = str;
// 리본 패널 생성
RibbonPanel rp = new RibbonPanel();
// 리본 패널에 리본 패널 소스 추가
rp.Source = bp;
// 리본 탭에 리본 패널 추가
rtab.Panels.Add(rp);
return bp;
}
private RibbonButton CreateButton(string str, string CommandParameter, string pngFile)
{
// 리본 버튼 생성
RibbonButton button = new RibbonButton();
// 리본 버튼 정보
button.Text = str;
button.Id = str;
button.CommandParameter = CommandParameter; // name of the command
button.ShowImage = true;
button.ShowText = true;
button.Size = RibbonItemSize.Large;
button.Orientation = Orientation.Vertical;
button.LargeImage = UtilityEtc.LoadImage(pngFile);
// to add command to the button write the line below
button.CommandHandler = new AdskCommandHandler();
return button;
}
}
Detail information about PackageContents.xml file is in following link.
http://adndevblog.typepad.com/autocad/2013/01/autodesk-autoloader-white-paper.html
2017년 3월 27일 월요일
2017년 3월 20일 월요일
[C#] To check if string includes "ftp://" using Regex.
string s = "ftp://127.0.0.1:6000";
bool isInclude = System.Text.RegularExpressions.Regex.IsMatch("^ftp://", s);
If s include "ftp://" at first, then isInclude is true.
bool isInclude = System.Text.RegularExpressions.Regex.IsMatch("^ftp://", s);
If s include "ftp://" at first, then isInclude is true.
2017년 3월 14일 화요일
[C#] To read a text file
// Opens a file using File.OpenText and closes a file using Dispose()
static void ReadFile()
{
StreamReader reader = null;
try
{
reader = File.OpenText("d:\\file.txt");
if(reader.EndOfStream)
return;
Console.WriteLine(reader.ReadToEnd());
}
finally
{
if(reader != null)
reader.Dispose();
}
}
2017년 2월 27일 월요일
[C#] To download files on the FTP using WebClient class
Following example is to download guestbook.file on the FTP using WebClient class.
That FTP needs login. It's very simple example.
When you need more detail option, you can use HttpClient;
But HttpClient is supported on the .Net Framework 4.5.
-----------------------------------------------------------------------------------------------
using System.Net;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
WebClient wc = new WebClient { Proxy = null };
wc.BaseAddress = "ftp://ftp.albahari.com";
string username = "nutshell";
string password = "oreilly";
wc.Credentials = new NetworkCredential(username, password);
wc.DownloadFile("guestbook.txt", @"d:\guestbook.txt");
}
}
}
That FTP needs login. It's very simple example.
When you need more detail option, you can use HttpClient;
But HttpClient is supported on the .Net Framework 4.5.
-----------------------------------------------------------------------------------------------
using System.Net;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
WebClient wc = new WebClient { Proxy = null };
wc.BaseAddress = "ftp://ftp.albahari.com";
string username = "nutshell";
string password = "oreilly";
wc.Credentials = new NetworkCredential(username, password);
wc.DownloadFile("guestbook.txt", @"d:\guestbook.txt");
}
}
}
[C#] To download content on the internet using WebClient
Following example is to download a website content using WebClient.
It's very simple.
It's very simple.
2017년 2월 21일 화요일
[C#] A example that to call any Native-DLL functions from C#.
using System;
using System.Runtime.InteropServices;
using System.Text;
// define class for struct of native-dll
[StructLayout(LayoutKind.Sequential)]
public class SystemTIme
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort Milliseconds;
}
// define some functions for Native-DLL
public class NativeDLL
{
[DllImport("user32.dll")]
static extern public int MessageBox(IntPtr hWnd, string text, string caption, int type);
[DllImport("kernel32.dll")]
static extern public int GetWindowsDirectory(StringBuilder sb, int maxChars);
[DllImport("kernel32.dll")]
static extern public void GetSystemTime(SystemTIme t);
}
// Call the functions of Native-DLL
public class Program
{
static void Main(string[] args)
{
// message box test
NativeDLL.MessageBox(IntPtr.Zero, "Please do not press this againg", "Attention", 0);
// get windows directory
StringBuilder sb = new StringBuilder();
NativeDLL.GetWindowsDirectory(sb, 256);
NativeDLL.MessageBox(IntPtr.Zero, sb.ToString(), "Directory", 0);
// get system time
SystemTIme t = new SystemTIme();
NativeDLL.GetSystemTime(t);
NativeDLL.MessageBox(IntPtr.Zero, string.Format("{0}년 {1}월 {2}일 {3}시 {4}분 {5}초", t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Second), "System Time", 0);
}
}
using System.Runtime.InteropServices;
using System.Text;
// define class for struct of native-dll
[StructLayout(LayoutKind.Sequential)]
public class SystemTIme
{
public ushort Year;
public ushort Month;
public ushort DayOfWeek;
public ushort Day;
public ushort Hour;
public ushort Minute;
public ushort Second;
public ushort Milliseconds;
}
// define some functions for Native-DLL
public class NativeDLL
{
[DllImport("user32.dll")]
static extern public int MessageBox(IntPtr hWnd, string text, string caption, int type);
[DllImport("kernel32.dll")]
static extern public int GetWindowsDirectory(StringBuilder sb, int maxChars);
[DllImport("kernel32.dll")]
static extern public void GetSystemTime(SystemTIme t);
}
// Call the functions of Native-DLL
public class Program
{
static void Main(string[] args)
{
// message box test
NativeDLL.MessageBox(IntPtr.Zero, "Please do not press this againg", "Attention", 0);
// get windows directory
StringBuilder sb = new StringBuilder();
NativeDLL.GetWindowsDirectory(sb, 256);
NativeDLL.MessageBox(IntPtr.Zero, sb.ToString(), "Directory", 0);
// get system time
SystemTIme t = new SystemTIme();
NativeDLL.GetSystemTime(t);
NativeDLL.MessageBox(IntPtr.Zero, string.Format("{0}년 {1}월 {2}일 {3}시 {4}분 {5}초", t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Second), "System Time", 0);
}
}
[C#] Calling a function in Native-DLL from C#(.NET)
It's a simple that to call a function in Native-DLL from C#.
using System.Runtime.InteropService;
static extern int MessageBox(IntPtr hWnd, string text, string caption, int type);
using System;
using System.Runtime.InteropServices;
public class MsgBoxTest
{
[DllImport("user32.dll")]
static extern public int MessageBox(IntPtr hWnd, string text, string caption, int type);
}
public class Program
{
static void Main(string[] args)
{
MsgBoxTest.MessageBox(IntPtr.Zero, "Please do not press this againg", "Attention", 0);
}
}
First, import some namespace.
using System;using System.Runtime.InteropService;
Next, define same function of Native-DLL with extern, static, DllImport attribute.
[DllImport("user32.dll")]static extern int MessageBox(IntPtr hWnd, string text, string caption, int type);
Last, call the function.
MessageBox(IntPtr.Zero, "Test message", "Test captin", 0);Following is example that to call MessageBox function of Windows DLL user32.dll.
using System;
using System.Runtime.InteropServices;
public class MsgBoxTest
{
[DllImport("user32.dll")]
static extern public int MessageBox(IntPtr hWnd, string text, string caption, int type);
}
public class Program
{
static void Main(string[] args)
{
MsgBoxTest.MessageBox(IntPtr.Zero, "Please do not press this againg", "Attention", 0);
}
}
2017년 2월 20일 월요일
[C#, Autodesk.NET] To get ObjectIdCollection of all entities in layer.
private ObjectIdCollection GetEntitiesOnLayer(string layerName)
{
Document doc = GetDocument();
Editor ed = GetEditor();
// Build a filter list so that only entities
// on the specified layer are selected
TypedValue[] tvs = new TypedValue[1] { new TypedValue((int)DxfCode.LayerName, layerName) };
SelectionFilter sf = new SelectionFilter(tvs);
PromptSelectionResult psr = ed.SelectAll(sf);
if (psr.Status == PromptStatus.OK)
return new ObjectIdCollection(psr.Value.GetObjectIds());
else
return new ObjectIdCollection();
}
{
Document doc = GetDocument();
Editor ed = GetEditor();
// Build a filter list so that only entities
// on the specified layer are selected
TypedValue[] tvs = new TypedValue[1] { new TypedValue((int)DxfCode.LayerName, layerName) };
SelectionFilter sf = new SelectionFilter(tvs);
PromptSelectionResult psr = ed.SelectAll(sf);
if (psr.Status == PromptStatus.OK)
return new ObjectIdCollection(psr.Value.GetObjectIds());
else
return new ObjectIdCollection();
}
[C#] Looping all keys of Dictionary
void Main()
{
// define a dictionary
Dictionary<string, bool> selectedLayers = new Dictionary<string, bool>();
// add items to dictionary
for (int i = 0; i < 10; ++i)
{
selectedLayers.Add(i.ToString(), true);
}
// looping all items of dictionary
foreach(var num in selectedLayers.Keys)
{
Console.WriteLine(num);
}
}
2017년 2월 16일 목요일
[C#] Extension method
You can add new method into exist class using extension method.
Extension method is defined with static class, static method and this before first parameter.
Here is some simple example that to add new method to string class and int variable.
void Main()
{
Console.WriteLine("Father".IsCaptilized());
Console.WriteLine(3.Duplicate());
}
public static class StringHelper
{
public static bool IsCaptilized(this string s)
{
if(string.IsNullOrEmpty(s))
return false;
return char.IsUpper(s[0]);
}
}
public static class IntHelper
{
public static int Duplicate(this int n) => n * 2;
}
StringHelper and IntHelper above classes are extension method for string and int.
Extension method is defined with static class, static method and this before first parameter.
Here is some simple example that to add new method to string class and int variable.
void Main()
{
Console.WriteLine("Father".IsCaptilized());
Console.WriteLine(3.Duplicate());
}
public static class StringHelper
{
public static bool IsCaptilized(this string s)
{
if(string.IsNullOrEmpty(s))
return false;
return char.IsUpper(s[0]);
}
}
public static class IntHelper
{
public static int Duplicate(this int n) => n * 2;
}
StringHelper and IntHelper above classes are extension method for string and int.
2017년 2월 15일 수요일
[C#] To use Array.CreateInstance
// Define Array with 3 strings and 2 based on.
Array a = Array.CreateInstance(typeof(string), new int[]{3}, new int[]{2});
a.SetValue("a", 2);
Console.WriteLine(a);
// Define Array with 3 x 3 strings and 0 based on.
Array b= Array.CreateInstance(typeof(string), 3, 3);
b.SetValue("a", 2, 2);
Console.WriteLine(b);
Result
Array a = Array.CreateInstance(typeof(string), new int[]{3}, new int[]{2});
a.SetValue("a", 2);
Console.WriteLine(a);
// Define Array with 3 x 3 strings and 0 based on.
Array b= Array.CreateInstance(typeof(string), 3, 3);
b.SetValue("a", 2, 2);
Console.WriteLine(b);
Result
[C#] Nullable object
Normal condition.
int i = 10; // OK
but
int i = null; // Compile error
To set a null to type of int variable, you can use int?.
int? i = null; // OK
To convert int and int?, do it following examples.
int i = 5; // OK
int y = i; // Compile error
int y = (int)i; // OK
To check if nullable type of int variable has a null, call HasValue property.
int? i = null;
if(i.HasValue == false)
{
Console.WriteLine("null");
}
int i = 10; // OK
but
int i = null; // Compile error
To set a null to type of int variable, you can use int?.
int? i = null; // OK
To convert int and int?, do it following examples.
int i = 5; // OK
int y = i; // Compile error
int y = (int)i; // OK
To check if nullable type of int variable has a null, call HasValue property.
int? i = null;
if(i.HasValue == false)
{
Console.WriteLine("null");
}
[C#] To convert string to int
int d = 0;
if(int.TryParse("123", out d) == false)
{
Console.WriteLine("false");
}
else
{
Console.WriteLine("true");
}
You can use int.Parse instead of int.TryParse.
But, if you use int.Parse instead of int.TryParse, throw exception.
if(int.TryParse("123", out d) == false)
{
Console.WriteLine("false");
}
else
{
Console.WriteLine("true");
}
You can use int.Parse instead of int.TryParse.
But, if you use int.Parse instead of int.TryParse, throw exception.
피드 구독하기:
글 (Atom)