Topic
6 replies Latest Post - ‏2013-05-06T18:53:16Z by llandale
AlexanderMiles
AlexanderMiles
3 Posts
ACCEPTED ANSWER

Pinned topic How do you set the current Object or Module?

‏2013-05-03T20:21:33Z |

I am having trouble setting the current Object and Module. For Modules, the API manual states to set the current module as follows:

 

current = Module 
module

which requires me to have a reference to a module. However, when I try to set a Module like so

Module m = edit("Name of a module")
print null m

"true" is printed. Similarly, when following the instructions for setting an Object (current or otherwise) I have similar problems. What is the proper way to set Objects and Modules so I can read or edit their data?

Updated on 2013-05-06T17:32:47Z at 2013-05-06T17:32:47Z by AlexanderMiles
  • jester76
    jester76
    68 Posts
    ACCEPTED ANSWER

    Re: How do you set the current Object or Module?

    ‏2013-05-03T21:01:16Z  in response to AlexanderMiles

    Module m = edit("Name of a module")

    current = m

    same goes for objects

  • llandale
    llandale
    2809 Posts
    ACCEPTED ANSWER

    Re: How do you set the current Object or Module?

    ‏2013-05-05T18:02:19Z  in response to AlexanderMiles

    Your example has an "open" problem, not a "current" problem, "Name of Module" must be either

    • fully qualified ("/NameProject/NameFolder/NameModule"), or
    • the 'current' folder must be the one with the module (you can see the module in the explorer right pane) and "Name of Module" is unqualified ("NameModule").

    Yes, once you get a valid Module handle you can set it "current".  In fact, some commands presume the "current" module which obligates you to set it current to be sure the command deals with the correct module

    • current = m
    • delete(ad)

    Likewise you must get an "Object" handle in order to make it current; and you can only make an Object "current" in the "current" module.

    • Object o = find(m, 12)    // find object AbsNo #12 in "m"
    • current = m
    • current = o

    For scripts that must run from an open module it is highly advisable you get a handle on that module at the top of the script:

    • Module mHome = current
    • if (null mHome) {warningBox("Run from open module"); halt}

    -Louie

    • This reply was deleted by AlexanderMiles 2013-05-06T16:58:04Z.
    • AlexanderMiles
      AlexanderMiles
      3 Posts
      ACCEPTED ANSWER

      Re: How do you set the current Object or Module?

      ‏2013-05-06T17:15:54Z  in response to llandale

      This works, I can set the current Module or current Object with no issues now. However, I'm having unexpected behavior when trying to access the "Object Text" property of the current Object. If I run the following DXL code:

      Module m = edit("/path/to/Module", false)
      current = m
      current = object 2 // retrieve object with ID of 2 in the current module
      string text = current."Object Text"
      print "Object current \"Object Text\": " text

      I get an error setting the text variable as follows: "-R-E- DXL: <Line:4> null ExternalLink parameter was passed into argument position 1". But if I alter line 3 like so:

      Module m = edit("/path/to/Module", false)
      current = m
      o = object 2 // retrieve object with ID of 2 in the current module
      string text = o."Object Text"
      print "Object current \"Object Text\": " text

      I get no errors and the final line executes and shows the expected output. Why can't I access this property when it is set as the current Object? This Object is not set up with links of any type, and there are no links to/from this Module.

      • llandale
        llandale
        2809 Posts
        ACCEPTED ANSWER

        Re: How do you set the current Object or Module?

        ‏2013-05-06T17:51:22Z  in response to AlexanderMiles

        Since DXL allows "overloading" you need to be careful when you use function names.  In this case there are lots of functions called "current" and it behoves you to be more explicit; if for no other reason the next dude to read your code can figure it out.  Also, get in the habit of using variable Handles; use an Object handle just like using "m" for the Module handle:

        • Object oCurr = object(2)
        • current = oCurr    // You really don't need to do this; there are very few commands presuming a "current" Object
        • string Text
        • if (null oCurr) //
        • then Text = ""     // Object #2 either doesn't exist; or is not displayed and the "object(2)" command didn't find it -
        • else Text = oCurr."Object Text"

        -Louie

        • AlexanderMiles
          AlexanderMiles
          3 Posts
          ACCEPTED ANSWER

          Re: How do you set the current Object or Module?

          ‏2013-05-06T18:13:19Z  in response to llandale

          Alright, so I think I get it. "current" identifiers should be treated as a global variable which is shared in memory, and should not be directly accessed or operated on without giving it a unique handle (e.g. Object o = current Object).

           

          Did I get this right? I'm assuming DXL comes much more easily to people who are well versed in C, which I have never worked with.

          • llandale
            llandale
            2809 Posts
            ACCEPTED ANSWER

            Re: How do you set the current Object or Module?

            ‏2013-05-06T18:53:16Z  in response to AlexanderMiles

            I never had "C" either.  Worked Fortran Assembly Machine Code.

            Don't know about your "shared memory" statement.  I'm saying:

            • the "current" Module can be unpredictable.  When you open a module then that module becomes the "current" one, but only most of the time.  Also, there are times that modules are opened and you don't notice and this can change the current module.  Almost all DXL programmers have had these troubles.  A good way to avoid them is to make sure you get a Module Handle on every open module you care about, and set the "current" one when you need to; such as before commands that presume the "current" module (mostly "Attribute" commands).
            • the "current" Object is predictable but basically useless since few commands presume the "current" Object.  Likewise get an Object Handle on Objects you care about and used these Handles.
            • Setting the "current" Module or Object can lead to troubles later; in poorly written code that started before your script and then proceeds afterwards.  I generally reset the "Current" when I'm done, just in case:
              • Module mCurr = current
              • current = mod
              • do things presuming "current" module
              • if (!null mCurr) current = mCurr   // Reset
            • Using native functions such as "current" instead of Handles like "oCurr" can lead to trouble.  In your case, your original line #4:
              ......          string text = current."Object Text"
              that "current" resolves to a variable of "ExternalLink" not to "Object" which is why you got that error.  No, I could not predict it would resolve to "ExternalLink" but I see that it did.  This would have worked:
              ......          string text = (Object current)."Object Text"
              Even if it DID resolve correctly, it is still hard to read.  And, it may resolve correctly in this version of DOORS and resolve differently in the next.  Don't do that.  Use the Object Handle; it is clearer and more precise.
            • Scripts that must run from an open module should get the "current" module at the top of the script:
              • Module mHome = current
              • if (null mHome) {warningBox("run from open module"); halt}
            • Scripts that run from the Explorer might as well do a similar thing:
              • Module mHome = current 
              • if (! null mHome) {warningBox("Do not run from open module"); halt}
            • Some scripts only work on the "current" object; so might as well do the same thing:
              •  Object oHome = current
              • if (null oHome) {warningBox("No current Object"); halt}

            -Louie