backends/autocad/base.ss
#lang scheme
;; base.ss
;; Base functions and variables for the AutoCAD backend
(require "com-utils.ss")


(provide start-backend

         acad
         acad-mspace
         acad-active-document
         ensure-autocad-is-started)

(define debug-var #t)

(define acad-instance #f)
(define (acad . args)
  (or acad-instance
      (begin (apply start-autocad args)
             acad-instance)))

(define active-document #f)
(define (acad-active-document)
  (or active-document
      (begin (start-autocad debug-var)
             active-document)))
(define mspace #f)
(define (acad-mspace)
  (or mspace
      (begin (start-autocad debug-var)
             mspace)))

(define (start-autocad . debug)
  (let ([new-acad (get-com-object "AutoCAD Application")])
    (set! acad-instance new-acad)
    (set! active-document (get-property acad-instance
                                        ActiveDocument))
    (set! mspace (get-property active-document ModelSpace))
    (set-property! acad-instance Visible #t)))

(define start-backend start-autocad)

(define (ensure-autocad-is-started)
  (or acad-instance (start-backend)))