Jump to content

Python, Clone Layout from another drawing


Recommended Posts

Posted (edited)
import traceback
from pyrx import Ap, Db, Ed, Ge


def validate_layout(source_db: Db.Database, target_db: Db.Database, layout_name: str):
    """Validates if the layout exists in source and is safe to write to target."""
    source_layout_dict = Db.Dictionary(source_db.layoutDictionaryId())
    if not source_layout_dict.has(layout_name):
        print(f"Layout '{layout_name}' not found in source drawing.")
        return False

    target_layout_dict = Db.Dictionary(target_db.layoutDictionaryId())
    if target_layout_dict.has(layout_name):
        print(f"Layout '{layout_name}' already exists in target drawing.")
        return False

    return True


def clone_layout_from_db(source_db: Db.Database, target_db: Db.Database, layout_name: str):
    """Clones a specified layout from a source database to a target database layout dictionary."""
    if not validate_layout(source_db, target_db, layout_name):
        return False

    try:
        source_layout_dict = Db.Dictionary(source_db.layoutDictionaryId())
        source_layout_id = source_layout_dict.getAt(layout_name)
        
        id_map = Db.IdMapping()
        id_map.setDestDb(target_db)
        
        source_db.wblockCloneObjects(
            [source_layout_id],
            target_db.layoutDictionaryId(),
            id_map,
            Db.DuplicateRecordCloning.kDrcIgnore,
        )
        return True
        
    except Exception as e:
        print(f"Error encountered during cloning: {e}")
        traceback.print_exc()
        return False


@Ap.Command()
def doit():
    layout_name = "S7"
    source_path = r"E:\Batch\06457 RE Submittal.dwg"

    target_db = Db.curDb()
    
    try:
        source_db = Db.Database.createFromDWG(source_path)
        
        if clone_layout_from_db(source_db, target_db, layout_name):
            print(f"Successfully cloned layout '{layout_name}'.")
            
            manager = Ap.LayoutManager()
            manager.updateLayoutTabs()
            
    except Exception as err:
        print(f"Failed to load or process source drawing: {err}")

 

layout.png.1550a0d2e2e2f76fdc3b4ca993d8df4e.png

Edited by Danielm103

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...