convert polyline to spline

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
ubi_laptop
Newbie Member
Posts: 5
Joined: Mon Dec 12, 2016 11:59 am

convert polyline to spline

Post by ubi_laptop » Wed Apr 02, 2025 8:13 am

MACOS 14.7.3 Qcad 3.27

is there a way to convert a convert polyline to spline through GUI or a script AUTOMATICALLY ?
I remember a post from andrew :

Here's a way to do it without clicking each point. First, we have to set an undocumented preference in the QCAD3.ini file (we only have to do this once): 1. Misc > Development > Script Shell 2. In dialog click "Show ECMAScript Shell" 3. In the script shell, type: RSettings.setValue("InfoStorePositions/PolylineNodes", true); 4. Press Enter To draw a spline with fit points from polyline nodes: 1. Select the polyline 2. Misc > Information > Store Positions 3. Draw > Spline > Spline (Fit Points) 4. Misc > Information > Use Positions 5. Escape More information about reusing drawing positions: https://qcad.org/tutorial-reusing-drawing-positions.

Any attemps I try to write a script result in a error:

Following is my last attemps.
I would appreciate if someone did successfully the same task.

Daniele

/**
* QCAD Polyline to Spline Automation Script
* This script automates the process of converting polylines to splines
* using fit points in QCAD on macOS.
*/

function init(basePath) {
// Nothing to do here
print("DEBUG: Script initialization started");
}

function run() {
print("DEBUG: Script execution started");

// First, make sure the preference is set
print("DEBUG: Checking preference setting...");
var currentValue = RSettings.getValue("InfoStorePositions/PolylineNodes", false);

if (!currentValue) {
print("DEBUG: Setting preference 'InfoStorePositions/PolylineNodes' to true");
RSettings.setValue("InfoStorePositions/PolylineNodes", true);
print("Preference 'InfoStorePositions/PolylineNodes' set to true");
} else {
print("DEBUG: Preference is already set correctly");
}

// Get the document and document interface
print("DEBUG: Getting document interface and document");
var di = EAction.getDocumentInterface();

if (!di) {
print("ERROR: Document interface not available");
return;
}

print("DEBUG: Document interface obtained successfully");
var doc = di.getDocument();

if (!doc) {
print("ERROR: No document available");
return;
}

print("DEBUG: Document obtained successfully");

// Get the current selection
print("DEBUG: Getting current selection");
var selection = doc.getSelectionSet();
print("DEBUG: Selection size: " + selection.size());

if (selection.size() === 0) {
print("ERROR: Nothing selected. Please select a polyline first.");
return;
}

if (selection.size() > 1) {
print("ERROR: Please select only one polyline at a time.");
return;
}

// Get the ID of the selected entity
print("DEBUG: Getting selected entity");
var objectId = selection.at(0);
print("DEBUG: Selected object ID: " + objectId);
var object = doc.queryEntityDirect(objectId);

if (!object) {
print("ERROR: Could not query the selected object");
return;
}

print("DEBUG: Entity type: " + object.getType());

if (!isPolylineEntity(object)) {
print("ERROR: The selected object is not a polyline");
return;
}

print("DEBUG: Selected object is a polyline");

// Store positions of polyline nodes
print("DEBUG: Storing polyline node positions...");
var success = di.callCommand("InfoStorePositions", ["PolylineNodes"]);
if (!success) {
print("ERROR: Failed to store polyline positions");
return;
}

print("DEBUG: Polyline node positions stored successfully");

// Create spline from stored positions
print("DEBUG: Creating spline...");
success = di.callCommand("SplineFitPoints", []);
if (!success) {
print("ERROR: Failed to create spline");
return;
}

print("DEBUG: Spline created successfully");

// Use stored positions for the spline
print("DEBUG: Using stored positions for spline...");
success = di.callCommand("InfoUsePositions", []);
if (!success) {
print("ERROR: Failed to use stored positions");
return;
}

print("DEBUG: Process completed successfully - polyline converted to spline");
}

