Page 1 of 1

Trying to use REntity intersectsWith()

Posted: Tue Jul 15, 2014 4:42 pm
by Taygete
Hi Andrew,

I have written some code (C++) to locate intersecting entities in the drawing.

The code runs fine with no errors however REntity::intersectsWith(...) always returns false even if there are entities that intersect.

I am testing with a PolyLine and Circle in the drawing, the entities are on different layers however I have also moved them onto the same layer with the same false result.

Any ideas?

Thanks,

Andrew.

Re: Trying to use REntity intersectsWith()

Posted: Tue Jul 15, 2014 5:05 pm
by andrew
Try rebuilding the spatial index before your query:
document->rebuildSpatialIndex();
If you import entities from a file using an existing file importer or if you add entities using operations or transactions, this is not necessary.
However, there are also ways to add entities to a document or storage bypassing the spatial index. In this case, you will have to rebuild the index when you're done.

You can also dump the spatial index for debugging:
qDebug() << document->getSpatialIndex();

Re: Trying to use REntity intersectsWith()

Posted: Tue Jul 15, 2014 6:07 pm
by Taygete
andrew wrote:Try rebuilding the spatial index before your query:
document->rebuildSpatialIndex();
If you import entities from a file using an existing file importer or if you add entities using operations or transactions, this is not necessary.
However, there are also ways to add entities to a document or storage bypassing the spatial index. In this case, you will have to rebuild the index when you're done.

You can also dump the spatial index for debugging:
qDebug() << document->getSpatialIndex();
Hi Andrew,

Thanks for the quick response.

I tried rebuilding the spatial index but this still didn't work, I will try sending the info to qDebug() to see what that reveals.

Will intersectsWith() work with items on different layers?

Thanks,

Andrew.

Re: Trying to use REntity intersectsWith()

Posted: Tue Jul 15, 2014 8:34 pm
by andrew
Taygete wrote:Will intersectsWith() work with items on different layers?
Yes. Perhaps you can post the relevant section of the code, so I can help more efficiently.

Re: Trying to use REntity intersectsWith()

Posted: Wed Jul 16, 2014 1:31 pm
by Taygete
andrew wrote:
Taygete wrote:Will intersectsWith() work with items on different layers?
Yes. Perhaps you can post the relevant section of the code, so I can help more efficiently.
Hi Andrew,

Sorry for the late reply, I have been in meetings all morning but will post again shortly with the relevant code.

Thanks,

Andrew.

Re: Trying to use REntity intersectsWith()

Posted: Wed Jul 16, 2014 2:31 pm
by Taygete
andrew wrote:
Taygete wrote:Will intersectsWith() work with items on different layers?
Yes. Perhaps you can post the relevant section of the code, so I can help more efficiently.
Hi Andrew,

Please find the code below with comments. Basically I am allowing users to drop a Library script onto a drawing, there are three scripts, two create polyline shapes and the third creates a circle. Each script creates custom properties.

Once the user has dropped the items onto the drawing to build up a plan they then click a menu item to call into my c++ plugin where I check for intersections.

Basically I need to search the drawing for all entities which have a specific custom property (in this case "JunctionType"), the shape is a circle so I build up a QList<REntity::Id> of these entities.

Here is that specific code which does build up a list of the correct type.
// Create a list of junction types.
    QList<REntity::Id> junctionList;

    // Rebuild the index
    doc.rebuildSpatialIndex();

    // Get a list of RS::EntityPolyline and RS::EntityCircle as these are the only items we are
    // interested in when dropping the modules onto the current drawing
    QSet<REntity::Id> entities = doc.queryAllEntities(false, false);

    // Get a list of junction entities
    foreach (REntity::Id entity, entities)
    {
        // Get the entity for this id
        QSharedPointer<REntity> theEntity = doc.queryEntity(entity);

        // Check for null and continue if it is null
        if (theEntity.isNull()) {
            continue;
        }

        // Get the list of properties for the QCAD title
        QStringList properties = theEntity->getCustomPropertyKeys("QCAD");

        // Make sure we have some custom properties for this item
        if (properties.count() > 0)
        {
            // Loop through the list of keys for each entity and add to our list
            foreach (QString property, properties)
            {
                if (property.contains("JunctionType"))
                {
                    junctionList.append(theEntity->getId());
                    break;
                }
            }
        }
    }
