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 />  (vl-load-com) <br />  (setq pDoc (vla-get-ActiveDocument (vlax-get-acad-object)))</p> <p>  ;get the DBConnect <br />  (setq pDBObj (vla-GetInterfaceObject <br />           (vlax-get-acad-object) "CAO.dbConnect")) <br />  (if (null pDBObj) <br />    (progn <br />      (alert "Cannot create CAO Automation server.") <br />      (exit))) </p> <p>  ;get the linktemplates <br />  (setq pLTs( <br />       vlax-invoke-method pDBObj "GetLinkTemplates" pDoc))</p> <p>  ;prepare the keydescriptions <br />  (setq pKeyDescs ( <br />         vla-GetInterfaceObject (vlax-get-acad-object) <br />          "CAO.KeyDescriptions")) <br />  (vlax-invoke-method pKeyDescs <br />  "ADD" "TAG_NUMBER" 3 nil nil) <br />  (vlax-invoke-method pKeyDescs <br />  "ADD" "Manufacturer" 1 nil nil)</p> <p>  ;create two link templates <br />  (vlax-invoke-method pLTs "ADD" "jet_dbsamples" <br />  nil nil "COMPUTER" "LTCreatedByVLispCAO" pKeyDescs) <br />  (vlax-invoke-method pLTs "ADD" "jet_dbsamples" <br />  nil nil "COMPUTER" "LTtobeDeleted" pKeyDescs)</p> <p>  ;sample code to show how to delete the link template <br />  (vlax-invoke-method pL
Ts "delete" "LTtobeDeleted")
)

Leave a Reply to HanauerCancel reply