nocent 发表于 2017-3-22 15:53:31

更新多行文字C#

Update:仅通过向该方法传入1个参数来解决,大家好,这里的初级开发人员和autocad API用户没有运气谷歌一下这个。希望有人能给我指出正确的方向,目标是更新多行文字内容。ForRead”可以很好地阅读多行文字内容,然而,当我试图”。forWrite“我得到以下异常:Autodesk,AcdbMgd.dll中的“AutoCAD . runtime . exception ”遍历代码时,它在第行失败:-但仅在写入模式下失败。传入的句柄im指回一个多行文字对象,并且在转换过程中似乎获得了正确的对象ID。但是我还没有排除这个问题的原因,谁有更新多行文字时可以参考的代码,或者可以给我指出正确的方向?public static void editMtextContents(string handle, string newValue)
{
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Handle hn = new Handle(ConvertHandleToInt(handle));
        Transaction tr = db.TransactionManager.StartTransaction();
        using (tr)
        {
                ObjectId id = db.GetObjectId(false, hn, 0);
                var obj = tr.GetObject(id, OpenMode.ForWrite, false);
                var mtext = obj as MText;
                mtext.Contents = newValue;
                tr.Commit();
        }
}谢谢!-Bruce更新:使用Autocad 2015。
绘图/图层未锁定。
**** Hidden Message *****

nocent 发表于 2017-3-22 18:42:00

还有错误信息吗?
可能在锁定的图层上?

MexicanCustard 发表于 2017-3-23 07:32:32

图层没有锁定,我在几个图纸上尝试过,试图消除dwg本身的问题。
这与使用autocad 2015有关。

nocent 发表于 2017-3-23 08:19:04

你能把你的工作图纸贴出来吗?

kdub 发表于 2017-3-23 09:12:15

我遇到了这个问题(解决方法?)!
传入两个参数时,该方法不起作用。 修复方法是修复方法签名以接受一个参数,在我的例子中是一个带有句柄和新值的对象。
唷!

nocent 发表于 2017-3-24 00:17:57

您好,Ken,
我遇到问题的方法(注意,我还必须添加“文档锁”来解决问题制作):
public static void EditMtextContents(UpdatedTag tag)
{
        Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        //create handle from long int
        Handle hn = new Handle(ConvertHandleToInt(tag.Handle));
        //attempt to get an ObjectID for the handle
        ObjectId id = db.GetObjectId(false, hn, 0);
        //open the object to edit it
        using (DocumentLock doclock = doc.LockDocument())
        {
                Transaction tr = doc.TransactionManager.StartTransaction();
                using (tr)
                {
                        DBObject obj = tr.GetObject(id, OpenMode.ForWrite);
                        //testing editing mtext contents
                        MText mtext = obj as MText;
                        mtext.Contents = tag.NewValue;
                        tr.Commit();
                }
        }
}

更新的标记只是一个简单的小类:
public class UpdatedTag
{
        public string Handle { get; set; }
        public string NewValue { get; set; }
        public UpdatedTag(string handle, string newValue)
        {
                Handle = handle;
                NewValue = newValue;
        }
}

我提取所有多行文字,并将其存储在列表/数据集中。用acad术语来说,部分信息是它的“句柄”。
我使用以下方法将字符串句柄转换为autocad对象句柄(我相信这是从Kean那里得到的,不管怎样,他都是一个巨大的资源):
public class UpdatedTag
{
        public string Handle { get; set; }
        public string NewValue { get; set; }
        public UpdatedTag(string handle, string newValue)
        {
                Handle = handle;
                NewValue = newValue;
        }
}

希望这比混乱更有帮助-我对acad和一般开发非常陌生

MexicanCustard 发表于 2017-3-24 15:57:54

您是否在会话之间存储UpdatedTag对象?如果没有,那么为什么不直接使用ObjectId呢?

MexicanCustard 发表于 2017-3-27 07:57:50

也可以代替Transaction tr=doc.TransactionManager.StartTransaction();。
使用(tr)您可以使用(Transaction tr=doc.TransactionManager.StartTransaction())。

MexicanCustard 发表于 2017-3-27 07:59:39

如果您在C# 6中   使用(var doclock = doc,LockDocument())。
{。
使用(var tr = doc,transaction manager . start transaction();)。
{。
var mtext = tr,GetObject(id,OpenMode。ForWrite)作为多行文字;。
if(mtext!= null)。
{。
多行文字,内容=标签,NewValue。
}。

trcommit();。
}。
}如果您已经开始使用C#7,请使用(var doclock = doc。LockDocument())。
{。
使用(var tr = doc,transaction manager . start transaction())。
{。
如果(tr,GetObject(id,OpenMode。ForWrite)是MText mtext)。
多行文字,内容=标签,NewValue。
trcommit();。
}。
}。

nocent 发表于 2017-3-27 08:07:36


这是一个单独的会话。感谢分享改进的代码,可读性更好了!
页: [1]
查看完整版本: 更新多行文字C#