Lab 2.3: Implementing a Job Data Exporter
Time Estimates for Completion
Implement a Job Data Exporter.
Description:
5 minutes
Lab exercise:
15 minutes
Description
A job data exporter must implement the interface IJobExporter:
setOutputDirectory — This method sets the directory where the job exporter writes the job data.
getOutputDirectory — The corresponding getter method to setOutputDirectory.
exportJobData(String elementElid, Properties jobParameters) — This method writes the job data for the specified database element and job parameters to the output directory.
Instead of implementing the interface IJobExporter, it may be more convenient to extend the class AbstractJobExporter that already provides some basic support for:
Organizing data and scripts in different subdirectories
Data export parameters
Writing an index.xml file with meta data (e. g. job parameters, time stamp, etc.)
For drawing- and model-related jobs it is also possible to extend:
DrawingJobExporter
ModelingJobExporter
Details about parameters used by the these job data exporter implementations can be found in their API documentation. More specific job data exporters are used for plot jobs (PlotJobExporter) and clash analysis jobs (ClashJobExporter).
Lab Exercise
Now we want to store the annotated model to a directory on the server using <model name>_<model state> as the file name.
1. Add the AnnotatedModelFileName placeholder in the LISP script that is used when building the file name for the annotated model.
2. Extend ModelingJobExporter to provide a job parameter AnnotatedModelFileName.
3. Update the job's XML configuration file to configure the new exporter.
Prerequisites
You need the SimpleModelGeometryCheckJob.xml and SimpleModelGeometryCheckTemplate.lsp files from Lab 2.2.
Add the AnnotatedModelFileName placholder to your LISP script
Edit SimpleModelGeometryCheckTemplate.lsp to add the %AnnotatedModelFileName% placeholder.
(in-package :frame2)

;;-------------------------
;; utilities
;;-------------------------
(load "JobUtilities.lsp")

;;------------------------------------------------------------------------------
;; This is the routine that is called by the automation framework in Modeling.
;; When this routine is called, the current directory of Modeling is the one
;; where the input data can be found.
;;
;; parameter : <output-directory> directory for output data
;; return : <error-message> return value 'nil' ==> success
(defun run-job (output-directory)

(let* (
;; parameters provided by the job
(ann-model-file-name "%AnnotatedModelFileName%")
(check-type :minimal_check)

;; the item to check
(part-to-check nil)

;; variables for XML serialization
(results-element (xml:xml-list-create-element "results"))
(xml-file (create-path output-directory "index.xml"))

;; variables for writing the output file
(file-zipname "model.pkg") ;; the real name of the result file in the ZIP archive
(file-path (create-path output-directory file-zipname)) ;; the path of the result file
(file-name (format nil "~A.pkg" (if (oli:sd-string= ann-model-file-name "") "AnnotatedModel" ann-model-file-name))) ;; the symbolic name of the result file
)

;;---------------------------------------------
;; load input & call the part checker
;;---------------------------------------------
(load-job-data "%ModelFile%")
(setf part-to-check (oli:sd-inq-obj-children (get-all-at-top)))
(k2-ui::check_part :objects part-to-check check-type :labels :on :keep_labels :on)
(collect-check-results (frame2:getres) results-element)

;; -------------------
;; write result data
;; -------------------
;; write the annotated model
(frame2::save-all-in-package file-path)
;; and make a corresponding entry in the index.xml
(xml:xml-list-add-child results-element (make-result-file-element "PKG" :file file-zipname :name file-name))

;;------------------------------------------------------------------------
;; write the index.xml
;; write-xml-to-list serializes the list into XML (see JobUtilities.lsp)
;;------------------------------------------------------------------------
(write-xml-list-to-file results-element xml-file)

;;------------------------------
;; return = nil means 'no error'
;;------------------------------
(get-automation-error)
) ; let
)

