Author Topic: VLisp Question  (Read 13691 times)

0 Members and 1 Guest are viewing this topic.

Craig

  • Guest
VLisp Question
« on: November 17, 2003, 11:59:03 AM »
Is there a way in vlisp to save a dwg file. I know in Auotlisp you can't use something like (command ".save" filename). Is there something in vlisp that will allow this? Basically what I'm doing is wanting to select a button and it store the drawing I am working on into another location, not the folder I am working out of. What I've started with so far is:
Code: [Select]
(defun c:store ()
  (setq name (getvar "dwgname"))
  (setq project (substr name 1 5))
  (setq path (strcat "h:/" project "/" name))
  (command "_save" (strcat "h:/" project "/" name)) <---I know this won't work


I just wanted to show the last line of what I am wanting to get to with vlisp. If you input each line into our drawing here is the return:
Code: [Select]
(setq name (getvar "dwgname"))
"03033MP1.dwg"
(setq project (substr name 1 5))
"03033"
(setq path (strcat "h:/" project "/" name))
"h:/03033/03033MP1.dwg"


I can save the drawing taking a few extra lines of code creating a .bat file, but I really want to get away from batch filing. Any ideas?

Craig

  • Guest
VLisp Question
« Reply #1 on: November 17, 2003, 12:07:39 PM »
Will vl-file-copy let me do this? I can put in the dwg name variable and then the strcat path. Sumthin like

(vl-file-copy "filename" (strcat "h:/" project "/" name))

Think this would work?

daron

  • Guest
VLisp Question
« Reply #2 on: November 17, 2003, 12:18:37 PM »
vla-save should do the trick. Look it up under activex and vba reference.

Craig

  • Guest
VLisp Question
« Reply #3 on: November 17, 2003, 12:27:53 PM »
vla-save is not working and is not listed in the documentation. Not sure where I should be looking for this. I know little about Vlisp. The line of code (vla-save name (strcat "h:/" project "/" name)) does not work. Not sure on how to implement this

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
VLisp Question
« Reply #4 on: November 17, 2003, 12:31:36 PM »
needs more error checking
Code: [Select]
;;; path = string
;;; c:\\temp\\ <-- needs ending '\\'
(defun cpCurdwg (path / fn dn nn)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq fn (vlax-get-property doc 'FullName)
dn (vlax-get-property doc 'Name)
nn (strcat path dn)
)
  (if (vl-file-copy fn nn)
    (princ "\nFile copied...")
    (princ "\nNo file copied...")
    )
  (princ)
  )
TheSwamp.org  (serving the CAD community since 2003)

Craig

  • Guest
VLisp Question
« Reply #5 on: November 17, 2003, 12:39:02 PM »
Thanks Mark, but something is going wrong. I get the follwing error

Quote
Error: cannot create file: h:\03033\03033MP1.dwg; error: An error has occurred
inside the *error* functionAutoCAD variable setting rejected: "orthomode" nil

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
VLisp Question
« Reply #6 on: November 17, 2003, 12:53:40 PM »
Code: [Select]
;;; path = string
;;; c:\\temp\\ <-- needs ending '\\'
(defun cpCurdwg (path / doc fn dn nn)
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq fn (vlax-get-property doc 'FullName)
dn (vlax-get-property doc 'Name)
nn (strcat path dn)
)
  (if (findfile nn)
    (alert "File exists.....")
    (if (vl-file-copy fn nn)
      (progn (princ "\nFile copied...")
    (princ "\nNo file copied...")
    )
      )
    )
  (princ)
  )
TheSwamp.org  (serving the CAD community since 2003)

Craig

  • Guest
VLisp Question
« Reply #7 on: November 17, 2003, 12:58:07 PM »
Line by line, here's whats going on.

Code: [Select]
(setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
#<VLA-OBJECT IAcadDocument 0108c8bc>
(setq fn (vlax-get-property doc 'FullName))
"C:\\DWG\\03033\\03033MP1.dwg"
(setq dn (vlax-get-property doc 'Name))
"03033MP1.dwg"
(setq nn (strcat "h:\\" (substr dn 1 5)"\\" dn))
"h:\\03033\\03033MP1.dwg"
(vl-file-copy fn nn)
Error: cannot create file: h:\03033\03033MP1.dwg; error: An error has occurred
inside the *error* functionAutoCAD variable setting rejected: "orthomode" nil


Alright, tell me what the hell I am doing wrong.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
VLisp Question
« Reply #8 on: November 17, 2003, 01:17:47 PM »
What happens when you run the program like this.
(cpCurdwg "h:/03033/")

does the \03033\ already exist?
TheSwamp.org  (serving the CAD community since 2003)

Craig

  • Guest
VLisp Question
« Reply #9 on: November 17, 2003, 01:19:14 PM »
When it does the findfile, it finds NIL but the file is there. I tried changing the path from h:\ to h:\\. I even did an strcase just to change them to all UPPERCASE since it extracts it that way, but to no avail.

Craig

  • Guest
VLisp Question
« Reply #10 on: November 17, 2003, 01:21:45 PM »
I tried (cpCurdwg "h:/03033/")  and still get the same error. Heres what I have.
Code: [Select]
(defun cpCurdwg   (path / doc fn dn nn)
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq   fn (vlax-get-property doc 'FullName)
   dn (vlax-get-property doc 'Name)
   nn (strcase(strcat "h:\\" (substr dn 1 5)"\\" dn ))
   )
  (if (findfile nn)
    (alert "File exists.....")
    (if   (vl-file-copy fn nn)
      (progn (princ "\nFile copied...")
        (princ "\nNo file copied...")
        )
      )
    )
  (princ)
  )

Columbia

  • Guest
VLisp Question
« Reply #11 on: November 17, 2003, 01:23:12 PM »
Craig,

What you need to remember about using the VLISP Active-X controls is the first argument in almost every VL- function is a VLA-OBJECT.

Therefore when using vla-save, you need to provide the document object to this function.

However, that won't get you what you are looking for will it?

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
VLisp Question
« Reply #12 on: November 17, 2003, 01:25:02 PM »
I'll get it right one of these days.........
Code: [Select]

;;; path = string
;;; c:\\temp\\ <-- needs ending '\\'
(defun cpCurdwg (path / doc fn dn nn)
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq fn (vlax-get-property doc 'FullName)
dn (vlax-get-property doc 'Name)
nn (strcat path dn)
)
  (if (findfile nn)
    (alert "File exists.....")
    (if (vl-file-copy fn nn)
      (princ "\nFile copied...")
      (princ "\nNo file copied...")
      )
    )
  (princ)
  )
TheSwamp.org  (serving the CAD community since 2003)

Craig

  • Guest
VLisp Question
« Reply #13 on: November 17, 2003, 01:28:24 PM »
Alright,here is the code I just tried, and get nothing but errors.

Code: [Select]
(defun cpCurdwg   (path / doc fn dn nn)
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq   fn (vlax-get-property doc 'FullName)
   dn (vlax-get-property doc 'Name)
   nn (strcase(strcat "h:\\" (substr dn 1 5)"\\" dn ))
   )
  (if (findfile nn)
    (alert "File exists.....")
    (if   (vla-save doc nn)
      (progn (princ "\nFile copied...")
        (princ "\nNo file copied...")
        )
      )
    )
  (princ)
  )


Bet you guys didn't know you would be tested like this on a Monday of all days. This is more like a Thursday problem.

The error I get with this one is: Error: too few arguments; error: An error has occurred inside the *error*
functionAutoCAD variable setting rejected: "orthomode" nil

Columbia

  • Guest
VLisp Question
« Reply #14 on: November 17, 2003, 01:35:37 PM »
You could try it this way...


Code: [Select]

(if (not (getvar "orthomode")) (setvar "orthomode" 0))
(defun cpCurdwg   (/ doc fn dn nn)
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq
    fn (vlax-get-property doc 'FullName)
   dn (vlax-get-property doc 'Name)
   nn (strcase(strcat "h:\\" (substr dn 1 5)"\\" dn ))
   )
  (if (findfile nn)
    (alert "File exists.....")
    (if   (vla-file-copy fn nn)
      (alert "File copied...")
      (alert "No file copied...")
    )
  )
  (princ)
  )


This might work...

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
VLisp Question
« Reply #15 on: November 17, 2003, 01:35:43 PM »
dont use this
(strcat "h:\\" (substr dn 1 5)"\\"
in the code, that's what 'path' is for.
TheSwamp.org  (serving the CAD community since 2003)

Craig

  • Guest
VLisp Question
« Reply #16 on: November 17, 2003, 02:13:48 PM »
Alright guys. I am a f&*%!#g moron. :oops:   It's my damn fault why it wouldn't work. I left out one vital piece of info. The main folder it was in. So here is what it should look like. I'll have to add another line of code to add the extra folder, but thats on me.

Code: [Select]
(vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq fn (vlax-get-property doc 'FullName))
  (setq dn (vlax-get-property doc 'Name))
  (setq nn (strcase(strcat "h:\\03\\" (substr dn 1 5)"\\" dn )))<----Notice the extra \\03\\?
  (if (findfile nn)
    (alert "File exists.....")
    (if   (vl-file-copy fn nn)
      (progn (princ "\nFile copied...")
        (princ "\nNo file copied...")
        )
      )
    )
  (princ)
  )


Mark, when I did the findfile on THE RIGHT FOLDER of course it found it. Once I ran this I got a nil      :roll:

Craig

  • Guest
VLisp Question
« Reply #17 on: November 17, 2003, 02:17:56 PM »
Oh yea, when I do (cpCurdwg "h:/03033/") it does alert me that file exist. Is there a way to overwrite automatically with vl-file-copy?

Quote
dont use this
(strcat "h:\\" (substr dn 1 5)"\\"
in the code, that's what 'path' is for.


I know, I was trying to eliminate a few lines of code   :oops:

Craig

  • Guest
VLisp Question
« Reply #18 on: November 17, 2003, 02:31:54 PM »
To answer my own question I can do this (vl-file-copy fn nn T) but I get 263149 on the command line in response to the request. It acts like it does something but when I look at the other dwg there is no change. It says in the documentation that: If specified and not nil, source-file is appended to destination-file (that is, copied to the end of the destination file). Is it basically trying to append the file instead of overwrite it? That won't work if thats true

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
VLisp Question
« Reply #19 on: November 17, 2003, 02:56:39 PM »
Then use (vl-file-delete <filename>)
TheSwamp.org  (serving the CAD community since 2003)

Craig

  • Guest
VLisp Question
« Reply #20 on: November 17, 2003, 02:58:00 PM »
Aiight guys, here seems to be what I'll be going with. Now all I need is the (vl-copy-file fn nn) to automatically overwrite, NOT APPEND. Any ideas on what I can use.

Code: [Select]
(defun cpCurdwg (/ doc fn dn nn)
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq
    fn   (vlax-get-property doc 'FullName)
    dn   (vlax-get-property doc 'Name)
    nn   (strcase (strcat "h:\\" (substr dn 1 2) "\\" (substr dn 1 5) "\\" dn)
  )
  )
  (if (findfile nn)
    (alert "File exists.....")
    (if (vla-save fn nn)
      (alert "File copied...")
      (alert "No file copied...")
    )
  )
  (princ)
)

Craig

  • Guest
VLisp Question
« Reply #21 on: November 17, 2003, 02:59:07 PM »
Aiight Mark, I'll give it a try.

Craig

  • Guest
VLisp Question
« Reply #22 on: November 17, 2003, 03:07:16 PM »
Thanks Mark, Daron & Columbia for your help. Thanks again Mark. Here is the final
Code: [Select]
(defun c:filestore (/ doc fn dn nn)
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq
    fn   (vlax-get-property doc 'FullName)
    dn   (vlax-get-property doc 'Name)
    nn   (strcase (strcat "h:\\" (substr dn 1 2) "\\" (substr dn 1 5) "\\" dn)
  )
  )
  (vl-file-delete nn)
  (vl-file-copy fn nn)
  (prompt "File Stored... ")
  (princ)
)


As simple as that

Columbia

  • Guest
VLisp Question
« Reply #23 on: November 17, 2003, 06:00:24 PM »
There's one other thing that you may have to watch out for, depending on the speed of your network and PC.

Sometimes, the interfacing with the Windows File System that vl-file-delete does takes longer to occur than VLISP allows for on its own.  Another way to put that is that your code is running too fast.  It goes onto the next step before the file deletion actually takes place.  Therefore you may get an error, or worse, unexpected results.

My suggestion is to wrap the vl-file-delete in a while loop that will keep testing for the deletion (non-existence) of the file before continuing on.  Much like the following:
Code: [Select]

(defun c:filestore (/ doc fn dn nn count)
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq
    fn      (vlax-get-property doc 'FullName)
    dn      (vlax-get-property doc 'Name)
    nn      (strcase (strcat "h:\\" (substr dn 1 2) "\\" (substr dn 1 5) "\\" dn)
    count 0
      )
  )
  (if (findfile nn) ;; if the file already exists  <-- notice the if statement
    (progn
      (vl-file-delete nn)
      (while (and (findfile nn) (< count 100)) ;; <-- here's the loop
        ;; if the file is still to be found, hold on and try again...
        (setq count (1+ count))
      )
    )
  )
  (vl-file-copy fn nn)
  (prompt "File Stored... ")
  (princ)
)


But that's just my final 2 cents worth...