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)插入新链接,
         ///达到了预期目的,更新了链接
         ///真奇葩!!!
     }
  • 相关阅读:
    scrapy怎么设置带有密码的代理ip base64.encodestring不能用 python3.5,base64库里面的encodestring()被换成了什么?
    ace模板dataTables_length控制是否显示分页
    Django AUTHENTICATION_BACKENDS
    Django自定义User模型和登录验证
    一个简单的django user.is_authenticated问题
    PHP函数(四)-变量函数
    PHP函数(三)-递归函数
    Python多进程
    Python多线程-生产者消费者模型
    Python多线程-队列
  • 原文地址:https://www.cnblogs.com/myzw/p/12337178.html
Copyright © 2011-2022 走看看