马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 yeah188 于 2017-5-26 16:55 编辑
大家好,小弟初来乍到。
最近接触.net二次开发,但在环境搭建上碰到了难题。
碰到的问题:我想通过WINFORM窗口程序,对AutoCAD进行操作。
但是运行期间碰到了如下错误:
出错提示
说一下运行环境,本人WIN10 64位系统,AUTOCAD2016,已经添加了对accoremgd.dll、acdbmgd.dll、acmgd.dll和AutoCAD 2016 Type Library的引用。
这些代码我通过传统的创建类库的方式,通过调试acad.exe的方式启动,执行相应命令是能够顺利运行的。但不知为何直接外部调用不可以。
代码如下:
Form1.cs:- namespace AcadConnectionTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Class1.ConnectToAcad();
- Class1.DrawingTest();
- Class1.AddNewCircleTransaction();
- }
- }
- }
前两个函数都能够正常运行,第三个调用到相关.dll文件之后就不行了,很是费解
Class1.cs://这些都是书上的例子,简单修改的
- using AutoCAD;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- namespace AcadConnectionTest
- {
- class Class1
- {
- public static AcadApplication acAppComObj = null;
- public static AcadDocument acDocComObj;
- [CommandMethod("ConnectToAcad")]
- public static void ConnectToAcad()
- {
- const string strProgId = "AutoCAD.Application.20";
- // Get a running instance of AutoCAD
- try
- {
- acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
- }
- catch // An error occurs if no instance is running
- {
- try
- {
- // Create a new instance of AutoCAD
- acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
- }
- catch
- {
- // If an instance of AutoCAD is not created then message and exit
- System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" +
- " could not be created.");
- return;
- }
- }
- // Display the application and return the name and version
- acAppComObj.Visible = true;
- System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name + " version " + acAppComObj.Version);
- }
- public static void DrawingTest()
- {
- // Get the active document
- acDocComObj = acAppComObj.ActiveDocument;
- double[] startPoint = new double[3] { 0, 0, 0 }; //声明直线起点坐标
- double[] endPoint = new double[3] { 300, 200, 0 }; //声明直线终点坐标
- acDocComObj.ModelSpace.AddLine(startPoint, endPoint);
- }
- public static void AddNewCircleTransaction()
- {
- // 获得当前文档和数据库 Get the current document and database
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- Database acCurDb = acDoc.Database;
- // 启动一个事务 Start a transaction
- using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
- {
- // 以只读方式打开块表 Open the Block table for read
- BlockTable acBlkTbl;
- acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
- OpenMode.ForRead) as BlockTable;
- // 以写方式打开模型空间块表记录 Open the Block table record Model space for write
- BlockTableRecord acBlkTblRec;
- acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
- OpenMode.ForWrite) as BlockTableRecord;
- // 创建一个半径为3圆心在5,5的圆 Create a circle with a radius of 3 at 5,5
- Circle acCirc = new Circle();
- acCirc.SetDatabaseDefaults();
- acCirc.Center = new Point3d(5, 5, 0);
- acCirc.Radius = 3;
- // 添加新对象到模型空间和事务中 Add the new object to Model space and the transaction
- acBlkTblRec.AppendEntity(acCirc);
- acTrans.AddNewlyCreatedDBObject(acCirc, true);
- // 提交修改并销毁事务 Commit the changes and dispose of the transaction
- acTrans.Commit();
- }
- }
- }
- }
求好心人解答,谢谢了!
|