Author Topic: Opening Drawings using LISP  (Read 12019 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Opening Drawings using LISP
« on: June 09, 2009, 08:04:31 PM »
I am trying to use LISP to open a drawing, and insert a block (purely for testing and learning purposes), however, I am receiving an error, and am not sure if I am approaching this in the correct way.

I am using the vla-open method, on the Documents Collection, then the vla-insertblock method on the ActiveDocument, as I understood that when the new drawing was opened, it then became the ActiveDocument.

This is a section of the code I am using:

Code: [Select]
(vla-open
  (vla-get-Documents
    (vlax-get-acad-object)) dwg :vlax-false)
(vla-insertBlock
  (vla-get-ModelSpace
    (vla-get-ActiveDocument
      (vlax-get-acad-object)))
  (vlax-3D-point '(0 0 0)) "test block" 1. 1. 1. 0.))))

Where "dwg" is just the filepath of a valid drawing name.

Any help and/or advice is appreciated as always,

Cheers,

Lee

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Opening Drawings using LISP
« Reply #2 on: June 09, 2009, 09:14:53 PM »
Thanks for your reply Alan,

I have seen another post on here with those links to ObjectDBX threads, but I wasn't sure if ObjectDBX was a necessity or not... is it the only way to accomplish this task...  **daunted by ObjDBX**

Thanks,

Lee

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
Re: Opening Drawings using LISP
« Reply #3 on: June 09, 2009, 09:44:39 PM »


Alan, I'd really like to know what sort of indexing system you keep for your links ... seems to be really effective :)
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Opening Drawings using LISP
« Reply #4 on: June 10, 2009, 12:37:17 AM »
Lisp operates within the environment of a single drawing. When you switch drawings or open a new drawing you are in a separate environment as far as Lisp is concerned. This will cause the Lisp to stop working.
To overcome this limitation you must use ObjectDBX or run a Lisp via a Script. If you run a script which runs a Lisp you must use some tricks to pass variables from one drawing environment to another.

I've seen better explanations but I'm headed to bed & don't feel like looking for the eloquent explanation. :-(
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Opening Drawings using LISP
« Reply #5 on: June 10, 2009, 12:42:21 AM »
Alan, I'd really like to know what sort of indexing system you keep for your links ... seems to be really effective :)
Sorry to disappoint you but I keep a hodge podge of links in a few files but my forte is research & remembering clues that lead me back to the source.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

FengK

  • Guest
Re: Opening Drawings using LISP
« Reply #6 on: June 10, 2009, 04:32:01 AM »
I am using the vla-open method, on the Documents Collection, then the vla-insertblock method on the ActiveDocument, as I understood that when the new drawing was opened, it then became the ActiveDocument.

i don't think that is the case. the active document does not change. but you can execute commands/lisps in the newly opened drawing using sendcommand.

Patrick_35

  • Guest
Re: Opening Drawings using LISP
« Reply #7 on: June 10, 2009, 04:53:31 AM »
Hi

Quote
you must use some tricks to pass variables from one drawing environment to another
Use the blackboard (vl-bb-set and vl-bb-ref)

@+

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Opening Drawings using LISP
« Reply #8 on: June 10, 2009, 08:39:07 AM »
Thanks for your responses guys, - I'd prefer to go down the ObjectDBX route than scripting, but one step at a time  :-)

I'll check out those links you posted Alan, and see where I can go with that - if I get stuck, no doubt I'll come running back...  8-)

Thanks for your time guys,

Lee

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Opening Drawings using LISP
« Reply #9 on: June 10, 2009, 08:41:15 AM »
I am using the vla-open method, on the Documents Collection, then the vla-insertblock method on the ActiveDocument, as I understood that when the new drawing was opened, it then became the ActiveDocument.

i don't think that is the case. the active document does not change. but you can execute commands/lisps in the newly opened drawing using sendcommand.

I read this from the ACAD help, which is where I got my information, but it has been wrong on things in the past:

Quote
VLA-OPEN
Opens an existing drawing file (DWG) and makes it the active document.

Patrick_35

  • Guest
Re: Opening Drawings using LISP
« Reply #10 on: June 10, 2009, 08:50:05 AM »
Quote
VLA-OPEN
Opens an existing drawing file (DWG) and makes it the active document.
To activate, use (vla-activate (vla-open ....))

@+

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Opening Drawings using LISP
« Reply #11 on: June 10, 2009, 09:15:59 AM »
Thanks Patrick - hadn't heard of vla-activate - nice one  8-)

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Opening Drawings using LISP
« Reply #12 on: June 10, 2009, 02:32:23 PM »
After taking a look at a few of the links as provided kindly by Alan, I have found that the users open the documents using expressions such as:

Code: [Select]
(setq odbx (vla-GetInterfaceObject *acad "ObjectDBX.AxDbDocument.16")

Where, I'm guessing, the ".16" is the ACAD version (in my case .18).

They then proceed to use:

Code: [Select]
(vla-open odbx)

And all else that follows, assumes odbx to be the ActiveDocument.

Am I correct in my thinking, or way way off the mark...    :?

Cheers,

Lee

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Opening Drawings using LISP
« Reply #13 on: June 10, 2009, 02:47:33 PM »
Code: [Select]
(setq odbx (vla-GetInterfaceObject *acad "ObjectDBX.AxDbDocument.16")This gets the application used to use ODBX.

Code: [Select]
(vla-open odbx <FullFilePath>)This opens the drawing.  The drawing is not current by any means.  The ' odbx ' variable is the document now, and to interact with it, you have to use that variable, like you would use a variable assigned to the active document, with some restrictions.

This are my understandings of what is happening.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Opening Drawings using LISP
« Reply #14 on: June 10, 2009, 02:57:54 PM »
Thank you for your insight Tim, it is much appreciated.

I currently have this code - which I using just as a learning tool, but I am having trouble handling the documents.

As a tester, I am just trying to make the code Draw a Circle at the origin in each drawing, then save and close the drawing, but I cannot seem to get it to work properly.

As I have said previously, I am new to this ObjectDBX game, so go easy on me folks   :wink:

Code: [Select]
;; ObjectDBX Tester  by Lee McDonnell (Lee Mac)
;; Credit to Tony Tanzillo for Directory Browser

(defun c:MacDwg (/ *error* *acad Shell fDir Dir dbx)
  (vl-load-com)

  (defun *error* (e)
    (if ov (mapcar 'setvar vl ov))
    (ObjRel (list Shell dbx *acad))
    (if (not (wcmatch (strcase e) "*CANCEL*,*EXIT*"))
      (princ (strcat "\n<< Error: " e " >>")))
    (princ))

  (setq *acad (vlax-get-acad-object)
        Shell (vla-getInterfaceObject *acad "Shell.Application")
        fDir (vlax-invoke-method Shell 'BrowseForFolder
               (vla-get-HWND *acad) "Select Directory: " 0))
  (if fDir
    (progn
      (setq Dir
        (vlax-get-property
          (vlax-get-property fDir 'Self) 'Path))
      (if (not (eq "\\" (substr Dir (strlen Dir))))
        (setq Dir (strcat Dir "\\")))
      (setq dbx (vla-getInterfaceObject
                  *acad "ObjectDBX.AxDbDocument.18"))
      (foreach dwg (mapcar
                     (function
                       (lambda (x)
                         (strcat Dir x)))
                     (vl-directory-files Dir "*.dwg" 1))
        (vla-open dbx dwg)
        (vla-addCircle (vla-get-ModelSpace dbx)
          (vlax-3D-point '(0 0 0)) 5)
        (vla-close dbx :vlax-true))))
 
  (ObjRel (list Shell dbx *acad))
  (gc)
  (princ))
           
(defun ObjRel (lst)
  (mapcar
    (function
      (lambda (x)
        (if (and (eq (type x) 'VLA-OBJECT)
                 (not (vlax-object-released-p x)))
          (vl-catch-all-apply
            'vlax-release-object (list x))))) lst))

Any help is of course appreciated - I admire your patience  :-)

Lee