I then research the entity list looking for intersections between a found junction item(s) and other entities, this is the part which always fails and I have debugged the code through the QCad intersectWith code but it doesn't find the intersection.

Here is that specific code which does looks for intersections.
// Using our list loop back through and see if each item is intersecting
    // with another entity
    foreach (REntity::Id j, junctionList)
    {
        // Get the entity for this id
        QSharedPointer<REntity> theJ = doc.queryEntity(j);

        // Check for null and continue if it is null
        if (theJ.isNull()) {
            continue;
        }

        //Get a list of junction entities
        foreach (REntity::Id entity, entities)
        {
            // Get the entity for this id
            QSharedPointer<REntity> theEntity = doc.queryEntity(entity);

            // Check for null and continue if it is null or the two entities are
            // the same object
            if (theEntity.isNull() || theEntity == theJ) {
                continue;
            }

            // Get the intersection
            bool bIntersected = theJ->intersectsWith(*theEntity->castToConstShape());

            // We have found an intersect.
            if (bIntersected == true)
            {
                // For debuging in release build
                QMessageBox::information(NULL, "Found an intersect", "The junction has intersected with another entity");
            }
        }
    }
Any help would be much appreciated.

Thanks,

Andrew.

Re: Trying to use REntity intersectsWith()

Posted: Wed Jul 16, 2014 9:43 pm
by andrew
I'd suggest to print theJ and theEntity to check if these are as expected:
qDebug() << "theJ: " << *theJ;
qDebug() << "theEntity: " << *theEntity;
The call to intersectsWith seems to be fine. No spatial index is required for this direct query.

Re: Trying to use REntity intersectsWith()

Posted: Thu Jul 17, 2014 1:35 pm
by Taygete
andrew wrote:I'd suggest to print theJ and theEntity to check if these are as expected:
qDebug() << "theJ: " << *theJ;
qDebug() << "theEntity: " << *theEntity;
The call to intersectsWith seems to be fine. No spatial index is required for this direct query.
Having trouble at the moment getting qDebug() to work, it isn't outputting anything.

Andrew, would it be okay if I pm you a test file to look at and confirm intersectsWith is what I should be using, more a sanity check on my part.

Thanks,

Andrew.

Re: Trying to use REntity intersectsWith()

Posted: Thu Jul 17, 2014 3:01 pm
by Taygete
Hi Andrew,

Finally got the entities printed out and they look fine to me.

I have pm'd you the output and also the dxf which the entites are from, would you please have a quick look over them to make sure they are okay and intersectWith is indeed the correct method to use?

Thanks,

Andrew.

Re: Trying to use REntity intersectsWith()

Posted: Thu Jul 17, 2014 4:19 pm
by Taygete
Hi Andrew,

After having no luck with the REntity::intersectsWith(...) method I started investigating other possibilities.

I am now getting the bounding box of the two entities I am comparing then calling RBox::intersects(...), this seems to be working.

I assume there is no downside to using this method over the REntity::intersectsWith(...) one?

Thanks,

Andrew.

Re: Trying to use REntity intersectsWith()

Posted: Thu Jul 17, 2014 4:55 pm
by andrew
Taygete wrote:I am now getting the bounding box of the two entities I am comparing then calling RBox::intersects(...), this seems to be working.

I assume there is no downside to using this method over the REntity::intersectsWith(...) one?
The results will be different.
The bounding boxes of two for (example parallel) lines may intersect even though the lines don't.

If you have a debugger handy, steping through the code should quickly reveal what the problem is.