乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 50|回复: 1

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

[复制链接]

2

主题

12

帖子

1

银币

初来乍到

Rank: 1

铜币
20
发表于 2015-4-25 17:08:00 | 显示全部楼层 |阅读模式
新手求帮忙,解决两个问题 谢谢!
1.AttributeDefinition  ad1 ad2 这两个属性的位置(添加第二个参照的时候,ad1 ad2 不能跟随块参照改变位置)
2. 如何另外添加 AttributeDefinition,求一个方法(希望调用另一个方法,将另一个AttributeDefinition ad3  添加到指定参照)。
代码如下。
     [CommandMethod("AddAttributeBlock")]
        public void AddAttributeBlock()
        {
            Document doc = Autodesk..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();
            }
        }
回复

使用道具 举报

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2015-4-25 19:37:00 | 显示全部楼层
attref.SetAttributeFromBlock(attdef, blkref.BlockTransform);
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-6-28 18:37 , Processed in 1.384604 second(s), 68 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表