Author Topic: vla-sendcommand  (Read 7176 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
vla-sendcommand
« on: July 14, 2004, 03:00:08 PM »
Anyone have any experience with vla-sendcommand ? What I'm doing is this, open another dwg using the documents object and sending a command to that document object. I can make it work, but it hangs open until I go to that dwg in acad.
Code: [Select]

(defun create-new ()
  (vla-add (vla-get-documents (vlax-get-acad-object)))
  )

(setq nf (create-new))
#<VLA-OBJECT IAcadDocument 0c01b9d0>

;; i have tried both of these
(vlax-invoke-method nf
  'SendCommand (strcat "(setvar 'filedia 0)" (chr 13))
  )

;; with several variants of this one
(vla-SendCommand nf (strcat "(setvar 'cmdecho 0)" "\r"))
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: vla-sendcommand
« Reply #1 on: July 14, 2004, 03:09:40 PM »
Quote from: Mark Thomas
Anyone have any experience with vla-sendcommand ? What I'm doing is this, open another dwg using the documents object and sending a command to that document object. I can make it work, but it hangs open until I go to that dwg in acad.
Code: [Select]

(defun create-new ()
  (vla-add (vla-get-documents (vlax-get-acad-object)))
  )

(setq nf (create-new))
#<VLA-OBJECT IAcadDocument 0c01b9d0>

;; i have tried both of these
(vlax-invoke-method nf
  'SendCommand (strcat "(setvar 'filedia 0)" (chr 13))
  )

;; with several variants of this one
(vla-SendCommand nf (strcat "(setvar 'cmdecho 0)" "\r"))


