close a polygon [SOLVED]

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
arun
Active Member
Posts: 34
Joined: Tue May 09, 2023 9:04 am

close a polygon [SOLVED]

Post by arun » Tue Jun 27, 2023 8:22 am

Hi,
I am running this test script where I have this polygon entity which is not closed.

Code: Select all

var doc = getDocument();
var entityIds = doc.queryAllEntities(); //58,59
var p1 = doc.queryEntity(59)
p1.isClosed() //false
p1.countVertices() //5
p1.getVertexAt(0) //"RVector(71505.600000, 5084.500000, 0.000000, 1)"
p1.getVertexAt(4) //"RVector(71505.600000, 5084.500000, 0.000000, 1)"
How do I close this polyline?

Thanks,
Arun
Last edited by arun on Tue Jun 27, 2023 5:17 pm, edited 1 time in total.

User avatar
andrew
Site Admin
Posts: 9063
Joined: Fri Mar 30, 2007 6:07 am

Re: close a polygon

Post by andrew » Tue Jun 27, 2023 8:35 am

There are two types of "closed" polylines in CAD / DXF:
Logically closed: The "closed" flag is set, the polyline is defined as being a closed polyline.
Geometrically closed: The polyline start point is identical with its end point.

isClosed returns true if the polyline is logically closed.
isGeometricallyClosed returns true if the polyline is geometrically closed

Logically closed polylines are always also geometrically closed.

You can also call autoClose on your polyline to logically close it if start and end points match.

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

Re: close a polygon

Post by CVH » Tue Jun 27, 2023 8:37 am

arun,
What you have is a geometrically closed polyline entity ... The end vertex is equal to the start vertex.
But it is considered not logically closed.
p1.isClosed() ... p1.setClosed() are about the logical closed nature.
There is also p1.isGeometricallyClosed()

Have a look here:
https://qcad.org/doc/qcad/3.0/developer ... cb6735889f
or
https://qcad.org/doc/qcad/3.0/developer ... 87c7cfedeb

When you set or convert the queried polyline to a closed one you need to return that to the document with an operation. :wink:

Regards,
CVH

arun
Active Member
Posts: 34
Joined: Tue May 09, 2023 9:04 am

Re: close a polygon

Post by arun » Tue Jun 27, 2023 9:42 am

Thanks CVH, Andrew

CVH
CVH wrote:
Tue Jun 27, 2023 8:37 am
When you set or convert the queried polyline to a closed one you need to return that to the document with an operation. :wink:
Didnt understand on this point.
How do I return the polyline to the document. I also observed that the changes to the polyline is not getting updated in the document.
Confused on how this is done.

Arun

arun
Active Member
Posts: 34
Joined: Tue May 09, 2023 9:04 am

Re: close a polygon

Post by arun » Tue Jun 27, 2023 5:16 pm

Got it.
This particular post from CVH helped in understanding https://www.qcad.org/rsforum/viewtopic. ... 28&p=35408

This is my code for reference

Code: Select all

var doc = getDocument();

var layerName = getText();
var entitiesIds = doc.queryLayerEntities(doc.getLayerId(layerName));

var fileName = doc.getFileName()

var openP = []

for (var i=0; i<entitiesIds.length; i++) {
    var entityId = entitiesIds[i];
    var entity = doc.queryEntity(entityId);
    if (!entity.isClosed()) {
        openP.push(entityId);
    }
}

var di = getDocumentInterface();
var op = new RModifyObjectsOperation();
for (var j=0; j<openP.length; j++) {
    var polyL = doc.queryEntity(openP[j]);
    polyL.autoClose();
    op.addObject(polyL, false);
}
di.applyOperation(op);
di.exportFile(fileName);

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”