明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: 介之推

[运行时] 如何编写代码实现从外部输入块,并插入到当前文件中?

  [复制链接]
发表于 2013-4-1 18:48 | 显示全部楼层
介之推 发表于 2013-4-1 16:01
你好,sieben。
你的第二个函数调试通了以后,编译不出问题,我这样调用你的InsertDwg()函数,运行时出错了 ...

我也不明所以,不过应该不是你所说的那一行出错,你的代码或许太简洁了。
发表于 2013-4-1 18:59 | 显示全部楼层
上面是什么程序语言啊?不象VBA
 楼主| 发表于 2013-4-1 19:52 | 显示全部楼层
本帖最后由 介之推 于 2013-4-1 19:54 编辑
sieben 发表于 2013-4-1 18:48
我也不明所以,不过应该不是你所说的那一行出错,你的代码或许太简洁了。


你好,我调试了,确实是那一行代码出错了。
完整的代码入下:
  1. public ObjectId InsertDwg(string fileName, ref string blockName)
  2.         {

  3.             try
  4.             {
  5.                 /*
  6.                 if (!replace)
  7.                 {
  8.                     ObjectId blockId = sn.GetRecordId(sc.db.BlockTableId, blockName);
  9.                     if (blockId != ObjectId.Null)
  10.                         return blockId;
  11.                 }
  12.                 //*/
  13.                 if (fileName == "" || !File.Exists(fileName))
  14.                 {
  15.                     Editor se = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  16.                     //se.WriteMessage(ex);
  17.                     se.WriteMessage("File not found");
  18.                     return ObjectId.Null;
  19.                 }
  20.                 if (!fileName.ToLower().EndsWith(".dwg"))
  21.                 {
  22.                     Editor se = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  23.                     //se.WriteMessage(ex);
  24.                     se.WriteMessage("Not dwg file");
  25.                     return ObjectId.Null;
  26.                 }                  
  27.             }
  28.             catch (System.Exception ex)
  29.             {
  30.                 Editor se = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  31.                 //se.WriteMessage(ex);
  32.                 se.WriteMessage(ex.StackTrace);
  33.                 return ObjectId.Null;
  34.             }
  35.             Database newDB = new Database(false, false);
  36.             bool newDBHasDispose = false;
  37.             try
  38.             {
  39.                 Database myDb = HostApplicationServices.WorkingDatabase;

  40.                 if (blockName == "")
  41.                 {
  42.                     FileInfo fileInfo = new FileInfo(fileName);
  43.                     blockName = fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
  44.                 }
  45.                 //newDB.ReadDwgFile(fileName, System.IO.FileShare.Read, true, fileName);
  46.                 newDB.ReadDwgFile(fileName, System.IO.FileShare.Read, true, null);
  47.                 if (newDB.Insbase.GetAsVector().Length > 0.00001)
  48.                 {
  49.                     if (newDB.Insbase.X >= newDB.Extmin.X && newDB.Insbase.X <= newDB.Extmax.X
  50.                       && newDB.Insbase.Y >= newDB.Extmin.Y && newDB.Insbase.Y <= newDB.Extmax.Y)
  51.                     {
  52.                         //Insbase位于图形范围,不进行移动,有可能不符合预期
  53.                         //return sc.db.Insert(blockName, newDB, true);
  54.                         return myDb.Insert(blockName, newDB, true);
  55.                     }
  56.                     else
  57.                     {
  58.                         if (newDB.Extmin.X > newDB.Extmax.X)
  59.                         {
  60.                             //图形范围Extmin Extmax未初始化,不进行移动,有可能不符合预期
  61.                             //return sc.db.Insert(blockName, newDB, true);
  62.                             return myDb.Insert(blockName, newDB, true);
  63.                         }
  64.                         else
  65.                         {
  66.                             Matrix3d tMat = Matrix3d.Displacement(newDB.Insbase.GetAsVector().Negate());
  67.                             newDB.Insbase = Point3d.Origin;
  68.                             //ObjectId blockId = sc.db.Insert(blockName, newDB, true);
  69.                             ObjectId blockId = myDb.Insert(blockName, newDB, true);
  70.                             if (0 >= newDB.Extmin.X && 0 <= newDB.Extmax.X && 0 >= newDB.Extmin.Y && 0 <= newDB.Extmax.Y)
  71.                             {
  72.                                 //坐标原点位于图形范围,不进行移动,有可能不符合预期
  73.                                 return blockId;
  74.                             }
  75.                             else
  76.                             {
  77.                                 //Insbase不位于图形范围 坐标原点也不位于图形范围,将图形从Insbase移到原点,有可能不符合预期
  78.                                 //using (Transaction ctrans = sc.db.TransactionManager.StartTransaction())
  79.                                 using(Transaction ctrans = myDb.TransactionManager.StartTransaction())
  80.                                 {
  81.                                     BlockTableRecord cbtr = (BlockTableRecord)ctrans.GetObject(blockId, OpenMode.ForWrite);
  82.                                     foreach (ObjectId tId in cbtr)
  83.                                     {
  84.                                         Entity ent = (Entity)ctrans.GetObject(tId, OpenMode.ForWrite);
  85.                                         ent.TransformBy(tMat);
  86.                                     }
  87.                                     ctrans.Commit();
  88.                                 }
  89.                                 return blockId;
  90.                             }
  91.                         }

  92.                     }
  93.                 }
  94.                 else
  95.                 {
  96.                     //Insbase位于原点,不用做任何处理
  97.                     //return sc.db.Insert(blockName, newDB, true);
  98.                     return myDb.Insert(blockName, newDB, true);         //这行出错了,错误信息:eSelfReference
  99.                 }
  100.             }
  101.             catch (System.Exception ex)
  102.             {
  103.                 Editor se = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  104.                 //se.WriteMessage(ex);
  105.                 se.WriteMessage(ex.StackTrace + "\n" + ex.Message + "\n" + ex.TargetSite);
  106.                 return ObjectId.Null;
  107.             }
  108.             finally
  109.             {
  110.                 if (!newDBHasDispose)
  111.                     newDB.Dispose();
  112.             }
  113.         }