<lameassedguess>
I've not tried this, but you can ...
(vlax-invoke-method nf 'SetVariable "cmdecho" 0)
</lameassedguess>
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
vla-sendcommand
« Reply #2 on: July 14, 2004, 03:23:59 PM »
Well that's true, bad example on my part. What I need to do is send the command (command "_MAPIINSERT" x "N") but all these need to happen in the background. I have a list of images to insert into new dwgs. So my thinking is this:
open new dwg;
saveas;
set some variables;
insert image;
save and close dwg;
repeat;
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
vla-sendcommand
« Reply #3 on: July 14, 2004, 03:44:20 PM »
Ok, well I'm getting closer. the following works, sorta! I still can't get control back after running it!
Code: [Select]

(vla-SendCommand nf1
  (strcat
"_MAPIINSERT " "D:/Temp/images/C2002271825.tif" " N" " "
)
  )
TheSwamp.org  (serving the CAD community since 2003)

Serge J. Gianolla

  • Guest
vla-sendcommand
« Reply #4 on: July 14, 2004, 07:56:15 PM »
Not sure if that is what you are after but assuming
Code: [Select]
(setq *ActivDoc* (vla-get-activedocument (vlax-get-acad-object)))
one possible use:
Code: [Select]
(vla-sendcommand *ActivDoc* "_.SPell ALL  ") ;2 spaces after ALL for pressing Enter

or with a drawing:
Code: [Select]
(setq LstDwgName (strcat (getvar "dwgprefix") (getvar "dwgname")))
then
Code: [Select]
(vla-sendcommand *ActivDoc* "_.SAVE LstDwgName ") to call up dialogue box!

Serge J. Gianolla

  • Guest
vla-sendcommand
« Reply #5 on: July 14, 2004, 09:11:31 PM »
After re-reading carefully! your post, realised that was not your question!!

Jeff_M

  • King Gator
  • Posts: 4098
  • C3D user & customizer
vla-sendcommand
« Reply #6 on: July 14, 2004, 09:57:25 PM »
Quote from: Mark Thomas
Ok, well I'm getting closer. the following works, sorta! I still can't get control back after running it!
Code: [Select]

(vla-SendCommand nf1
  (strcat
"_MAPIINSERT " "D:/Temp/images/C2002271825.tif" " N" " "
)
  )


This command must work differently in 2005 from what I have, 2002, since I can't get the line you use to work at all. It just goes straight to a dialogue box, no command line interface. Since it doesn't appear that you are doing anything with the image, other than loading it, why not add it with ActiveX and do away with the command?

This works in the current document, but it would be easy (in a MDI environment) to use a different open drawing. You could also use ObjectDBX to eliminate the need to open the drawings in the editor, which would allow you to run it in SDI.
Code: [Select]

(setq fname "C:/Documents and Settings/Master/My Documents/My Pictures/menu.jpg")
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(setq pspace (vla-get-paperspace doc))
(vlax-invoke pspace "addraster" fname '(0.0 0.0 0.0) 1 0)

This inserts an image at 0,0,0 with a scale of 1 and 0 degrees rotation.

HTH,
Jeff

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
vla-sendcommand
« Reply #7 on: July 15, 2004, 01:35:13 PM »
- Jeff
The reason I want to use MAPIINSERT over ADDRASTER is because these images are geo-referenced and get there insertion and scale from a world file. But anyway I think I have it working, news at eleven ..........

thanks.
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
vla-sendcommand
« Reply #8 on: July 15, 2004, 01:38:56 PM »
Wish I could help but I know nada about Map ... :(
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
vla-sendcommand
« Reply #9 on: July 15, 2004, 03:38:06 PM »
This is what I ended up with. It ain't pretty but it works. This is _not_ a general use app, it was designed to do _one_ thing in one version of acad. But hopefully someone will find some use for the code. The one cool thing about this app is that it opens another session of acad in the background.
Code: [Select]

;;; FUNCTION
;;; reads a list of image file names from a .txt file
;;; creates a new dwg for each image and inserts the image
;;; on a layer with the same name as the image less the
;;; extension.
;;;
;;; ARGUMENTS
;;; none
;;;
;;; USAGE
;;;
;;;
;;;
;;; PLATFORMS
;;; Map 5
;;;
;;; AUTHOR
;;; Copyright© 2004 Mark S. Thomas
;;; mark@theswamp.org
;;;
;;; VERSION
;;; 1.0 Thu Jul 15, 2004

;;; example file
;;; D:\Temp\images\C2002271825.tif
;;; D:\Temp\images\C2002271826.tif
;;; D:\Temp\images\C2002271827.tif

(defun release (obj /)
  (if
    (= (type obj) 'VLA-OBJECT)
    (if (not (vlax-object-released-p obj))
      (vlax-release-object obj)
      )
    )
  )

;;; opens reads and creates a list from a given file name
;;; note if path to image has spaces in them you need to
;;; add double quotes on each end.
(defun read-parse-file (fn / ln name_lst)
  (setq fo (open fn "r"))
  (while (setq ln (read-line fo))
         (setq name_lst (cons ln name_lst))
         )
  (close fo)
  (reverse name_lst)
  )

(defun get-active-doc (app)
  (vla-get-activedocument app)
  )

;;; not in use
(defun get-state (app)
  (vlax-get-property
    (vlax-invoke-method app 'GetAcadState)
    'IsQuiescent
    )
  )

;;; makes a new layer and sets it active
(defun make-layer (acad_doc layName / lay_obj ln)
  (setq lay_obj (vla-get-layers acad_doc)
        ln (vla-add lay_obj layName)
        )

  (vlax-put-property acad_doc 'ActiveLayer ln)
  (release ln)
  )

(defun get-ver ()
  (if
    (vl-string-search "R16.1"
      (vlax-product-key))
    T)
  )

;;; main function
(defun c:imagenow (/ dir image_names_lst
                     dwg_names_lst  acad_app acad_app_docs
                     new_doc cntr lay_name
                     )
  (vl-load-com)

  ;; check acad version
  (if (not (get-ver))
    (progn
      (alert "Wrong version of AutoCAD for this app.")
      (exit)
      )
    )

  ;; set your path here if it doesn't exist the program will create it
  ;; this is where the dwg's will be saved
  (setq dir "d:\\aerial_dwgs"
        cntr 0)

  (if (not (vl-file-directory-p dir))
    (vl-mkdir dir); if dir not found then make it
    )

  ;; user dialog box for selecting the path\image file
  (if (setq fn (getfiled "Select File to Read" "" "txt" 8))
    (setq image_names_lst (read-parse-file fn))
    (exit)
    )

  ;; create a list of dwg names
  (foreach x image_names_lst
           (setq dwg_names_lst
                 (cons
                   (strcat dir "\\" (vl-filename-base x) ".dwg")
                   dwg_names_lst
                   )
                 )
           )

  (setq dwg_names_lst (reverse dwg_names_lst))

  ;; open another acad session in the background
  (setq acad_app (vlax-create-object "AutoCAD.Application.16"))


  (setq acad_app_docs (vla-get-documents acad_app))

  (foreach name  dwg_names_lst
           (setq new_doc (vla-add acad_app_docs))

           (vlax-invoke-method new_doc 'SaveAs name)
           (vlax-invoke-method new_doc 'SetVariable "filedia" 0)

           (setq lay_name (vl-filename-base (nth cntr image_names_lst)))
           (make-layer new_doc lay_name)

           (vla-SendCommand
             new_doc
             (strcat "_MAPIINSERT " (nth cntr image_names_lst) " N" " ")
             )

           (vlax-invoke-method new_doc 'SetVariable "filedia" 1)
           (vlax-invoke-method new_doc 'Save)
           (vlax-invoke-method new_doc "Close")
           (release new_doc)
           (setq cntr (1+ cntr))
           )

  (vlax-invoke-method
  (vla-item acad_app_docs 0) "Close" :vlax-false); closes the intial dwg (Drawing1.dwg)
  (vlax-invoke-method acad_app 'Quit)

  (release acad_app)

  (princ)
  )
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10655
vla-sendcommand
« Reply #10 on: July 16, 2004, 07:04:08 PM »
hey, Your itterating tru a list of drawings in another AutoCAD session!!?  ...Cool!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Bhull1985

  • Guest
Re: vla-sendcommand
« Reply #11 on: December 31, 2013, 02:28:22 PM »
So this in effect would let you stay in one session of autocad and script other sessions while remaining in the one session all the while?
Seems powerful, nice job!

Jeff H

  • Needs a day job
  • Posts: 6150
Re: vla-sendcommand
« Reply #12 on: January 08, 2014, 09:24:34 PM »
The document has to be active for SendCommand.

SendCommand API only works with active document or if another document is to be used it must become active before it has something to send to.