分享:无需引用dll,通过windows窗体启动CAD并画图
其实是利用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;
visargs = 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;
args = filePath;
args = 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;
double[] endPoint = new double;
startPoint = 0; startPoint = 0; startPoint = 0;
endPoint = 100; endPoint = 100; endPoint = 0;
drawLineInObject(oSpace, startPoint, startPoint, endPoint, endPoint, "0");
//全屏显示图形
AcadApp.GetType().InvokeMember("ZoomExtents", BindingFlags.InvokeMethod, null, AcadApp, null);
object[] closeargs = new object;
closeargs = true;
closeargs = 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;
double[] endpRef = new double;
begpRef = startX;
begpRef = startY;
begpRef = 0.0;
endpRef = endX;
endpRef = endY;
endpRef = 0.0;
object[] pts = new object;
pts = begpRef;
pts = 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;
}
com,vb用这个相当方便 菜鸟路过,不是很明白哦 是通过COM的后期绑定方式,这种是不需要引用,如果采用前期绑定,则需要引用,当然开发上也更快捷。 后期绑定确实不用引用COM,但通过反射调用起来效率大打折扣啊。当然特定情况还是有优势的。 外部开发,不错,习惯这类的了
页:
[1]