Sheet Count Attribute across different dwg files

Sheet Count Attribute across different dwg files

renaldy_wibisonoSHQ64
Participant Participant
961 Views
13 Replies
Message 1 of 14

Sheet Count Attribute across different dwg files

renaldy_wibisonoSHQ64
Participant
Participant

Hello!

 

I achieved the totallayout count within a dwg file by using this code : 

 

(defun TotalSheetNumber (a r)
(setq totalLayouts (length (layoutlist)))
)
 
(vlr-command-reactor nil '((:vlr-commandWillStart . TotalSheetNumber)))
 
The Problem is, i just realised that i would have to count also sheets from other dwg drawings. 
I'm not sure if this would work, but i was thinking about if this LISP code will work if i set up a Sheet set which includes all the dwg documents

 

0 Likes
Accepted solutions (1)
962 Views
13 Replies
Replies (13)
Message 2 of 14

Moshe-A
Mentor
Mentor

@renaldy_wibisonoSHQ64  hi,

 

(layoutlist) function works only on the current active dwg

even if you work with SSM, at each time only 1 dwg is currently active.

 

So to get what you want, you will need hold all files open and iterate 

 

you install TotalSheetNumber function on (vlr-command-reactor) for :vlr-commandWillStart

do you really want it to run at each command invocation?

 

Moshe

 

 

0 Likes
Message 3 of 14

pbejse
Mentor
Mentor

@renaldy_wibisonoSHQ64 wrote:
The Problem is, i just realised that i would have to count also sheets from other dwg drawings

  • Opened drawings?
  • I take it its more than one layout tab per drawing?
  • Do you want this to include subfolder?

 

0 Likes
Message 4 of 14

renaldy_wibisonoSHQ64
Participant
Participant

so what should i do? 

 

for example i have 3 dwg files, 

A has 12 Layouts, B has 14 Layouts and C has 8 

so in each of those titleblocks in each layout, the total sheet number should read 34, across all files

 

0 Likes
Message 5 of 14

renaldy_wibisonoSHQ64
Participant
Participant

what is a subfolder?

0 Likes
Message 6 of 14

daniel_cadext
Advisor
Advisor

You could us an API with access to the layout manager, ARX, .NET pr Python

 

from pyrx_imp import Rx, Ge, Gi, Db, Ap, Ed
import traceback

def getLayoutCount(db):
    try:
        lman = Db.LayoutManager()
        token = lman.setupForLayouts(db)
        print("Num Layouts = {}: ", lman.countLayouts(db))
        print("Layouts info = {}: ", lman.getLayouts(db))
    finally:
        lman.clearSetupForLayouts(token)
        
def processDb(path):
    sideDb = Db.Database(False, True)
    sideDb.readDwgFile(path)
    sideDb.closeInput(True)
    getLayoutCount(sideDb)

def PyRxCmd_doit():
    try:
        path = "E:\\Source.dwg"
        processDb(path)
    except Exception as err:
        traceback.print_exception(err)

 

lman.png

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 7 of 14

Sea-Haven
Mentor
Mentor

Sub folder start at C:

Then C:\Cad dwg

Then C:\Cad dwg\projects

Then C:\Cad dwg\projects\2024-123

Then C:\Cad dwg\projects\2024-123\images

Then C:\Cad dwg\projects\2024-123\otherstuff

Then C:\Cad dwg\projects\2024-123\otherstuff\notes 4 subfolders !

 

 

0 Likes
Message 8 of 14

pbejse
Mentor
Mentor

@renaldy_wibisonoSHQ64 wrote:

what is a subfolder?


A folder contained within another folder.

 

pbejse_1-1720160565460.png

 

 

0 Likes
Message 9 of 14

pbejse
Mentor
Mentor
Accepted solution

@renaldy_wibisonoSHQ64 wrote:

so in each of those titleblocks in each layout, the total sheet number should read 34, across all files


(defun C:HML ( / totalLayouts dwgFiles odbx layoutOndwgFile); HowManyLayouts
(if (And
      (setq folder (acet-ui-pickdir
		   "Select Project Folder"
		   (if folder folder (getvar 'dwgprefix)
		   )
		 )
    )
      	(setq dwgFiles (vl-directory-files folder "*.dwg" 1 )))
  (progn
    
    (if (< (atoi (substr (getvar "ACADVER") 1 2)) 16)
	(setq odbx (vlax-get-or-create-object "ObjectDBX.AxDbDocument"))
	(setq odbx (vlax-get-or-create-object
		     (strcat "ObjectDBX.AxDbDocument."
			     (substr (getvar "ACADVER") 1 2)
		     )
		   )
	)
      )
  (foreach itm dwgFiles   
	(if  (not (vl-catch-all-error-p
			 (vl-Catch-All-Apply
			   '(lambda () (vla-open odbx (strcat folder "\\"itm))
			 )
		       )
		  )
	       )
	   (progn
	     (setq layoutOndwgFile (vla-get-count (vla-get-layouts odbx)))
	     (setq totalLayouts (cons (list itm (1- layoutOndwgFile )) totalLayouts))
	     (print (car totalLayouts))
	   )
	   (print (strcat "Odbx connection no available with file :\n"
			  (strcat folder "\\"itm)
		  )
	   )
	)
      )
     (vlax-release-object odbx)
     (print (strcat "Total number of layouts from " (itoa (length totalLayouts)) " drawing file(s): " (itoa (apply '+ (mapcar 'cadr totalLayouts))))) 
    )
  
  
  (print "\No drawing file(s) found")
  )
  (princ)
  )
