Multiple snap types

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
Taygete
Full Member
Posts: 50
Joined: Wed May 14, 2014 8:53 am

Multiple snap types

Post by Taygete » Thu Dec 04, 2014 2:48 pm

Hi Andrew,

I am trying to enable RSnapOnEntity and RSnapDistanceExtension so both occur at the same when a user is using our script to drop an item on the document.

I can setSnap so it snaps to the entity but have not been able to also enable RSnapDistanceExtension.

I have the following code in our script

Code: Select all

this.getDocumentInterface().setSnap(new RSnapOnEntity());

var guiAction = RGuiAction.getByScriptFile("scripts/Snap/SnapDistance/SnapDistance.js");
if (!isNull(guiAction)) {
    guiAction.slotTrigger();
}

this.getDocumentInterface().setSnapRestriction(new RSnapDistanceExtension());
The line

Code: Select all

this.getDocumentInterface().setSnapRestriction(new RSnapDistanceExtension());
fails with error

Code: Select all

ReferenceError: Can't find variable: RSnapDistanceExtension
It is most likely I am doing something silly, my scripts are contained in the resource of a C++ plugin.

Hope you can help.

Thanks,

Andrew.

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

Re: Multiple snap types

Post by andrew » Thu Dec 04, 2014 2:52 pm

What is RRestrictAngleLengthExtension, one of your own classes?

Taygete
Full Member
Posts: 50
Joined: Wed May 14, 2014 8:53 am

Re: Multiple snap types

Post by Taygete » Thu Dec 04, 2014 2:57 pm

andrew wrote:What is RRestrictAngleLengthExtension, one of your own classes?
Hi Andrew,

Sorry that was a mistake in the post, I edited the original post to refer to RSnapDistanceExtension, which is in the QCad script "scripts/Snap/SnapDistance/SnapDistance.js"

Thanks,

Andrew.

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

Re: Multiple snap types

Post by andrew » Thu Dec 04, 2014 4:18 pm

OK. To use RSnapDistanceExtension, you will have to include the file SnapDistance.js.

Please note that RSnapDistanceExtension is not a snap restriction, but a regular snap function. So you cannot pass is to setSnapRestriction.

Possible snap restrictions are RRestrictAngleLength, RRestrictOrthogonal, RRestrictVertical and RRestrictHorizontal.

If I understand your situation correctly, you would like to snap to points with a given distance to an end point, but disallow points that lay not on the entity (e.g. a point 10 units away from the end point of a line which is only 5 units long).
Is that correct?

Taygete
Full Member
Posts: 50
Joined: Wed May 14, 2014 8:53 am

Re: Multiple snap types

Post by Taygete » Thu Dec 04, 2014 4:42 pm

andrew wrote:OK. To use RSnapDistanceExtension, you will have to include the file SnapDistance.js.

Please note that RSnapDistanceExtension is not a snap restriction, but a regular snap function. So you cannot pass is to setSnapRestriction.

Possible snap restrictions are RRestrictAngleLength, RRestrictOrthogonal, RRestrictVertical and RRestrictHorizontal.

If I understand your situation correctly, you would like to snap to points with a given distance to an end point, but disallow points that lay not on the entity (e.g. a point 10 units away from the end point of a line which is only 5 units long).
Is that correct?
Hi Andrew,

Thanks for the update.

Your understanding is correct, being able to snap to a grid would work for me but when I SnapOnEntity

Code: Select all

this.getDocumentInterface().setSnap(new RSnapOnEntity());
it no longer respects the grid snapping.

The workflow I have is:
1. The user selects an entity in the scene they wish to snap too.
2. They then select the start point from which to measure out a distance to place the item on the entity they selected in step 1.
3. They click a third time to place the item into the document.

With regards step 2, I would like to give them the option to enter a distance (similar to how RestrictAngleLength works but without the Angle part hence why I was looking at SnapDistance) or snap to a grid, but all the while snapping to the entity selected in step 1.

Hope that makes sense.

Andrew.

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

Re: Multiple snap types

Post by andrew » Thu Dec 04, 2014 4:55 pm

OK. This sounds like a specialised snap tool that snaps to a point with a given distance on a specific, previously chosen entity.

I'd recommend to implement your own snap tool, similar to the one implemented in SnapDistance.js

You can essentially copy that file and rename the classes or derive from those classes. Add an entityId to the class, so you can indicate what entity has already been chosen in step 1. Maybe pass RS::FromStart or RS::FromEnd to indicate if user has chosen the start or end point to measure from.

In addition, implement a function RMySnapDistanceExtension.prototype.snapEntity (I call your derived or reimplemented snap class RMySnapDistanceExtension here):
RMySnapDistanceExtension.prototype.snapEntity = function(entity, point, queryBox, view) {
    // perform additional checking here (for example to make sure entity is the entity that was previously chosen:
    ...

    // call existing implementation that snaps to snap point with a given distance.
    var ret = entity.getPointsWithDistanceToEnd(distance, RS::FromAny, queryBox); // replace RS::FromAny with your already known start or end point option

    // perform additional checking on candidates (array of RVector) if desirable:
    ...
    return ret;
};
PS: you might have to update to the latest master branch in git to make this work though. I had to fix the script wrappers to allow overriding of snapEntity. Sorry about that.

Taygete
Full Member
Posts: 50
Joined: Wed May 14, 2014 8:53 am

Re: Multiple snap types

Post by Taygete » Fri Dec 05, 2014 10:08 am

Hi Andrew,

Thanks for the input, I will take a look at this after grabbing the latest source from git.

Thanks,

Andrew.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”