就是你给我的第二个函数,就是把几个变量名换了,其他都没有动。
调用这个函数的命令如下:
  1. [CommandMethod("IB")]
  2.         public void InsertBlock()
  3.         {
  4.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.             try
  8.             {
  9.                 using (Transaction trans = db.TransactionManager.StartTransaction())
  10.                 {
  11.                     //ImportBlock_DB("test.dwg", "sample");
  12.                     string blockName = "sample"; InsertDwg("test.dwg", ref blockName);

  13.                     trans.Commit();
  14.                 }
  15.             }
  16.             catch (System.Exception ex)
  17.             {               
  18.                 ed.WriteMessage(ex.StackTrace + "\n" + ex.Message + "\n" + ex.TargetSite);
  19.                 return;
  20.             }
  21.                      
  22.         }
我用的是AutoCAD2008,难道是版本的问题?
 楼主| 发表于 2013-4-1 19:55 | 显示全部楼层
清风明月名字 发表于 2013-4-1 18:59
上面是什么程序语言啊?不象VBA

VBA?哈哈,这个真不是VBA呢。哈哈。
发表于 2013-4-1 20:04 | 显示全部楼层
你的代码太简洁了,即使调用的函数内部不出错,你的代码也是行不通的
 楼主| 发表于 2013-4-1 20:33 | 显示全部楼层
本帖最后由 介之推 于 2013-4-1 20:35 编辑
sieben 发表于 2013-4-1 20:04
你的代码太简洁了,即使调用的函数内部不出错,你的代码也是行不通的

你好,你说的是我的调用函数[CommandMethod("IB")]InsertBlock(){...}太简单了,还是下面调用你的函数的这行代码太简洁了?
  1. string blockName = "sample"; InsertDwg("test.dwg", ref blockName);
复制代码

如何正确调用呢?请帮帮忙!
发表于 2013-4-1 22:00 | 显示全部楼层
介之推 发表于 2013-4-1 20:33
你好,你说的是我的调用函数[CommandMethod("IB")]InsertBlock(){...}太简单了,还是下面调用你的函数的这 ...

对,我就是想说你这一行代码太简单了,我怀疑你是不是在用Lisp的语法来写这个代码
发表于 2013-4-1 22:01 | 显示全部楼层
另外,这句代码的前面一行和后面一行也是不必要的,甚至是致命的
 楼主| 发表于 2013-4-1 22:47 | 显示全部楼层
sieben 发表于 2013-4-1 22:01
另外,这句代码的前面一行和后面一行也是不必要的,甚至是致命的

我没有用Lisp啊,不会用Lisp,我只有采用.NET方式开发啊。
我的调用可能太简单了,但我只会这些了。
请问如何在调用你的函数之前做好铺垫?
发表于 2013-4-1 23:07 | 显示全部楼层
建议LZ还是在看看块表的帮助,然后再来看Sieben提供的代码啊,这样你就容易理解啦
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-9 03:26 , Processed in 0.268572 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表