- 积分
- 603
- 明经币
- 个
- 注册时间
- 2011-7-31
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2013-4-1 19:52:56
|
显示全部楼层
本帖最后由 介之推 于 2013-4-1 19:54 编辑
sieben 发表于 2013-4-1 18:48
我也不明所以,不过应该不是你所说的那一行出错,你的代码或许太简洁了。
你好,我调试了,确实是那一行代码出错了。
完整的代码入下:- public ObjectId InsertDwg(string fileName, ref string blockName)
- {
- try
- {
- /*
- if (!replace)
- {
- ObjectId blockId = sn.GetRecordId(sc.db.BlockTableId, blockName);
- if (blockId != ObjectId.Null)
- return blockId;
- }
- //*/
- if (fileName == "" || !File.Exists(fileName))
- {
- Editor se = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- //se.WriteMessage(ex);
- se.WriteMessage("File not found");
- return ObjectId.Null;
- }
- if (!fileName.ToLower().EndsWith(".dwg"))
- {
- Editor se = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- //se.WriteMessage(ex);
- se.WriteMessage("Not dwg file");
- return ObjectId.Null;
- }
- }
- catch (System.Exception ex)
- {
- Editor se = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- //se.WriteMessage(ex);
- se.WriteMessage(ex.StackTrace);
- return ObjectId.Null;
- }
- Database newDB = new Database(false, false);
- bool newDBHasDispose = false;
- try
- {
- Database myDb = HostApplicationServices.WorkingDatabase;
- if (blockName == "")
- {
- FileInfo fileInfo = new FileInfo(fileName);
- blockName = fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
- }
- //newDB.ReadDwgFile(fileName, System.IO.FileShare.Read, true, fileName);
- newDB.ReadDwgFile(fileName, System.IO.FileShare.Read, true, null);
- if (newDB.Insbase.GetAsVector().Length > 0.00001)
- {
- if (newDB.Insbase.X >= newDB.Extmin.X && newDB.Insbase.X <= newDB.Extmax.X
- && newDB.Insbase.Y >= newDB.Extmin.Y && newDB.Insbase.Y <= newDB.Extmax.Y)
- {
- //Insbase位于图形范围,不进行移动,有可能不符合预期
- //return sc.db.Insert(blockName, newDB, true);
- return myDb.Insert(blockName, newDB, true);
- }
- else
- {
- if (newDB.Extmin.X > newDB.Extmax.X)
- {
- //图形范围Extmin Extmax未初始化,不进行移动,有可能不符合预期
- //return sc.db.Insert(blockName, newDB, true);
- return myDb.Insert(blockName, newDB, true);
- }
- else
- {
- Matrix3d tMat = Matrix3d.Displacement(newDB.Insbase.GetAsVector().Negate());
- newDB.Insbase = Point3d.Origin;
- //ObjectId blockId = sc.db.Insert(blockName, newDB, true);
- ObjectId blockId = myDb.Insert(blockName, newDB, true);
- if (0 >= newDB.Extmin.X && 0 <= newDB.Extmax.X && 0 >= newDB.Extmin.Y && 0 <= newDB.Extmax.Y)
- {
- //坐标原点位于图形范围,不进行移动,有可能不符合预期
- return blockId;
- }
- else
- {
- //Insbase不位于图形范围 坐标原点也不位于图形范围,将图形从Insbase移到原点,有可能不符合预期
- //using (Transaction ctrans = sc.db.TransactionManager.StartTransaction())
- using(Transaction ctrans = myDb.TransactionManager.StartTransaction())
- {
- BlockTableRecord cbtr = (BlockTableRecord)ctrans.GetObject(blockId, OpenMode.ForWrite);
- foreach (ObjectId tId in cbtr)
- {
- Entity ent = (Entity)ctrans.GetObject(tId, OpenMode.ForWrite);
- ent.TransformBy(tMat);
- }
- ctrans.Commit();
- }
- return blockId;
- }
- }
- }
- }
- else
- {
- //Insbase位于原点,不用做任何处理
- //return sc.db.Insert(blockName, newDB, true);
- return myDb.Insert(blockName, newDB, true); //这行出错了,错误信息:eSelfReference
- }
- }
- catch (System.Exception ex)
- {
- Editor se = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- //se.WriteMessage(ex);
- se.WriteMessage(ex.StackTrace + "\n" + ex.Message + "\n" + ex.TargetSite);
- return ObjectId.Null;
- }
- finally
- {
- if (!newDBHasDispose)
- newDB.Dispose();
- }
- }
就是你给我的第二个函数,就是把几个变量名换了,其他都没有动。
调用这个函数的命令如下:- [CommandMethod("IB")]
- public void InsertBlock()
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- try
- {
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- //ImportBlock_DB("test.dwg", "sample");
- string blockName = "sample"; InsertDwg("test.dwg", ref blockName);
- trans.Commit();
- }
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage(ex.StackTrace + "\n" + ex.Message + "\n" + ex.TargetSite);
- return;
- }
-
- }
我用的是AutoCAD2008,难道是版本的问题?
|
|