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);
}
}

Leave a Reply