Hello.
I want to create a new entity from the offset of an existing entity in my drawing. How can I do this in a script? More precisely, how can I specify the distance and the side where the offsetted shape should be? It should work without a user interactivity respectively a mouse event.
Regards.
Using offset in a script
Moderator: andrew
Forum rules
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Always indicate your operating system and QCAD version.
Attach drawing files, scripts and screenshots.
Post one question per topic.
Using offset in a script
Arch Linux, QCad 3.22.0 Prof.
Re: Using offset in a script
I tried this code:
Am I right, that getOffsetShapes returns nothing because I'm using the trial version? https://github.com/qcad/qcad/blob/fe6ef ... .cpp#L1897 seems to be empty.
Code: Select all
var doc = getDocument();
var di = getDocumentInterface();
var itms = doc.queryAllEntities();
var itm = doc.queryEntity(itms[0]);
var s = itm.getPolylineShape();
var offs = s.getOffsetShapes(.5, 1, RS.LeftHand);
Arch Linux, QCad 3.22.0 Prof.
Re: Using offset in a script
getOffsetShapes is only implemented for simple shapes like lines, arcs, circles and ellipses.
Offsetting polylines requires more information (join type) and can take multiple polylines as input.
If QCAD Professional (or indeed a QCAD Professional trial version) is running, you can offset polylines from a script as follows:
Note: This is currently not part of the public QCAD API.
Offsetting polylines requires more information (join type) and can take multiple polylines as input.
If QCAD Professional (or indeed a QCAD Professional trial version) is running, you can offset polylines from a script as follows:
Code: Select all
// distance: Distance of the offset
// number: Number of offsets to create
// pos Position indicating side of offset (see also setForceInside, setForceSide)
// joinType Join type for sharp corners (RS.JoinRound, RS.JoinMiter)
// preview True for preview mode (fast but not complete)
var worker = new RPolygonOffset(distance, number, pos, joinType, preview);
// optionally: force side and ignore pos:
worker.setForceSide(RS.LeftHand);
// add input polylines:
worker.addPolyline(polyline1);
worker.addPolyline(polyline2);
worker.addPolyline(polyline3);
...
var resultingPolylines = worker.getOffsetShapes();
Re: Using offset in a script
Thanks for your answer.
Here is my working script https://github.com/zippy84/lc-qcad/blob ... offsets.js, for those who are interested in.
... but it should be.andrew wrote: Note: This is currently not part of the public QCAD API.
Here is my working script https://github.com/zippy84/lc-qcad/blob ... offsets.js, for those who are interested in.
Arch Linux, QCad 3.22.0 Prof.
Re: Using offset in a script
I found a special case where RPolygonOffset does not work proper. My drawing is attached and my script is:
Itm is defined as:
It is obviously not clean, because of the zero-length line at the end.
Why is offs empty and is there another way to remove the line?
Code: Select all
var doc = getDocument();
var di = getDocumentInterface();
var entities = doc.queryAllEntities();
var ent = doc.queryEntity(entities[0]);
var blkId = ent.getReferencedBlockId();
var itms = doc.queryBlockEntities(blkId);
di.setCurrentBlock(blkId);
var itm = doc.queryEntity(100);
var newShapes = [];
for (var k = 0; k < itm.countSegments(); k++) {
var seg = itm.getSegmentAt(k);
if (isLineShape(seg)) {
if (seg.getLength() < 1e-5) {
continue;
}
}
newShapes.push(seg.clone());
}
itm.setShape(new RPolyline(newShapes));
var op = new RModifyObjectsOperation();
itm.reverse();
op.addObject(itm, false);
di.applyOperation(op);
var worker = new RPolygonOffset(.15/2, 1, RVector.invalid, RS.JoinMiter, false);
worker.setForceSide(RS.RightHand);
worker.addPolyline(itm.castToShape());
var offs = worker.getOffsetShapes();
Code: Select all
RLine(RShape(address: "0x104f9fc0"), startPoint: RVector(7.46514, -3.58589, 0, true), endPoint: RVector(7.46514, -16.9197, 0, true))
RLine(RShape(address: "0x4fee5460"), startPoint: RVector(7.46514, -16.9197, 0, true), endPoint: RVector(15.0485, -16.9197, 0, true))
RLine(RShape(address: "0x8651390"), startPoint: RVector(15.0485, -16.9197, 0, true), endPoint: RVector(15.0485, -3.58647, 0, true))
RArc(RShape(address: "0x75c84000"), center: RVector(11.256, -14.0402, 0, true), radius: 11.1204, startAngle: 70.0601, endAngle: 109.931, startPoint: RVector(15.0485, -3.58647, 0, true), endPoint: RVector(7.46514, -3.58589, 0, true), sweep: 0.695883, reversed: false)
RLine(RShape(address: "0x162a2f30"), startPoint: RVector(7.46514, -3.58589, 0, true), endPoint: RVector(7.46514, -3.58589, 0, true))
Why is offs empty and is there another way to remove the line?
- Attachments
-
- Err.dxf
- (105.03 KiB) Downloaded 541 times
Arch Linux, QCad 3.22.0 Prof.
Re: Using offset in a script
Also this does not work:
Code: Select all
var doc = getDocument();
var di = getDocumentInterface();
var entities = doc.queryAllEntities();
var ent = doc.queryEntity(entities[0]);
var blkId = ent.getReferencedBlockId();
var itms = doc.queryBlockEntities(blkId);
di.setCurrentBlock(blkId);
var itm = doc.queryEntity(100);
var pl = new RPolyline();
for (var k = 0; k < itm.countSegments(); k++) {
var seg = itm.getSegmentAt(k);
if (isLineShape(seg)) {
if (seg.getLength() < 1e-5) {
continue;
}
}
pl.appendShapeAuto(seg.clone());
}
pl.convertToClosed();
pl.reverse();
var worker = new RPolygonOffset(.15/2, 1, RVector.invalid, RS.JoinMiter, false);
worker.setForceSide(RS.RightHand);
worker.addPolyline(pl);
var offs = worker.getOffsetShapes();
Arch Linux, QCad 3.22.0 Prof.
Re: Using offset in a script
This script works:
Note that the arc-shape is not longer the last element in a. It looks like a bug.
Code: Select all
var doc = getDocument();
var di = getDocumentInterface();
var entities = doc.queryAllEntities();
var ent = doc.queryEntity(entities[0]);
var blkId = ent.getReferencedBlockId();
var itms = doc.queryBlockEntities(blkId);
di.setCurrentBlock(blkId);
var itm = doc.queryEntity(100);
var pl = new RPolyline();
var a = [];
for (var k = 0; k < itm.countSegments(); k++) {
var seg = itm.getSegmentAt(k);
if (isLineShape(seg)) {
if (seg.getLength() < 1e-5) {
continue;
}
}
a.push(seg.clone());
}
a.push(a.shift());
for (var i = 0; i < a.length; i++) {
pl.appendShapeAuto(a[i]);
}
pl.convertToClosed();
pl.reverse();
var worker = new RPolygonOffset(.15/2, 1, RVector.invalid, RS.JoinMiter, false);
worker.setForceSide(RS.RightHand);
worker.addPolyline(pl);
var offs = worker.getOffsetShapes();
var op = new RAddObjectsOperation();
op.addObject(shapeToEntity(doc, offs[0].data()), false);
di.applyOperation(op);
Arch Linux, QCad 3.22.0 Prof.