Creating Link Templates using ActiveX API in VBA and Lisp

By Gopinath Taget

Here is the sample VBA and VLisp code that shows how to create and delete link templates in AutoCAD. Do not forget to set references to CAO type library before running this code in AutoCAD’s VBA IDE.

Sub fCreateLinkTemplate()

Dim pDB As CAO.DbConnect      Dim pLTs As CAO.LinkTemplates      Dim pLT As CAO.LabelTemplate      Dim pKeyDescs As CAO.KeyDescriptions      Dim psDataSrc As String      Dim psLinkTempName1 As String      Dim psLinkTempName2 As String      psDataSrc = "jet_dbsamples"      psLinkTempName1 = "LTCreatedByVBCAO"      psLinkTempName2 = "LTtobeDeleted"           'get the DBCONNECT object      Set pDB = ThisDrawing.Application.        GetInterfaceObject("CAO.DBConnect.16")      Set pLTs = pDB.GetLinkTemplates(ThisDrawing)           'prepare the keydescriptions      Set pKeyDescs = ThisDrawing.Application.        GetInterfaceObject("CAO.KeyDescriptions.16")      pKeyDescs.Add "TAG_NUMBER", kCaoTypeInteger      pKeyDescs.Add "Manufacturer", kCaoTypeText           'create two Link Templates      pLTs.Add psDataSrc, "Catalog1",         "Schema1", "Computer", psLinkTempName1, pKeyDescs      pLTs.Add psDataSrc, "Catalog2",         "Schema2", "Computer", psLinkTempName2, pKeyDescs      'created Link Templates      MsgBox "Created Link Templates : " & Chr(13) & "1) " &         pLTs.Item(psLinkTempName1).Name _      & Chr(13) & "2) " & pLTs.Item(psLinkTempName2).Name _           'connect to the datasource      pDB.Connect psDataSrc      'delete the second linktemplate.       'Comment the following statement if       'you want to see both the link templates      pLTs.Delete psLinkTempName2    End Sub
<p>And here’s the Visual Lisp code using the ActiveX API:</p>  <p>(defun c:CreateLT()    <br />&#160; (vl-load-com)    <br />&#160; (setq pDoc (vla-get-ActiveDocument (vlax-get-acad-object)))</p>  <p>&#160; ;get the DBConnect   <br />&#160; (setq pDBObj (vla-GetInterfaceObject    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (vlax-get-acad-object) &quot;CAO.dbConnect&quot;))    <br />&#160; (if (null pDBObj)    <br />&#160;&#160;&#160; (progn     <br />&#160;&#160;&#160;&#160;&#160; (alert &quot;Cannot create CAO Automation server.&quot;)     <br />&#160;&#160;&#160;&#160;&#160; (exit))) </p>  <p>&#160; ;get the linktemplates   <br />&#160; (setq pLTs(    <br />&#160;&#160;&#160;&#160;&#160;&#160; vlax-invoke-method pDBObj &quot;GetLinkTemplates&quot; pDoc))</p>  <p>&#160; ;prepare the keydescriptions   <br />&#160; (setq pKeyDescs (    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; vla-GetInterfaceObject (vlax-get-acad-object)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &quot;CAO.KeyDescriptions&quot;))    <br />&#160; (vlax-invoke-method pKeyDescs    <br />&#160; &quot;ADD&quot; &quot;TAG_NUMBER&quot; 3 nil nil)    <br />&#160; (vlax-invoke-method pKeyDescs    <br />&#160; &quot;ADD&quot; &quot;Manufacturer&quot; 1 nil nil)</p>  <p>&#160; ;create two link templates   <br />&#160; (vlax-invoke-method pLTs &quot;ADD&quot; &quot;jet_dbsamples&quot;    <br />&#160; nil nil &quot;COMPUTER&quot; &quot;LTCreatedByVLispCAO&quot; pKeyDescs)    <br />&#160; (vlax-invoke-method pLTs &quot;ADD&quot; &quot;jet_dbsamples&quot;    <br />&#160; nil nil &quot;COMPUTER&quot; &quot;LTtobeDeleted&quot; pKeyDescs)</p>  <p>&#160; ;sample code to show how to delete the link template   <br />&#160; (vlax-invoke-method pL

Ts "delete" "LTtobeDeleted")
)


Comments

4 responses to “Creating Link Templates using ActiveX API in VBA and Lisp”

  1. Why not provide examples in AutoCAD. NET? VBA in 64bits with connection in Access, sql, etc.?
    I’m migration my old Bubble CAO VBA appliation to AutoCAD .NET and not has reason for today use CAO Library in VBA. And today I have some problems in AutoCAD 2013 with conversion and OldObjectId ObjectId.
    I would be very happy if in this blog found detailed examples showing the use of CAO Library with AutoCAD. NET.

  2. Hi Hanauer,
    Thanks for the feedback. I will look into creating a blog post for using CAO in .NET in the near future.
    Cheers
    Gopinath

  3. Sorry for my bad english.
    Its possible create an entity link with DBDictionaty ObjectId?
    I’m tried this:

    using (Transaction tr = _database.TransactionManager.StartTransaction())
    {
    DBDictionary dictNODKey = null;
    try
    {
    // open the named object dictionary
    dictNODKey = (DBDictionary)tr.GetObject(_linkDictionaryNodObjectId, OpenMode.ForWrite);
    }
    catch
    {
    this.GetLinkDictionaryNodObjectId();
    dictNODKey = (DBDictionary)tr.GetObject(_linkDictionaryNodObjectId, OpenMode.ForWrite);
    }
    DBDictionary dictNumber = null;
    try
    {
    dictNumber = (DBDictionary)tr.GetObject(dictNODKey.GetAt(Convert.ToString(number)), OpenMode.ForWrite);
    }
    catch
    {
    dictNODKey.UpgradeOpen();
    dictNumber = new DBDictionary();
    dictNODKey.SetAt(Convert.ToString(number), dictNumber);
    tr.AddNewlyCreatedDBObject(dictNumber, true);
    try
    {
    ObjectId dictNumberId = dictNumber.ObjectId;
    CAO.Link link = _linkTemplate.CreateLink(dictNumberId.OldId, keyValues);
    // CAO.Link link = _linkTemplate.CreateLink(id1.OldId, keyValues);
    ObjectId linkObjectId = new ObjectId(link.ObjectID);
    _lom.Add(number, linkObjectId);
    }
    catch (System.Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    }
    // save the new objects to the database
    tr.Commit();
    }

    and not works. Give this exception: “System.Runtime.InteropServices.COMException (0xC022001F): Failed to create link at CAO.LinkTemplateClass.CreateLink(Int32 ObjectID, KeyValues pKeyValues)”.
    The same above routine works when the link is create to common entities, like lines, circles, blockreferences, etc.
    Why is not possible do create an entity link with dictionaries objectIds?
    Thanks.
    Hanauer.

  4. xiaodong Avatar
    xiaodong

    Hi Hanauer,
    I have posted a similar demo in .NET. Hope it helps:
    http://adndevblog.typepad.com/autocad/2013/04/obtaining-the-top-level-object-in-cao-using-net.html

Leave a Reply to HanauerCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading