Author Topic: Datalink update via lisp  (Read 1483 times)

0 Members and 1 Guest are viewing this topic.

diego65

  • Newt
  • Posts: 51
  • Learning LISP is a cool thing
Datalink update via lisp
« on: February 20, 2013, 10:30:11 AM »
What I am missing in this code, please any help will be appreciate it.

What I am trying to accomplish, is to update info from a workbook or spreadsheet that is linked to our drawing template by selecting the informatin from that workbook using the mouse insted of linking the info manually to a different cell on the spreadsheet.
right now we create a new drawing from a template and we manually linked the info to a different cell on a spreasheet, that is time cosuming.
E.g. drawing number, Date, Project Number etc, these atributtes changes as drawings are created, then the user must go to the Datalink within Autocad and poinitng to the right cell within the workbook.

does this makes sense?

File Location:       C:\Work Project\Data Table Link\
File Name:      Basemap Link.xlsx
Sheet (Tab) Name:   Revison (in where the information is to be linked onto the active drawing)



;; local defun
(defun RefSelection (/ *error* addr c2 c2 Excelapp Sel Sht r1 r2 Rng Vl Wbk)
  (vl-load-com)
  (defun *error*  (msg)
    (if
      (vl-position
   msg
   '("console break"
     "Function cancelled"
     "quit / exit abort"
     )
   )
       (princ "Error!")
       (princ msg)
       )
            (vl-catch-all-apply
   'vlax-invoke-method
   (list Wbk "Close")
      )

   (vl-catch-all-apply
     'vlax-invoke-method
     (list ExcelApp "Quit")
   )

  (mapcar
    (function (lambda (x)(vl-catch-all-apply(function (lambda()
      (if (not (vlax-object-released-p x))
        (progn
        (vlax-release-object x)
        (setq x nil))
      )
         )
    )
            )
      )
         )
    (list Sel Sht Wbk ExcelApp)
  ) 
  (gc)
  (gc)
    (princ)
    )
 

(setq ExcelApp (vl-catch-all-apply
          (function (lambda ()(vlax-get-or-create-object "Excel.Application")))))

(if (vl-catch-all-error-p
      (setq Wbk
        (vl-catch-all-apply
          (function (lambda ()
            (vlax-get-property ExcelApp "ActiveWorkBook"))))))
  (progn
    (alert "Excel WorkBook Must Be Open Before!")
    (exit)
    (*error* nil)
    (princ)
    )
  )
(setq Sht
       (vl-catch-all-apply
    (function (lambda ()
           (vlax-get-property ExcelApp "ActiveSheet")))))

(vlax-put-property ExcelApp 'visible :vlax-true)

(vlax-put-property ExcelApp 'ScreenUpdating :vlax-true)

(vlax-put-property ExcelApp 'DisplayAlerts :vlax-false)

(if (not (vl-catch-all-error-p
      (setq Rng
        (vl-catch-all-apply
          (function (lambda ()
            (vlax-variant-value
              (vlax-invoke-method
                (vlax-get-property Wbk 'Application)
                'Inputbox
                "Select a Range: "
                "Range Selection Example"
                nil
                nil
                nil
                nil
                nil
                )))))))
  (progn
    (vlax-put-property ExcelApp 'DisplayAlerts :vlax-true)

    (setq r1 (vlax-get-property Rng 'row))
    (setq c1 (vlax-get-property Rng 'column))
    (setq r2 (vlax-get-property (vlax-get-property Rng 'rows) 'count))
    (setq c2 (vlax-get-property (vlax-get-property Rng 'columns) 'count))

    (setq addr (strcat (chr (+ 64 c1))
             (itoa r1)
             ":"
             (chr (+ (ascii (chr (+ 64 c1))) (1- c2)))
             (itoa (+ r1 (1- r2)))))

    (setq Rng (vlax-get-property sht 'Range addr))

    (vlax-invoke Rng 'Select)
    )
  )

(if Rng
  (progn
    (setq vl (mapcar (function (lambda (x)
             (mapcar 'vlax-variant-value x)))
           (vlax-safearray->list
             (vlax-variant-value
          (vlax-get-property Rng 'value2)))))
    (princ "\n")
      (alert (vl-princ-to-string vl))
      )
    (progn
      (alert "Select Excel Range Before!")
      (exit)
      (*error* nil)
      (princ)
      )

    )

(*error* nil)
)

;;Usage:
(defun C:Xss ()

(RefSelection)
(princ)
)
(princ "\nType Xss in the command line")
(princ)