Author Topic: Objectdbx.. image path  (Read 7469 times)

0 Members and 1 Guest are viewing this topic.

HelpLispinSeattle

  • Guest
Objectdbx.. image path
« on: January 14, 2009, 11:45:33 AM »
Hello,

I am just learning autolisp. I picked a code from this site using ObjectDBX.
I'm trying to repath all the images in a drawing to "..\\Image\\" folder.
What am I doing wrong?.

usage..(PutImagePath "c:\\temp\\test.dwg" "..\\Image\\")


(defun PutImagePath (File Newpath / odbxSaveFlag file odbx index yetRedefine saveError objBlock image iname iextn imagename ipath)
; main
  (progn
      (if (< (atoi (substr (getvar "ACADVER") 1 2)) 17)
        (setq odbx (vlax-get-or-create-object "ObjectDBX.AxDbDocument"))
        (setq odbx (vlax-get-or-create-object (strcat "ObjectDBX.AxDbDocument." (substr (getvar "ACADVER") 1 2))))
     )

      ; main loop
       (if (vl-catch-all-error-p (vl-Catch-All-Apply '(lambda () (vla-open odbx (strcat file)))))
          (alert (strcat "Sorry..This drawing is open :\n" file))
          (progn
          (setq odbxSaveFlag nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        (vlax-for lo (vla-get-Layouts odbx)
                (vlax-for i (vla-get-Block lo)
                    (if (= (vla-get-ObjectName i) "AcDbRasterImage")(progn
                                                                                      (setq image (vla-get-ImageFile i))
                                                                                      (setq iname (vl-filename-base image))
                                                                                      (setq iextn (vl-filename-extension image))
                                                                                      (setq imagename (strcat iname iextn))
                                                                                      (setq ipath (strcat newpath imagename))
                                                                                      (vla-put-ImageFile i ipath)
                                                                                      )
                    );if
                )
           )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
         (if odbxSaveFlag (vla-saveas odbx (strcat file)))
         );progn
      );if vl-catch
      (acet-ui-status)
     (vlax-release-object odbx)
   );progn
   (princ)
)


; variables set
 (setq
    saveError *error*
    *error* errorFunction
    index 0
 )

; error function
(defun errorFunction ()
  (setq *error* saveError)
  (acet-ui-status)
  (if (and objBlock (not (vlax-object-released-p objBlock))) (vlax-release-object objBlock))
  (if (and odbx (not (vlax-object-released-p odbx))) (vlax-release-object odbx))
  (foreach x '( odbxSaveFlag file odbx index yetRedefine saveError objBlock image iname iextn imagename ipath) 
    (set x nil)
  )
  (gc)
  (princ)
)
(princ)



T.Willey

  • Needs a day job
  • Posts: 5251
Re: Objectdbx.. image path
« Reply #1 on: January 14, 2009, 12:17:05 PM »
I didn't test it; just an FYI.

What is it not doing?  It looks like it should work.
Tim

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

Please think about donating if this post helped you.

HelpLispinSeattle

  • Guest
Re: Objectdbx.. image path
« Reply #2 on: January 14, 2009, 12:35:49 PM »
It gives me this error message..

error: An error has occurred inside the *error* functiontoo many
arguments

It works okay till "(vla-put-ImageFile i ipath)".

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Objectdbx.. image path
« Reply #3 on: January 14, 2009, 12:56:08 PM »
That line works, it just returns nil, but it does put the new path; at least here in a simple test it did.  I think the error might be here
Code: [Select]
(if odbxSaveFlag (vla-saveas odbx (strcat file)))Why are you trying to make a new path for the file?  If not, then it should just be
Code: [Select]
(if odbxSaveFlag (vla-saveas odbx file))
Tim

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

Please think about donating if this post helped you.

HelpLispinSeattle

  • Guest
Re: Objectdbx.. image path
« Reply #4 on: January 14, 2009, 01:16:12 PM »
Tim,

Still didn't not work.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Objectdbx.. image path
« Reply #5 on: January 14, 2009, 01:35:53 PM »
This works.  Try and find what I did different.  If you can't, I'll try and remember.  Can't point it out right now, sorry.

Code: [Select]
(defun PutImagePath (File Newpath / odbxSaveFlag file odbx index yetRedefine saveError objBlock image iname iextn imagename ipath)
; main
    (setq odbx
        (if (< (atoi (setq oVer (substr (getvar "acadver") 1 2))) 16)
            (vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument")
            (vla-GetInterfaceObject (vlax-get-acad-object) (strcat "ObjectDBX.AxDbDocument." oVer))
        )
    )
      ; main loop
       (if (vl-catch-all-error-p (vl-Catch-All-Apply '(lambda () (vla-open odbx file))))
          (alert (strcat "Sorry..This drawing is open :\n" file))
          (progn
          (setq odbxSaveFlag T)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        (vlax-for lo (vla-get-Layouts odbx)
                (vlax-for i (vla-get-Block lo)
                    (if (= (vla-get-ObjectName i) "AcDbRasterImage")(progn
                                                                                      (setq image (vla-get-ImageFile i))
                                                                                      (setq iname (vl-filename-base image))
                                                                                      (setq iextn (vl-filename-extension image))
                                                                                      (setq imagename (strcat iname iextn))
                                                                                      (setq ipath (strcat newpath imagename))
                                                                                      (vla-put-ImageFile i ipath)
                                                                                      )
                    );if
                )
           )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
         (if odbxSaveFlag (vla-saveas odbx file))
         );progn
      );if vl-catch
      ;(acet-ui-status)
     (vlax-release-object odbx)
   (princ)
)
Tim

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

Please think about donating if this post helped you.

HelpLispinSeattle

  • Guest
Re: Objectdbx.. image path
« Reply #6 on: January 14, 2009, 01:45:33 PM »
Tim,

You made it work!..Thank you! Thank you!...I will try to find what you
did differently and learn from it.

HelpLispinSeattle

  • Guest
Re: Objectdbx.. image path
« Reply #7 on: January 16, 2009, 09:27:15 AM »
Tim,

The code works good, but sometimes I get this error message
on some of my drawings.."error: Automation Error. File access error"

When I open the drawing I ran the code with I get an alert box saying
it is currently used by me. (When I ran the code I have only one (1) drawing
open). I have to quit autocad and fire it up again, then the drawing is okay.

Any ideas why sometimes it does not work?

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Objectdbx.. image path
« Reply #8 on: January 16, 2009, 11:03:53 AM »
The error message would mean to me that someone has the drawing open.  But you shouldn't get the error, since that part of the code is wrapped up in a ' vl-catch-all-apply ' call.

The second part is most likely because it isn't closing the drawing, and releasing the ObjectDBX object.  Which shouldn't happen either, unless it is error'ing with the portion of the code about the images.

To fix both of these problems, you could create a local error function, just for this function.  Within that section, you will see if the ODBX object equals anything, and if so, release it and set it to nil.  I know if you have it in the local vars list this will happen, but I got in the habit of just setting it anyway.

Hope that is enough to get you going with trying to code it yourself.  If you get stuck, let us know, and we can help you some more.
Tim

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

Please think about donating if this post helped you.

HelpLispinSeattle

  • Guest
Re: Objectdbx.. image path
« Reply #9 on: January 16, 2009, 11:24:51 AM »

I'm new with autolisp, but I understand you correctly you mean this? (Which came with
the code I picked up from this site.)

; variables set
 (setq
    saveError *error*
    *error* errorFunction
    index 0
 )

; error function
(defun errorFunction ()
  (setq *error* saveError)
  (acet-ui-status)
  (if (and objBlock (not (vlax-object-released-p objBlock))) (vlax-release-object objBlock))
  (if (and odbx (not (vlax-object-released-p odbx))) (vlax-release-object odbx))
  (foreach x '( odbxSaveFlag file odbx index yetRedefine saveError objBlock image iname iextn imagename ipath) 
    (set x nil)
  )
  (gc)
  (princ)
)


T.Willey

  • Needs a day job
  • Posts: 5251
Re: Objectdbx.. image path
« Reply #10 on: January 16, 2009, 11:32:24 AM »
Kind of, but not really.  The way that is currently coded, it is not a local error function.  That is the old style of error function handling.  Now one use a local like

(defun c:SomeName ( / localVariables And ErrorFunction *error* )

    (defun *error* ( msg )

        .... error function code here

    )

   ..... normal code here

)

This way the error function ( *error* ) will only be known for the function ' SomeName '.  The only thing is you have to name your error function ' *error* '.
Tim

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

Please think about donating if this post helped you.

HelpLispinSeattle

  • Guest
Re: Objectdbx.. image path
« Reply #11 on: January 16, 2009, 12:17:41 PM »
How about this?...(I'm really guessing at this point).


(defun PutImagePath (File Newpath / file newpath odbxSaveFlag  odbx objBlock image iname iextn imagename ipath *error* )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;error function
(defun *error* ( msg )
(if (= msg "error: Automation Error. File access error") (vlax-release-object odbx))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;normal code
    (setq odbx
        (if (< (atoi (setq oVer (substr (getvar "acadver") 1 2))) 16)
            (vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument")
            (vla-GetInterfaceObject (vlax-get-acad-object) (strcat "ObjectDBX.AxDbDocument." oVer))
        )
    )
        (if (vl-catch-all-error-p (vl-Catch-All-Apply '(lambda () (vla-open odbx file))))
          (alert (strcat "Sorry..This drawing is open :\n" file))
          (progn
          (setq odbxSaveFlag T)
         (vlax-for lo (vla-get-Layouts odbx)
                (vlax-for i (vla-get-Block lo)
                    (if (= (vla-get-ObjectName i) "AcDbRasterImage")(progn
                                                                    (setq image (vla-get-ImageFile i))
                                                                    (setq iname (vl-filename-base image))
                                                                    (setq iextn (vl-filename-extension image))
                                                                    (setq imagename (strcat iname iextn))
                                                                    (setq ipath (strcat newpath imagename))
                                                                    (vla-put-ImageFile i ipath)
                                                                    )
                    );if
                )
           )
         (if odbxSaveFlag (vla-saveas odbx file))
         );progn
      );if vl-catch
      ;(acet-ui-status)
     (vlax-release-object odbx)
   (princ)
)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Objectdbx.. image path
« Reply #12 on: January 16, 2009, 12:40:44 PM »
See if this works for you.  I changed the error function a little, and the ' if ' call.  This is how I would use ' vl-catch-all-apply '.  I call the error function with no arguments, so that you only have to have code in one place to release the ODBX object.  Just a little simpler.  And I formatted the code to the way I like to look at it.

Code: [Select]
(defun PutImagePath (File Newpath / oVer odbxSaveFlag  odbx image iname iextn imagename ipath *error* )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;error function
    (defun *error* ( msg )
       
        (if odbx (vlax-release-object odbx))
        (if msg
            (prompt (strcat "\n Error-> " msg))
        )
    )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;normal code
    (setq odbx
        (if (< (atoi (setq oVer (substr (getvar "acadver") 1 2))) 16)
            (vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument")
            (vla-GetInterfaceObject (vlax-get-acad-object) (strcat "ObjectDBX.AxDbDocument." oVer))
        )
    )
    (if (vl-catch-all-error-p (vl-Catch-All-Apply 'vla-open (list odbx file)))
        (alert (strcat "Sorry..This drawing is open :\n" file))
        (progn
            (setq odbxSaveFlag T)
            (vlax-for lo (vla-get-Layouts odbx)
                (vlax-for i (vla-get-Block lo)
                    (if (= (vla-get-ObjectName i) "AcDbRasterImage")
                        (progn
                            (setq image (vla-get-ImageFile i))
                            (setq iname (vl-filename-base image))
                            (setq iextn (vl-filename-extension image))
                            (setq imagename (strcat iname iextn))
                            (setq ipath (strcat newpath imagename))
                            (vla-put-ImageFile i ipath)
                        )
                    );if
                )
            )
            (if odbxSaveFlag (vla-saveas odbx file))
        );progn
    );if vl-catch
    ;(acet-ui-status)
    (*error* nil)
    (princ)
)
Tim

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

Please think about donating if this post helped you.

HelpLispinSeattle

  • Guest
Re: Objectdbx.. image path
« Reply #13 on: January 16, 2009, 01:13:34 PM »
I think I understand what you did. And regardless of what the error "msg" you
released the object.

I opened the drawing and this time it was not a read-only. But it did not
repathed the image. There was a new error rmessage

Error-> ActiveX Server returned an error: Parameter not optional

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Objectdbx.. image path
« Reply #14 on: January 16, 2009, 01:24:50 PM »
In the error function add ' (vl-bt) '.  Then paste the whole command line prompt.  This will tell me where the code is erroring, as I'm not sure why you would get that message, as I didn't change anything really.
Tim

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

Please think about donating if this post helped you.

HelpLispinSeattle

  • Guest
Re: Objectdbx.. image path
« Reply #15 on: January 16, 2009, 02:35:16 PM »
Tim,

I caught my mistake. I'm a little embarrass to tell you.
I was repathing to a non existing location. As soon as
I created the desired location & copied the images it
worked.

Thank you for your time & tip for error handling. Keep
up the good work!

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Objectdbx.. image path
« Reply #16 on: January 16, 2009, 02:36:54 PM »
Tim,

I caught my mistake. I'm a little embarrass to tell you.
I was repathing to a non existing location. As soon as
I created the desired location & copied the images it
worked.

Thank you for your time & tip for error handling. Keep
up the good work!

We all have made mistakes like that.  Glad you caught it on your own, and you're welcome for the help/tips I provided.  You also keep up the good work.  As long as you are willing to learn, there are smart people here willing to teach.
Tim

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

Please think about donating if this post helped you.

JCUTRONA

  • Guest
Re: Objectdbx.. image path
« Reply #17 on: January 27, 2009, 02:42:09 PM »
Tim,

I caught my mistake. I'm a little embarrass to tell you.
I was repathing to a non existing location. As soon as
I created the desired location & copied the images it
worked.

Thank you for your time & tip for error handling. Keep
up the good work!

We all have made mistakes like that.  Glad you caught it on your own, and you're welcome for the help/tips I provided.  You also keep up the good work.  As long as you are willing to learn, there are smart people here willing to teach.
Now that is rich.  True sense of community.

Maverick®

  • Seagull
  • Posts: 14778
Re: Objectdbx.. image path
« Reply #18 on: January 27, 2009, 02:43:29 PM »
there are smart people here willing to teach.

And I'm here too!  :-D

dustinthiesse

  • Guest
Re: Objectdbx.. image path
« Reply #19 on: January 27, 2009, 04:12:12 PM »
Tim,

I caught my mistake. I'm a little embarrass to tell you.
I was repathing to a non existing location. As soon as
I created the desired location & copied the images it
worked.

Thank you for your time & tip for error handling. Keep
up the good work!

We all have made mistakes like that.  Glad you caught it on your own, and you're welcome for the help/tips I provided.  You also keep up the good work.  As long as you are willing to learn, there are smart people here willing to teach.
Now that is rich.  True sense of community.

Probably the main reason I like this forum so much!

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Objectdbx.. image path
« Reply #20 on: January 27, 2017, 03:57:57 PM »
I know this is an OLD topic. I was messing around with the idea. Its a neat concept. However, with my awesome skill level. I keep getting an error. I am sure.... wait... I know I did something to make it not work.

Code: [Select]
(PutImagePath "c:\\temp\\test.dwg" "..\\Image\\")
Code: [Select]
Command: (PutImagePath "c:\\temp\\test.dwg" "..\\Image\\")
 Error-> Automation Error. File access error

thanks for any ideas!
Civil3D 2020

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Objectdbx.. image path
« Reply #21 on: February 09, 2017, 09:33:33 AM »
In my testing, it looks like a valid image has to be in the new location.
Quote
Command: (putimagepath "c:/test/images/image.dwg" "../images/")
Command: (putimagepath "c:/test/image.dwg" "../images/")
 Error-> Automation Error. File access error
Command: (putimagepath "c:/test/image.dwg" "../images")
 Error-> Automation Error. File access error
Command: (putimagepath "c:/test/image.dwg" "/images/")
Command: (putimagepath "c:/test/image.dwg" "./images/")
Command: (putimagepath "c:/test/image.dwg" "../images/")
 Error-> Automation Error. File access error
Command: (putimagepath "c:/test/dwg/image.dwg" "../images/")

My testing setup looked like
c:/test/
 |- dwg/
      |- image.dwg
 |- images/
      |- image.png
      |- image.dwg
Tim

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

Please think about donating if this post helped you.

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Objectdbx.. image path
« Reply #22 on: February 09, 2017, 09:37:20 AM »
Just curious here, so there is not a way that it can copy the source "image" from where ever and place it in the new folder image and repath?
Civil3D 2020

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Objectdbx.. image path
« Reply #23 on: February 09, 2017, 09:44:20 AM »
A routine could be written, but as this one stands. No.  It only repaths images, and within the new path there has to be a valid file (at least that is what I get from my quick testing).
Tim

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

Please think about donating if this post helped you.

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Objectdbx.. image path
« Reply #24 on: February 09, 2017, 09:47:17 AM »
Interesting... Thank you for sharing. Looks like I will "lol" looking for something that may do this. Thank you for the suggestion.

Its amazing... I know how people work on projects and attach images from other projects or what not; but to have it copy the images attached in a dwg to a \images\ folder makes sense.
Civil3D 2020