(defun collect-check-results (part-check-result results-element)
(let* (
(error-entries (cdr (assoc k2-ui::*body-check-error-string* part-check-result)))
(model-state (if (> (length error-entries) 0) "model_corrupt" "model_ok"))
)
(xml:xml-list-add-attribute results-element "modelcheck" model-state)
)
)
Extend the ModelingJobExporter class
In your Java development environment, create SimpleModelGeometryCheckJobExporter.java that extends ModelingJobExporter to provide AnnotatedModelFileName in the job parameters. (Click here for a file you can copy and paste.)
package com.acme;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Properties;
import com.osm.automation.ModelingJobExporter;
import com.osm.biz.WMSession;
import com.osm.datamgmt.biz.Model;
import com.osm.datamgmt.biz.VersionableDoc;
import com.osm.exception.WMException;
public class SimpleModelGeometryCheckJobExporter extends ModelingJobExporter {
public static final String ANNOTATED_MODEL_FILE_NAME_KEY = "AnnotatedModelFileName";
public SimpleModelGeometryCheckJobExporter() {
super("SimpleModelGeometryCheckTemplate.lsp", VersionableDoc.HIGHEST_REVISIONS);
}
@Override
protected void writeDataFiles(String modelElid, String modelLoadRule, Properties jobParameters)
throws IOException, WMException {
String annotatedModelFileName = null;
Model model = null;
try {
model = (Model) WMSession.getWMSession().openElement(modelElid, false);
annotatedModelFileName = MessageFormat.format("{0}_{1}", model.getName(), model.getState());
} finally {
WMSession.getWMSession().close(model);
}
jobParameters.put(ANNOTATED_MODEL_FILE_NAME_KEY, annotatedModelFileName);
super.writeDataFiles(modelElid, modelLoadRule, jobParameters);
}
}
* 
Many model attributes are also accessible from LISP. Don't write custom Java classes if it is not really necessary.
Update the job's XML configuration
Replace the ModelingJobExporter with your new SimpleModelGeometryCheckJobExporter, and configure the job result handler to store the result to the C:\taskagent directory.
<?xml version="1.0" encoding="UTF-8"?>
<Job type="modeling.simplegeometrycheck" has_result_data="true">

<DisplayName>Simple Model Geometry Check</DisplayName>

<UI>
<ActionMenu wm_java_class="com.osm.datamgmt.biz.Model" include_derived="true">
<Name>Simple Model Geometry Check</Name>
<JobGroup>Model Checks</JobGroup>
<Action java_class="com.osm.automation.action.MultiSelectCreateJobScheduleAction">
<Description>Checks a model's geometry</Description>
</Action>
</ActionMenu>
</UI>
<Exporter java_class="com.acme.SimpleModelGeometryCheckJobExporter">
<StartScriptTemplate>SimpleModelGeometryCheckTemplate.lsp</StartScriptTemplate>
<ModelLoadRule catalog="model" msg_num="728">Highest Revisions</ModelLoadRule>
</Exporter>
<ResultHandler java_class="com.osm.automation.SaveToFileSystemJobResultHandler">
<TargetDirectory>C:\taskagent</TargetDirectory>
</ResultHandler>
</Job>
The job's index.xml
The job exporters also write an index.xml file with additional information regarding the job execution (the job parameters). The information in index.xml is used by the server side to replace the job parameter placeholders in scripts and macros before execution.
The structure of the default index.xml created by the AbstractJobExporter class looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<job type="modeling.simplegeometrycheck" version="20.8">

<element id="BY6ID3742VLI3N" timestamp="1192720161000"/>
<datadirectory/>
<scriptdirectory/>
<startscript>SimpleModelGeometryCheckTemplate.lsp</startscript>

<parameters>
<parameter key="parameter-name">parameter-value</parameter>
...
</parameters>

</job>
The job element has attributes for its type and version. As already mentioned earlier, the type (or type prefix) determines the job execution target (Creo Elements/Direct Modeling or Creo Elements/Direct Drafting). The element entry represents the Creo Elements/Direct Manager Server data element this job refers to with its id (ELID) and the timestamp. The AbstractJobExporter class writes a single zip file containing all data and scripts required for the job execution. The XML tags datadirectory and scriptdirectory are relative path expressions for the data and script directory in the zip file structure. Startscript indicates the main script or macro to be executed. The parameters section lists all job parameters depending on the job type.
An instance of an index.xml for a model geometry check job with its job specific job parameters may look like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<job type="modeling.simplegeometrycheck" version="20.8">
<element id="BY6ID3742VLI3N" timestamp="1192720161000"/>
<datadirectory/>
<scriptdirectory/>
<startscript>SimpleModelGeometryCheckTemplate.lsp</startscript>
<parameters>
<parameter key="AnnotatedModelFileName">Cylinder_work</parameter>
<parameter key="ModelFile">model.sdexp</parameter>
<parameter key="ModelLoadRule">AS_STORED</parameter>
<parameter key="ModelName">Cylinder</parameter>
<parameter key="StartScriptTemplate">SimpleModelGeometryCheckTemplate.lsp</parameter>
<parameter key="TargetDirectory">E:\taskagent</parameter>
</parameters>
</job>
The job parameter AnnotatedModelFileName is the one we introduced with SimpleModelGeometryCheckJobExporter, the job parameters ModelFile, ModelLoadRule, ModelName, and StartScriptTemplate are contributed by the super class ModelingJobExporter. The job parameter TargetDirectory is a contribution of SaveToFileSystemJobResultHandler.
Was this helpful?