AutoCAD 实体添加超级链接
How to create a hyperlink on an entity with AutoCAD .NET API?
Entities in AutoCAD can be associated with hyperlinks. Users can visit the referenced locations from the hyperlink on an entity. There are three types of hyperlink locations on AutoCAD entities. They are URLs, files, and DWG file targets (model, layout1, and so on). This article shows how to use .NET API to create a URL hyperlink on an entity. The code is in C#. [CommandMethod("createHLink")] static public void CmdCreateHyperLink() { Editor ed = Application.DocumentManager. MdiActiveDocument.Editor; Database db = Application.DocumentManager. MdiActiveDocument.Database; PromptEntityResult selectedEntity = ed.GetEntity("Please Select an Entity: "); ObjectId objectId = selectedEntity.ObjectId; try { using (Transaction trans = db.TransactionManager.StartTransaction()) { //Get the entity Entity ent = trans.GetObject(objectId, OpenMode.ForWrite) as Entity; //Get the hyperlink collection from the entity HyperLinkCollection linkCollection = ent.Hyperlinks; //Create a new hyperlink HyperLink hyperLink = new HyperLink(); hyperLink.Description = "ADN DevBlog"; hyperLink.Name = "ADN DevBlog"; hyperLink.SubLocation = "http://adndevblog.typepad.com/autocad/"; //Add the hyperlink to the collection linkCollection.Add(hyperLink); trans.Commit(); } } catch (System.Exception ex) { ed.WriteMessage(ex.Message); } }
遇到的问题:
如果重复执行相同操作,
但后续操作时想替换原有的超级链接,
如果直接删除,
会出现崩溃的问题,
回帖中的VB代码看不懂,
没法转换成C#。
后来我自己试着采用如下的方法,
居然达到了自己的预期:
if (ent.Hyperlinks.Count > 0) { //ent.Hyperlinks[0].Name = csvName; HyperLink hl = new HyperLink(); hl.Name = csvName; hl.Description = ent.Hyperlinks[0].Description; ent.Hyperlinks.RemoveAt(0); ent.Hyperlinks.Insert(0,hl); ///2020年2月20日 ///直接清除clear Hyperlinks会造成崩溃, ///使用add添加新链接后,仍显示原有链接, ///即使RemoveAt(0)也不行, ///后来尝试使用Insert(0,hl)插入新链接, ///达到了预期目的,更新了链接 ///真奇葩!!! }
分类:
AutoCAD API相关
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2019-02-20 样例文件C3DCustomUI无法编译、加载
2018-02-20 Civil 3D 2017本地化中VBA程序移植到2018版中