Java nxopen编辑参数之后doUpdate时JRE崩溃
很简单的需求,编辑模型中的表达式,提交更改(再生模型)。如果新的参数不合理,导致无法再生,则滚回到以前的模型。核心函数: (我在官方论坛的发帖)
The core function is like this, where HashMap<String, Double> expressionMap contains the parameters to update:
private static void EditParameters(HashMap<String, Double> expressionMap) { if (currentOpenPart == null || expressionMap.isEmpty()) { return; } try { Expression exps[] = currentOpenPart.expressions().getVisibleExpressions(); openSessionUndoMark = openSession.setUndoMark(Session.MarkVisibility.VISIBLE, "EditParameter"); for (int i = 0; i < exps.length; ++i) { Expression exp = exps[i]; String expName = exp.name(); if (expressionMap.containsKey(expName)) { System.err.println(expName); exp.setRightHandSide(expressionMap.get(expName).toString()); } } System.err.println("Updating"); openSession.updateManager().doUpdate(openSessionUndoMark); System.err.println("Updated"); } catch (NXException ex) { System.err.println(ex.getMessage()); Logger.getLogger(NXParametricServer.class.getName()).log(Level.SEVERE, null, ex); } catch (RemoteException ex) { Logger.getLogger(NXParametricServer.class.getName()).log(Level.SEVERE, null, ex); } }
The program workd well when all the input expressions are reasonable (so the updateManager().doUpdate(openSessionUndoMark) works well and there is no need to roll back).
然而,如果参数不合理,即运行到openSession.updateManager().doUpdate(openSessionUndoMark)
时需要回滚(正常动作是抛异常NXException ex
),且你是从某个IDE(NetBeans/Eclipse/甚至Visual Studio)启动的JRE,则整个JRE将立即崩溃……
在NX 6.0下,测试了JDK1.6~1.8,结果一样。
灵异在于,如果使用内部模式运行一样的代码,程序正常。如果自己从命令行启动外部模式的程序,也不会崩溃。
最后只能做结论是兼容性问题,换到NX 8.0,这个问题就消失了。