Author Topic: Execute commands on all open drawings  (Read 12663 times)

0 Members and 1 Guest are viewing this topic.

terrycadd

  • Guest
Execute commands on all open drawings
« on: February 05, 2007, 02:45:32 PM »
I’m trying to find a way to execute a few commands on every open drawing. Such as “pspace”, “zoom e”, and “qsave”.  Or the commands may be different per the users or programmers requirements.  Here are two functions that I was hoping would help get me started, GetDwgsList and ActivateDwg.  The problem I’ve encountered is that once ActivateDwg changes to another open drawing, no further AutoLISP command are executed.  I’ve heard that AutoCAD 2007 has something similar as in saving all open drawings, but it’s going to be a long wait before we get 2007.  Also it would be great to have it execute any custom set of functions also.  Here is what I’ve got to start from.
Code: [Select]
;-------------------------------------------------------------------------------
; GetDwgsList - Returns a list of open drawings
; Use (length (GetDwgsList)) for the number of open drawings.
;-------------------------------------------------------------------------------
 (defun GetDwgsList (/ AcadOBJ DocsOBJ DwgsList@)
  (if (>= (atoi (getvar "ACADVER")) 15)
    (progn
      (setq AcadOBJ (vlax-get-acad-object)
            DocsOBJ (vlax-get-property AcadOBJ "Documents")
            DwgsList@ nil
      );setq
      (vlax-for ForItem DocsOBJ
        (setq DwgsList@ (cons (strcat (vlax-get-property ForItem "Path") "\\"
        (vlax-get-property ForItem "Name")) DwgsList@))
      );vlax-for
      (setq DwgsList@ (reverse DwgsList@))
    );progn
    (setq DwgsList@ (list (strcat (getvar "DWGPREFIX") (getvar "DWGNAME"))))
  );if
  DwgsList@
);defun GetDwgsList
;-------------------------------------------------------------------------------
; ActivateDwg - Switches between open drawings
; Arguments: 1
;   DwgPathFilename$ = Path and filename string of drawing
; Returns: Makes the drawing of DwgPathFilename$ current
;-------------------------------------------------------------------------------
(defun ActivateDwg (DwgPathFilename$ / Cnt# Num#)
  (setq Cnt# 0 Num# 0)
  (foreach Item (GetDwgsList)
    (if (= (strcase DwgPathFilename$) (strcase Item))
      (setq Num# Cnt#)
    );if
    (setq Cnt# (1+ Cnt#))
  );foreach
  (command "vbastmt" (strcat "Application.Documents.Item(" (itoa Num#) ").Activate"))
  (princ)
);defun ActivateDwg

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Execute commands on all open drawings
« Reply #1 on: February 05, 2007, 03:14:22 PM »
Not going to happen with lisp.  Unless you can use a reactor to execute when you switch it from one drawing to the next, and then code in the reactor to switch to the next one until it reaches the end.
Tim

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

Please think about donating if this post helped you.

terrycadd

  • Guest
Re: Execute commands on all open drawings
« Reply #2 on: February 05, 2007, 03:23:43 PM »
VBA code will be great.  Are there any examples around doing this?

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Execute commands on all open drawings
« Reply #3 on: February 05, 2007, 03:26:46 PM »
You might find it better to post to the VBA forumn then.  I know some of them look here also, but you might be a faster respond by just posting your questions there, as I really have no idea how to code in VBA.
Tim

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

Please think about donating if this post helped you.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Execute commands on all open drawings
« Reply #4 on: February 05, 2007, 04:05:13 PM »
Forgive me if I am wrong, but with VLisp you can work on ANY open document regardless of which document is current .. can't you?
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Execute commands on all open drawings
« Reply #5 on: February 05, 2007, 04:49:58 PM »
Forgive me if I am wrong, but with VLisp you can work on ANY open document regardless of which document is current .. can't you?

Care to explain ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Execute commands on all open drawings
« Reply #6 on: February 05, 2007, 04:55:13 PM »
Forgive me if I am wrong, but with VLisp you can work on ANY open document regardless of which document is current .. can't you?
Yes, but you can't do what is asked.  Here is what you can do with a document that isn't active in the editor.
Quote
Command: (dump dbxapp)
; IAxDbDocument: IAxDbDocument Interface
; Property values:
;   Application (RO) = Exception occurred
;   Blocks (RO) = #<VLA-OBJECT IAcadBlocks 0df4d834>
;   Database (RO) = #<VLA-OBJECT IAcadDatabase 0df48dd4>
;   Dictionaries (RO) = #<VLA-OBJECT IAcadDictionaries 0df4d4a4>
;   DimStyles (RO) = #<VLA-OBJECT IAcadDimStyles 0df4d284>
;   ElevationModelSpace = 0.0
;   ElevationPaperSpace = 0.0
;   FileDependencies (RO) = #<VLA-OBJECT IAcadFileDependencies 0df48d74>
;   Groups (RO) = #<VLA-OBJECT IAcadGroups 0df4fca4>
;   Layers (RO) = #<VLA-OBJECT IAcadLayers2 0df4f894>
;   Layouts (RO) = #<VLA-OBJECT IAcadLayouts 0df4f664>
;   Limits = (0.0 0.0 12.0 9.0)
;   Linetypes (RO) = #<VLA-OBJECT IAcadLineTypes 0df4f474>
;   ModelSpace (RO) = #<VLA-OBJECT IAcadModelSpace3 0df4f144>
;   Name = ""
;   PaperSpace (RO) = #<VLA-OBJECT IAcadPaperSpace3 0df4d744>
;   PlotConfigurations (RO) = #<VLA-OBJECT IAcadPlotConfigurations 0df4ee64>
;   Preferences (RO) = #<VLA-OBJECT IAcadDatabasePreferences 0df48da4>
;   RegisteredApplications (RO) = #<VLA-OBJECT IAcadRegisteredApplications 0df4e7c4>
;   SummaryInfo (RO) = #<VLA-OBJECT IAcadSummaryInfo 0df4c884>
;   TextStyles (RO) = #<VLA-OBJECT IAcadTextStyles 0df4e474>
;   UserCoordinateSystems (RO) = #<VLA-OBJECT IAcadUCSs 0df4e424>
;   Viewports (RO) = #<VLA-OBJECT IAcadViewports 0df4e774>
;   Views (RO) = #<VLA-OBJECT IAcadViews 0df4c264>
; Methods supported:
;   CopyObjects (3)
;   DxfIn (2)
;   DxfOut (3)
;   HandleToObject (1)
;   ObjectIdToObject (1)
;   Open (2)
;   Save ()
;   SaveAs (2)
Tim

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

Please think about donating if this post helped you.

Guest

  • Guest
Re: Execute commands on all open drawings
« Reply #7 on: February 05, 2007, 05:05:31 PM »
This should work for you.

Code: [Select]
Option Explicit

Public Const AppName = "VBA Thing-a-ma-jig"

Public Sub Main()
    Dim oDwg As AcadDocument
    Dim oAcad As AcadApplication
    Dim iDwgCnt As Integer
   
    Set oAcad = AcadApplication.Application
   
    iDwgCnt = 0
   
    For Each oDwg In oAcad.Documents
        oAcad.Documents.Item(iDwgCnt).Activate
        oDwg.ActiveSpace = acPaperSpace
        oDwg.Application.ZoomExtents
        oDwg.Save
       
        iDwgCnt = iDwgCnt + 1
    Next oDwg
   
    MsgBox "Done!", vbInformation + vbOKOnly, AppName
End Sub

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Execute commands on all open drawings
« Reply #8 on: February 05, 2007, 05:21:42 PM »
I was thinking of something along the lines of

Code: [Select]
(defun c:alldocs ()
 (setq app (vlax-get-acad-object))
 (setq docs (vlax-get-property app 'Documents))
 (vlax-for doc docs
       (do_something doc)
 )
 (vlax-release-object docs)
 (vlax-release-object app)
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Execute commands on all open drawings
« Reply #9 on: February 05, 2007, 05:40:47 PM »
I was thinking of something along the lines of

Code: [Select]
(defun c:alldocs ()
 (setq app (vlax-get-acad-object))
 (setq docs (vlax-get-property app 'Documents))
 (vlax-for doc docs
       (do_something doc)
 )
 (vlax-release-object docs)
 (vlax-release-object app)
)
Still no zoom method
Quote
; Methods supported:
;   Activate ()
;   AuditInfo (1)
;   Close (2)
;   CopyObjects (3)
;   EndUndoMark ()
;   Export (3)
;   GetVariable (1)
;   HandleToObject (1)
;   Import (3)
;   LoadShapeFile (1)
;   New (1)
;   ObjectIdToObject (1)
;   Open (2)
;   PurgeAll ()
;   Regen (1)
;   Save ()
;   SaveAs (3)
;   SendCommand (1)
;   SetVariable (2)
;   StartUndoMark ()
;   Wblock (2)
But it does look like you might be able to change the active layout.
Quote
;   ActiveLayout = #<VLA-OBJECT IAcadLayout 1beb2ae4>
Tim

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

Please think about donating if this post helped you.

terrycadd

  • Guest
Re: Execute commands on all open drawings
« Reply #10 on: February 05, 2007, 06:06:55 PM »
Matt W.
In your code is it possible to pass arguments of commands or functions, so anybody can customize it as they need to? Or are we limited to editing VBA code per each change we want to make?
TC

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Execute commands on all open drawings
« Reply #11 on: February 05, 2007, 09:48:16 PM »
Still no zoom method

You are quite correct, however that is not necessarily a fault of the software. If you understand that zooming in a drawing requires the calculation of the "visible" attributes of the objects in the drawing, it will be easy to understand.

Now there IS a way you can do it by simply toggling through each drawing in succession invoking the zoom method, which is the only way to do a zoom, however, you CAN cache a zoom command to be executed as soon as the drawing is made active by using

Code: [Select]
(vla-sendcommand doc (strcat "zoom" (chr 13) "e" (chr 13)))

Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

dan19936

  • Guest
Re: Execute commands on all open drawings
« Reply #12 on: February 06, 2007, 01:10:24 AM »
Can't you zoom with points? Zoom > Window > 0,0 > 11,17

FengK

  • Guest
Re: Execute commands on all open drawings
« Reply #13 on: February 06, 2007, 01:59:03 AM »
I think with lisp, you can use SendCommand to excute (lisp) commands in non-active documents.

Chuck Gabriel

  • Guest
Re: Execute commands on all open drawings
« Reply #14 on: February 06, 2007, 07:55:43 AM »
Terry,

Would it be an acceptable alternative to have a program open each drawing in succession and perform a given set of tasks on each one instead of having the program performs the tasks on all open drawings simultaneously?