DeferredVariables with strings

mark.lamneck
Contributor
Contributor

DeferredVariables with strings

mark.lamneck
Contributor
Contributor

In my current pp I'm creating a section in the header of my ncp files with some statistics about the movement during the processed jobs. It looks like this. 

 

N95 ; | movement statistics
N100 ; | X: min=4101 max=337500
N105 ; | Y: min=4685 max=112693
N110 ; | Z: min=-500 max=12000
N115 ; | A: min=0.00 max=0.00
N120 ; | B: min=0.00 max=0.00
 
Previously I did the output at the end of the file, but now I found that I can use DeferredVariables to move this section to the header of my output file. Unfortunately DeferredVariables doesn't accept strings or objects.
 
My solution now is the following:
 

 

...

stats = [statX,statY,statZ,statA,statB];
statFormat = createFormat({});
statFormat.format = function(idx){ return stats[idx].toString(); }

...

function onOpen(){
...
  writeComment(DeferredVariables.get("statX",statFormat));
  ... 
  writeComment(DeferredVariables.get("statA",statFormat));
}

function onClose() {
  ...
  DeferredVariables.set("statX",stats.indexOf(statX));
  ...
  DeferredVariables.set("statB",stats.indexOf(statB));
  ...
}

 

 
where statX,... are objectes with an update method that is called in the onchange callback of the according output variable and a toString method which outputs a string i.e Y: min=-124930 max=-105630.
 
Are there any Doubts about this solution? If not it might help others to use strings or objects in DeferredVariables.
 
Thanks in advance
 
0 Likes
Reply
Accepted solutions (1)
149 Views
2 Replies
Replies (2)

viacheslav.shapilov
Autodesk
Autodesk
Accepted solution

Thank you for your feedback.

Interesting solution. It is not required to create real format, you can just specify it as object directly:

 

statFormat = {
  format: function(idx){ return stats[idx].toString(); }
};

 

But it is up to you really.

From our side, we probably should make format argument optional and use just toString if it not specified. In this case you will be able to set object with toString method as value:

 

// Writing variable
DeferredVariables.get("statX")
...
// Setting it
DeferredVariables.set("statX", statX);

 

BTW, you can simplify your format function to use object directly:

 

statFormat = {
  format: function(obj){ return obj.toString(); }
};​

 

And set variable in a simpler way:

 

DeferredVariables.set("statX", statX);

 


Viacheslav Shapilov
Developer Technical Services
Autodesk Developer Network


0 Likes

mark.lamneck
Contributor
Contributor

Thanks for the quick response and the great tip of giving the instance of the object directly to the set method. I did a quick test and it works even though the "Post Processor Training Guide" says:

 

Assigns value to the deferred variable named id. value must be numeric, it cannot be a text string, boolean, or an object.

 

0 Likes