How to handle new features in post engine

How to handle new features in post engine

fonsecr
Alumni Alumni
942 Views
3 Replies
Message 1 of 4

How to handle new features in post engine

fonsecr
Alumni
Alumni

As new features are being added in our CAM products Inventor HSM or HSMWorks and Fusion 360 we also need to extend the API of the post processor engine accordingly. However, this causes a problem since we want to ensure that we only have a single version of a particular post configuration that everybody can use even when using an older version of the products.

 

The recommended way to keep supporting the old releases is to add a bit more code that would otherwise be required.

 

Let says we add a new function called 'myNewFunction' for the Section class.

 

 

currentSection.myNewFunction(); // will fail for old releases since myNewFunction doesnt exist in Section.

 

 

To avoid failure for old release we instead first check if the feature is available:

 

// if statement will be skipped if myNewFunction isn't defined
if (currentSection.myNewFunction) { // note this is not a function call - here we ask if myNewFunction exists
  currentSection.myNewFunction(); // now we can call the function
}

 

 

Note that this approach works the best for function calls.

 

If you try the same for say properties than you can get in trouble since you cannot differentiate between non-existing property and a property that casts to false without asking JavaScript to do this explicitly.

 

 

if (currentSection.myProperty) { // bad since myProperty may not exist or could be false
  currentSection.myProperty; // use property
}

 

 

Instead we check if the property is undefined like this:

 

if (currentSection.myProperty !== undefined) { // test if property is defined - the !== operator will check both value and type - do not confuse with the != operator which doesnt check the type
  currentSection.myProperty; // use property - even if it is set to 0 or false or empty string
}

 

 

If the code gets too complicated, then the alternative is to tell the post processor that the configuration requires a newer version of the engine by setting the "minimumRevision" property. That way the user will get an explicit error message stating that the product needs to be updated to use the processor. However, I really never had a good reason to make use of this approach so far. So you should be reluctant to use this feature also.

 

If you want you can get the version of the post engine using the "revision" property and skip specific features in the post depending on the version the user uses.

 

 

if (revision >= 12345) { // if post engine is later than this then do something
  myNewFunction();
}

 

 

The posts in our library https://cam.autodesk.com/posts/ commonly make use of the above. Here is a common example for checking if the force tool change feature is available:

 

  var insertToolCall = isFirstSection() ||
    currentSection.getForceToolChange && currentSection.getForceToolChange() ||
    (tool.number != getPreviousSection().getTool().number);

 

 

If you need to check if a global function is available then the code gets a little more complicated since we now have to use the typeof operator:

if ((typeof getHeaderVersion != "function") && getHeaderVersion()) {
  writeComment(localize("post version") + ": " + getHeaderVersion());
}

René Fonseca
Software Architect

943 Views
3 Replies
Replies (3)
Message 2 of 4

ArjanDijk
Advisor
Advisor

Hi Rene. It is possible to add the required version to the posts in the library?


Inventor HSM and Fusion 360 CAM trainer and postprocessor builder in the Netherlands and Belgium.


0 Likes
Message 3 of 4

fonsecr
Alumni
Alumni

For the post library we are making sure to support all releases way way back by following the guidelines above. So there is currently no need to list the required version. However, it would be complicated to do anyway since the releases for Inventor HSM, HSMWorks and Fusion are all happening independently. So as a user they wouldn't be able to tell which product requires a specific post. That is, which version of the post engine is included with a particular release. So basically much simpler to just make sure we support any release unless there is extreme cases.


René Fonseca
Software Architect

Message 4 of 4

fonsecr
Alumni
Alumni

Bob Schultz from post team spotted a typo != should be ==:

 

If you need to check if a global function is available then the code gets a little more complicated since we now have to use the typeof operator:

if ((typeof getHeaderVersion == "function") && getHeaderVersion()) {
  writeComment(localize("post version") + ": " + getHeaderVersion());

 


René Fonseca
Software Architect

0 Likes