currently I'm implementing a script which converts all spline entities in a drawing into polylines and exports then the complete drawing to a new DXF file. The conversion from spline to polyline works well (thanks to the scripting sources in Explode.js) as long as I stay in the original drawing and overwrite the spline entities with polylines. But I want to preserve the original drawing for the user and want to make the conversion and export in the background (offline the GUI). So I created a new Document and DocumentInterface and applied the addObjects with
applyOperation to the new DocumentInterface with the result of the Warning "Transaction failed. Please check for block recursions and locked or invisible layers or blocks." and an empty exported DXF file. Below is the source code of the involved function where all the stuff is done:
Code: Select all
Export2visitexLOC.explodeSplines = function(di) {
var document = di.getDocument();
var ids = document.queryAllEntities();
var i, k;
var appWin = EAction.getMainWindow();
var splineTolerance = RSettings.getDoubleValue("Explode/SplineTolerance", 0.01);
var splineSegments = RSettings.getIntValue("Explode/SplineSegments", 64);
var splinesToLineSegments = RSettings.getBoolValue("Explode/SplinesToLineSegments", false);
var op = new RAddObjectsOperation();
for (i=0; i<ids.length; i++) {
var id = ids[i];
var entity = document.queryEntity(id);
if (isNull(entity)) {
debugger;
}
var newObj;
var valid = true;
if (isSplineEntity(entity)) {
// explode spline into polyline with line segments (polyline and splines are shapes):
var spline = entity.getData().castToShape();
newObj = spline.toPolyline(splineSegments);
newObj.simplify();
valid = !newObj.isEmpty();
}
else {
newObj = entity.clone();
}
if (!valid) {
continue;
}
if (isEntity(newObj)) {
op.addObject(newObj, false, true);
}
else {
var e = shapeToEntity(entity.getDocument(), newObj);
if (!isNull(e)) {
e.setSelected(true);
e.copyAttributesFrom(entity.data(), true);
if (!isNull(newObj.color)) {
e.setColor(new RColor(newObj.color));
}
appWin.handleUserMessage(qsTr("new entity blockID: %1 [%2] ").arg(e.getBlockId()).arg(entity.getBlockId()));
op.addObject(e, false);
}
}
}
debugger;
if (!op.isEmpty()) {
// create new document for export:
var info = new QFileInfo(document.getFileName());
var fileName = info.absolutePath() + "/" + info.baseName() + "_exp." + info.completeSuffix();
var newDoc = new RDocument(new RMemoryStorage(), new RSpatialIndexSimple());
newDoc.setFileName(fileName);
var newDI = new RDocumentInterface(newDoc);
newDI.applyOperation(op);
if (!newDI.exportFile(fileName)) {
appWin.handleUserWarning("Cannot export file: " + fileName);
}
newDI.destroy();
}
};
So maybe someone can give me a hint how to set up the document for the export correctly.
Thanks for any help!