导入 acdbmgd 和 acmgb 两个cad类库文件,想往 cad 里插入一条直线.程序采用的是传统winform结构(非WPF)
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;namespace CAD_Excel_Library
{
public static class CAD_Excel_Library
{
[CommandMethod ("AddLine")]
public static void AddLineCmd()
{
using (Database db = HostApplicationServices.WorkingDatabase)//获得当前工作空间的数据库
{
using (BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead))//获得块表
{
using (BlockTableRecord btr = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite))//获得模型空间的块表记录
{
Line line = new Line(new Point3d(0, 0, 0), new Point3d(200, 200, 0));//创建一条直线
line.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Red);
btr.AppendEntity(line);//将直线添加到模型空间中
}
}
}
}
}
}一直报错: 组件"Autodesk.AutoCAD.AcInfoCenterConn.MyAutoCADBalloon"不具有由 URI "/AcWindows:component/infocenteracconn/myautocadballoon.xaml"识别的资源..
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;namespace CAD_Excel_Library
{
public static class CAD_Excel_Library
{
[CommandMethod ("AddLine")]
public static void AddLineCmd()
{
using (Database db = HostApplicationServices.WorkingDatabase)//获得当前工作空间的数据库
{
using (BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead))//获得块表
{
using (BlockTableRecord btr = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite))//获得模型空间的块表记录
{
Line line = new Line(new Point3d(0, 0, 0), new Point3d(200, 200, 0));//创建一条直线
line.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Red);
btr.AppendEntity(line);//将直线添加到模型空间中
}
}
}
}
}
}一直报错: 组件"Autodesk.AutoCAD.AcInfoCenterConn.MyAutoCADBalloon"不具有由 URI "/AcWindows:component/infocenteracconn/myautocadballoon.xaml"识别的资源..
public static void AddLineCmd()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
BlockTableRecord btr = tr.GetObject(doc.Database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
Line line = new Line(new Point3d(0, 0, 0), new Point3d(200, 200, 0));//创建一条直线
line.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Red);
btr.AppendEntity(line);//将直线添加到模型空间中
tr.AddNewlyCreatedDBObject(line, true);
tr.Commit();
}
}
我改了下,这个应该可以吧,我运行了下你的程序,bt取不到的
1。把运行目录下的acdbmgd.dll 和 acmgb.dll删除,
2。取消引用性质中的拷贝到本地。