TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Hrishikesh on August 17, 2017, 12:22:14 PM

Title: Zoom Extend All Layouts in All Open Drawings?
Post by: Hrishikesh on August 17, 2017, 12:22:14 PM
Hello All,
Is there any way to zoom all layouts in all open drawings & save all open drawings?

Thanks in advance..
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Dommy2Hotty on August 17, 2017, 01:01:50 PM
Code below for all layouts, current drawings only:

Code: [Select]
;Zoom Extents *ALL* layouts
(defun c:ZoomExtentsAllLayouts (/ acad acdoc aclay)
  (setq acad (vlax-get-acad-object)
acdoc (vla-get-ActiveDocument acad)
aclay (vla-get-ActiveLayout acdoc)
)
  (vlax-for layout (vla-get-Layouts acdoc)
    (vla-put-ActiveLayout acdoc layout)
    (vla-ZoomExtents acad)
    )
  (vla-put-ActiveLayout acdoc aclay)
  (alert "Zoom Extents has been applied to all layouts!")
  (princ)
  )

(vl-load-com)
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on August 17, 2017, 01:18:57 PM
I am sure this not right. It kinda works. I did not write this.


Code: [Select]
(DEFUN C:ZOOMANDSAVE ( / DOCS COUNT I  TEMP_ELE)
(SETQ DOCS (VLAX-GET-PROPERTY (VLAX-GET-ACAD-OBJECT) "DOCUMENTS"))
(SETQ COUNT (VLAX-GET-PROPERTY DOCS "COUNT"))
(SETQ I 0)
(SETQ TEMP_ELE NIL)


(WHILE (< I COUNT)
  (SETQ TEMP_ELE (VLAX-INVOKE-METHOD DOCS "ITEM" I))
  (VLA-POSTCOMMAND TEMP_ELE "ZOOM E ")
  (VLAX-INVOKE-METHOD TEMP_ELE "SAVE")
  (SETQ I (+ I 1))
)
)
 
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Hrishikesh on August 17, 2017, 02:59:21 PM
Code below for all layouts, current drawings only:

Code: [Select]
;Zoom Extents *ALL* layouts
(defun c:ZoomExtentsAllLayouts (/ acad acdoc aclay)
  (setq acad (vlax-get-acad-object)
acdoc (vla-get-ActiveDocument acad)
aclay (vla-get-ActiveLayout acdoc)
)
  (vlax-for layout (vla-get-Layouts acdoc)
    (vla-put-ActiveLayout acdoc layout)
    (vla-ZoomExtents acad)
    )
  (vla-put-ActiveLayout acdoc aclay)
  (alert "Zoom Extents has been applied to all layouts!")
  (princ)
  )

(vl-load-com)

Thanks Dommy2Hotty..
is it possible to use one more vlax-for loop for all open drawings? This is just a quick thought comes in my mind don't know realistic or not.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: PKENEWELL on August 21, 2017, 05:14:50 PM
The following ALMOST works:

Code: [Select]
;Zoom Extents *ALL* Document and layouts
(defun c:ZoomExtAllDocsLayouts (/ acad acdoc aclay doc lout)
  (setq acad  (vlax-get-acad-object)
     acdoc (vla-get-ActiveDocument acad)
)
   (vlax-for doc (vla-get-documents acad)
      (vla-put-ActiveDocument acad doc)
      (setq aclay (vla-get-ActiveLayout doc))
      (vlax-for lout (vla-get-Layouts doc)
        (vla-put-ActiveLayout doc lout)
        (vla-ZoomExtents acad)
      )
      (vla-put-ActiveLayout doc aclay)
   )
   (vla-put-ActiveDocument acad acdoc)
   (vla-Regen acdoc AcActiveViewport)
   (alert "Zoom Extents has been applied to all Documents and layouts!")
   (princ)
)

(vl-load-com)

if you comment out this line
Code: [Select]
(vla-put-ActiveLayout doc aclay) the code works but does not return the documents to the starting layout. I have not figured out why it errors on this yet. Anyone else have an idea?
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: roy_043 on August 22, 2017, 04:11:49 AM
@PKENEWELL:

If I test your code in BricsCAD this line causes an error:
Code: [Select]
(vla-regen acdoc)I have changed it to:
Code: [Select]
(vla-regen acdoc acactiveviewport)
BricsCAD does not have an issue with:
Code: [Select]
(vla-put-ActiveLayout doc aclay)


Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Dommy2Hotty on August 22, 2017, 12:06:19 PM
Code below for all layouts, current drawings only:

Code: [Select]
;Zoom Extents *ALL* layouts
(defun c:ZoomExtentsAllLayouts (/ acad acdoc aclay)
  (setq acad (vlax-get-acad-object)
acdoc (vla-get-ActiveDocument acad)
aclay (vla-get-ActiveLayout acdoc)
)
  (vlax-for layout (vla-get-Layouts acdoc)
    (vla-put-ActiveLayout acdoc layout)
    (vla-ZoomExtents acad)
    )
  (vla-put-ActiveLayout acdoc aclay)
  (alert "Zoom Extents has been applied to all layouts!")
  (princ)
  )

(vl-load-com)

Thanks Dommy2Hotty..
is it possible to use one more vlax-for loop for all open drawings? This is just a quick thought comes in my mind don't know realistic or not.

Unfortunately, I didn't write that code.  I also didn't document who I got the code from [most likely from within TheSwamp].   I have since started documenting properly with the help from another thread in TheSwamp: {will update with link when I find it!}.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: PKENEWELL on August 22, 2017, 04:20:05 PM
@PKENEWELL:

If I test your code in BricsCAD this line causes an error:
Code: [Select]
(vla-regen acdoc)I have changed it to:
Code: [Select]
(vla-regen acdoc acactiveviewport)
BricsCAD does not have an issue with:
Code: [Select]
(vla-put-ActiveLayout doc aclay)

@Roy_043,

Yes - I see it did have a problem with vla-regen. I will update the code above. However - I still get an "Automation Error. Invalid argument" with (vla-put-ActiveLayout doc aclay) in AutoCAD.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Hrishikesh on September 02, 2017, 04:51:27 AM
The following ALMOST works:

Code: [Select]
;Zoom Extents *ALL* Document and layouts
(defun c:ZoomExtAllDocsLayouts (/ acad acdoc aclay doc lout)
  (setq acad  (vlax-get-acad-object)
     acdoc (vla-get-ActiveDocument acad)
)
   (vlax-for doc (vla-get-documents acad)
      (vla-put-ActiveDocument acad doc)
      (setq aclay (vla-get-ActiveLayout doc))
      (vlax-for lout (vla-get-Layouts doc)
        (vla-put-ActiveLayout doc lout)
        (vla-ZoomExtents acad)
      )
      (vla-put-ActiveLayout doc aclay)
   )
   (vla-put-ActiveDocument acad acdoc)
   (vla-Regen acdoc AcActiveViewport)
   (alert "Zoom Extents has been applied to all Documents and layouts!")
   (princ)
)

(vl-load-com)

if you comment out this line
Code: [Select]
(vla-put-ActiveLayout doc aclay) the code works but does not return the documents to the starting layout. I have not figured out why it errors on this yet. Anyone else have an idea?

Thanks for your code PKENEWELL,
Sorry to reply so late, I was on vacations.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: PKENEWELL on September 05, 2017, 10:49:27 AM
I would still like to know why I get an "Automation Error. Invalid argument" with (vla-put-ActiveLayout doc aclay) in AutoCAD.  :blink:

Am I doing something wrong or is this a bug? Please anyone give feedback if you know. Thanks!
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: ribarm on September 05, 2017, 03:42:37 PM
This is only my understanding of the problem, so watch out for someone more knowledge person...
I think the problem isn't in (vla-put-activelayout) function, but in fact that you can't activate another opened document model space to be active, so when you call from only currently loaded routine active model space another documents model space layout through (vla-put-activelayout doc aclay) it fails which is ordinary situation... What is not good is that you can't zoom extents exactly those documents model spaces layouts, otherwise and your code would work, and this mod would be also good :

