ROUTINE FOR HANDLING LINK TEMPLATE TO OBJECT DATA

ROUTINE FOR HANDLING LINK TEMPLATE TO OBJECT DATA

BriamR
Advocate Advocate
291 Views
0 Replies
Message 1 of 1

ROUTINE FOR HANDLING LINK TEMPLATE TO OBJECT DATA

BriamR
Advocate
Advocate

I am building a routine to obtain the values of the LINK TEMPLATE fields of the selected object or drawing and then copy the fields "INTERNO_DEMARCACION" and "CLASE_DEMARCACION" in the OBJECT DATA with name "SDM_DEM_DEMARCACION" in the fields of the same name of the LINK TEMPLATE,


for now I have managed to get the field "INTERNO_DEMARCACION" which is the key value of the LINK TEMPLATE but when trying to get the field "CLASS_DEMARCATION" I can't find the way to do it.

 

I share the code:

(defun c:LTOD (/ acad doc ss obj linkTemplates dbConnect gCmnFiles gCaoTlb gADO-DLLpath linkTemplate)
  (vl-load-com)

  ;; Get AutoCAD object and active document
  (setq acad (vlax-get-acad-object))
  (setq doc (vla-get-ActiveDocument acad))

  ;; Route of required libraries
  (setq gCmnFiles (getenv "COMMONPROGRAMFILES") 
        gCaoTlb (strcat gCmnFiles "\\AUTODESK SHARED\\" "cao20enu.tlb")
        gADO-DLLpath (strcat gCmnFiles "\\system\\ado\\msado15.dll"))

  ;; Verify the existence of the ADO library
  (if (null (findfile gADO-DLLpath))
    (progn
      (alert (strcat "Could not load ADO type library from: " gADO-DLLpath)) 
      (exit)
    )
  )

  ;; Import ADO type library
  (if (null adok-adStateOpen)  ;; Verificar que la biblioteca de tipos ADO no se haya cargado aún
    (vlax-import-type-library :tlb-filename gADO-DLLpath
                              :methods-prefix "adom-"
                              :properties-prefix "adop-"
                              :constants-prefix "adok-"))

  ;; Import CAO type library
  (if (null caom-GetLinkTemplates)  ;; Verificar que la biblioteca de tipos CAO no se haya cargado aún
    (vlax-import-type-library :tlb-filename gCaoTlb
                              :methods-prefix "caom-"
                              :properties-prefix "caop-"
                              :constants-prefix "caok-"))
							  
  ;; Obtain the object and its ID
  (setq ent1 (car (entsel "\nSelect entity ")))
  (setq Obj (vlax-ename->vla-object ent1))
  (setq ob_ID (vla-get-objectid Obj))

  ;; Create DbConnect object
  (setq dbConnect (vlax-create-object "CAO.DbConnect.20"))

  ;; Obtain link templates
  (setq linkTemplates (vlax-invoke-method dbConnect "GetLinkTemplates"))

  (vlax-for LT linkTemplates
    (vlax-for thisLink (vlax-invoke-method dbConnect "GetLinks" LT nil nil nil)
      (setq l_Obj_ID (vlax-get-property thisLink "ObjectID"))
      (if (= l_Obj_ID ob_ID)
        (setq lA_Obj_IDS (cons thisLink lA_Obj_IDS))
      )
    )
  )

  (setq linkTemplate (car lA_Obj_IDS))
  (setq linkTemplatename (vlax-get-property (vlax-get-property linkTemplate "LinkTemplate") "Name"))

  ;; Get value of INTERNO_DEMARCACION from template link
  (setq thisKeyValue (vlax-invoke-method (vlax-get-property linkTemplate "KeyValues") "Item" 0))
  (setq strKeyName (vlax-get-property thisKeyValue "FieldName"))
  (setq thisValue (vlax-variant-value (vlax-get-property thisKeyValue "Value")))

  ;; Connect to data source
  (defun cmdConnectDataSource_Click (linkTemplate / strDataSourceLoc strDataSource currConnectionString)
    (setq strDataSourceLoc (strcat (vlax-get-property dbConnect "DataSourceLocation") "\\")
          strDataSource (vlax-get-property (vlax-get-property linkTemplate "LinkTemplate") "DataSource"))
    (if adoConnect
      (if (= adok-adStateOpen (vlax-get-property ADOConnect "State"))
          (vlax-invoke-method ADOConnect "Close")))
    (setq adoconnect nil)
    (setq ADOConnect (vlax-create-object "ADODB.Connection"))
    (setq currConnectionString (strcat "File Name=" strDataSourceLoc strDataSource ".udl;User ID=;Password=;"))
    (vlax-put-property ADOConnect "ConnectionString" currConnectionString)
    (vlax-invoke-method ADOConnect "Open" currConnectionString "" "" -1)
  )
  
  (cmdConnectDataSource_Click linkTemplate)

  ;; Prepare the ADO command
  (setq cmd (vlax-create-object "ADODB.Command"))
  (vlax-put-property cmd "ActiveConnection" ADOConnect)
  (vlax-put-property cmd "CommandTimeout" 30)
  (vlax-put-property cmd "CommandText" (strcat "SELECT * FROM " (vlax-get-property (vlax-get-property linkTemplate "LinkTemplate") "Table")))
  
  ;; Open Recordset
  (setq rs (vlax-create-object "ADODB.Recordset"))
  (vlax-invoke-method rs "Open" cmd nil adok-adOpenDynamic adok-adLockBatchOptimistic adok-adCmdUnknown)

  ;; Find the row corresponding to INTERNO_DEMARCACION
  (vlax-invoke-method rs "MoveFirst")
  (setq found nil)
  (while (and (not (vlax-get-property rs "EOF")) (not found))
    (setq fields (vlax-get-property rs "Fields"))
    (setq fieldInterno (vlax-invoke-method fields "Item" 0))
    (setq internoValue (vlax-variant-value (vlax-get-property fieldInterno "Value")))
    (if (= thisValue internoValue)
      (setq found t)
      (vlax-invoke-method rs "MoveNext"))
  )
  
  (if found
    (progn
      ;; Get the value of CLASE_DEMARCACION
      (setq fieldClase (vlax-invoke-method fields "Item" 1))
      (setq claseValue (vlax-variant-value (vlax-get-property fieldClase "Value")))
    )
    (setq claseValue "No encontrado")
  )

  ;; Copy values to Object Data
  (setq tn "SDM_DEM_DEMARCACION")                
  (ade_odaddrecord tn)
  (ade_odsetfield ent1 tn "INTERNO_DEMARCACION" (vl-princ-to-string thisValue))
  (ade_odsetfield ent1 tn "CLASE_DEMARCACION" (vl-princ-to-string claseValue))

  ;; Show results
  (princ (strcat "\n" (itoa (vlax-get-property rs "RecordCount")) " Rows\n"))
  (princ (strcat "INTERNO_DEMARCACION: " (vl-princ-to-string thisValue) "\n"))
  (princ (strcat "CLASE_DEMARCACION: " (vl-princ-to-string claseValue) "\n"))

  (princ)
)


I have an alternative to obtain CLASE_DEMARCACION

;; Get value of CLASE_DEMARCACION from linkTemplate
(setq fields (vlax-get-property rs "Fields"))
(setq thisField2 (vlax-get-property fields "Item" 1)) ; Obtener el segundo campo (índice 1)
(setq thisFieldName2 (vlax-get-property thisField2 "Name"))
(setq thisValue2 (vlax-variant-value (vlax-get-property thisField2 "Value")))

 

but this one gives me any value from the recordset and not from the linktemplate

I appreciate your help with the issue

 

0 Likes
292 Views
0 Replies
Replies (0)