Automatically Create Links Between Modules

dxltools25This script automatically creates links from the current module to a selected target module. If you do not have a linkset pairing set up then the links are created in the DOORS Links module. Text objects, not headings are linked. This is helpful in creatign test data.

//  Create links from the current module to a selected module

/*
    Automatically creates links to a selected target module.
    Links are created in the DOORS Links module.

    Tony Goodman.
*/

//Additional DB
DB  addDB      = null
DBE srcFld 	   = null
DBE trgFld     = null
DBE trgBtn     = null
DBE linkChoice = null
DBE lnkBtn     = null

Folder currFolder = null
Module currModule = null

/************************************
	doBrowseTarget

************************************/
void doBrowseTarget(DBE dbe)
{
    string szModuleName = fnMiniExplorer(addDB, currFolder, MINI_EXP_FORMAL_MODS, "Select Target Module", "Select Target Module")

    // ensure that mini explorer has returned a valid module name
    if (szModuleName != "" && module(szModuleName))
    {
        set(trgFld, szModuleName )
    }
}

/************************************
	doClose
************************************/
void doClose(DB db)
{
	release(addDB)
}

/************************************
	doCreateLinks

************************************/
void doCreateLinks(DB addDB)
{
	string lm       = ""
	string res      = ""
	Object src      = null
	Object tgt      = null
	Object rato     = null
	Module tgtMod   = null
	int    numSrc   = 0
	int    numTgt   = 0
	int    numLinks = 0
	int    s        = 0
	int    t        = 0
	int    i        = 0
	Skip   srcObjs  = create
	Skip   tgtObjs  = create

    string trg = get trgFld
    tgtMod = read(trg, true)

    // count source objects and add to skip list
	for src in currModule do
	{
		if (src."Object Text" "" != "")
		{
			for (i = 0; i < level(src); i++)
			{
				numSrc++
				put(srcObjs, numSrc, src)
			}
		}
	}

	numLinks = numSrc

	for tgt in tgtMod do
	{
		if (tgt."Object Text" "" != "")
		{
			for (i = 0; i < level(src); i++)
			{
				numTgt++
				put(tgtObjs, numTgt, tgt)
			}
		}
	}

	if (numTgt < (numSrc / 10))
	{
		if (!confirm("The target module contains " numTgt " objects only.\nAre you sure you want to continue?"))
		{
			return
		}
	}

	// create the links
	for (i = 0; i < numLinks; i++)
	{
		s = random(numSrc) + 1
		t = random(numTgt) + 1

		if (find(srcObjs, s, src) && find(tgtObjs, t, tgt))
		{
			src->"DOORS Links"->tgt
		}
	}

   	doClose(addDB)
}

/************************************
	createSomeLinks 

	CALLBACK for addButton
************************************/
void createSomeLinks()
{
	if (!isEdit(currModule))
	{
		infoBox("The module must be open for exclusive edit to run this.")
		return
	}

	addDB = create(currModule, "Create some links")

	srcFld = field(addDB,  "Source Module    ", name(currModule), 30, true)
    trgFld = field(addDB,  "Target Module    ", "", 30, true)
    beside addDB
    trgBtn = button(addDB, "Browse ...", doBrowseTarget, styleStandardSize)
    left addDB

    apply(addDB, "Create", doCreateLinks)
    close(addDB, true, doClose)

    realize addDB

    block(addDB)

    destroy(addDB)
    addDB = null

    refresh(currModule)
}

/************************************
	MAIN
************************************/
currFolder = current Folder
currModule = current Module

if (null currModule)
{
	infoBox("This can only be run from a formal module")
	halt
}

createSomeLinks()