// Helper function to check if the entity is a polyline
function isPolylineEntity(entity) {
print("DEBUG: Checking if entity is a polyline");
if (!entity) {
print("DEBUG: Entity is null or undefined");
return false;
}

var type = entity.getType();
print("DEBUG: Entity type is: " + type + " (Polyline type would be: " + RS.EntityPolyline + ")");
return type === RS.EntityPolyline;
}

CVH
Premier Member
Posts: 4957
Joined: Wed Sep 27, 2017 4:17 pm

Re: convert polyline to spline

Post by CVH » Wed Apr 02, 2025 4:43 pm

Hi,

First of all:
Edit your post, select all what is intended as code and hit the [</>] button above the text panel.
Re-submit the posting.
Thank you.

The preference is no longer a 'hidden' preference but still an 'under'-documented one.
(Even standard tools of the Misc section are not listed in the QCAD Reference Manual)
There is a tutorial on the use: Reusing Drawing Positions (QCAD Pro only)

See menu Edit .. Application Preferences .. Info .. Store Positions and opt for Polylines .. Store nodes.
- Select a polyline.
- Misc .. Information .. Store Positions.
- Start the Spline (Fit Points) tool (SL). (QCAD Pro only)
- Misc .. Information .. Use Positions.


About your script ...
Not sure where the print output is going to ... On a Windows system it would be lost.
In fact, there is nothing that calls the run() function.

callCommand(...) is not a member of the RDocumentInterface class.
GUI commands can not be called this way.
Typically GUI tools are event driven and require user interaction.
To get a list of currently selected IDs use: var ids = RDocument.querySelectedEntities();
Your helper code to verify on polylines may overwrite the standard test resource included in the library.js script.

Not really QCAD scripting minded ... :wink:
Something functional below ( :!: Limited to QCAD Pro only :!: ).
Hit 'Select All' and copy text with an R-click.
Save as a JS script in a familiar user place with a plain text editor, for example as 'PolyToSpline.js'.
Make a selection and run the script with XC or menu Misc .. Development .. Run Script (XC)

Code: Select all

include("scripts/EAction.js");
include("scripts/library.js");

// QCAD Polyline to Spline Automation Script
// This script automates the process of converting polylines to splines
// using fit points in QCAD.

var di = EAction.getDocumentInterface();
if (isNull(di)) {
    EAction.handleUserWarning("ERROR: Document interface not available");
    return;
}

var doc = di.getDocument();
var ids = doc.querySelectedEntities();
if (ids.isEmpty()) {
    EAction.handleUserWarning("ERROR: Nothing selected. Please select a polyline first.");
    return;
}
if (ids.length > 1) {
    EAction.handleUserWarning("ERROR: Please select only one entity at a time.");
    return;
}

var entity = doc.queryEntity(ids[0]);
if (isNull(entity) || !isPolylineEntity(entity)) {
    EAction.handleUserWarning("ERROR: Please select a polyline entity.");
    return;
}

var entityData = entity.getData();
var vertices = entityData.getVertices();

var splineShape = new RSpline();
splineShape.setDegree(3);
splineShape.setFitPoints(vertices);
splineShape.setPeriodic(entity.isClosed());
var splineEntity = shapeToEntity(doc, splineShape);

var op = new RAddObjectsOperation();
op.addObject(splineEntity);
//op.deleteObject(entity);    // Optional to remove the original polyline. Delete or rule-out when not required.
di.applyOperation(op);

EAction.handleUserMessage("DEBUG: Process completed successfully - polyline converted to spline");
Because a Spline over the vertices is something completely different I ruled out: //op.deleteObject(entity);
Keeping the original Polyline to start with, or temporary for a test phase.
As is, the spline is created on the current layer.

Regards,
CVH

Post Reply

Return to “QCAD 'How Do I' Questions”