明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1177|回复: 1

[基础] 新手求帮忙块参照,翻了好几天的论坛也没找到办法(因实力有限)

[复制链接]
发表于 2015-4-25 17:08 | 显示全部楼层 |阅读模式
本帖最后由 糕笔仔 于 2015-4-25 17:22 编辑

新手求帮忙,解决两个问题 谢谢!
1.AttributeDefinition  ad1 ad2 这两个属性的位置(添加第二个参照的时候,ad1 ad2 不能跟随块参照改变位置)
2. 如何另外添加 AttributeDefinition,求一个方法(希望调用另一个方法,将另一个AttributeDefinition ad3  添加到指定参照)。
代码如下。
     [CommandMethod("AddAttributeBlock")]
        public void AddAttributeBlock()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            ObjectId id;
            BlockTableRecord btr = new BlockTableRecord();

            Point3d pt = Pick1("\n指定位置");
            Line li = new Line(pt, new Point3d(0, 0, 0));
            String cbf = GetString("\n承包方");
            String dkbh = GetString("地块编号");

            AttributeDefinition ad1 = AttributeDefinition("cbr", "承包方", cbf, new Point3d(pt.X + 3, pt.Y + 3, 0));
            AttributeDefinition ad2 = AttributeDefinition("dkbh", "地块编号", dkbh, new Point3d(pt.X - 3, pt.Y - 3, 0));

            btr.Name = "LineBlock";
            btr.AppendEntity(li);
            btr.AppendEntity(ad1);
            btr.AppendEntity(ad2);

            id = AddToBlockTable(btr);
            ObjectId id1 = ToModelSpace(id, pt, db);
            adKzsx(id1);
        }

        //创建属性
        public static AttributeDefinition AttributeDefinition(string label, string prompt, string value, Point3d pt)
        {
            AttributeDefinition ad = new AttributeDefinition();
            ad.Constant = false;
            ad.Tag = label;
            ad.Prompt = prompt;
            ad.TextString = value;
            ad.Position = pt;
            return ad;
        }

        //将指定的块定义变成块参照添加到指定的模型空间
        public static ObjectId ToModelSpace(ObjectId blkid, Point3d pt, Database db)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database dbd = doc.Database;
            ObjectId blkrfid = new ObjectId();
            using (Transaction trans = dbd.TransactionManager.StartTransaction())
            {
                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelspace = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                BlockTableRecord block = trans.GetObject(blkid, OpenMode.ForWrite) as BlockTableRecord;
                BlockReference br = new BlockReference(pt, blkid);//通过块定义 添加块参照


                blkrfid = modelspace.AppendEntity(br);//把块参照添加到块表记录
                trans.AddNewlyCreatedDBObject(br, true);

                foreach (ObjectId id in block)
                {
                    if (id.ObjectClass.Equals(RXClass.GetClass(typeof(AttributeDefinition))))
                    {
                        AttributeDefinition ad = trans.GetObject(id, OpenMode.ForRead) as AttributeDefinition;
                        AttributeReference ar = new AttributeReference(ad.Position, ad.TextString, ad.Tag, new ObjectId());
                        br.AttributeCollection.AppendAttribute(ar);
                    }
                }
                trans.Commit();
            }
            return blkrfid;
        }
        //将块表记录加入到块表中
        public static ObjectId AddToBlockTable(BlockTableRecord Record)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            ObjectId id;
            using (Transaction transaction = db.TransactionManager.StartTransaction())
            {
                BlockTable table = transaction.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                if (table.Has("LineBlock"))
                {
                    id = table["LineBlock"];
                }
                else
                {
                    id = table.Add(Record);
                    transaction.AddNewlyCreatedDBObject(Record, true);
                    transaction.Commit();
                }
            }
            return id;
        }
        public void adKzsx(ObjectId id)
        {
            Document doc =

         Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {

                DocumentLock doclock = doc.LockDocument();

                Entity acent = trans.GetObject(id, OpenMode.ForWrite) as Entity;


                acent.ColorIndex = 1;
                RegAppTable appTbl = trans.GetObject(db.RegAppTableId,

          OpenMode.ForWrite) as RegAppTable;

                if (!appTbl.Has("Qmap"))
                {
                    RegAppTableRecord appTblRcd = new RegAppTableRecord();
                    appTblRcd.Name = "Qmap";
                    appTbl.Add(appTblRcd);
                    trans.AddNewlyCreatedDBObject(appTblRcd, true);
                }
                ResultBuffer resBuf = new ResultBuffer();
                resBuf.Add(new TypedValue(1001, "Qmap"));//注册程序名称
                resBuf.Add(new TypedValue(1000, "2015"));

                acent.XData = resBuf;

                trans.Commit();


                doclock.Dispose();


            }
        }

发表于 2015-4-25 19:37 | 显示全部楼层
attref.SetAttributeFromBlock(attdef, blkref.BlockTransform);
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-6 12:42 , Processed in 0.286644 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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