Code - Auto/Visual Lisp: [Select]
  1. ;Zoom Extents *ALL* Document and layouts
  2. (defun c:ZoomExtAllDocsLayouts ( / acad acdoc aclay doc lout )
  3.  
  4.  
  5.   (setq acdoc (vla-get-ActiveDocument acad))
  6.   (setq aclay (vla-get-ActiveLayout acdoc))
  7.   (vla-ZoomExtents acad)
  8.   (vlax-for lout (vla-get-Layouts acdoc)
  9.     (if (not (equal lout aclay))
  10.       (progn
  11.         (vla-put-ActiveLayout acdoc lout)
  12.         (vla-ZoomExtents acad)
  13.       )
  14.     )
  15.   )
  16.   (vla-put-ActiveLayout acdoc aclay)
  17.     (if (not (equal doc acdoc))
  18.       (progn
  19.         (vla-put-ActiveDocument acad doc)
  20.         (setq aclay (vla-get-ActiveLayout doc))
  21.         (vla-ZoomExtents acad)
  22.         ;;;(vla-addline (vla-get-block aclay) (vlax-3d-point '(0 0 0)) (vlax-3d-point '(1 0 0)))
  23.         (vlax-for lout (vla-get-Layouts doc)
  24.           (if (not (equal lout aclay))
  25.             (progn
  26.               (vla-put-ActiveLayout doc lout)
  27.               (vla-ZoomExtents acad)
  28.             )
  29.           )
  30.         )
  31.       )
  32.     )
  33.   )
  34.   (vla-put-ActiveDocument acad acdoc)
  35.   (vla-Regen acdoc AcActiveViewport)
  36.   (alert "Zoom Extents has been applied to all Documents and layouts!")
  37.   (princ)
  38. )
  39.  

If you remove comment from the line (vla-addline ... ), you'll notice that instead of CAD drawing line in various opened documents active layout block - model space, CAD will draw only lines - in active layout from which routine was executed...

All I can say and if I may suggest something is that you save all your opened documents in one working folder, and then attempt to do this through SCRIPT... Something like this : (I modified it to imitate zooming extents all layouts)...

