欢迎来到我的博客
Civil 3D开发与应用,欢迎加入QQ群:484124761
AutoCAD开发,欢迎加入QQ群:193522571

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)插入新链接,
     ///达到了预期目的,更新了链接
     ///真奇葩!!!
 }
复制代码

 

posted @   david96007  阅读(466)  评论(0编辑  收藏  举报
编辑推荐:
· 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版中
点击右上角即可分享
微信分享提示