How can I create hyperlink for View (View Manger - View of This Drawings) ?

How can I create hyperlink for View (View Manger - View of This Drawings) ?

tdoYBRN7
Explorer Explorer
305 Views
3 Replies
Message 1 of 4

How can I create hyperlink for View (View Manger - View of This Drawings) ?

tdoYBRN7
Explorer
Explorer

 

tdoYBRN7_1-1731497975256.png

tdoYBRN7_3-1731498125237.png

 

 

[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 = "A1";

      hyperLink.Name = "A1";

      hyperLink.SubLocation ="";

 

      //Add the hyperlink to the collection

      linkCollection.Add(hyperLink);

      trans.Commit();

    }

  }

  catch (System.Exception ex)

  {

    ed.WriteMessage(ex.Message);

  }

}

0 Likes
306 Views
3 Replies
Replies (3)
Message 2 of 4

ActivistInvestor
Mentor
Mentor

To create a hyperlink to a view in model space and add it to an entity in model space, just set the SubLocation property to the view's name.

 

To create a hyperlink to a view in a layout from an entity in model space, set the SubLocation to "<ViewName>,<LayoutName>"

 

e.g., to hyperlink to "VIEW1" in "Layout1", use "VIEW1,Layout1" as the SubLocation.

Message 3 of 4

Gepaha
Collaborator
Collaborator

Thanks for the clarification, it's correct. For views only sublocation should be filled and name should not be filled.
It may be intuitive to fill in the name but it will cause an error.

0 Likes
Message 4 of 4

Gepaha
Collaborator
Collaborator
        [CommandMethod("CreateHyperLinkToView")]
        static public void CreateHyperLinkToView()
        {
            // - for web page: the complete URL to an Internet file
            //   hyperLink.Description = "AutoCAD DevBlog" ;
            //   hyperLink.Name = "http://adndevblog.typepad.com/autocad/";
            //   hyperLink.Sublocation = "";

            // - for e-mail:
            //   hyperLink.Name = "mailto:e-mail@xxxx.com = ? subject =" + "Somebody"
            //   hyperLink.Sublocation = "";

            // - for complete path local file
            //   hyperLink.Name = "C:\Users\xxxx\Documents\MyDrawing.dwg"
            //   hyperLink.Sublocation = "";

            // - for relative path local file
            //   hyperLink.Name = ".\MyDrawing.dwg"
            //   hyperLink.Sublocation = "";

            // for complete path local file target layout name           
            // hyperLink.Name = "C:\Users\xxxx\Documents\MyDrawing.dwg"
            // hyperLink.Sublocation = "," + layoutName

            // for relative path local file target layout name:
            // hyperLink.Name = ".\MyDrawing.dwg"
            // hyperLink.Sublocation = "," + layoutName"       

            // - for view in model space:           
            // hyperLink.Name = "";
            // hyperLink.Sublocation = viewName;

            // - for view in paper space:        
            // hyperLink.Name = "";
            // hyperLink.Sublocation = viewName + "," + layoutName;

            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
            PromptResult pr = ed.GetString("\nEnter view name:" );
            if (pr.Status != PromptStatus.OK)
                return;

            PromptEntityResult selectedEntity = ed.GetEntity("\nSelect an Entity: ");
            if (selectedEntity.Status != PromptStatus.OK)
                return;

            ObjectId objectId = selectedEntity.ObjectId;

            try
            {
                using (Transaction trans =  db.TransactionManager.StartTransaction())
                {
                    ViewTable vt = trans.GetObject(db.ViewTableId, OpenMode.ForRead) as ViewTable;
                    foreach (ObjectId id in vt)
                    {
                        ViewTableRecord vtr = trans.GetObject(id, OpenMode.ForRead) as ViewTableRecord;
                        if (vtr.Name.Equals(pr.StringResult))
                        {                            
                            HyperLink hyperLink = new HyperLink();
                            hyperLink.Description = vtr.Name;  
                            // for view leave name blank
                            hyperLink.Name = "";
                            hyperLink.SubLocation = vtr.Name;

                            if (vtr.IsPaperspaceView)
                            {
                                Layout layout = trans.GetObject(vtr.Layout, OpenMode.ForRead) as Layout;
                                hyperLink.Description = hyperLink.Description + "," + layout.LayoutName;                                
                                hyperLink.SubLocation = hyperLink.SubLocation + "," + layout.LayoutName;
                            }
                           
                            Entity ent = trans.GetObject(objectId, OpenMode.ForWrite) as Entity;                            
                            HyperLinkCollection linkCollection = ent.Hyperlinks;                           
                            linkCollection.Add(hyperLink);
                        }
                    }
                    trans.Commit();
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
            }
        }
0 Likes