首先,我是cad2018.net vs2015開發環境,利用向導生成后,只加了一小段csdn.net上的別人寫好的代碼,
很簡單的一段,但是我這里就是運行不了,會出錯。系統生成的別的命令都沒有問題。
請大神幫看一下,哪里的問題?
// (C) Copyright 2020 by Microsoft
//
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in4.MyCommands))]
namespace AutoCAD_CSharp_plug_in4
{
// This class is instantiated by AutoCAD for each document when
// a command is called by the user the first time in the context
// of a given document. In other words, non static data in this class
// is implicitly per-document!
public class MyCommands
{
// The CommandMethod attribute can be applied to any public member
// function of any public class.
// The function should take no arguments and return nothing.
// If the method is an intance member then the enclosing class is
// intantiated for each document. If the member is a static member then
// the enclosing class is NOT intantiated.
//
// NOTE: CommandMethod has overloads where you can provide helpid and
// context menu.
// Modal Command with localized name
[CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
public void MyCommand() // This method can have any name
{
// Put your command code here
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed;
if (doc != null)
{
ed = doc.Editor;
ed.WriteMessage("Hello, this is your first command.");
}
}
// Modal Command with pickfirst selection
[CommandMethod("MyGroup", "MyPickFirst", "MyPickFirstLocal", CommandFlags.Modal | CommandFlags.UsePickSet)]
public void MyPickFirst() // This method can have any name
{
PromptSelectionResult result = Application.DocumentManager.MdiActiveDocument.Editor.GetSelection();
if (result.Status == PromptStatus.OK)
{
// There are selected entities
// Put your command using pickfirst set code here
}
else
{
// There are no selected entities
// Put your command code here
}
}
// Application Session Command with localized name
[CommandMethod("MyGroup", "MySessionCmd", "MySessionCmdLocal", CommandFlags.Modal | CommandFlags.Session)]
public void MySessionCmd() // This method can have any name
{
// Put your command code here
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// 啟動一個事務
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 打開塊表
var acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
// 以寫方式打開模型空間塊表記錄
var acBlkTblReccircle = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
var acBlkTblRecarc = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
// 創建一個中心點在 (2,3,0) ,半徑為4.25 的圓
Circle acCirc = new Circle();//實體化一個圓
acCirc.SetDatabaseDefaults();//我沒用著段代碼,也可以運行,不知道他是干嘛的,望大家多多指教
acCirc.Center = new Point3d(2, 3, 0);//指定圓心位置
acCirc.Radius = 4.25;//指定半徑
// 創建一個中心點在 (6.25,9.125,0),半徑為6,起始角度為1.117(64度),終點角度為3.5605(204度)的圓弧。
Arc acArc = new Arc(new Point3d(6.25, 9.125, 0), 6, 1.117, 3.5605);
// 添加新物件到塊表記錄和事務中
acBlkTblReccircle.AppendEntity(acCirc);//將圓添加到塊表記錄中
acBlkTblRecarc.AppendEntity(acArc);//將圓弧添加到塊表記錄中
acTrans.AddNewlyCreatedDBObject(acCirc, true);//添加到事物處理管理器中
acTrans.AddNewlyCreatedDBObject(acArc, true);//添加到事物處理管理器中
acTrans.Commit(); // 保存新物件到資料庫中
}
}
// LispFunction is similar to CommandMethod but it creates a lisp
// callable function. Many return types are supported not just string
// or integer.
[LispFunction("MyLispFunction", "MyLispFunctionLocal")]
public int MyLispFunction(ResultBuffer args) // This method can have any name
{
// Put your command code here
// Return a value to the AutoCAD Lisp Interpreter
return 1;
}
}
}
運行MySessionCmd命令,出錯資訊如下:
應用程式不支持實時(JIT)除錯。
有關詳細資訊,請參見此訊息的結尾。
************** 例外文本 **************
Autodesk.AutoCAD.Runtime.Exception: eLockViolation
在 Autodesk.AutoCAD.DatabaseServices.TransactionManager.GetObjectInternal(AcDbTransactionManager* pTM, ObjectId id, OpenMode mode, Boolean openErased, Boolean forceOpenOnLockedLayer)
在 Autodesk.AutoCAD.DatabaseServices.Transaction.GetObject(ObjectId id, OpenMode mode)
在 AutoCAD_CSharp_plug_in4.MyCommands.MySessionCmd() 位置 E:\DevDir\AutoCAD CSharp plug-in4\myCommands.cs:行號 77
在 Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)
在 Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)
在 Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)
在 Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()
補充:除錯中發現,都是這三句出錯。
var acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
var acBlkTblReccircle = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
var acBlkTblRecarc = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
因為我只要把ForWrite改為Read就能往下走。。。改一個走一句。
請問是什么情況 啊,非常感謝!
uj5u.com熱心網友回復:
大神們,快來搭救我啊轉載請註明出處,本文鏈接:https://www.uj5u.com/net/60539.html
標籤:C#
上一篇:想自學.net
