# Script to substitute the the Title Block property "PN" with a partno value according to 
# a list contained in a file "VarPNs.txt". The file is assumed to be in the same directory as the schematic.
# The list contains pairs of variant names and their respective part numbers. The last pair is the default, no variant.
# Use this file at your own discretion. Backup data before using.

package require Tcl 8.4

namespace eval ::VarUpdt {
}

proc ::VarUpdt::EXL_VariantPN_Update {  } {

	####################################################################################### Getting the current Capture session
	set lSession $::DboSession_s_pDboSession 
	DboSession -this $lSession
	set lStatus [DboState]

	####################################################################################### Get active design
	set lDesign [$lSession GetActiveDesign]
		set lCurrentVariant [GetActiveVariant]
		set lNullObj NULL
	############################################# Open file VarPNs.txt and read each pair of values of variant and partno
	# The first variant that matches the current design variant name stops the while loop and its partno is used.
	# If no variant matches, the last line in the file gives the default partno.
		
	set inVarPNfile [open "VarPNs.txt" r]
	while {[gets $inVarPNfile line] != -1} {
		set pair [split $line " "]
		set variantName [lindex $pair 0]
		set PartNo [lindex $pair 1]
	#puts "\nCurrent variant name = $lCurrentVariant"
	#puts "\nFile Variant Name = $variantName , PartNo = $PartNo"
		if {$variantName eq $lCurrentVariant} {
			break
		} elseif {$variantName eq $lNullObj} {
			break
			}
	}
	close $inVarPNfile

	####################################################################################### Iterating through all schematics of a design
	set lSchematicIter [$lDesign NewViewsIter $lStatus $::IterDefs_SCHEMATICS]
	#get the first schematic view
	set lView [$lSchematicIter NextView $lStatus]
	set lNullObj NULL

	while { $lView != $lNullObj} {
		#dynamic cast from DboView to DboSchematic
		set lSchematic [DboViewToDboSchematic $lView]
		#placeholder: do your processing on $lSchematic
		#get the next schematic view
	####################################################################################### Iterating through all pages of a schematic

	set lPagesIter [$lSchematic NewPagesIter $lStatus]
	#get the first page
	set lPage [$lPagesIter NextPage $lStatus]
	set lNullObj NULL
	while {$lPage!=$lNullObj} {
		#placeholder: do your processing on $lPage
		#get the next page
	####################################################################################### Iterating through all title-blocks in a page

	set lTitleBlocksIter [$lPage NewTitleBlocksIter $lStatus] 

	#get the first title block

	set lTitle [$lTitleBlocksIter NextTitleBlock $lStatus] 

	while {$lTitle!=$lNullObj} { 
	 #placeholder: do your processing on $lTitle
	#	set lPropNameCoreDesign [DboTclHelper_sMakeCString "PN_Var0"]
		set lPropNameDisplay [DboTclHelper_sMakeCString "PN"]
		set lPropValueDisplay [DboTclHelper_sMakeCString]
		set lCStr [DboTclHelper_sMakeCString]
	foreach lObj $lTitle {	
		$lObj GetTypeString $lCStr
		set lObjTypeStr [DboTclHelper_sGetConstCharPtr $lCStr]
		if { $lObjTypeStr == "Titleblock" } {
			$lObj GetEffectivePropStringValue $lPropNameDisplay $lPropValueDisplay
			set lCurrentValue [DboTclHelper_sGetConstCharPtr $lPropValueDisplay]
			DboTclHelper_sSetCString $lPropValueDisplay $PartNo
			$lObj SetEffectivePropStringValue $lPropNameDisplay $lPropValueDisplay
			}
	}
	 #get the next title block 
	 set lTitle [$lTitleBlocksIter NextTitleBlock $lStatus] 

	} 
	delete_DboPageTitleBlocksIter $lTitleBlocksIter

		set lPage [$lPagesIter NextPage $lStatus]
	}
	#Page Iter
	delete_DboSchematicPagesIter $lPagesIter
	# Stop after first schematic
	set lNullObj NULL 
	# Stop after first schematic
		set lView [$lSchematicIter NextView  $lStatus]
	} 
	#Schematic Iter
	delete_DboLibViewsIter $lSchematicIter

}

proc ::VarUpdt::VariantPropSetEnabler {args} {
    return true
}


RegisterAction "_cdnOrPreVariantViewMode" "::VarUpdt::VariantPropSetEnabler" "" "::VarUpdt::EXL_VariantPN_Update  ""