明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3537|回复: 4

C#对AUTOCAD二次开发插入图元的问题,求高手指点

[复制链接]
发表于 2009-5-6 15:29:00 | 显示全部楼层 |阅读模式
我使我使用c#对AutoCAD进行二次开发,吧已经做好的DWG插入到模板空间中,插入的位置由鼠标来选择
  我本来想用-INSERT命令的,但是调用后发现输入命令和块名称后要点击下鼠标右键,我在块名称后面直接空格都无法执行回车命令。。。。。。这个很郁闷,不知道如何解决。
  后来我又发现了一个类叫blockreference但是不太会用这个类。
希望高手给点提示或者实例代码。我的目的就是想把DWG插入到模板空间中去,因本人菜鸟刚刚接触AUTOCAD二次开发。求高手指点。

发表于 2009-5-7 12:53:00 | 显示全部楼层

先调用Database的Insert方法把文件插入快表,然后new BlockReference

//下面的代码是我用在自定义类里的,要自己改下:)

        public void GetBlockdefFromFile(string filename, bool over)
        {
            try
            {
                FileInfo fi = new FileInfo(filename);
                string blkdefname = fi.Name;
                if (blkdefname.Contains("."))
                {
                    blkdefname = blkdefname.Substring(0, blkdefname.LastIndexOf('.'));
                }

                bool has = m_BlockTable.Has(blkdefname);
                if ((has && over) || !has)
                {
                    //using (DocumentLock doclock = Application.DocumentManager.MdiActiveDocument.LockDocument())
                    //{
                        Database db = new Database();
                        db.ReadDwgFile(filename, FileShare.Read, true, null);
                        m_Database.Insert(BlockTableRecord.ModelSpace, blkdefname, db, false);
                    //}
                }
            }
            catch
            { }
        }

 楼主| 发表于 2009-5-11 16:18:00 | 显示全部楼层

我写的代码每次添加2个相同的块后,如果把光标移到第一个块上就会出现错误;