Message 10 of 14

Sea-Haven
Mentor
Mentor

What about part 2 ? You have asked for total sheet number, but what about "Sheet X of Y" if you add or remove a layout from A are the individual sheet numbers updated ? dwgB needs +1 dwgC +1 and so on.

 

To work need the dwg's selected in an order of numbering. For me a dcl with add dwg number order. The excellent code by PBE could possibly be added to include sheet numbering. Something start with something like this.

SeaHaven_0-1720223196925.png

 

You could store the dwg names in all dwgs using LDATA. So in any dwg from a group update the sheet number and total sheets.

 

Just a comment we for 99% of the time only worked with one dwg, but does not Sheet set manager support sheet number ? We did not use it. 

 

0 Likes
Message 11 of 14

renaldy_wibisonoSHQ64
Participant
Participant

Thank you for the solution!

 

I would love to understand this code, if you have the time to explain this, i would very much appreciate it!

0 Likes
Message 12 of 14

pbejse
Mentor
Mentor

@renaldy_wibisonoSHQ64 wrote:

Thank you for the solution!


Did the solution worked and helped you with what you need @renaldy_wibisonoSHQ64 ?   then you know what to do next right?

 


@renaldy_wibisonoSHQ64 wrote:

I would love to understand this code, if you have the time to explain this, i would very much appreciate it!


I would love to, all you need now is to purchase a ticket, board a plane and fly out here so we can have a beer and at the same time i'll explain the code to you 🙂

 

0 Likes
Message 13 of 14

renaldy_wibisonoSHQ64
Participant
Participant

after studying this code ive come to somewhat of an understanding (or more like assumptions)- 

 

So the function is called C:HML (How Many Layouts)  with arguments 'totalLayouts' 'dwgFiles' 'obdx' and 'layoutOndwgFile'

 

>with the first (And   ) command, we get the directory that contains the project's dwg files "folder" and create a lists of all the dwg files "dwgFiles" of the poject.

> if the autocad version is earlier than version 16, we have to first set obdx to "ObjectDBX.AxDbDocument", "ACADVER"

> for each element in the 'dwgFiles' list, we ; 

     > first verify wether there is any errors while calling the individual files, 

     > then we return the value of the layout count for the files, and set this as 'LayoutOndwgFile'

     > then insert this value in the first element of the list 'totalLayouts'

