Class ScriptCommand
- Namespace
- Hi.SessionCommands
- Assembly
- HiNc.dll
Command for executing C# scripts in the milling process.
[CultureText("zh-Hant", "腳本")]
[CultureText("zh-Hans", "脚本")]
[CultureText("vi", "Tập lệnh")]
[CultureText("ja", "スクリプト")]
[CommandCatalog(CommandCategory.Program, Order = 3)]
public class ScriptCommand : ITitleCommand, ISessionCommand, IMakeXmlSource, IGetSelectionName
- Inheritance
-
ScriptCommand
- Implements
- Inherited Members
- Extension Methods
Constructors
ScriptCommand()
Ctor.
public ScriptCommand()
ScriptCommand(XElement, string, IProgress<IMessage>)
Ctor.
public ScriptCommand(XElement src, string baseDirectory, IProgress<IMessage> progress)
Parameters
srcXElementXML
baseDirectorystringBase directory for resolving relative paths
progressIProgress<IMessage>Receives
ScriptFile-Read–Failedwhen the<ScriptFile>ofsrccannot be read (see UnreadableScriptFile); null reports nothing.
Properties
ExecutionTemplate
Gets the execution template command.
public static ScriptCommand ExecutionTemplate { get; }
Property Value
OptimizationConfigurationTemplate
Gets the optimization configuration template command.
public static ScriptCommand OptimizationConfigurationTemplate { get; }
Property Value
PreSettingTemplate
Gets the pre-setting template command.
public static ScriptCommand PreSettingTemplate { get; }
Property Value
ScriptText
The script text content.
public string ScriptText { get; set; }
Property Value
ScriptTitle
Title or name of the script.
public string ScriptTitle { get; set; }
Property Value
UnreadableScriptFile
The script file, relative to the project's directory, whose text could not
be read when this command was loaded (a missing file or folder, a locked
file), or null. While ScriptText is empty the command plays
nothing but ScriptCommand-ScriptFile–Unreadable, and a save writes
the reference back (<ScriptFile>) instead of an empty text, so
the next load reads the file again; a script typed in replaces it. A file
that reads is taken in as ScriptText and saved inline, so this
stays null for it.
public string UnreadableScriptFile { get; }
Property Value
XName
Name for XML IO.
public static string XName { get; }
Property Value
Methods
GenTemplateCommand()
Generates a template script command with default values.
public static ScriptCommand GenTemplateCommand()
Returns
- ScriptCommand
A new ScriptCommand with template values
GetCommandTitle(ICommandTextSource)
Gets the title of the command — one row in a command list or a mission tree — composed from the caller's vocabulary: the command owns the composition and degradation rules, the caller owns the language.
The shape is Name, or Name [detail] once there is a
detail worth showing. The NAME always leads and is never dropped:
a row must say WHAT it is even when the detail is a title the user
wrote, otherwise a newcomer cannot tell a command list from a
script. The detail trails because these rows truncate from the
right. An empty detail takes the bare-name form rather than
rendering dangling brackets.
public string GetCommandTitle(ICommandTextSource text)
Parameters
textICommandTextSourceCulture-bearing vocabulary the static words are taken from.
Returns
- string
The command title.
GetSelectionName()
Gets a name that can be used for selection in UI or other contexts.
public string GetSelectionName()
Returns
- string
The selection name for this object
MakeXmlSource(string, string, bool)
Creates an XML representation of the object. This method may also generate additional resources such as related files.
public XElement MakeXmlSource(string baseDirectory, string relFile, bool exhibitionOnly)
Parameters
baseDirectorystringThe base directory for resolving relative paths
relFilestringThe relative file path for the XML source
exhibitionOnlyboolif true, the extended file creation is suppressed.
Returns
- XElement
An XML element representing the object's state
Remarks
For the demand of easy moving source folder (especially project folder) without configuration file path corruption, the relative file path is applied.
The baseDirectory is typically the folder at the nearest configuration file folder.
Since the folder can be moving with the configuration file.
Reg(XFactory)
Registers this type's deserializer (and its legacy alias) with the
given XFactory (or Default when
factory is null). Idempotent.
public static void Reg(XFactory factory = null)
Parameters
factoryXFactory
Run(SessionShell)
Runs the command, delegating execution to the provided session shell.
public IEnumerable<Action> Run(SessionShell sessionShell)
Parameters
sessionShellSessionShellSession shell that exposes the machining facade to commands.
Returns
- IEnumerable<Action>
Sequence of actions to be executed. A command may do its work as it is enumerated and yield
nullbetween the pieces of it. The player invokes each action in order and paces after it – a Pause holds there, jobs queued for the run's pace point run there, and a Stop of its run ends the run there – and it ends a stopped run at anullitem too, and before the next row of a ListCommand. An action is not checked for a Stop before it runs: one handed over after the Stop runs, and the run ends at the pace after it. Where the run ends, the player disposes the enumeration: the command's code after that item does not run, and what must run whatever happens belongs in afinallyof the iterator. An exception that leaves the enumeration while the run is live ends the run as a fault: the player reports it asPlayer-Run–Fault, the run ends there as not finished, and no later row of a ListCommand runs; a command that catches and reports its own failure (a ScriptCommand‘sScriptCommand-Run–Fault, the CSV mapping commands’Map-SingleCsv–FaultandMap-SeriesCsv–Fault) lets the mission go on. Once the run is stopped, whatever leaves the enumeration – the run's own cancellation, or an exception its unwinding throws – is the Stop, and no message is said for it. A command that catches and reports its own failure still says it after the Stop: a ScriptCommand reports any exception other than a cancellation that comes out of its script's body – the body's own, or one the stopped run's unwinding throws inside the body – asScriptCommand-Run–Fault, and the CSV mapping commands' catches let a cancellation through alone; the stopped run then starts no later row.
Remarks
A row that holds no script text because its script file could not be read runs nothing and says so
(ScriptCommand-ScriptFile–Unreadable, UnreadableScriptFile); every other row is
Run(SessionShell, string) on ScriptText, whose remarks state what a script that does
not compile, throws or meets a Stop does.
Run(SessionShell, string)
Runs scriptText as a C# script on sessionShell – the shell is the
script's globals, ScriptOptions its references and imports – and yields the actions
the script returns. The script's body runs whole when the enumeration starts, before its first item: it starts on
the thread that enumerates (the player's, in a mission), which waits for it to end; the actions it returns run as
they are yielded.
public IEnumerable<Action> Run(SessionShell sessionShell, string scriptText)
Parameters
sessionShellSessionShellThe session shell that scripts delegate to.
scriptTextstringThe script text to run; null runs nothing.
Returns
- IEnumerable<Action>
The actions the script returned (an IEnumerable<T> of Action), in order; none when it returned anything else, did not compile or was reported as faulted (the remarks).
Remarks
A script that does not compile is reported on the shell sink as ScriptCommand-Compile–Failed,
with every diagnostic, and runs nothing. One whose body throws is reported once as ScriptCommand-Run–Fault,
with the script's title, the exception's message and the exception itself as the detail, whether the body threw
before or after an await that did not complete at once: the report is said on the thread that enumerates, before
the enumeration ends – in a mission within the script's row, before the next row starts – and the
ListCommand goes on to its next row. So is a cancellation the body throws while the player's run is
live: its own OperationCanceledException, or the TaskCanceledException of a task it
awaited on a token of its own. An exception other than a cancellation is reported whether or not the run has been
stopped – after a Stop it is said as the script's, the body's own or one the stopped run's unwinding throws
inside the body – and a stopped run starts no later row. The sequence the script returned is enumerated, and
its actions run, outside this method's catch, as any command's actions are: an exception from either ends a live
run with Player-Run–Fault.
The run's own stop is not a fault of the script. A Stop while the script runs is seen at the next play or
act the script makes – its pace point throws the run's cancellation; so may a mapping the script awaits, when the
stop cancels it – and ends the script there with nothing reported: the cancellation goes on to the player, which
ends the run as stopped, and no later row runs, the rule of an NC-embedded script
(ProcAct(IAct, MachiningSession, IMachiningService, ISentenceCarrier, CancellationToken, StepDiagnosticProgress)). The stop is told from the script's own cancellation
by the player's run token as it stands when the cancellation comes back: in a mission the live run's; on an idle
player None, so a script run off the player reports every
cancellation as its own. A Stop that lands in the same instant as the script's own cancellation, before the token
is read, is taken as the stop, and the script's own is not reported. A script that waits on a mapping with
.Wait() instead of awaiting it gets the stop inside that task's AggregateException, which
is reported as a fault of the script.