Code - Auto/Visual Lisp: [Select]
  1. (defun mr nil
  2.   (if (findfile (strcat (getvar 'tempprefix) "mr.scr")) (vl-file-delete (findfile (strcat (getvar 'tempprefix) "mr.scr"))))
  3.   (setq mr nil)
  4.   (princ)
  5. )
  6.  
  7. (defun c:zoomextents ( / *error* getfiles scroperation fd fullfilenames )
  8.  
  9.   (defun *error* ( msg )
  10.     (if fd (setvar 'filedia fd))
  11.     (if msg (prompt msg))
  12.     (princ)
  13.   )
  14.  
  15.   (defun getfiles ( path / dirs files )
  16.     (setq path (strcat path "\\"))
  17.     (setq dirs (vl-directory-files path nil -1))
  18.     (setq dirs (vl-remove "." dirs))
  19.     (setq dirs (vl-remove ".." dirs))
  20.     (setq files (vl-directory-files path "*.dwg" 1))
  21.     (setq fullfilenames (append fullfilenames (mapcar '(lambda ( x ) (strcat path x)) files)))
  22.     (mapcar '(lambda ( x ) (getfiles (strcat path x))) dirs)
  23.     fullfilenames
  24.   )
  25.  
  26.   (defun scroperation ( fullfilename / fn )
  27.     (if (not (findfile (strcat (getvar 'tempprefix) "mr.scr")))
  28.       (setq fn (open (strcat (getvar 'tempprefix) "mr.scr") "w"))
  29.       (setq fn (open (strcat (getvar 'tempprefix) "mr.scr") "a"))
  30.     )
  31.     (write-line "open" fn)
  32.     (write-line (strcat "\"" fullfilename "\"") fn)
  33.     (write-line "(setq aclay (getvar 'ctab))" fn)
  34.     (write-line "(foreach lay (layoutlist) (setvar 'ctab lay) (vl-cmdf \"_.ZOOM\" \"_E\"))" fn)
  35.     (write-line "(setvar 'ctab aclay)" fn)
  36.     (write-line "(vl-cmdf \"_.ZOOM\" \"_E\")" fn)
  37.     (write-line "(setq aclay nil)" fn)
  38.     (write-line "qsave" fn)
  39.     (write-line "close" fn)
  40.     (close fn)
  41.     (princ)
  42.   )
  43.  
  44.   (setq fd (getvar 'filedia))
  45.   (setvar 'filedia 0)
  46.   (alert "After execution of routine, type (mr) to finish routine task properly")
  47.   (setq fullfilenames (getfiles (vl-filename-directory (getfiled "Select main directory" "" "dwg" 4))))
  48.   (foreach file fullfilenames
  49.     (scroperation file)
  50.   )
  51.   (vl-cmdf "_.script" (strcat (getvar 'tempprefix) "mr.scr"))
  52.   (*error* nil)
  53. )
  54.  
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: PKENEWELL on September 05, 2017, 05:49:32 PM
Hi Marko. Thanks for your insights. I can see what's going on now. Both your code and mine fails to do a Zoom Extents outside the document that the code was run from. I attempted using (vla-sendcommand doc "_Zoom e "), but it seems there are asynchronous problems with this. I will keep playing around with it.

Your script version would be fine in certain circumstances, but I think the OP wanted a more general solution without having to move documents around.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on September 06, 2017, 08:05:24 AM
This might be another topic but within Express Tools under Files. You can save all opened drawings in the current session. How does that work compared to what we are trying to do with this type of routine?
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: PKENEWELL on September 06, 2017, 09:51:46 AM
This might be another topic but within Express Tools under Files. You can save all opened drawings in the current session. How does that work compared to what we are trying to do with this type of routine?

The Saving is not the problem. Both SAVEALL and QQUIT in Express Tools will do that. The OP would like to Zoom Extents in all layouts within the open drawings before saving. I have tried (vla-ZoomExtents), (command-s), and (vla-SendCommand). None seem to work on anything but the drawing that the program was run within.

This seems to be a namespace issue. Perhaps using a separate namespace VLX?
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on September 06, 2017, 09:59:19 AM
I guess my question is, what programming language is used with the express tool work? Is it a .NET thing? I guess something in the way the mechanics work.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: PKENEWELL on September 06, 2017, 10:08:44 AM
I guess my question is, what programming language is used with the express tool work? Is it a .NET thing? I guess something in the way the mechanics work.

I am not sure what those commands are programmed in, but my point is that (vla-save) DOES work fine in Visual Lisp. However, The Zoom Extents via (Vla-ZoomExtents) and other methods do not seems to work.

BTW - I attempted to compile the program to a separate namespace VLX file and it still did not work.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on September 06, 2017, 10:18:36 AM
I wonder what other vla commands work similar to the vla-save. Like you said the vla zoomextents does not work.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: ChrisCarlson on September 06, 2017, 10:19:14 AM
Correct, as written previously, you are only zoom-extenting the current tab or layout. You need to process each layout separately.

Lee had written this up a few years ago

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/zoom-extents-for-all-tabs/m-p/3226104/highlight/true#M300276

Code - Auto/Visual Lisp: [Select]
  1. (defun c:zea ( / acapp acdoc aclay )
  2.     (setq acapp (vlax-get-acad-object)
  3.           acdoc (vla-get-activedocument acapp)
  4.           aclay (vla-get-activelayout acdoc)
  5.     )
  6.     (vlax-for layout (vla-get-layouts acdoc)
  7.         (vla-put-activelayout acdoc layout)
  8.         (if (eq acpaperspace (vla-get-activespace acdoc))
  9.             (vla-put-mspace acdoc :vlax-false)
  10.         )
  11.         (vla-zoomextents acapp)
  12.     )
  13.     (vla-put-activelayout acdoc aclay)
  14.     (princ)
  15. )
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: PKENEWELL on September 06, 2017, 10:25:55 AM
Correct, as written previously, you are only zoom-extenting the current tab or layout. You need to process each layout separately.

Lee had written this up a few years ago

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/zoom-extents-for-all-tabs/m-p/3226104/highlight/true#M300276
...

@Master_Shake

Yes - I understand this. If you look at my original code and Marko's, we were attempting to do this in ALL open drawings, not just in the currently active one. The (vla-ZoomExtents) works correctly in all the layouts of the currently active drawing. but does not work when switching to another open drawing.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: ChrisCarlson on September 06, 2017, 11:10:43 AM
Oh, sorry it appears I mis-read.  The zoom function is part of the application object within the document namespace which means the lisp routine is limited to the active document it is ran in.

I had to edit it a few times to try and word it correctly.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on September 06, 2017, 11:56:08 AM
Would a .NET type of application be able to do this?
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: ChrisCarlson on September 06, 2017, 03:09:41 PM
.NET should be able to accomplish this, personally I would go with a script routine. Lee's SW is quite powerful.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on September 06, 2017, 03:30:14 PM
I love using Lee's Scriptwriter. The other thing I am kinda leveraging is the acaddoc.lsp. Basically I put the routine I need in there. Save it, then drag all the drawings I need to do this to into AutoCAD.

But I do see how a user could be in several drawings and need to run a routine on all opened dwgs. Weather its a save each dwg or zoom extents, or plot for that matter.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: PKENEWELL on September 06, 2017, 05:29:22 PM
Oh, sorry it appears I mis-read.  The zoom function is part of the application object within the document namespace which means the lisp routine is limited to the active document it is ran in.

I had to edit it a few times to try and word it correctly.

Yep. I had though I could get away with using (vla-SendCommand doc "_zoom _e ") since it directly references the document object. However, it does not work and can make the application unstable. It seems to try to run the command in the calling document no matter what document object is referenced and confuses AutoCAD. I would classify this as a bug as it should send the command to the referenced document object. Perhaps it just creates a namespace conflict however.

FYI - I also tried (vla-ZoomExtents (Vla-Get-Application doc)) and got the same results as (vla-ZoomExtents acd). Though maybe getting the application from the referenced document would make a difference - Nope.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 06, 2017, 05:44:08 PM
Quick and dirty, minimalist, no hand holding code ...

Place this code in a vb module, then save to a dvb file, e.g.  "z:\dvblib\zooms.dvb"

Code: [Select]
Sub ZoomExtentsInAllLayouts( )

    Dim layout  As AcadLayout, _
        restore As AcadLayout
       
    With ThisDrawing
       
        Set restore = .ActiveLayout
           
        For Each layout In .Layouts
            .ActiveLayout = layout
            If 1 = .GetVariable("TILEMODE") Or _
               1 = .GetVariable("CVPORT") Then
                Application.ZoomExtents
            Else
                .MSpace = False
                Application.ZoomExtents
                .MSpace = True
            End If
        Next layout
       
        If restore.Name <> .ActiveLayout.Name Then .ActiveLayout = restore
       
    End With

End Sub

Sub ZoomExtentsInAllDocs( )

    Dim doc     As AcadDocument, _
        restore As AcadDocument
       
    Set restore = ThisDrawing
   
    For Each doc In Documents
        doc.Activate
        Call ZoomExtentsInAllLayouts( )
    Next
   
    restore.Activate

End Sub

Invoke from LISP:

(vla-loaddvb (vlax-get-acad-object) "z:\\dvblib\\zooms.dvb")

(command ".vbastmt" "ZoomExtentsInAllDocs")

*poof*
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 06, 2017, 11:02:30 PM
Some quick & dirty helper / wrapper code (edit: updated LoadedDVBs function) ...

Code: [Select]
(defun _LoadedDVBs ( / i pjx pth rst )

    (repeat
        (setq i
            (vlax-get
                (setq pjx
                    (vlax-get
                        (vlax-get (vlax-get-acad-object) 'vbe)
                        'VBProjects
                    )
                )
               'count
            )
        )
        (and
            (setq pth (vl-catch-all-apply 'vlax-get (list (vlax-invoke pjx 'item i) 'filename)))
            (eq 'str (type pth))
            (setq rst (cons pth rst))
        )   
        (setq i (1- i))
    )

    rst

)

And ...

Code: [Select]
(defun C:ZAll ( )

    (if
        (null
            (vl-some
                (function (lambda (s) (eq "ZOOMS" (strcase (vl-filename-base s)))))
                (_LoadedDVBs)
            )
        )
        (vl-catch-all-apply
            'vla-loaddvb
            ;;  mod to suit | replace with findfile algo | yada
            (list (vlax-get-acad-object) "z:\\dvblib\\zooms.dvb")
        )
    )

    (vl-cmdf ".vbastmt" "ZoomExtentsInAllDocs")

    (princ)

)

Cheers.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: ribarm on September 07, 2017, 01:06:30 AM
I hope I copied it fine...

It gives me this error :
Code: [Select]
Command: ZALL
Initializing VBA System...
Syntax Error

Here is what I've used :

Code: [Select]
(defun _LoadedDVBs ( / i pjx rst )

    (repeat
        (setq i
            (vlax-get
                (setq pjx
                    (vlax-get
                        (vlax-get (vlax-get-acad-object) 'vbe)
                        'VBProjects
                    )
                )
               'count
            )
        )
        (setq
            rst (cons (vlax-get (vlax-invoke pjx 'item i) 'filename) rst)
            i   (1- i)
        )
    )

    (reverse rst)

)

(defun C:ZAll ( / fn )

    (if
        (null
            (vl-some
                (function (lambda (s) (eq "ZOOMS" (strcase (vl-filename-base s)))))
                (_LoadedDVBs)
            )
        )
        (progn
            (setq fn (open (strcat (getvar 'tempprefix) "zooms.dvb") "w"))
            (write-line "Sub ZoomExtentsInAllLayouts( )" fn)
            (write-line "" fn)
            (write-line "    Dim layout  As AcadLayout, _" fn)
            (write-line "        restore As AcadLayout" fn)
            (write-line "        " fn)
            (write-line "    With ThisDrawing" fn)
            (write-line "        " fn)
            (write-line "        Set restore = .ActiveLayout" fn)
            (write-line "            " fn)
            (write-line "        For Each layout In .Layouts" fn)
            (write-line "            .ActiveLayout = layout" fn)
            (write-line "            If 1 = CInt(.GetVariable(\"TILEMODE\")) Or _" fn)
            (write-line "               1 = CInt(.GetVariable(\"CVPORT\")) Then" fn)
            (write-line "                Application.ZoomExtents" fn)
            (write-line "            Else" fn)
            (write-line "                .MSpace = False" fn)
            (write-line "                Application.ZoomExtents" fn)
            (write-line "                .MSpace = True" fn)
            (write-line "            End If" fn)
            (write-line "        Next layout" fn)
            (write-line "        " fn)
            (write-line "        If restore.Name <> .ActiveLayout.Name Then .ActiveLayout = restore" fn)
            (write-line "        " fn)
            (write-line "    End With" fn)
            (write-line "" fn)
            (write-line "End Sub" fn)
            (write-line "" fn)
            (write-line "Sub ZoomExtentsInAllDocs( )" fn)
            (write-line "" fn)
            (write-line "    Dim doc     As AcadDocument, _" fn)
            (write-line "        restore As AcadDocument" fn)
            (write-line "        " fn)
            (write-line "    Set restore = ThisDrawing" fn)
            (write-line "    " fn)
            (write-line "    For Each doc In Documents" fn)
            (write-line "        doc.Activate" fn)
            (write-line "        Call ZoomExtentsInAllLayouts( )" fn)
            (write-line "    Next" fn)
            (write-line "    " fn)
            (write-line "    If restore.Name <> ThisDrawing.Name Then restore.Activate" fn)
            (write-line "" fn)
            (write-line "End Sub" fn)
            (close fn)
            (vl-catch-all-apply
                'vla-loaddvb
                ;;  mod to suit | replace with findfile algo | yada
                (list (vlax-get-acad-object) (strcat (getvar 'tempprefix) "zooms.dvb"))
            )
            (if (findfile (strcat (getvar 'tempprefix) "zooms.dvb"))
                (vl-file-delete (strcat (getvar 'tempprefix) "zooms.dvb"))
            )
        )
    )

    (vl-cmdf ".vbastmt" "ZoomExtentsInAllDocs")

    (princ)

)

What is wrong, I can't see... Help, please...
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 07, 2017, 01:16:20 AM
That's not how one creates a dvb file.

You need to open the VBA editor (vbaide) -- create a new code module -- add the code I supplied to the new module. Save the project as zooms.dvb ...
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 07, 2017, 01:24:27 AM
That said -- I could read in the binary stream from the dvb file I created via my _ReadStream function -- store the data in lisp format -- write out to a dvb file via my _WriteStream function -- then invoke per previous posed code -- ha.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 07, 2017, 01:25:58 AM
And with that good night folks. :-P
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: ribarm on September 07, 2017, 01:28:43 AM
Thanks MP...
It's working now...
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 07, 2017, 01:31:19 AM
You're most welcome -- thanks for letting me know -- cheers. Zzzzzzzz...
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Marc'Antonio Alessi on September 07, 2017, 04:41:56 AM
That said -- I could read in the binary stream from the dvb file I created via my _ReadStream function -- store the data in lisp format -- write out to a dvb file via my _WriteStream function -- then invoke per previous posed code -- ha.
>> (vl-cmdf ".vbastmt" "ZoomExtentsInAllDocs")
Using vbastmt on 64 bit might I have this problem?
https://www.theswamp.org/index.php?topic=43869.msg491387#msg491387
Code: [Select]
"So if I use VBASTMT in >= 2010 I need to load VBA
(x64VBAServer.exe on 64 bit) and *everything* runs 6-8x slower,
including AutoCAD commands! It is much true particularly if you
must use many commands inside of the Lisp function.
Then I need to close AutoCAD to restore previous speed."
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on September 07, 2017, 07:16:09 AM
Marko,
Just curious here, why is your code different than MP's? To me, it looks like you are repeating the dvb code twice.

Still getting the darn Syntax error.

Hopefully I am missing something here. For me to get the "ZALL" routine to work.
1. VBaid
2. Create new module
3. Paste the mode in the module
4. Debug / Compile ACADProject
5. Save out module to Zooms.dvb
6. In AutoCAD, Run "zall"

Works.

When I close out of AutoCAD completely and open a new session. I run the "zall" routine and can not get it to work without doing the above steps.




I hope I copied it fine...

It gives me this error :
Code: [Select]
Command: ZALL
Initializing VBA System...
Syntax Error

Here is what I've used :

Code: [Select]
(defun _LoadedDVBs ( / i pjx rst )

    (repeat
        (setq i
            (vlax-get
                (setq pjx
                    (vlax-get
                        (vlax-get (vlax-get-acad-object) 'vbe)
                        'VBProjects
                    )
                )
               'count
            )
        )
        (setq
            rst (cons (vlax-get (vlax-invoke pjx 'item i) 'filename) rst)
            i   (1- i)
        )
    )

    (reverse rst)

)

(defun C:ZAll ( / fn )

    (if
        (null
            (vl-some
                (function (lambda (s) (eq "ZOOMS" (strcase (vl-filename-base s)))))
                (_LoadedDVBs)
            )
        )
        (progn
            (setq fn (open (strcat (getvar 'tempprefix) "zooms.dvb") "w"))
            (write-line "Sub ZoomExtentsInAllLayouts( )" fn)
            (write-line "" fn)
            (write-line "    Dim layout  As AcadLayout, _" fn)
            (write-line "        restore As AcadLayout" fn)
            (write-line "        " fn)
            (write-line "    With ThisDrawing" fn)
            (write-line "        " fn)
            (write-line "        Set restore = .ActiveLayout" fn)
            (write-line "            " fn)
            (write-line "        For Each layout In .Layouts" fn)
            (write-line "            .ActiveLayout = layout" fn)
            (write-line "            If 1 = CInt(.GetVariable(\"TILEMODE\")) Or _" fn)
            (write-line "               1 = CInt(.GetVariable(\"CVPORT\")) Then" fn)
            (write-line "                Application.ZoomExtents" fn)
            (write-line "            Else" fn)
            (write-line "                .MSpace = False" fn)
            (write-line "                Application.ZoomExtents" fn)
            (write-line "                .MSpace = True" fn)
            (write-line "            End If" fn)
            (write-line "        Next layout" fn)
            (write-line "        " fn)
            (write-line "        If restore.Name <> .ActiveLayout.Name Then .ActiveLayout = restore" fn)
            (write-line "        " fn)
            (write-line "    End With" fn)
            (write-line "" fn)
            (write-line "End Sub" fn)
            (write-line "" fn)
            (write-line "Sub ZoomExtentsInAllDocs( )" fn)
            (write-line "" fn)
            (write-line "    Dim doc     As AcadDocument, _" fn)
            (write-line "        restore As AcadDocument" fn)
            (write-line "        " fn)
            (write-line "    Set restore = ThisDrawing" fn)
            (write-line "    " fn)
            (write-line "    For Each doc In Documents" fn)
            (write-line "        doc.Activate" fn)
            (write-line "        Call ZoomExtentsInAllLayouts( )" fn)
            (write-line "    Next" fn)
            (write-line "    " fn)
            (write-line "    If restore.Name <> ThisDrawing.Name Then restore.Activate" fn)
            (write-line "" fn)
            (write-line "End Sub" fn)
            (close fn)
            (vl-catch-all-apply
                'vla-loaddvb
                ;;  mod to suit | replace with findfile algo | yada
                (list (vlax-get-acad-object) (strcat (getvar 'tempprefix) "zooms.dvb"))
            )
            (if (findfile (strcat (getvar 'tempprefix) "zooms.dvb"))
                (vl-file-delete (strcat (getvar 'tempprefix) "zooms.dvb"))
            )
        )
    )

    (vl-cmdf ".vbastmt" "ZoomExtentsInAllDocs")

    (princ)

)

What is wrong, I can't see... Help, please...
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 07, 2017, 07:34:16 AM
... Using vbastmt on 64 bit might I have this problem?
https://www.theswamp.org/index.php?topic=43869.msg491387#msg491387

I'm running 2015 64 bit. Speedy here -- acknowledging one test environment does not make conclusive. Thanks for the heads up. PS -- there's more than one way to skin this cat ... to load code into vbe ... or ... :evil:

Still getting the darn Syntax error.

Define your error handler like this: (defun *error* (x) (vl-bt))

Then execute the code line by line & show us the error message thrown please.

PS: You don't need step 4.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on September 07, 2017, 07:40:20 AM
Clean session.

Code: [Select]
Command: zall
Backtrace:
[0.52] (VL-BT)
[1.48] (*ERROR* "ACADProject: Path not found")
[2.43] (_call-err-hook #<SUBR @00000000567d32c8 *ERROR*> "ACADProject: Path not found")
[3.37] (sys-error "ACADProject: Path not found")
:ERROR-BREAK.32 nil
[4.29] (vlax-get #<VLA-OBJECT _VBProject 000000002aaaf148> FILENAME)
[5.23] (_LOADEDDVBS)
[6.19] (C:ZALL)
[7.15] (#<SUBR @00000000568af098 -rts_top->)
[8.12] (#<SUBR @000000003d568700 veval-str-body> "(C:ZALL)" T #<FILE internal>)
:CALLBACK-ENTRY.6 (:CALLBACK-ENTRY)
:ARQ-SUBR-CALLBACK.3 (nil 0)

Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 07, 2017, 08:11:42 AM
Ah. Found the issue. If the VBIDE has been fired up but a dvb has not been loaded there is an active project but the filename property returns an error. Ez fix.

Code: [Select]
(defun _LoadedDVBs ( / i pjx pth rst )

    (repeat
        (setq i
            (vlax-get
                (setq pjx
                    (vlax-get
                        (vlax-get (vlax-get-acad-object) 'vbe)
                        'VBProjects
                    )
                )
               'count
            )
        )
        (and
            (setq pth (vl-catch-all-apply 'vlax-get (list (vlax-invoke pjx 'item i) 'filename)))
            (eq 'str (type pth))
            (setq rst (cons pth rst))
        )   
        (setq i (1- i))
    )

    rst

)

Thanks MST.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on September 07, 2017, 08:24:23 AM
Hey I helped! one more for you. Typically if this is used in a drawing, it will be saved with the drawing. So others who do not have VBA installed or enabled would get the popup enable VBA stuff. Is there a way that once we run the routine the macro can be deleted from the drawing? If not that's ok. Just did not know.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 07, 2017, 08:37:06 AM
There's a way to do everything. :-) unfortunately out of time at present ... off to shower ... then my real job ... maybe tomorrow I can play again ... thanks. :-)
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on September 07, 2017, 08:50:41 AM
lol... Have a great day!
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: PKENEWELL on September 07, 2017, 10:06:34 AM
Quick and dirty, minimalist, no hand holding code ...

Place this code in a vb module, then save to a dvb file, e.g.  "z:\dvblib\zooms.dvb"
...

Thanks MP! That does work and gives the OP an option using VBA.

The only minor thing I noticed is that - at least on my machine - the sub does not return the active document to the original. I can see that you have code in there for this i.e.
Code: [Select]
If restore.Name <> ThisDrawing.Name Then restore.ActivateHowever this does not seem to be doing the job to return focus to the calling document.

Otherwise - a good simple solution provided the OP has VBA Installed.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 07, 2017, 10:44:50 AM
Thanks MP! That does work and gives the OP an option using VBA.

The only minor thing I noticed is that - at least on my machine - the sub does not return the active document to the original.

Not sure why that's happening and don't have time to investigate. Could just force it, so ...

Replace:
    If restore.Name <> ThisDrawing.Name Then restore.Activate

With:
    restore.Activate

Which I've done in the original post.

Thanks for the feedback & comments, cheers! :)
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 07, 2017, 10:48:10 AM
... PS -- there's more than one way to ... load code into vbe ...

confirmed (abuse vbe directly) lol ... when I've time ... :evil:
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: PKENEWELL on September 07, 2017, 11:28:32 AM
Not sure why that's happening and don't have time to investigate. Could just force it, so ...

Replace:
    If restore.Name <> ThisDrawing.Name Then restore.Activate

With:
    restore.Activate

Which I've done in the original post.

Thanks for the feedback & comments, cheers! :)

No problem MP!

Yes - I tried changing the line you mentioned, and I also tried:

Code: [Select]
    If restore.Active = False Then
        restore.Activate
    End If

Neither seem to work. Still leaves you in the last drawing in the list. In any case - not a big deal since it still does what the OP wants. I just added "doc.Save" and "restore.Save" into your version of the code to match what the OP asked for:

Code: [Select]
Sub ZoomExtentsInAllLayouts()

    Dim layout  As AcadLayout, _
        restore As AcadLayout
       
    With ThisDrawing
       
        Set restore = .ActiveLayout
           
        For Each layout In .Layouts
            .ActiveLayout = layout
            If 1 = .GetVariable("TILEMODE") Or _
               1 = .GetVariable("CVPORT") Then
                Application.ZoomExtents
            Else
                .MSpace = False
                Application.ZoomExtents
                .MSpace = True
            End If
        Next layout
       
        If restore.Name <> .ActiveLayout.Name Then .ActiveLayout = restore
       
    End With

End Sub

Sub ZoomExtentsInAllDocs()

    Dim doc     As AcadDocument, _
        restore As AcadDocument
       
    Set restore = ThisDrawing
   
    For Each doc In Documents
        doc.Activate
        Call ZoomExtentsInAllLayouts
        doc.Save
    Next
   
    'If restore.Name <> ThisDrawing.Name Then restore.Activate
    If restore.Active = False Then
        restore.Activate
    End If
   
    restore.Save

End Sub
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 07, 2017, 12:36:38 PM
Perhaps:

Code: [Select]
Sub ZoomExtentsInAllDocs()

    Dim doc     As AcadDocument, _
        restore As AcadDocument
       
    Set restore = Application.ActiveDocument
   
    For Each doc In Documents
        doc.Activate
        Call ZoomExtentsInAllLayouts
    Next
   
    restore.Activate

End Sub
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: PKENEWELL on September 07, 2017, 12:55:53 PM
Perhaps:

Code: [Select]
Sub ZoomExtentsInAllDocs()

    Dim doc     As AcadDocument, _
        restore As AcadDocument
       
    Set restore = Application.ActiveDocument
   
    For Each doc In Documents
        doc.Activate
        Call ZoomExtentsInAllLayouts
    Next
   
    restore.Activate

End Sub

That did It! Now works as expected. Thanks! LOL - I don't even know if the OP is paying attention anymore.  :-D
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on September 07, 2017, 01:00:42 PM
I think this is great! Think of all the things you can do now you have this piece of the code squared away.

User has a bunch of drawings open: (They may want to:) i.e.

zoom extents in all layout tabs and save

purge all the opened drawings

possibly perform page setups


Again this is me, brain storming around. All in all. pretty cool.

Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Grrr1337 on September 07, 2017, 05:25:40 PM
Good job MP,
fluent on several languages, sadly only the computer completely understands what you said (wrote)! :lol:

That said -- I could read in the binary stream from the dvb file I created via my _ReadStream function -- store the data in lisp format -- write out to a dvb file via my _WriteStream function -- then invoke per previous posed code -- ha.

That sounds intriguing!  :-o
But (as a VBA newbie) it makes me wonder are there any advantages in Visual Basic, in order to the functionality, over LISP?
Like I was going thru some VBA tutorials and I saw how much easier is to create dialogs (forms), access objects with properties'n'methods (esp the scripting objects) - but the same could be achieved with pure LISP (although very hard, but still doable).
So is there anything that can be done in VBA and not in LISP? (finally found a VBA'n'LISP guy to ask!)
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 07, 2017, 08:09:13 PM
Good job MP

Merci. :)

... sadly only the computer completely understands what you said (wrote)! :lol:

So very not true, lol.

...are there any advantages in Visual Basic, in order to the functionality, over LISP?

Well there's the ability to iterate over all drawings open in the editor and perform a zoom extents on every layout. :-P


















Sorry, couldn't resist -- way tired and not capable of much more than humor at present.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Marc'Antonio Alessi on September 08, 2017, 11:01:32 AM
... Using vbastmt on 64 bit might I have this problem?
https://www.theswamp.org/index.php?topic=43869.msg491387#msg491387

I'm running 2015 64 bit. Speedy here -- acknowledging one test environment does not make conclusive. Thanks for the heads up. PS -- there's more than one way to skin this cat ... to load code into vbe ... or ... :evil:
Brutal test on old PC with A2017+VBA:
Code: [Select]
(defun BenchVBA (Times)
  (setvar 'CMDECHO 0)
  (setq #BS (getvar "Millisecs"))
  (repeat Times (command "_.POINT" "_NONE" "0.0,0.0,0.0"))
  (print (strcat "Elapsed: "(rtos (* 0.001 (- (getvar "Millisecs") #BS)))))
  (setvar 'CMDECHO 1)
  (princ)
)
Code: [Select]
Command: (progn
(_> (BenchVBA 10000)
(_> (repeat 10 (BenchVBA 10000))
(_> )
"Elapsed: 16.5"
"Elapsed: 16.406"
"Elapsed: 16.437"
"Elapsed: 16.437"
"Elapsed: 16.437"
"Elapsed: 16.5"
"Elapsed: 16.532"
"Elapsed: 16.485"
Code: [Select]
With VBA loaded:
Command: (progn
(_> (BenchVBA 10000)
(_> (command "_.VBASTMT" "ThisDrawing.SetVariable \"UserS1\", \"Test\"")
(_> (repeat 10 (BenchVBA 10000))
(_> )
"Elapsed: 16.609" _.VBASTMT
Expression: ThisDrawing.SetVariable "UserS1", "Test"
Command:
"Elapsed: 21.625"
"Elapsed: 21.641"
"Elapsed: 21.594"
"Elapsed: 21.578"
"Elapsed: 22.14"
"Elapsed: 21.735"
"Elapsed: 21.687"
"Elapsed: 21.64"
"Elapsed: 21.579"
"Elapsed: 22"
speed reduction is appreciable but NOT progressive (not depending by the number of commands performed) as with VBA6.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Grrr1337 on September 08, 2017, 03:25:02 PM
Sorry, couldn't resist -- way tired and not capable of much more than humor at present.

No worries, your contribution to this forum is alot anyways.
I just liked the idea you mentioned, about creating and running temporary .dvb files thru lisp, so with my previous question I wanted to see is such technique 'worthy'. :)
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 08, 2017, 11:00:56 PM
No worries, your contribution to this forum is alot anyways.

Awesome comment -- thank you -- I will be worthy it some day.

I wanted to see is such technique 'worthy'. :)

Any automation that takes minutes to produce that increases efficiency, accuracy ... or reduces waste, errors ... ad infinitum is worthy.

And then some.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on September 08, 2017, 11:09:11 PM
Watch out... we might drain you with all our wacko ideas
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 08, 2017, 11:19:58 PM
I've observed a wide spectrum of ideas during my tenure here and few if any qualify as wacky. My only frustration is having nominal time to play and thus having to cherry pick one of every hundred or so challenges that are interesting yet can be solved quickly -- and of some benefit to the swamp community, me or those I serve -- ideally all three.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Marc'Antonio Alessi on September 10, 2017, 05:45:19 AM
I apologize if I insist but i am interested in knowing if AutoCAD + VB7 has solved the problems that had AutoCAD + VB6 / 64bit.
The second question is why in A2017 I get an error: INTERNAL ERROR: VL namespace mismatch?
Code: [Select]
(defun C:TVBA (/ NewDoc FlsLst)
  (vl-load-com)
  (or *AcadApp* (setq *AcadApp* (vlax-get-acad-object)))
  (or *AcAcDwg* (setq *AcAcDwg* (vla-get-ActiveDocument *AcadApp*)))
  (or *AcDrwgs* (setq *AcDrwgs* (vla-get-documents *AcadApp*)))
  (setq FlsLst
   '("Z:/Temp/Test/Dwg1.dwg"
     "Z:/Temp/Test/Dwg2.dwg"
     "Z:/Temp/Test/Dwg3.dwg"
     "Z:/Temp/Test/Dwg4.dwg"
    )
  )
  (close (open "Z:/Temp/Test/Report.txt" "w"))
  (foreach ForElm FlsLst ; Drawing list
    (if
      (vl-catch-all-error-p
         (setq NewDoc (vl-catch-all-apply 'vlax-invoke-method (list *AcDrwgs* 'Open ForElm :vlax-false)))
      )
      (progn
        (alert (vl-catch-all-error-message NewDoc))
        (setq NewDoc nil)
      )
      (vl-cmdf "_.VBASTMT" "documents.item(documents.count-1).sendcommand \"(load \"\"ExeTest\"\")\n\"")
    )
  );foreach
)


; File to load and run every dwg > ExeTest.lsp
(defun ExeTest (DwgNam / FilPtr)
  (setvar 'CMDECHO 0)
  (setq FilPtr (open "Z:/Temp/Test/Report.txt" "a"))
  (setq #BS (getvar "Millisecs"))
  (repeat 10000
    (command "_.POINT" "_NONE" "0.0,0.0,0.0") (command "_.ERASE" "_LAST" "")
    (command "_.LAYER" "_OFF" "*" "" "") (command "_.LAYER" "_ON" "*" "")
  )
  (write-line (strcat DwgNam "  Elapsed: "(rtos (* 0.001 (- (getvar "Millisecs") #BS)))) FilPtr)
  (setvar 'CMDECHO 1)
  (close FilPtr)
  (if (= (getvar "DBMOD") 0) (vl-cmdf "_.CLOSE") (vl-cmdf "_.CLOSE" "_Y"))
)
(ExeTest (getvar "DWGNAME"))

Results:
Code: [Select]
A2013
(ExeTest (getvar "DWGNAME")) > Drawing1.dwg  Elapsed: 8.86 > No VBA

(c:tvba):
Dwg1.dwg  Elapsed: 92.032
Dwg2.dwg  Elapsed: 97.109
Dwg3.dwg  Elapsed: 92.187
Dwg4.dwg  Elapsed: 89.125



A2017
(ExeTest (getvar "DWGNAME")) > Drawing1.dwg  Elapsed: 8.688 > No VBA

with A2017
Command:_.VBASTMT Initializing VBA System...
Expression: documents.item(documents.count-1).sendcommand "(load ""ExeTest"")
"
Command: "\n*** INTERNAL ERROR: VL namespace mismatch\n"" type Y to reset: "y

Manual test
(vl-cmdf "_.VBASTMT" "documents.item(documents.count-1).sendcommand \"(load \"\"ExeTest\"\")\n\""):
Dwg1.dwg  Elapsed: 8.921
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 10, 2017, 09:35:34 AM
Sorry I missed your posts Marc and I'm just heading off to work. That said, and it's a wild assed blurry eyed haven't had coffee yet guess, try:

(setq
    *AcadApp* (vlax-get-acad-object)
    *AcAcDwg* (vla-get-ActiveDocument *AcadApp*)
    *AcDrwgs* (vla-get-documents *AcadApp*)
)


(I'm guessing) There's nominal speed gains to be realized by trying to leverage the values between calls as globals and it may be the source of the problem. As for the other question I cannot answer at present as I've only access to AutoCAD 2012 and 2015 currently.  Thanks for your valued investigations in this thread. Cheers.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Marc'Antonio Alessi on September 12, 2017, 08:03:06 AM
Sorry I missed your posts Marc and I'm just heading off to work. That said, and it's a wild assed blurry eyed haven't had coffee yet guess, try:

(setq
    *AcadApp* (vlax-get-acad-object)
    *AcAcDwg* (vla-get-ActiveDocument *AcadApp*)
    *AcDrwgs* (vla-get-documents *AcadApp*)
)


(I'm guessing) There's nominal speed gains to be realized by trying to leverage the values between calls as globals and it may be the source of the problem. As for the other question I cannot answer at present as I've only access to AutoCAD 2012 and 2015 currently.  Thanks for your valued investigations in this thread. Cheers.
The problem is not due to this (the function only run once).

VBA 7 is for >= A2014 see:
https://knowledge.autodesk.com/support/autocad/learn-explore/caas/sfdcarticles/sfdcarticles/Unable-to-download-VBA-Enabler-for-previous-versions-of-AutoCAD.html

Try this simple test (A2013 and VBA 6):

Code: [Select]
Comando: (progn
(_>   (setvar 'CMDECHO 0)
(_>   (setq #BS (getvar "Millisecs"))
(_>   (repeat 10000
((_>     (command "_.POINT" "_NONE" "0.0,0.0,0.0") (command "_.ERASE" "_LAST" "")
((_>     (command "_.LAYER" "_OFF" "*" "" "") (command "_.LAYER" "_ON" "*" "")
((_>   )
(_>   (print (strcat "Elapsed: "(rtos (* 0.001 (- (getvar "Millisecs") #BS)))))
(_>   (setvar 'CMDECHO 1)
(_>   (princ)
(_> )

"Elapsed: 9.88"

Comando: VBASTMT
Inizializzazione del sistema VBA in corso...
Espressione: *Annulla*

Comando: (progn
(_>   (setvar 'CMDECHO 0)
(_>   (setq #BS (getvar "Millisecs"))
(_>   (repeat 10000
((_>     (command "_.POINT" "_NONE" "0.0,0.0,0.0") (command "_.ERASE" "_LAST" "")
((_>     (command "_.LAYER" "_OFF" "*" "" "") (command "_.LAYER" "_ON" "*" "")
((_>   )
(_>   (print (strcat "Elapsed: "(rtos (* 0.001 (- (getvar "Millisecs") #BS)))))
(_>   (setvar 'CMDECHO 1)
(_>   (princ)
(_> )

"Elapsed: 20.69"

With VBA 7 the difference after the load of VBA is lower but I can not use:
(vl-cmdf "_.VBASTMT" "documents.item(documents.count-1).sendcommand \"(load \"\"ExeTest\"\")\n\"")
due to error:
Command:_.VBASTMT Initializing VBA System...
Expression: documents.item(documents.count-1).sendcommand "(load ""ExeTest"")
"
Command: "\n*** INTERNAL ERROR: VL namespace mismatch\n"" type Y to reset: "y

Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 12, 2017, 08:41:58 AM
That performance hit is unfortunate if it cannot be resolved. Guess it means one has to be judicious in VB's use -- sometimes the performance hit is will be worth it when VB hosted automation yields big benefits, dumping the session when the tasks are complete.

I believe you're getting the VL namespace mismatch error because you're trying to load lisp into a document that is not active.

Said another way, there's no guarantee documents.item(documents.count-1) will return the active document.

Have you tried:

Command: _.VBASTMT
Command: activedocument.sendcommand "(load ""ExeTest"") "


Cheers.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 12, 2017, 09:04:20 AM
On the performance hit -- if you unload all the vba projects (this can be done programmatically) performance returns to normal even tho the vba editor is still alive. Cheers.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Marc'Antonio Alessi on September 12, 2017, 04:06:18 PM
On the performance hit -- if you unload all the vba projects (this can be done programmatically) performance returns to normal even tho the vba editor is still alive. Cheers.
Maybe I can not explain well (my english is not very good) ...
There is no VBA project loaded only: Command:_.VBASTMT Initializing VBA System...
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Marc'Antonio Alessi on September 12, 2017, 04:24:27 PM
That performance hit is unfortunate if it cannot be resolved. Guess it means one has to be judicious in VB's use -- sometimes the performance hit is will be worth it when VB hosted automation yields big benefits, dumping the session when the tasks are complete.

I believe you're getting the VL namespace mismatch error because you're trying to load lisp into a document that is not active.

Said another way, there's no guarantee documents.item(documents.count-1) will return the active document.

Have you tried:

Command: _.VBASTMT
Command: activedocument.sendcommand "(load ""ExeTest"") "


Cheers.
I used this method for over 10 years with VBA 32bit up to 64bit VBA arrival to avoid writing scripts and having a much better error control.

Comando: (setq
(_>       *AcadApp* (vlax-get-Acad-Object)
(_>       *AcDrwgs* (vla-get-documents      *AcadApp*)
(_>       *AcAcDwg* (vla-get-ActiveDocument *AcadApp*)
(_> )
#<VLA-OBJECT IAcadDocument 000000002d8363c8>  <<< ACTIVE DOCUMENT

Comando: (setq DwgNum (vlax-get-property *AcDrwgs* 'Count))
1

Comando: (vla-item *AcDrwgs* (1- DwgNum))
#<VLA-OBJECT IAcadDocument 000000002d8363c8>  <<< ACTIVE DOCUMENT

When I open with:
Comando: (setq NewDoc (vl-catch-all-apply 'vlax-invoke-method (list *AcDrwgs* 'Open "Z:/Temp/Test/Dwg1.dwg" :vlax-false)))
#<VLA-OBJECT IAcadDocument 00000000329439c8>

then it does not give the active document:

Comando: (setq DwgNum (vlax-get-property *AcDrwgs* 'Count))
2

Comando: (vla-item *AcDrwgs* (1- DwgNum))
#<VLA-OBJECT IAcadDocument 00000000329439c8> <<< NEW DOCUMENT

It works in 2013 not in 2017.

%%
(vl-cmdf "_.VBASTMT" "documents.item(documents.count-1).sendcommand \"(load \"\"ExeTest\"\")\n\"")

Run in the NEW DOCUMENT and when closed return back to the ACTIVE DOCUMENT.
With this I can do anything in the new document and in case of error the control returns to the starting document.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on September 12, 2017, 06:18:05 PM
Marc my friend -- my brief observations with 2015 suggest documents.item(documents.count-1) is not reliable -- in particular if one has cycled to other drawings via [ctrl][tab] or [ctrl][shift][tab] or via the pull-down drawing selector after initial file loading --  whereas activedocument has proven consistent and reliable -- and why I shared in the hopes of helping you.

Did you try --

Command: _.VBASTMT
Command: activedocument.sendcommand "(load ""ExeTest"") "


I don't have 2017 at my disposal so I cannot test.

Off to check out some yoga pants ...
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Marc'Antonio Alessi on September 13, 2017, 04:15:54 PM
Marc my friend -- my brief observations with 2015 suggest documents.item(documents.count-1) is not reliable -- in particular if one has cycled to other drawings via [ctrl][tab] or [ctrl][shift][tab] or via the pull-down drawing selector after initial file loading --  whereas activedocument has proven consistent and reliable -- and why I shared in the hopes of helping you.

Did you try --

Command: _.VBASTMT
Command: activedocument.sendcommand "(load ""ExeTest"") "


I don't have 2017 at my disposal so I cannot test.

Off to check out some yoga pants ...
Michael, -- Command: activedocument.sendcommand "(load ""ExeTest"") " load ExeTest.lsp in the starting document not in the new document just opened (that is what I want to do) maybe this can explain:
Code: [Select]
; TVBA.lsp
(defun C:TVBA (/ NewDoc FlsLst FilPtr)
  (vl-load-com) (defun errdump (s) (vl-bt) (princ)) (setq *error* errdump)
  (setq FlsLst
   '("Z:/Temp/Test/Dwg1.dwg"
     "Z:/Temp/Test/Dwg2.dwg"
     "Z:/Temp/Test/Dwg3.dwg"
     "Z:/Temp/Test/Dwg4.dwg"
    )
  )
  (close (open "Z:/Temp/Test/Report.txt" "w"))
  (foreach ForElm FlsLst ; Drawing list
    (if
      (vl-catch-all-error-p
         (setq NewDoc (vl-catch-all-apply 'vlax-invoke-method (list (vla-get-documents (vlax-get-acad-object)) 'Open ForElm :vlax-false)))
      )
      (progn
        (alert (vl-catch-all-error-message NewDoc))
        (setq NewDoc nil)
      )
      (progn
        (setq FilPtr (open "Z:/Temp/Test/Report.txt" "a"))
        (princ "\nActive document BEFORE of VBASTMT: " FilPtr) (princ (vla-get-ActiveDocument (vlax-get-acad-object)) FilPtr)
        (close FilPtr)
        (vl-cmdf "_.VBASTMT" "documents.item(documents.count-1).sendcommand \"(load \"\"ExeTest\"\")\n\"")
        (setq FilPtr (open "Z:/Temp/Test/Report.txt" "a"))
        (princ "\nActive document AFTER  of VBASTMT: " FilPtr)
        (princ (vla-get-ActiveDocument (vlax-get-acad-object)) FilPtr)
        (princ "\n\n\n" FilPtr)
        (close FilPtr)
      )
    )
  );foreach
)
Code: [Select]
; ExeTest.lsp
(defun ExeTest (DwgNam / FilPtr)
  (setvar 'CMDECHO 0)
  (setq FilPtr (open "Z:/Temp/Test/Report.txt" "a"))
  (setq #BS (getvar "Millisecs"))
  (repeat 10
    (command "_.POINT" "_NONE" "0.0,0.0,0.0") (command "_.ERASE" "_LAST" "")
    (command "_.LAYER" "_OFF" "*" "" "") (command "_.LAYER" "_ON" "*" "")
  )
  (princ (strcat "\n" DwgNam "  Elapsed: "(rtos (* 0.001 (- (getvar "Millisecs") #BS)))) FilPtr)
  (setvar 'CMDECHO 1)
  (princ "\nActive document INSIDE of VBASTMT: " FilPtr) (prin1 (vla-get-ActiveDocument (vlax-get-acad-object)) FilPtr)
  (close FilPtr)
  (vl-cmdf "_.CLOSE" "_Y")
)
(ExeTest (getvar "DWGNAME"))

After load TVBA.lsp the command: TVBA make this report:
Code: [Select]
Active document BEFORE of VBASTMT: #<VLA-OBJECT IAcadDocument 000000002cc78568>
Dwg1.dwg  Elapsed: 0.078
Active document INSIDE of VBASTMT: #<VLA-OBJECT IAcadDocument 0000000032ef0298>
Active document AFTER  of VBASTMT: #<VLA-OBJECT IAcadDocument 000000002cc78568>



Active document BEFORE of VBASTMT: #<VLA-OBJECT IAcadDocument 000000002cc78568>
Dwg2.dwg  Elapsed: 0.093
Active document INSIDE of VBASTMT: #<VLA-OBJECT IAcadDocument 0000000032ef0bb8>
Active document AFTER  of VBASTMT: #<VLA-OBJECT IAcadDocument 000000002cc78568>



Active document BEFORE of VBASTMT: #<VLA-OBJECT IAcadDocument 000000002cc78568>
Dwg3.dwg  Elapsed: 0.078
Active document INSIDE of VBASTMT: #<VLA-OBJECT IAcadDocument 0000000032ef0728>
Active document AFTER  of VBASTMT: #<VLA-OBJECT IAcadDocument 000000002cc78568>



Active document BEFORE of VBASTMT: #<VLA-OBJECT IAcadDocument 000000002cc78568>
Dwg4.dwg  Elapsed: 0.078
Active document INSIDE of VBASTMT: #<VLA-OBJECT IAcadDocument 0000000032ef0298>
Active document AFTER  of VBASTMT: #<VLA-OBJECT IAcadDocument 000000002cc78568>

The Active document change only inside VBASTMT, when (vl-cmdf "_.CLOSE" "_Y") the Active document return the starting document.

In AutoCAD 2017 (and I think >=2014, see link below) I get the error: *** INTERNAL ERROR: VL namespace mismatch
maybe is due to this:
http://through-the-interface.typepad.com/through_the_interface/2014/03/autocad-2015-calling-commands.html
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on February 05, 2018, 11:31:49 AM
This "challenge" can also be addressed using the RunAll (http://www.theswamp.org/index.php?topic=53912.0) utility.

For example:

Code: [Select]
(defun-q c:ZEAL ( / app doc restore )

    ;;  Zoom Extents All Layouts

    (vl-load-com)
   
    (setq
        app     (vlax-get-acad-object)
        doc     (vla-get-activedocument app)
        restore (vla-get-activelayout doc)
    )
   
    (vlax-for layout (vla-get-layouts doc)
        (vla-put-activelayout doc layout)
        (if (or (eq 1 (getvar 'tilemode)) (eq 1 (getvar 'cvport)))
            (vla-zoomextents app)
            (progn
                (vla-put-mspace doc :vlax-false)
                (vla-zoomextents app)
                (vla-put-mspace doc :vlax-true)
            )
        )
    )
   
    (vla-put-activelayout doc restore)
   
    (princ)

)

(vl-propagate 'c:ZEAL)

(_RunAll "(c:ZEAL)")


Normally I'd just add c:ZEAL's (defun) definition to my standard library and thus it would already be available. I used the defun-q option to demonstrate the fast propagation technique.

FWIW, Cheers.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on February 05, 2018, 01:01:14 PM
I was thinking something like this . . . with you routine... ;)
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on February 05, 2018, 01:21:23 PM
I was thinking something like this . . . with you routine... ;)

(http://vignette.wikia.nocookie.net/sailormoon/images/8/8d/Burns-excellent.gif)
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: Marc'Antonio Alessi on February 06, 2018, 02:32:20 AM
I hope mine is a coherent question, is it possible to use your function _RunAll on many files without opening them all together? For example, to perform operations on hundreds of files and avoid the use of scripts?
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on February 06, 2018, 09:39:58 AM
My lazy cheat at the moment is to use the acaddoc.lsp. Its just a pain to remember to change it back to default once I am done running what ever commands I need. (I kinda use it as a script) lol
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on February 06, 2018, 10:18:54 AM
I hope mine is a coherent question, is it possible to use your function _RunAll on many files without opening them all together? For example, to perform operations on hundreds of files and avoid the use of scripts?

The intention of _RunAll is to run an arbitrary command string in every drawing currently open.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on February 06, 2018, 10:21:20 AM
I do have a question. In the Express Tools \ File Tools \ Save All Drawings - Close All Drawings.

How do those lisp routines work? They go through all open drawings... Just curious.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MP on February 06, 2018, 11:24:49 AM
They iterate the docs collection and then invoke the close or save method as applicable for the desired action and their state etc.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: MSTG007 on February 06, 2018, 11:28:30 AM
It basically does what yours does except with a heck of a lot more capability.

If I have a dozen drawings open... (Similar to the express tools)

regen all drawings
set imageframe to 0 in all drawings
etc...

watchout when are able to record what you want to do to all opened drawings lol.

again... pretty sweet stuff here.
Title: Re: Zoom Extend All Layouts in All Open Drawings?
Post by: sumitkr1108 on July 28, 2023, 12:26:49 AM
can somebody add save command also in the below code

Code - Auto/Visual Lisp: [Select]
  1. ;Zoom Extents *ALL* Document and layouts
  2. (defun c:ZoomExtAllDocsLayouts ( / acad acdoc aclay doc lout )
  3.  
  4.  
  5.   (setq acdoc (vla-get-ActiveDocument acad))
  6.   (setq aclay (vla-get-ActiveLayout acdoc))
  7.   (vla-ZoomExtents acad)
  8.   (vlax-for lout (vla-get-Layouts acdoc)
  9.     (if (not (equal lout aclay))
  10.       (progn
  11.         (vla-put-ActiveLayout acdoc lout)
  12.         (vla-ZoomExtents acad)
  13.       )
  14.     )
  15.   )
  16.   (vla-put-ActiveLayout acdoc aclay)
  17.     (if (not (equal doc acdoc))
  18.       (progn
  19.         (vla-put-ActiveDocument acad doc)
  20.         (setq aclay (vla-get-ActiveLayout doc))
  21.         (vla-ZoomExtents acad)
  22.         ;;;(vla-addline (vla-get-block aclay) (vlax-3d-point '(0 0 0)) (vlax-3d-point '(1 0 0)))
  23.         (vlax-for lout (vla-get-Layouts doc)
  24.           (if (not (equal lout aclay))
  25.             (progn
  26.               (vla-put-ActiveLayout doc lout)
  27.               (vla-ZoomExtents acad)
  28.             )
  29.           )
  30.         )
  31.       )
  32.     )
  33.   )
  34.   (vla-put-ActiveDocument acad acdoc)
  35.   (vla-Regen acdoc AcActiveViewport)
  36.   (alert "Zoom Extents has been applied to all Documents and layouts!")
  37.   (princ)
  38. )
  39.  

EDIT (John): Added code tags.