• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Allegro X Scripting - TCL
  3. TCl script to replace selected parts with a component from...

Stats

  • State Verified Answer
  • Replies 16
  • Subscribers 14
  • Views 5248
  • Members are here 0
More Content

TCl script to replace selected parts with a component from library

soliman1991
soliman1991 over 1 year ago

Hello, 

I need to replace components in a schematic page with parts from the library, I tried DboPage_ReplaceInst, but the documentation is very vague.

Here is my attempt for reference: 

set selected [GetSelectedObjects]
puts $selected
set newPackageStr {CAP CER 0.1UF 10V X8M 0201} #that is the package name in the library
set lStatus [DboState]
set lSession $::DboSession_s_pDboSession
DboSession -this $lSession


set pLibName [file normalize {d:\random\Library.olb}]
set lLibName [DboTclHelper_sMakeCString $pLibName]
set lLib [$lSession GetLib $lLibName $lStatus]

set LibraryName [DboTclHelper_sMakeCString]

$lLib GetName $LibraryName
set newPackageName [DboTclHelper_sMakeCString newPackageStr]
set newPackage [$lLib GetPackage $newPackageName $lStatus]

foreach obj $selected {
    set device [$obj GetDevice $lStatus]
    DboPage_ReplaceInst $obj $newPackageName $newPackage $device $lStatus 0 0 1 1
}
  • Sign in to reply
  • Cancel
Parents
  • CadAP
    0 CadAP over 1 year ago

    Hello Soliman1991,

    Please use the below code to replace the selected part with a part from another library.

    set selected [GetSelectedObjects]
    # puts $selected
    set lPackage "C" ;#make sure the package name in the library.
    set lStatus [DboState]
    set lSession $::DboSession_s_pDboSession
    DboSession -this $lSession


    set pLibName [file normalize {d:\random\Library.olb}]

    foreach obj $selected {
    # set device [$obj GetDevice $lStatus]
    # $lPage ReplaceInst $obj $newPackageStr $lPart $device $lStatus 0 0 1 1
    ReplacePart "$pLibName" "$lPackage" "$lPackage.NORMAL" ""

    }

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • soliman1991
    0 soliman1991 over 1 year ago in reply to CadAP

    Unfortunately ReplacePart doesn't preserve RefDes, anyway around that?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • CadAP
    0 CadAP over 1 year ago in reply to soliman1991

    Hello Soliman1991,

    Please use the below code to replace the selected part with a part from another library with preserver refdef.

    set selected [GetSelectedObjects]
    # puts $selected
    set newPackageStr [DboTclHelper_sMakeCString "C"] ;#that is the package name in the library
    set lStatus [DboState]
    set lSession $::DboSession_s_pDboSession
    DboSession -this $lSession


    set pLibName [file normalize {d:\random\Library.olb}]

    foreach obj $selected {
    set device [$obj GetDevice $lStatus]
    set lPart [$obj GetPart $lStatus]
    set lPage [$obj GetOwner]
    $lPage ReplaceInst $obj $newPackageStr $lPart $device $lStatus 1 0 1 1
    # ReplacePart "$pLibName" "$lPackage" "$lPackage.NORMAL" ""

    }

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • soliman1991
    0 soliman1991 over 1 year ago in reply to CadAP

    Thanks again, this code changes the parts "Graphics" but it keep all other properties as the old part including value, part number, etc.. Also if I tried to move the part in the schematics, capture crashes. 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • CadAP
    0 CadAP over 1 year ago in reply to soliman1991

    Hi , 

    Please add the command "DboTclHelper_sEvalPage $lPage" after command "$lPage ReplaceInst $obj $newPackageStr $lPart $device $lStatus 1 0 1 1".

    set selected [GetSelectedObjects]
    # puts $selected
    set newPackageStr [DboTclHelper_sMakeCString "C"] ;#that is the package name in the library
    set lStatus [DboState]
    set lSession $::DboSession_s_pDboSession
    DboSession -this $lSession


    set pLibName [file normalize {d:\random\Library.olb}]

    foreach obj $selected {
    set device [$obj GetDevice $lStatus]
    set lPart [$obj GetPart $lStatus]
    set lPage [$obj GetOwner]
    $lPage ReplaceInst $obj $newPackageStr $lPart $device $lStatus 1 0 1 1

    DboTclHelper_sEvalPage $lPage


    # ReplacePart "$pLibName" "$lPackage" "$lPackage.NORMAL" ""

    }

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • soliman1991
    0 soliman1991 over 1 year ago in reply to CadAP

    This fixed the crash, but it preserved more than what it should. I only need to preserve refdes and update everything else.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • CadAP
    0 CadAP over 1 year ago in reply to soliman1991

    Hi soliman1991,

    Please use command with below arguments:

    $lPage ReplaceInst $obj $newPackageStr $lPart $device $lStatus 0 0 1 1

    This will preserve the refdes and rest values will be updated from the library.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • CadAP
    0 CadAP over 1 year ago in reply to soliman1991

    Hi soliman1991,

    Please use command with below arguments:

    $lPage ReplaceInst $obj $newPackageStr $lPart $device $lStatus 0 0 1 1

    This will preserve the refdes and rest values will be updated from the library.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • soliman1991
    0 soliman1991 over 1 year ago in reply to CadAP

    Hello! Thank you so much for your help. I tried the change suggested, and it still only change "Graphic" property, it still preserves all other properties.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • CadAP
    +1 CadAP over 1 year ago in reply to soliman1991

    HI soliman1991

    I believe you have take the dboLibPart and Dbodevice form the library from which you want to change it.

    Please try the below code just replace the location C:/Users/test/Desktop/generateOlb/LIBRARY2.OLB with your .olb location.

    At my end it changes all properties value except refdes.

    set selected [GetSelectedObjects]


    set package "C"
    set newPackageStr [DboTclHelper_sMakeCString "$package"] ;#that is the package name in the library
    set newPartStr "$package.Normal" ;#that is the package name in the library
    set lStatus [DboState]
    set lSession $::DboSession_s_pDboSession
    DboSession -this $lSession
    set pLibName [DboTclHelper_sMakeCString "[file normalize {C:/Users/test/Desktop/generateOlb/LIBRARY2.OLB}]"]

    set lLib [$lSession GetLib $pLibName $lStatus]
    set lnewpartIter [$lLib NewPartsIter $lStatus]

    set lPart [$lnewpartIter NextPart $lStatus]

    set nm [DboTclHelper_sMakeCString]
    while {$lPart != "NULL"} {

    set lDbopackg [$lPart GetPackagePtr]
    set lDevice [$lDbopackg GetDevice 0 $lStatus]
    # puts $lDevice
    # puts $lPart
    $lPart GetName $nm
    set lPartname [DboTclHelper_sGetConstCharPtr $nm]
    # puts [DboTclHelper_sGetConstCharPtr $nm]
    if {$lPartname == "$newPartStr"} {
    # puts "changing"
    foreach obj $selected {
    set lPage [$obj GetOwner]
    $lPage ReplaceInst $obj $newPackageStr $lPart $lDevice $lStatus 0 0 1 1
    DboTclHelper_sEvalPage $lPage

    }

    }
    set lPart [$lnewpartIter NextPart $lStatus]
    }

    delete_DboLibPartsIter $lnewpartIter

    Please try and let me know your fedback

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • soliman1991
    0 soliman1991 over 1 year ago in reply to CadAP

    This worked!! Thank you so much.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • E2202407295816
    0 E2202407295816 over 1 year ago in reply to CadAP

    CAD AP you are a GEM for this TCL

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Cadence Guidelines

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information