See the top rated post in this thread. Click here

Results 1 to 10 of 12

Thread: Verify Image exists. Crashes if no image exists.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Member
    Join Date
    2001-08
    Posts
    24
    Login to Give a bone
    0

    Default Verify Image exists. Crashes if no image exists.

    Currently this works if there is an image in the drawing.
    Code:
    (setq Img1 (ssget "x" '((0 . "image"))))
      (if (Tblsearch "Layer" "Image")
        (command "_Layer" "m" "Image" "")
        (command "_chprop" Img1 "" "LA" "Image" "" ""))
        (command "_layer" "S" "Line1" "Freeze" "Image" "")
    It doesn't work if there isn't an image in the drawing.
    I'm missing something simple, I'm sure.

    J. Logan
    Last edited by BlackBox; 2017-11-13 at 07:03 PM. Reason: Please use [CODE] Tags

  2. #2
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    1

    Default Re: Verify Image exists. Crashes if no image exists.

    Give this a try:

    Code:
    (vl-load-com)
    
    (defun c:FOO (/ *error* acDoc layerName oLayer ss)
    
      (defun *error* (msg)
        (if ss
          (vla-delete ss)
        )
        (if acDoc
          (progn
            (vla-endundomark acDoc)
            (vla-regen acDoc acallviewports)
          )
        )
        (cond ((not msg))                                                   ; Normal exit
              ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
              ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
        )
        (princ)
      )
    
      (if (ssget "_:L" '((0 . "IMAGE") (8 . "~Image")))
        (progn
          (vla-startundomark
            (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
          )
    
          ;; get or create layer
          (setq oLayer
                 (vla-add (vla-get-layers acDoc) (setq layerName "Image"))
          )
    
          ;; set image layer
          (vlax-for x (setq ss (vla-get-activeselectionset acDoc))
            (vla-put-layer x layerName)
          )
    
          ;; freeze image layer, set clayer
          (vla-put-freeze oLayer :vlax-true)
          (setvar 'clayer "Line1")
        )
      )
    
      (*error* nil)
    )

    Cheers
    Last edited by BlackBox; 2017-11-13 at 08:08 PM.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  3. #3
    Member
    Join Date
    2001-08
    Posts
    24
    Login to Give a bone
    0

    Default Re: Verify Image exists. Crashes if no image exists.

    Quote Originally Posted by BlackBox View Post
    Give this a try:

    Code:
    (vl-load-com)
    
    (defun c:FOO (/ *error* acDoc layerName oLayer ss)
    
      (defun *error* (msg)
        (if ss
          (vla-delete ss)
        )
        (if acDoc
          (progn
            (vla-endundomark acDoc)
            (vla-regen acDoc acallviewports)
          )
        )
        (cond ((not msg))                                                   ; Normal exit
              ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
              ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
        )
        (princ)
      )
    
      (if (ssget "_:L" '((0 . "IMAGE") (8 . "~Image")))
        (progn
          (vla-startundomark
            (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
          )
    
          ;; get or create layer
          (setq oLayer
                 (vla-add (vla-get-layers acDoc) (setq layerName "Image"))
          )
    
          ;; set image layer
          (vlax-for x (setq ss (vla-get-activeselectionset acDoc))
            (vla-put-layer x layerName)
          )
    
          ;; freeze image layer, set clayer
          (vla-put-freeze oLayer :vlax-true)
          (setvar 'clayer "Line1")
        )
      )
    
      (*error* nil)
    )

    Cheers
    That works nicely thanks.

    I'm getting a select objects prompt to put the .CAL file on the "Image" layer at the end when I run it.

    Code:
    ;; freeze image layer, set clayer
          (vla-put-freeze oLayer :vlax-true)
          (setvar 'clayer "Line1")
        )
      )
    I'm not terribly fluent here, is this freezing the Image layer once the .CAL file is on the image layer. Or is it supposed to be?
    Last edited by BlackBox; 2017-11-13 at 08:58 PM. Reason: Please use [CODE] Tags

  4. #4
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Verify Image exists. Crashes if no image exists.

    Yeah, I was confused by that as well - the last line of your original LISP above, sets "Line1" layer current, then freezes "Image" layer - so I included in my quick adaptation.

    If those steps aren't needed (as shown in OP code), just comment them out, or remove them altogether.


    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  5. #5
    Member
    Join Date
    2001-08
    Posts
    24
    Login to Give a bone
    0

    Default Re: Verify Image exists. Crashes if no image exists.

    I suppose I should have told the whole story.

    I'm going through the process of replacing our old title blocks with an attributed title block. Seems easy enough but someone updated the Title block template so I have to replace the outdated Border and Title block in existing drawings at the same time. Some drawings have .CAL files in them while some don't. I need to protect against erasing the .CAL file when erasing the old Border and Title block via lisp.

    Happily the powers that be don't believe in using blocks or object based layer names. So I'm relegated to hunting down each specific entity that is the border and Title block while also preserving the original content within both. the guts if you will. A lot of freezing and thawing going on.

    My first move is to see if an image (.CAL file) exists. If so make sure the .CAL image is on the correct layer and freeze the layer to avoid being erased or manipulated during the greater lisp operation and then thawed once the operation is complete. If the .CAL doesn't exist, do nothing and move on. That was what the OP was about. Making sure the .CAL file is preserved during the run of the lisp routine if indeed the .CAL file exists.

    Upon further inspection, your addition to my routine is asking for a selection of the .CAL file in a drawing where one doesn't exist. I was trying to make it so the user doesn't have to do anything.
    Last edited by jlogan02; 2017-11-13 at 09:37 PM.

  6. #6
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Verify Image exists. Crashes if no image exists.

    No worries, thanks for clarifying.

    Up to you on how to proceed - anything I post, you can take and modify how you see fit, no attribution needed.

    Now, the code above doesn't test for .CAL file extension, it just handles all images, but it can if you need it to?

    Also be mindful, that freezing doesn't prevent deletion, only graphical selection; to prevent deletion, simply lock the layer instead.


    If you've got more information that you can post, I can better streamline what I've already posted; just be sure to identify any prudent conditions, or if-then situations, and I'll help where I can.

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

Similar Threads

  1. 2015: FAMILY ALREADY EXISTS
    By gordolake in forum Revit Architecture - General
    Replies: 2
    Last Post: 2014-08-26, 11:29 PM
  2. test if level exists
    By sfaust in forum Revit - API
    Replies: 44
    Last Post: 2012-01-11, 04:38 PM
  3. napkin sketch still exists???
    By ccook.135811 in forum ACA General
    Replies: 1
    Last Post: 2007-10-16, 03:38 PM
  4. Detach Image IF it exists
    By jpaulsen in forum AutoLISP
    Replies: 9
    Last Post: 2007-03-02, 11:01 PM
  5. Need help verifying file exists
    By stephen.coff in forum AutoLISP
    Replies: 3
    Last Post: 2006-09-18, 01:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •