添加项目文件。
This commit is contained in:
394
MetroGauges/General/CadInstance.cs
Normal file
394
MetroGauges/General/CadInstance.cs
Normal file
@@ -0,0 +1,394 @@
|
||||
using Autodesk.AutoCAD;
|
||||
using Autodesk.AutoCAD.Interop;
|
||||
using Autodesk.AutoCAD.Interop.Common;
|
||||
|
||||
//using Autodesk.AutoCAD;
|
||||
//using Autodesk.AutoCAD.Interop;
|
||||
//using Autodesk.AutoCAD.Interop.Common;
|
||||
|
||||
using Microsoft.Win32;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace MetroGauges.General
|
||||
{
|
||||
public class CadHelper
|
||||
{
|
||||
private static WpfCadTool frm;
|
||||
//"Autodesk.AutoCAD.17" stands for AutoCAD 2007-2009
|
||||
|
||||
//"Autodesk.AutoCAD.18" stands for AutoCAD 2010-2012
|
||||
|
||||
//"Autodesk.AutoCAD.19" stands for AutoCAD 2013-2014
|
||||
|
||||
//"Autodesk.AutoCAD.20" stands for AutoCAD 2015-2016
|
||||
|
||||
//"Autodesk.AutoCAD.21" stands for AutoCAD 2017
|
||||
const string _autocadClassId = "AutoCAD.Application.19";
|
||||
public const int RED = 0X0A;
|
||||
public const int GREEN = 0X0B;
|
||||
public const int BLUE = 0X0C;
|
||||
public const int YELLOW = 0X0D;
|
||||
public const int CYAN = 0X0E;
|
||||
|
||||
|
||||
|
||||
|
||||
public static WpfCadTool CadTool
|
||||
{
|
||||
get
|
||||
{
|
||||
if (frm == null)
|
||||
{
|
||||
frm = new WpfCadTool();
|
||||
}
|
||||
|
||||
return frm;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 在现有的cad绘图界面画图
|
||||
/// </summary>
|
||||
public CadHelper()
|
||||
{
|
||||
initCad();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新建一个名为name的绘图界面绘图
|
||||
/// </summary>
|
||||
/// <param name="name">绘图界面名字</param>
|
||||
public CadHelper(string name)
|
||||
{
|
||||
initCad();
|
||||
AddNewGraph(name);
|
||||
}
|
||||
|
||||
private void AddNewGraph(string name)
|
||||
{
|
||||
if (!IsCadRunning())
|
||||
{
|
||||
return;
|
||||
}
|
||||
AcadApplication CadApp = _application;
|
||||
CadApp.Documents.Add(name);
|
||||
}
|
||||
|
||||
public void ZoomAll()
|
||||
{
|
||||
AcadApplication CadApp = _application;
|
||||
CadApp.Visible = true;
|
||||
CadApp.ZoomAll();
|
||||
}
|
||||
|
||||
public bool AddLayer(string name, int color)
|
||||
{
|
||||
if (!IsCadRunning())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
AcadApplication CadApp = _application;
|
||||
AcadDocument CadDoc;
|
||||
|
||||
try
|
||||
{
|
||||
CadDoc = CadApp.ActiveDocument;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
CadApp.Documents.Add();
|
||||
}
|
||||
|
||||
AcadLayer newLayer = CadApp.ActiveDocument.Layers.Add(name);
|
||||
CadApp.ActiveDocument.ActiveLayer = newLayer;
|
||||
switch (color)
|
||||
{
|
||||
case RED:
|
||||
newLayer.color = AcColor.acRed;
|
||||
break;
|
||||
case GREEN:
|
||||
newLayer.color = AcColor.acGreen;
|
||||
break;
|
||||
case BLUE:
|
||||
newLayer.color = AcColor.acBlue;
|
||||
break;
|
||||
case YELLOW:
|
||||
newLayer.color = AcColor.acYellow;
|
||||
break;
|
||||
case CYAN:
|
||||
newLayer.color = AcColor.acCyan;
|
||||
break;
|
||||
|
||||
default:
|
||||
newLayer.color = AcColor.acWhite;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//画线
|
||||
public bool DrawLine(double[] startPoint, double[] endPoint)
|
||||
{
|
||||
if (!IsCadRunning())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (startPoint == null || startPoint.Length < 2 ||
|
||||
endPoint == null || endPoint.Length < 2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
double[] sp = startPoint;
|
||||
double[] ep = endPoint;
|
||||
if (sp.Length == 2)
|
||||
{
|
||||
sp = new double[3];
|
||||
sp[0] = startPoint[0];
|
||||
sp[1] = startPoint[1];
|
||||
sp[2] = 0.0;
|
||||
}
|
||||
if (ep.Length == 2)
|
||||
{
|
||||
ep = new double[3];
|
||||
ep[0] = endPoint[0];
|
||||
ep[1] = endPoint[1];
|
||||
ep[2] = 0.0;
|
||||
}
|
||||
AcadApplication CadApp = _application;
|
||||
CadApp.ActiveDocument.ModelSpace.AddLine(sp, ep);
|
||||
return true;
|
||||
}
|
||||
//画圆
|
||||
public bool DrawCircle(double[] Centre, double Radius)//画圆
|
||||
{
|
||||
if (!IsCadRunning())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
AcadCircle CircleObj;
|
||||
double[] C = Centre;
|
||||
if (C.Length == 2)
|
||||
{
|
||||
C = new double[3];
|
||||
C[0] = Centre[0];
|
||||
C[1] = Centre[1];
|
||||
C[2] = 0.0;
|
||||
}
|
||||
AcadApplication CadApp = _application;
|
||||
CircleObj = CadApp.ActiveDocument.ModelSpace.AddCircle(C, Radius);
|
||||
return true;
|
||||
}
|
||||
//画矩形
|
||||
public bool DrawRectang(double[] Point0, double[] Point1, double[] Point2, double[] Point3)//画矩形
|
||||
{
|
||||
if (!IsCadRunning())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
double[] P0 = Point0;
|
||||
double[] P1 = Point1;
|
||||
double[] P2 = Point2;
|
||||
double[] P3 = Point3;
|
||||
if (P0.Length == 2)
|
||||
{
|
||||
P0 = new double[3];
|
||||
P0[0] = Point0[0];
|
||||
P0[1] = Point0[1];
|
||||
P0[2] = 0.0;
|
||||
}
|
||||
if (P1.Length == 2)
|
||||
{
|
||||
P1 = new double[3];
|
||||
P1[0] = Point1[0];
|
||||
P1[1] = Point1[1];
|
||||
P1[2] = 0.0;
|
||||
}
|
||||
if (P2.Length == 2)
|
||||
{
|
||||
P2 = new double[3];
|
||||
P2[0] = Point2[0];
|
||||
P2[1] = Point2[1];
|
||||
P2[2] = 0.0;
|
||||
}
|
||||
if (P3.Length == 2)
|
||||
{
|
||||
P3 = new double[3];
|
||||
P3[0] = Point3[0];
|
||||
P3[1] = Point3[1];
|
||||
P3[2] = 0.0;
|
||||
}
|
||||
AcadApplication CadApp = _application;
|
||||
CadApp.ActiveDocument.ModelSpace.AddLine(P0, P1);
|
||||
CadApp.ActiveDocument.ModelSpace.AddLine(P1, P2);
|
||||
CadApp.ActiveDocument.ModelSpace.AddLine(P2, P3);
|
||||
CadApp.ActiveDocument.ModelSpace.AddLine(P3, P0);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool DrawText(string text, double[] insertPoint)//坐标点标注
|
||||
{
|
||||
return DrawText(text, insertPoint, 5.0);
|
||||
}
|
||||
|
||||
public bool DrawText(string text, double[] insertPoint, double height)
|
||||
{
|
||||
if (!IsCadRunning())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (insertPoint == null || insertPoint.Length < 2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
double[] p = insertPoint;
|
||||
if (p.Length == 2)
|
||||
{
|
||||
p = new double[3];
|
||||
p[0] = insertPoint[0];
|
||||
p[1] = insertPoint[1];
|
||||
p[2] = 0.0;
|
||||
}
|
||||
AcadApplication CadApp = _application;
|
||||
AcadDocument CadDoc;
|
||||
try
|
||||
{
|
||||
CadDoc = CadApp.ActiveDocument;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
CadApp.Documents.Add();
|
||||
}
|
||||
|
||||
CadApp.ActiveDocument.ModelSpace.AddText(text, p, height);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static dynamic _application;
|
||||
|
||||
public bool IsCadRunning()
|
||||
{
|
||||
if (_application == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void GetAutoCAD()
|
||||
{
|
||||
_application = Marshal.GetActiveObject(_autocadClassId);
|
||||
AcadApplication cad = _application as AcadApplication;
|
||||
cad.BeginQuit += Cad_BeginQuit;
|
||||
}
|
||||
|
||||
private void Cad_BeginQuit(ref bool Cancel)
|
||||
{
|
||||
frm.Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
frm.Hide();
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
private void StartAutoCad()
|
||||
{
|
||||
var t = Type.GetTypeFromProgID(_autocadClassId, true);
|
||||
// Create a new instance Autocad.
|
||||
var obj = Activator.CreateInstance(t, true);
|
||||
// No need for casting with dynamics
|
||||
_application = obj;
|
||||
|
||||
//_application = new AcadApplication();
|
||||
//AcadApplication CadApp = _application;
|
||||
//CadApp.Visible = true;
|
||||
|
||||
AcadApplication cad = _application as AcadApplication;
|
||||
cad.BeginQuit += Cad_BeginQuit;
|
||||
}
|
||||
|
||||
private void initCad()
|
||||
{
|
||||
try
|
||||
{
|
||||
initCadWithEx();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
_application = null;
|
||||
initCad();
|
||||
}
|
||||
}
|
||||
|
||||
private void initCadWithEx()
|
||||
{
|
||||
Console.WriteLine("Loading Autocad: {0}", _autocadClassId);
|
||||
if (_application == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
GetAutoCAD();
|
||||
}
|
||||
catch (COMException)//删除ex参数
|
||||
{
|
||||
try
|
||||
{
|
||||
StartAutoCad();
|
||||
}
|
||||
catch (Exception e2x)
|
||||
{
|
||||
Console.WriteLine(e2x);
|
||||
//ThrowComException(ex);
|
||||
_application = null;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
//ThrowComException(ex);
|
||||
_application = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public List<string> GetAutoCADLocations()
|
||||
//获取系统各CAD版本集合的这个方法摘自才鸟新书里的例子
|
||||
{
|
||||
//用于存储系统中安装的AutoCAD路径列表
|
||||
List<string> locations = new List<string>();
|
||||
// 获取HKEY_LOCAL_MACHINE键
|
||||
RegistryKey keyLocalMachine = Registry.LocalMachine;
|
||||
// 打开AutoCAD所属的注册表键:HKEY_LOCAL_MACHINE\Software\Autodesk\AutoCAD
|
||||
RegistryKey keyAutoCAD = keyLocalMachine.OpenSubKey("Software\\Autodesk\\AutoCAD");
|
||||
//获得表示系统中安装的各版本的AutoCAD注册表键
|
||||
string[] cadVersions = keyAutoCAD.GetSubKeyNames();
|
||||
foreach (string cadVersion in cadVersions)
|
||||
{
|
||||
//打开特定版本的AutoCAD注册表键
|
||||
RegistryKey keyCADVersion = keyAutoCAD.OpenSubKey(cadVersion);
|
||||
//获取表示各语言版本的AutoCAD注册表键值
|
||||
string[] cadNames = keyCADVersion.GetSubKeyNames();
|
||||
foreach (string cadName in cadNames)
|
||||
{
|
||||
if (cadName.EndsWith("804"))//中文版本
|
||||
{
|
||||
//打开中文版本的AutoCAD所属的注册表键
|
||||
RegistryKey keyCADName = keyCADVersion.OpenSubKey(cadName);
|
||||
//获取AutoCAD的安装路径
|
||||
string location = keyCADName.GetValue("Location").ToString();
|
||||
locations.Add(location);//将路径添加到列表中
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return locations;//返回系统中安装的AutoCAD路径列表
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user