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
And here’s the Visual Lisp code using the ActiveX API:
(defun c:CreateLT()
(vl-load-com)
(setq pDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
;get the DBConnect
(setq pDBObj (vla-GetInterfaceObject
(vlax-get-acad-object) "CAO.dbConnect"))
(if (null pDBObj)
(progn
(alert "Cannot create CAO Automation server.")
(exit)))
;get the linktemplates
(setq pLTs(
vlax-invoke-method pDBObj "GetLinkTemplates" pDoc))
;prepare the keydescriptions
(setq pKeyDescs (
vla-GetInterfaceObject (vlax-get-acad-object)
"CAO.KeyDescriptions"))
(vlax-invoke-method pKeyDescs
"ADD" "TAG_NUMBER" 3 nil nil)
(vlax-invoke-method pKeyDescs
"ADD" "Manufacturer" 1 nil nil)
;create two link templates
(vlax-invoke-method pLTs "ADD" "jet_dbsamples"
nil nil "COMPUTER" "LTCreatedByVLispCAO" pKeyDescs)
(vlax-invoke-method pLTs "ADD" "jet_dbsamples"
nil nil "COMPUTER" "LTtobeDeleted" pKeyDescs)
;sample code to show how to delete the link template
(vlax-invoke-method pLTs "delete" "LTtobeDeleted")
)