Set module as current

Greetings,
I am trying to create a tool for a user that needs to create several links between several modules. Since the user needs to take some time to look for the requirement, I proposed to open all modules that need to be linked and select the the objects to be linked in each module and run the script. For some reason, probably a dumb one, i cannot set up the target modules as current to get the absolute numbers and then copy them into the source module specified attributes.
I know that if I use the code below I can, but is like opening the module every time I run the script and I just need to reference and get the data from the specified module.

Module m = read("Target Module 1")
m = current
Object o = current
string absno = o."Absolute Number"
etc...

 


Code below is not working as expected, any help will be appreciated.

 

 

 

/* This program copy the selected objects attributes from one module to another.
It was created to copy the links absolute number of the module to then create links by attribute 
Include Outlinks and Inlinks */
 
 
//Several Target Modules
ModName_ m = module("Target Module 1")
Object oTarget1 = current
 
string linkAtt1 = oTarget1."Absolute Number"""
 
 
ModName_ m2 = module("Target Module 2")
Object oTarget2 = current
 
string linkAtt2 = oTarget2."Absolute Number"""
 
ModName_ m3 = module("Target Module 3")
Object oTarget3 = current
 
string linkAtt3 = oTarget."Absolute Number"""
 
 
 
//Source Module
 
ModName_ m4 = module("Source Module")
Object oSource
 
oSource."LinktoTarget1" = linkAtt1
oSource."LinktoTarget2" = linkAtt2
oSource."LinktoTarget3" = linkAtt3

 

 


SystemAdmin - Tue Jun 09 15:13:59 EDT 2009

Re: Set module as current
Tony_Goodman - Wed Jun 10 02:55:10 EDT 2009

To set the current module you need a Module type handle. This can only be obtained by opening the module using read/edit/share. Once a module is open, subsequent calls to read/edit/share do not open the moddule again, but do refresh the module handle.

When you open a module, the current object in that module is not set and may be null. You need to explicitly set the current object.

I am not quite sure what you are trying to do, but relying on "current" modules and objects is a very risky way of doing this. The current module can be changed by DOORS automatically opening other modules without you knowing.

Re: Set module as current
Peter_Albert - Wed Jun 10 03:40:00 EDT 2009

I agree with Tony that it is risky to rely on the current Module, and if I understand correctly what you have in mind, you actually do not have to, as you can use the 'Object current(Module m)' perm to get a handle on the selected object in each module.

If you create a small dialogue box script for the task, you can define Module type variables globally à la
 

Module targetMod1 = read("Target Module 1")

 


and in the callBack routines get handles on the current objects in each module using

 

Object oTarget1 = current(targetMod1)

 


Regards,

Peter

 

Re: Set module as current
llandale - Wed Jun 10 12:04:32 EDT 2009

Setting a module current is as follows:

current = mTarget   // mTarget is type 'Module'

 


Simple? You need to have module handles ahead of time to do that.

Here are some thoughts:
>>>The current module may change when you open other modules. I don't know the exact rules but am sure they have changed over time. The rules used to involve whether that other module is already open. Anyway, you need to bullet proof your code against these nebulous rules; and in any case this bullet-proofing will make your code MUCH easier to read later on down the road.

>>>For scripts that require an open module to run, you should routinely have code like this at the top of the script:

 

 

Module g_mCurr = current()
if (null mCurr or type(g_mCurr) != "Formal"){errorBox("Run from open formal module"); halt}


The rest of the code never changes variable g_mCurr and in fact may refer to it. In this case, 'current' really means 'Original'. In fact, scripts that run from a folder or project should have code like that, halting if there is indeed a current module.

>>>Most DXL commands don't care about the current module since you send the module handle to the command. Some commands have overloaded varients that presume it; for those commands you should routinely use the explicit Module version. Those commands that do indeed presume a current module (several attribute commands come to mind) pretty much require you to routinely set the current module first to be sure you are dealing with the right module. You may want to reset the current module after these commands:

 

 

Module mCurr = current()
mTarget = read("/MyProject/MyFolder/MyModule", false)
current = mTarget    // make sure we know which module is current
search for attributes in mTarget
if (!null mCurr) current = mCurr



>>>I haven't really though about it but I don't think the 'current' module matters when you are displaying it to the user. I think the 'refresh(mod)' command will set it to the front window, and it doesn't matter (I think) whether your DXL thinks its 'current'.

-Louie



 

Re: Set module as current
jhaury - Wed Aug 19 19:43:01 EDT 2009

I just discovered another fun tip on for this thread: I have a function that gets passed an Object. From there, I want the function to return a set of Attribute values, but first, I want to make sure those Attributes exist. As Louie said, many Attribute perms require the "current" Module to be set first. I count that the line "current = module(o)" does the job quite quickly. A slower alternative is "current Module = read(fullName(module(o))). That slower alternative seems to do a quick refresh on all the objects in the Module (btw, I do have the Module GUIs showing...I can't run my script in batch mode because I need View info!).

Hope this helps future coders!

Jason

Re: Set module as current
llandale - Thu Aug 20 10:51:07 EDT 2009

jhaury - Wed Aug 19 19:43:01 EDT 2009
I just discovered another fun tip on for this thread: I have a function that gets passed an Object. From there, I want the function to return a set of Attribute values, but first, I want to make sure those Attributes exist. As Louie said, many Attribute perms require the "current" Module to be set first. I count that the line "current = module(o)" does the job quite quickly. A slower alternative is "current Module = read(fullName(module(o))). That slower alternative seems to do a quick refresh on all the objects in the Module (btw, I do have the Module GUIs showing...I can't run my script in batch mode because I need View info!).

Hope this helps future coders!

Jason

But WAIT!!! If you change the current module inside some function, you may very well mess up some code in your main program that's presuming the current module, not knowing its changed. I hate it when that happens. In fact, I hated it when it DID happen; all 15 or so times...

This led me to a standard technique for sections of code that presume (or set) a current module.
Here's the library routine:

Module    fSetCurrMod(Module modNew)
{     // Set the "Current" module, returning the previous current
        // module.  You cannot "clear" current by passing null. 
        Module  modPrev = current
        if (!null modNew and modNew != modPrev)
          (current ModuleRef__) = modNew
        return(modPrev)
}     // end fSetCurrMod()


Here's how its used:

 

Module mCurrPrevious = fSetCurrMod(mod)
// deal with mod as the current module, perhaps looking at attrs.
fSetCurrMod(mCurrPrevious)


And of course like this:

 

 

Module mCurrPrevious = current()
// Do things that do or might change the current module, 
//     such as opening modules or using 'fnGetCPModulePath'.
fSetCurrMod(mCurrPrevious)


And then there's a sibling, which isn't anywhere near as useful:

 

 

//******************
Object    fSetCurrObj(Object objNew)
{     // set the "current" object, returning the previous current
        // object.  You cannot "clear" current by passing null.
        // The "current" module MAY need to be set to make this happen,
        // since you cannot set a current object in a non-current module.
 
        Object  objPrev = current
 
        if (null objNew) return(objPrev)
 
        Module  modNew  = module(objNew)        // Module houses the object
        if (null modNew) return(objPrev)        // ?? Impossible error ??
 
        fSetCurrMod(modNew)
        (current ObjectRef__) = objNew
        return(objPrev)
}     // end fSetCurrObj()

 

 

 

  • Louie