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

 

上一篇:PHP 常用函数


下一篇:决策树算法的理解及实现