Jump to content

Batch, change dynamic property with Python


Danielm103

Recommended Posts

Posted (edited)

as a response to this thread

https://www.cadtutor.net/forum/topic/86222-script-to-change-status-stamp-block-across-300-drawings-please/#comment-642701

 

uses https://github.com/CEXT-Dan/PyRx

 

changes the drawings found in a folder

 

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

import wx
import os, pathlib

def findIdsByName(db, blkname):# -> list:
    ids = []
    allids = db.objectIds(Db.BlockReference.desc())
    for id in allids:
        ref = Db.BlockReference(id)
        if ref.getBlockName() == blkname:
            ids.append(id)
    return ids

def changeit(db : Db.Database, blkname):
    refids = findIdsByName(db, blkname)
    for id in refids:
        dyn = Db.DynBlockReference(id)
        if not dyn.isDynamicBlock():
            continue
        for prop in dyn.getBlockProperties():
            if prop.propertyName() != 'Visibility1':
                continue
            vals = dict([(p.getString(), p) for p in prop.getAllowedValues()])
            prop.setValue(vals['AS CONSTRUCTED'])
            
def openSideDrawing(path, blkname):
    print("\nProcessing {} ".format(path))
    db = Db.Database(False, True)
    db.readDwgFile(path)
    db.closeInput(True)
    changeit(db,blkname)
    db.saveAs(path)
            
def PyRxCmd_doit():
    try:
        blkname = 'PTA_Stamp_Dynamic'

        dlg = wx.DirDialog(None, "Choose input directory","",
                wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)
    
        if dlg.ShowModal() != wx.ID_OK:
            print("You Cancelled The Dialog!")
            return
        fnames = next(os.walk(dlg.GetPath()), (None, None, []))[2]
        dwgex = ".dwg".casefold()
    
        for fname in fnames:
            ext = pathlib.Path(fname).suffix.casefold()
            if ext != dwgex:
                continue
            fpath = '{}\\{}'.format(dlg.GetPath(),fname)
            openSideDrawing(fpath, blkname)
        print("yay")
    except Exception as err:
        traceback.print_exception(err)

 

Edited by Danielm103
context
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...