"致命错误:unhandled Access Violation Reading 0x0000 Exception at 64906b98h"望高手帮忙解决下。我把我的代码放在下面,大家帮忙看看,谢谢大家了。急等答案。

  /// <summary>
        /// 模板空间中插入图元
        /// </summary>
        public void InsertBlock(string BlockName,string OBJName)
        {
            try
            {
                ObjectId objectid = new ObjectId();
                Database db = new Database();
                db.ReadDwgFile(BlockName, System.IO.FileShare.None, true, "");
                db.CloseInput(true);
                //建立属性块
                AttributeReference AttRef = new AttributeReference();
                using (Transaction tran = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tran.GetObject(db.BlockTableId, OpenMode.ForRead);
                   
                    SymbolUtilityServices s = new SymbolUtilityServices();

                    OBJName = s.GetBlockNameFromInsertPathName(OBJName);
                    //OBJName=db.gett
                    if (bt.Has(OBJName))
                    {
                        objectid = bt[OBJName];
                    }
                    //遍历块查找属性
                    BlockTableRecord BlockBtr = (BlockTableRecord)tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                    if (BlockBtr.HasAttributeDefinitions)
                    {
                        foreach (ObjectId id in BlockBtr)
                        {
                            Entity entity = (Entity)tran.GetObject(id, OpenMode.ForRead, false);
                            if (entity is AttributeDefinition)
                            {
                                AttributeDefinition AttDef = ((AttributeDefinition)(entity));
                                AttRef.SetPropertiesFrom(AttDef);
                                //AttRef.Position = new Point3d(AttDef.Position.X + brf.Position.X, AttDef.Position.Y + brf.Position.Y, AttDef.Position.Z + brf.Position.Z);
                                AttRef.Height = AttDef.Height;
                                AttRef.Rotation = AttDef.Rotation;
                                AttRef.Tag = AttDef.Tag;
                                AttRef.TextString = AttDef.TextString;
                            }
                        }
                    }
                    tran.Commit();
                }

              
                        Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

                   

                        PromptPointOptions ppo = new PromptPointOptions("选择插入点");
                        PromptPointResult pps;

                        pps = ed.GetPoint(ppo);
                        Point3d pt3d;
                        if (pps.Status == PromptStatus.OK)
                        {
                            pt3d = pps.Value;
                        }
                        Database database = HostApplicationServices.WorkingDatabase;

                        using (Transaction tr = database.TransactionManager.StartTransaction())
                        {

                            BlockTable bt1 = (BlockTable)tr.GetObject(database.BlockTableId, OpenMode.ForRead, false);
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt1[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
                           
                            BlockReference brf = new BlockReference(pt3d, objectid);

                            btr.AppendEntity(brf);

                            //tr.AddNewlyCreatedDBObject(brf, true);

                            #region  加入块属性                         
                         
                            //btr.AppendEntity(brf);
                            brf.AttributeCollection.AppendAttribute(AttRef);
                            tr.AddNewlyCreatedDBObject(AttRef,true);
                            tr.AddNewlyCreatedDBObject(brf,true);
                            #endregion

                            tr.Commit();
                        }
                      
                  
               
               
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

 /// <summary>
        /// 得到数据库对象
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private Database GetDatabaseFromFile(string fileName)
        {
            Database databaseFromFile = new Database(false, true);
            databaseFromFile.ReadDwgFile(fileName, System.IO.FileShare.None, false, "");
            databaseFromFile.CloseInput(true);
            return databaseFromFile;
        }

  /// <summary>
        /// 启动事件调用添加图块方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            this.Hide();
          
            this.InsertBlock("F:\\AutoCADTest\\AutoCAD\\TestWindows\\DWG\\_HL-_soar_4.dwg", "SYMB1_S_73");
            this.Dispose();
        }

发表于 2009-5-11 16:28:00 | 显示全部楼层
试试这个?
  1.         public bool AppendAttribRefToBlockRef(ObjectId blockdefid, ObjectId blockrefid, List<string> attribs)
  2.         {
  3.             try
  4.             {
  5.                 BlockTableRecord blkdef = (BlockTableRecord)m_Transaction.GetObject(blockdefid, OpenMode.ForRead);
  6.                 int i = 0;
  7.                 if (blkdef.HasAttributeDefinitions)
  8.                 {
  9.                     BlockReference blkref = (BlockReference)m_Transaction.GetObject(blockrefid, OpenMode.ForWrite);
  10.                     foreach (ObjectId id in blkdef)
  11.                     {
  12.                         DBObject ent = GetObject(id, OpenMode.ForRead);
  13.                         if (ent.GetType() == typeof(AttributeDefinition))
  14.                         {
  15.                             AttributeDefinition attdef = (AttributeDefinition)ent;
  16.                             AttributeReference attref = new AttributeReference();
  17.                             attref.SetAttributeFromBlock(attdef, blkref.BlockTransform);
  18.                             if (attdef.Justify == AttachmentPoint.BaseLeft)
  19.                                 attref.Position = attdef.Position.TransformBy(blkref.BlockTransform);
  20.                             else
  21.                                 attref.AlignmentPoint = attdef.AlignmentPoint.TransformBy(blkref.BlockTransform);
  22.                             if (attref.IsMTextAttribute)
  23.                                 attref.UpdateMTextAttribute();
  24.                             if (i < attribs.Count)
  25.                                 attref.TextString = attribs[i++];
  26.                             else
  27.                                 attref.TextString = attdef.TextString;
  28.                             blkref.AttributeCollection.AppendAttribute(attref);
  29.                             m_Transaction.AddNewlyCreatedDBObject(attref, true);
  30.                         }
  31.                     }
  32.                 }
  33.                 return true;
  34.             }
  35.             catch
  36.             { }
  37.             return false;
  38.         }
复制代码

发表于 2013-9-21 23:16:36 | 显示全部楼层
2006 下
db.ReadDwgFile(filename, FileShare.Read, true, null);
会在关闭文档后报  unhandled Access Violation Reading 的错误
改成  db.ReadDwgFile(filename, FileShare.Read, false, "");
就不报了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-25 14:51 , Processed in 0.172283 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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