zoukankan      html  css  js  c++  java
  • 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)插入新链接,
         ///达到了预期目的,更新了链接
         ///真奇葩!!!
     }
  • 相关阅读:
    JSON跨域请求
    2013.9.26 心得体会
    MemCached用法
    使用SQL联合查询来构建临时vo对象的应用
    ubuntu 16.04 安装php 5 6等版本
    mac php版本切换
    windows 查看端口占用
    nginx 反向代理到目录
    linux挂在samba服务器到本地(用于备份文件到nas或者windows的文件服务器)
    ubuntu 加载新硬盘或分区
  • 原文地址:https://www.cnblogs.com/myzw/p/12337178.html
Copyright © 2011-2022 走看看