Session Objects
This section describes how to program on the session level using Creo.JS.
Overview of Session Objects
The Creo Parametric pfcSession object, contained in the class pfcSession, is the highest level object in Creo.JS. Any program that accesses data from Creo Parametric must first get a handle to the pfcSession object before accessing more specific data.
The pfcSession object contains methods to perform the following operations:
•  Accessing models and windows, described in the Models and Windows chapters.
•  Working with the Creo Parametric user interface.
•  Allowing interactive selection of items within the session.
•  Accessing global settings such as line styles, colors, and configuration options.
The following sections describe these operations.
Getting the Session Object
Methods Introduced:
  • pfcGetCurrentSessionWithCompatibility()
  • pfcGetCurrentSession()
  • For every application, Creo assigns a unique session. The session contains license information, the compatibility information as a pfcCreoCompatibility enumerated data type, and other additional data of the application. When a session is assigned to an application, Creo sets the compatibility to CompatibilityUndefined in the associated pfcAppInfo object. You must set the compatibility of the application before working with sessions. To set the compatibility, call the method pfcGetCurrentSessionWithCompatibility(). Use the values defined in the enumerated data type pfcCreoCompatibility to set the compatibility of the application.
    Use the method pfcGetCurrentSession() to get the current pfcSession object in synchronous mode. If the compatibility is not set, the method throws the exception pfcXCompatibilityNotSet.
    The method pfcGetProESession() is deprecated. Use the method pfcGetCurrentSession() instead. If you call this method without setting the compatibility, the method sets the compatibility to C3Compatible. This setting ensures forward compatibility of the Creo applications. If you set a specific compatibility using the method pfcGetCurrentSessionWithCompatibility(), and call the method pfcGetCurrentSession(), then all calls to pfcGetCurrentSession() return the session with the set compatibility.
    Note
    You can make multiple calls to this method, but each call gives you a handle to the same object.
    Getting Session Information
    Methods Introduced:
  • pfcGetProEArguments()
  • pfcGetProEVersion()
  • pfcGetProEBuildCode()
  • The method pfcGetProEArguments() returns an array containing the command-line arguments passed to Creo Parametric, if these arguments follow one of two formats:
    •  Any argument starting with a plus sign (+) followed by a letter character.
    •  Any argument starting with a minus (-) followed by a capitalized letter.
    The first argument passed in the array is the full path to the Creo Parametric executable.
    The method pfcGetProEVersion() returns a string that represents the Creo Parametric version, for example “Wildfire”.
    The method pfcGetProEBuildCode() returns a string that represents the build code of the Creo Parametric session.
    Note
    The preceding methods can only access information in synchronous mode.
    Directories
    Methods Introduced:
  • pfcBaseSession.GetCurrentDirectory()
  • pfcBaseSession.ChangeDirectory()
  • The method pfcBaseSession.GetCurrentDirectory() returns the absolute path name for the current working directory of Creo Parametric.
    The method pfcBaseSession.ChangeDirectory() changes Creo Parametric to another working directory.
    File Handling
    Methods Introduced:
  • pfcBaseSession.ListFiles()
  • pfcBaseSession.ListSubdirectories()
  • The method pfcBaseSession.ListFiles() returns a list of files in a directory, given the directory path. You can filter the list to include only files of a particular type, as specified by the file extension.
    The method pfcBaseSession.ListFiles() can also list instance objects when accessing Windchill workspaces or folders. A PDM location for workspace or commonspace, must be passed as the directory path. The following options are added in the pfcFileListOpt enumerated type:
    •  FILE_LIST_ALL—Lists all the files. It may also include multiple versions of the same file.
    •  FILE_LIST_LATEST—Lists only the latest version of each file.
    •  FILE_LIST_ALL_INST—Same as the FILE_LIST_ALL option. It returns instances only for PDM locations.
    •  FILE_LIST_LATEST_INST—Same as the FILE_LIST_LATEST option. It returns instances only for PDM locations.
    The method pfcBaseSession.ListSubdirectories() returns the subdirectories in a given directory location.
    Configuration Options
    Methods Introduced:
  • pfcBaseSession.GetConfigOptionValues()
  • pfcBaseSession.SetConfigOption()
  • pfcBaseSession.LoadConfigFile()
  • You can access configuration options programmatically using the methods described in this section.
    Use the method pfcBaseSession.GetConfigOptionValues() to retrieve the value of a specified configuration file option. Pass the Name of the configuration file option as the input to this method. The method returns an array of values that the configuration file option is set to. It returns a single value if the configuration file option is not a multivalued option. The method returns a null if the specified configuration file option does not exist.
    The method pfcBaseSession.SetConfigOption() is used to set the value of a specified configuration file option. If the option is a multi-value option, it adds a new value to the array of values that already exist.
    The method pfcBaseSession.LoadConfigFile() loads an entire configuration file into Creo Parametric.
    Macros
    Method Introduced:
  • pfcBaseSession.RunMacro()
  • The method pfcBaseSession.RunMacro() runs a macro string. A Creo.JS macro string is equivalent to a Creo Parametric mapkey minus the key sequence and the mapkey name. To generate a macro string, create a mapkey in Creo Parametric. Refer to the Creo Parametric online help for more information about creating a mapkey.
    Copy the Value of the generated mapkey Option from the Tools  Options dialog box. An example Value is as follows:
    $F2 @MAPKEY_LABELtest;
    ~ Activate `main_dlg_cur` `ProCmdModelNew.file`;
    ~ Activate `new` `OK`;
    The key sequence is $F2. The mapkey name is @MAPKEY_LABELtest. The remainder of the string following the first semicolon is the macro string that should be passed to the method pfcBaseSession.RunMacro().
    In this case, it is as follows:
    ~ Activate `main_dlg_cur` `ProCmdModelNew.file`;
    ~ Activate `new` `OK`;
    Note
    Creating or editing the macro string manually is not supported as the mapkeys are not a supported scripting language. The syntax is not defined for users and is not guaranteed to remain constant across different versions of Creo Parametric.
    Macros are executed from synchronous mode only when control returns to Creo Parametric from the Creo.JS program. Macros are stored in reverse order, last in, first out.
    Colors and Line Styles
    Methods Introduced:
  • pfcBaseSession.SetStdColorFromRGB()
  • pfcBaseSession.GetRGBFromStdColor()
  • pfcBaseSession.SetTextColor()
  • pfcBaseSession.SetLineStyle()
  • These methods control the general display of a Creo Parametric session.
    Use the method pfcBaseSession.SetStdColorFromRGB() to customize any of the Creo Parametric standard colors.
    To change the color of any text in the window, use the method pfcBaseSession.SetTextColor().
    To change the appearance of nonsolid lines such as datums, use the method pfcBaseSession.SetLineStyle().
    Accessing the Interface
    The pfcSession object has methods that work with the Creo Parametric interface. These methods provide access to the message window.
    The Text Message File
    A text message file is where you define strings that appear in the Creo Parametric user interface. This includes the strings on the command buttons that you add to the Creo Parametric number, the help string that appears when a cursor is positioned over such a command button, and text strings that appear in the Message Window. You have the option of including a translation for each string in the text message file.
    Note
    Remember that Creo.JS applications, as unregistered web pages, do not currently support the setting of the Creo Parametric text directory. All the resource files for messages must be located under $PRO_DIRECTORY/text folder. You can force Creo Parametric to load a message file by registering a Creo TOOLKIT or application using the web page, and calling a function or method that requires the message file. After the file is loaded by Creo Parametric, Creo.JS applications may use any of its key strings for displaying messages in the message window.
    Restrictions on the Text Message File
    When you name your message file, ensure the following:
    •  Name of the file is 30 characters or less, including the extension.
    •  Name of the file contains lower-case characters only.
    •  File extension is three characters.
    •  Version number is in the range 1 to 9999.
    •  All message file names and all message key strings are unique across all applications that run with Creo Parametric. Duplicate message file names or message key strings can cause unexpected results in Creo Parametric. To avoid conflicts with the names of Creo Parametric or foreign application message files or message key strings, PTC recommends that you choose a prefix unique to your application. Then, prepend that prefix to the file name for each message and to the key string for each message corresponding to that application.
    Note
    Message files are loaded into Creo Parametric only once during a session. If you make a change to the message file while Creo Parametric is running you must exit and restart Creo Parametric before the change takes effect.
    Contents of the Message File
    The message file consists of groups of four lines, one group for each message you want to write. The four lines are as follows:
    1. A string that acts as the identifier for the message. This keyword must be unique for all Creo Parametric messages.
    2. The string that will be substituted for the identifier.
    This string can include placeholders for run-time information stored in a stringseq object which is shown in Writing Messages to the Message Window.
    3. The translation of the message into another language. This can be blank.
    4. An intentionally blank line reserved for future extensions.
    Writing a Message Using a Message Pop-up Dialog Box
    Method and Properties Introduced:
  • pfcSession.UIShowMessageDialog()
  • pfcMessageDialogOptions.Buttons
  • pfcMessageDialogOptions.DefaultButton
  • pfcMessageDialogOptions.DialogLabel
  • pfcMessageDialogOptions.MessageDialogType
  • The method pfcSession.UIShowMessageDialog() displays the UI message dialog. The input arguments to the method are:
    •  Message—The message text to be displayed in the dialog.
    •  Options—An instance of the pfcMessageDialogOptions containing other options for the resulting displayed message. If this is not supplied, the dialog shows a default message dialog with an Info classification and an OK button. If this is not to be null, create an instance of this options type with pfcMessageDialogOptions.Create(). You can set the following options:
      Buttons—Specifies an array of buttons to include in the dialog. If not supplied, the dialog includes only the OK button. Use the property pfcMessageDialogOptions.Buttons to set this option.
      DefaultButton—Specifies the identifier of the default button for the dialog box. This must match one of the available buttons. Use the property pfcMessageDialogOptions.DefaultButton to set this option.
      DialogLabel—The text to display as the title of the dialog box. If not supplied, the label is the English string Info. Use the property pfcMessageDialogOptions.DialogLabel to set this option.
      MessageDialogType—The type of icon to be displayed with the dialog box (Info, Prompt, Warning, or Error). If not supplied, an Info icon is used. Use the property pfcMessageDialogOptions.MessageDialogType to set this option.
    Accessing the Message Window
    The following sections describe how to access the message window using Creo.JS. The topics are as follows:
    Writing Messages to the Message Window
    Methods Introduced:
  • pfcSession.UIDisplayMessage()
  • pfcSession.UIDisplayLocalizedMessage()
  • pfcSession.UIClearMessage()
  • Use these methods to display program information..
    The input arguments to the methods pfcSession.UIDisplayMessage() and pfcSession.UIDisplayLocalizedMessage() include the names of the message file, a message identifier, and (optionally) a stringseq object that contains up to 10 pieces of run-time information. For pfcSession.UIDisplayMessage(), the strings in the stringseq are identified as %0s, %1s, ... %9s based on their location in the sequence. For pfcSession.UIDisplayLocalizedMessage(), the strings in the stringseq are identified as %0w, %1w, ... %9w based on their location in the sequence. To include other types of run-time data such as integers or reals, you must first convert the data to strings and store it in the string sequence.
    The method pfcSession.UIClearMessage() scrolls the text in the message area up one line after the method pfcSession.UIDisplayMessage() is called.
    Writing Messages to an Internal Buffer
    Methods Introduced:
  • pfcBaseSession.GetMessageContents()
  • pfcBaseSession.GetLocalizedMessageContents()
  • You can use the methods pfcBaseSession.GetMessageContents() and pfcBaseSession.GetLocalizedMessageContents() to write a message to an internal buffer instead to the Creo Parametric message area.
    These methods take the same input arguments and perform exactly the same argument substitution and translation as the pfcSession.UIDisplayMessage() and pfcSession.UIDisplayLocalizedMessage() methods described in the previous section.
    Message Classification
    Messages displayed in Creo.JS include a symbol that identifies the message type. Every message type is identified by a classification that begins with the characters %C. A message classification requires that the message key line, line one in the message file must be preceded by the classification code.
    Note
    Any message key string used in the code should not contain the classification.
    Creo.JS applications can display any or all of the following message symbols:
    •  Prompt—This Creo.JS message is preceded by a green arrow. The user must respond to this message type. Responding includes, specifying input information, accepting the default value offered, or canceling the application. If no action is taken, the progress of the application is halted. A response may either be textual or a selection. The classification for Prompt messages is %CP
    •  Info—This Creo.JS message is preceded by a blue dot. Info message types contain information such as user requests or feedback from Creo.JS or Creo Parametric. The classification for Info messages is %CI
    Note
    Do not classify messages that display information regarding problems with an operation or process as Info. These types of messages must be classified as Warnings.
    •  Warning—This Creo.JS message is preceded by a triangle containing an exclamation point. Warning message types contain information to alert users to situations that could potentially lead to an error during a later stage of the process. Examples of warnings could be a process restriction or a suspected data problem. A Warning does not prevent or interrupt a process. Also, a Warning should not be used to indicate a failed operation. Warnings must only caution a user that the completed operation may not have been performed in a completely desirable way. The classification for Warning messages is %CW
    •  Error—This Creo.JS message is preceded by a broken square. An Error message informs the user that a required task was not completed successfully. Depending on the application, a failed task may or may not require intervention or correction before work can continue. Whenever possible redress this situation by providing a path. The classification for Error messages is %CE
    •  Critical—This Creo.JS message is preceded by a red X. A Critical message type informs the user of an extremely serious situation that is usually proceeded by loss of user data. Options redressing this situation, if available, should be provided within the message. The classification for a Critical message is %CC
    Reading Data from the Message Window
    Methods Introduced:
  • pfcSession.UIReadIntMessage()
  • pfcSession.UIReadRealMessage()
  • pfcSession.UIReadStringMessage()
  • These methods enable a program to get data from the user. These methods obtain keyboard input from a text box in the Creo Parametric user interface.
    Note
    When the user presses ESC or clicks Cancel in the Creo Parametric user interface, these methods throw the exception pfcXToolkitMsgUserQuit.
    The pfcSession.UIReadIntMessage() and pfcSession.UIReadRealMessage() methods contain optional arguments that limit the value of the data to a certain range.
    The method pfcSession..UIReadStringMessage() includes an optional Boolean argument that specifies whether to echo characters that are. Use this argument when prompting a user to enter a password.
    Note
    A default value is displayed in the text box as input. When a user presses ENTER as input in the user interface, the default value is not passed to the Creo.JS method; instead, the method returns a constant string use_default_string. When this string is returned, the application must interpret that the user wants to use the default value.
    Displaying Feature Parameters
    Method Introduced:
  • pfcSession.UIDisplayFeatureParams()
  • The method pfcSession.UIDisplayFeatureParams() forces Creo Parametric to show dimensions or other parameters stored on a specific feature. The displayed dimensions may then be interactively selected by the user.
    File Dialogs
    Methods and Properties Introduced:
  • pfcSession.UIOpenFile()
  • pfcFileOpenOptions.Create()
  • pfcFileOpenOptions.FilterString
  • pfcFileOpenOptions.PreselectedItem
  • pfcFileUIOptions.DefaultPath
  • pfcFileUIOptions.DialogLabel
  • pfcFileUIOptions.Shortcuts
  • pfcFileOpenShortcut.Create
  • pfcFileOpenShortcut.ShortcutName
  • pfcFileOpenShortcut.ShortcutPath
  • pfcSession.UISaveFile()
  • pfcFileSaveOptions.Create()
  • pfcSession.UISelectDirectory()
  • pfcDirectorySelectionOptions.Create()
  • The method pfcSession.UIOpenFile() opens the relevant dialog box for browsing directories and opening files. The method lets you specify several options through the input arguments pfcFileOpenOptions and pfcFileUIOptions.
    The method pfcSession.UIOpenFile() returns the file selected by you. The application must use other properties or techniques to perform the desired action on the file.
    Use the method pfcFileOpenOptions.Create() to create a new instance of the pfcFileOpenOptions object. A list of options for this object with a description about what each option specifies, follows:
    •  FilterString—Filter string for the type of file accepted by the dialog box. Multiple file types should be listed with wildcards and separated by commas, for example, *.prt, *.asm, *.txt, *.avi, and so on. Use the property pfcFileOpenOptions.FilterString to set this option.
    •  PreselectedItem—Specifies the name of an item to preselect in the dialog box. Use the property pfcFileOpenOptions.PreselectedItem to set this option.
    The pfcFileUIOptions object contains the following options:
    •  DefaultPath—Specifies the name of the path to be opened by default in the dialog box. Use the property pfcFileUIOptions.DefaultPath to set this option.
    •  DialogLabel—Specifies the title of the dialog box. Use the property pfcFileUIOptions.DialogLabel to set this option.
    •  Shortcuts—Array of file shortcuts of the type pfcFileOpenShortcut. Create this object using the property pfcFileOpenShortcut.Create. This object contains the following attributes:
      ShortcutName—Specifies the name of shortcut path to be made available in the dialog box.
      ShortcutPath—Specifies the string for the shortcut path.
    Use the property pfcFileOpenShortcut.ShortcutName to set the string for the shortcut name.
    Use the property pfcFileOpenShortcut.ShortcutPath to set the string for the shortcut path.
    Use the property pfcFileUIOptions.Shortcuts to set the array of file shortcuts.
    The method pfcSession.UISaveFile() opens the dialog box for saving a file. The method accepts options similar to pfcSession.UIOpenFile() through the pfcFileSaveOptions and pfcFileUIOptions objects. Use the method pfcFileSaveOptions.Create() to create a new instance of the pfcFileSaveOptions object. When using the Save dialog box, you can set the name to a nonexistent file. The method pfcSession.UISaveFile() returns the name of the file selected by you; the application must use other methods or techniques to perform the desired action on the file.
    The method pfcSession.UISelectDirectory() prompts the user to select a directory using the Creo Parametric dialog box for browsing directories. The method accepts options through the pfcDirectorySelectionOptions object which is similar to the pfcFileUIOptions object (described for the method pfcSession.UIOpenFile()). Specify the default directory path, the title of the dialog box, and a set of shortcuts to other directories to start browsing. If the default path is specified as NULL, the current directory is used. Use the method pfcDirectorySelectionOptions.Create() to create a new instance of the pfcDirectorySelectionOptions object. The method pfcSession.UISelectDirectory() returns the selected directory path; the application must use other methods or techniques to perform other tasks with this selected path.
    Customizing the Navigation Area
    The Creo Parametric navigation area includes the Model and Layer Tree pane, Folder browser pane, and Favorites pane. The methods described in this section enable Creo.JS applications to add custom panes that contain Web pages to the Creo Parametric navigation area.
    Adding Custom Web Pages
    To add custom Web pages to the navigation area, the Creo.JS application must:
    1. Add a new pane to the navigation area.
    2. Set an icon for this pane.
    3. Set the URL of the location that will be displayed in the pane.
    Methods Introduced:
  • pfcSession.NavigatorPaneBrowserAdd()
  • pfcSession.NavigatorPaneBrowserIconSet()
  • pfcSession.NavigatorPaneBrowserURLSet()
  • The method pfcSession.NavigatorPaneBrowserAdd() adds a new pane that can display a Web page to the navigation area. The input parameters follow:
    •  PaneName—Unique name for the pane. Use this name in subsequent calls to pfcSession.NavigatorPaneBrowserIconSet() and pfcSession.NavigatorPaneBrowserURLSet().
    •  IconFileName—Name of the icon file, including the extension. A valid format for the icon file is the PTC-proprietary format used by Creo Parametric .BIF, .GIF, .JPG, or .PNG. The new pane is displayed with the icon image. If you specify the value as NULL, the default Creo Parametric icon is used.
    The default search paths for finding the icons are:
      <creo_loadpoint>\<version>\Common Files\text\resource
      <Application text dir>\resource
      <Application text dir>\<language>\resource
    The location of the application text directory is specified in the registry file.
    •  URL—URL of the location to be accessed from the pane.
    Use the method pfcSession.NavigatorPaneBrowserIconSet() to set or change the icon of a specified browser pane in the navigation area.
    Use the method pfcSession.NavigatorPaneBrowserURLSet() to change the URL of the page that appears in the browser pane in the navigation area.