- 积分
- 75
- 明经币
- 个
- 注册时间
- 2012-12-5
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
其实是利用VBA来画图,无需引用其他dll,用VB的高手就简单了,见代码:
private void button9_Click(object sender, EventArgs e)
{
MessageBox.Show("请选择要保存的dwg文件!");
OpenFileDialog pOpenDG = new OpenFileDialog();
pOpenDG.Filter = "CAD文件(*.dwg)|*.dwg|CAD图形文件(*.dxf)|*.dxf";
pOpenDG.InitialDirectory = defaultPath;
pOpenDG.Title = "打开CAD文件";
if (pOpenDG.ShowDialog() == DialogResult.OK)
{
string filePath = pOpenDG.FileName;
//引用CAD/////////////////////////////////////////////////////////////////////////////////
System.Globalization.CultureInfo thisThread = System.Threading.Thread.CurrentThread.CurrentCulture;
thisThread = new System.Globalization.CultureInfo("zh-CHS");
string appProgID = "AutoCAD.Application";
Type AcadType = Type.GetTypeFromProgID(appProgID);
object AcadApp = Activator.CreateInstance(AcadType);
object[] visargs = new object[1];
visargs[0] = true;
AcadApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, AcadApp, visargs, null);
object AcadDocs = AcadApp.GetType().InvokeMember("Documents", BindingFlags.GetProperty, null, AcadApp, null);
object[] args = new object[2];
args[0] = filePath;
args[1] = false;
object AcDoc = AcadDocs.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, AcadDocs, args, null);
object Util = new object();
//////////////////////////////////////////////////////////////////////////////
try
{
//定义CAD数据
AcDoc = AcadApp.GetType().InvokeMember("ActiveDocument", BindingFlags.GetProperty, null, AcadApp, null, null);
Util = AcDoc.GetType().InvokeMember("Utility", BindingFlags.GetProperty, null, AcDoc, null);
object oSpace = AcDoc.GetType().InvokeMember("ModelSpace", BindingFlags.GetProperty, null, AcDoc, null);
//画直线
double[] startPoint = new double[3];
double[] endPoint = new double[3];
startPoint[0] = 0; startPoint[1] = 0; startPoint[2] = 0;
endPoint[0] = 100; endPoint[1] = 100; endPoint[2] = 0;
drawLineInObject(oSpace, startPoint[0], startPoint[1], endPoint[0], endPoint[1], "0");
//全屏显示图形
AcadApp.GetType().InvokeMember("ZoomExtents", BindingFlags.InvokeMethod, null, AcadApp, null);
object[] closeargs = new object[2];
closeargs[0] = true;
closeargs[1] = filePath;
AcDoc.GetType().InvokeMember("Close", BindingFlags.InvokeMethod, null, AcDoc, closeargs);
AcadApp.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, AcadApp, null);
MessageBox.Show("平绘制完成,图形已保存并关闭!");
}
catch
{
MessageBox.Show("绘图错误!");
}
finally
{
releaseObject(AcDoc);
releaseObject(AcadDocs);
releaseObject(AcadApp);
GC.WaitForPendingFinalizers();
GC.GetTotalMemory(true);
GC.WaitForPendingFinalizers();
GC.GetTotalMemory(true);
releaseObject(AcDoc);
releaseObject(AcadDocs);
releaseObject(AcadApp);
GC.WaitForPendingFinalizers();
GC.GetTotalMemory(true);
GC.WaitForPendingFinalizers();
GC.GetTotalMemory(true);
System.Threading.Thread.CurrentThread.CurrentUICulture = thisThread;
}
}
}
//释放CAD变量
public static void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
obj = null;
}
catch
{
obj = null;
}
finally
{
GC.Collect();
}
}
//CAD画直线
public static object drawLineInObject(object oBlock, double startX, double startY, double endX, double endY, string layer)
{
double[] begpRef = new double[3];
double[] endpRef = new double[3];
begpRef[0] = startX;
begpRef[1] = startY;
begpRef[2] = 0.0;
endpRef[0] = endX;
endpRef[1] = endY;
endpRef[2] = 0.0;
object[] pts = new object[2];
pts[0] = begpRef;
pts[1] = endpRef;
object oLine = oBlock.GetType().InvokeMember("AddLine", BindingFlags.InvokeMethod, null, oBlock, pts);
oLine.GetType().InvokeMember("Layer", BindingFlags.SetProperty, null, oLine, new object[] { layer });
oLine.GetType().InvokeMember("Update", BindingFlags.InvokeMethod, null, oLine, null);
return oLine;
}
|
|