Author Topic: Zoom Extend All Layouts in All Open Drawings?  (Read 960 times)

0 Members and 1 Guest are viewing this topic.

Hrishikesh

  • Newt
  • Posts: 35
Zoom Extend All Layouts in All Open Drawings?
« 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..

Dommy2Hotty

  • Swamp Rat
  • Posts: 1102
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #1 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)

MSTG007

  • Water Moccasin
  • Posts: 2007
  • I can't remeber what I already asked! I need help!
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #2 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))
)
)
 
Autodesk Infrastructure Design Suite 2017

Hrishikesh

  • Newt
  • Posts: 35
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #3 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.

PKENEWELL

  • Newt
  • Posts: 158
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #4 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?
« Last Edit: August 22, 2017, 04:20:35 pm by PKENEWELL »
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

roy_043

  • Swamp Rat
  • Posts: 1464
  • BricsCAD 16
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #5 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)



Dommy2Hotty

  • Swamp Rat
  • Posts: 1102
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #6 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!}.

PKENEWELL

  • Newt
  • Posts: 158
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #7 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.
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

Hrishikesh

  • Newt
  • Posts: 35
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #8 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.

PKENEWELL

  • Newt
  • Posts: 158
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #9 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!
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

ribarm

  • Water Moccasin
  • Posts: 1677
  • Marko Ribar, architect
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #10 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.  
« Last Edit: September 06, 2017, 10:14:36 am by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

PKENEWELL

  • Newt
  • Posts: 158
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #11 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.
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

MSTG007

  • Water Moccasin
  • Posts: 2007
  • I can't remeber what I already asked! I need help!
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #12 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?
Autodesk Infrastructure Design Suite 2017

PKENEWELL

  • Newt
  • Posts: 158
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #13 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?
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

MSTG007

  • Water Moccasin
  • Posts: 2007
  • I can't remeber what I already asked! I need help!
Re: Zoom Extend All Layouts in All Open Drawings?
« Reply #14 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.
Autodesk Infrastructure Design Suite 2017