> repeat for all files in 'dwgFiles' and therefore, 'totalLayouts' will be a list containing all the Layout counts of each file
> print the value (integer to array) the sum of all the elements of "totalLayouts' (?)

few questions : 
1) i dont understand this if loop in the setq command ; 

(setq folder (acet-ui-pickdir (if folder folder (getvar 'dwgprefix))))

so you set a variable folder, by first determining if the not-yet-defined variable folder has the dwgprefix?

 

2) in (setq dwgFiles (vl-directory-files folder ".dwg" 1)) , what is the 1 for? 

 

3)why do we have to add the ACADVER if the AutoCAD version is earlier than 16? i assume it has to do with the format but idk 

 

4) in line 31;

(setq totalLayouts (cons (list itm ( 1- LayoutOndwgFile )) totalLayouts))

a) itm is item? as in an element in a list?

b) you used a decrement of 1 in the LayoutOndwgFile - is this because the model is counted as a layout and therefore has to be substracted? 

c) again question about the syntax : you set 'totalLayouts' as a list of itself ( which in the first loop, should not have been defined yet) - is this because, it is a foreach loop and therefore, in the first iteration where it has not been defined yet, it will return as an empty list? and we fill it from there with the foreach loop?

 

5) in the line 41 ;
(print.....(itoa (apply '+ (mapcar 'cadr totalLayouts))

i thoght cadr returns the second element of the list TotalLayouts? i dont understand this line of code 

0 Likes
Message 14 of 14

pbejse
Mentor
Mentor

@renaldy_wibisonoSHQ64 wrote:

few questions : 
1) i dont understand this if loop in the setq command ; 

(setq folder (acet-ui-pickdir (if folder folder (getvar 'dwgprefix))))

so you set a variable folder, by first determining if the not-yet-defined variable folder has the dwgprefix?


DWGPREFIX is a system variable as you probably already know,  at initial run where variable folder is null , the program will use the value stored from that system variable DWGPREFIX as the path argument for the function acet-ui-pickdir

 

On the next run the value that was assigned to folder the first run will now be the default, as we did NOT declare that variable as local.  ( / totalLayouts dwgFiles odbx layoutOndwgFile).

 


@renaldy_wibisonoSHQ64 wrote:

2) in (setq dwgFiles (vl-directory-files folder ".dwg" 1)) , what is the 1 for? 


vl-directory-files (AutoLISP)  <--- 1-- List files only

 


@renaldy_wibisonoSHQ64 wrote:

3)why do we have to add the ACADVER if the AutoCAD version is earlier than 16? i assume it has to do with the format but idk 

Yes, you can say that. 

 


@renaldy_wibisonoSHQ64 wrote:

(setq totalLayouts (cons (list itm ( 1- LayoutOndwgFile )) totalLayouts))

a) itm is item? as in an element in a list?

itm is the Variable that each element in the list will be assigned to at every loop of foreach 

in this case, its the name of drawing currently being process

 


@renaldy_wibisonoSHQ64 wrote:

b) you used a decrement of 1 in the LayoutOndwgFile - is this because the model is counted as a layout and therefore has to be substracted? 


Correct

 


@renaldy_wibisonoSHQ64 wrote:

c) again question about the syntax : you set 'totalLayouts' as a list of itself ( which in the first loop, should not have been defined yet) - is this because, it is a foreach loop and therefore, in the first iteration where it has not been defined yet, it will return as an empty list? and we fill it from there with the foreach loop?

That is the beauty of using the function cons, <-- read about them 

 


@renaldy_wibisonoSHQ64 wrote:

5) in the line 41 ;
(print.....(itoa (apply '+ (mapcar 'cadr totalLayouts))

i thoght cadr returns the second element of the list TotalLayouts? i dont understand this line of code 


 

Read these and get back to us if you having a hard time understanding them

apply  / mapcar  / lambda 

 

 

 

0 Likes