Identify Entity Intersection with Script

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
wildspidee
Full Member
Posts: 84
Joined: Sat Nov 03, 2012 2:00 am

Identify Entity Intersection with Script

Post by wildspidee » Tue Jan 20, 2015 1:04 am

I fear I'm becoming the problem child. I'm creating an outline for a script that will draw a part that will reside in the QCAD part library. One of the things I have to do is draw a line of a fixed length from one known point to somewhere on a guideline. The solution when drawing by hand was provided to me by Clive in 2012. Please refer to this post: viewtopic.php?f=32&t=2241.

As you can see, the point is located using a circle with the correct radius. This works great manually and the circle is easily scripted, but how would you do the rest with script? Based on examples I’ve been studying, the RAddObjectOperation is called and the entities are drawn and the script is done. This won’t work for me.

Idea #1 - Since the entities don’t exist in the document yet, there is no intersection to find. I’m guessing that I would need multiple .js files. One would be the main script and the others would be called to draw items and add them to the document one by one. I can assign each entity a persistent name by assigning a Custom property. Then I would have to find the intersection of Custom1 (circle) and Custom2 (guideline) in the script referencing the entities that exist at that time in the document. Then I would add the new entity (connecting line) and proceed with the main script. The only class I can find in the API with intersection is the RSnap, but perhaps there is another for this?

Idea #2 - Some or all of Idea #1 isn’t possible and I’ll have to do the math in the script manually (Pythagorean theorem).

Almost this identical problem exists with this process, also solved manually by Andrew - viewtopic.php?f=32&t=3215.

Thank you for your time,
Lori

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

Re: Identify Entity Intersection with Script

Post by andrew » Tue Jan 20, 2015 1:38 pm

You can also use the geometry classes of QCAD without adding entities to a drawing.

Let's say you have two lines whose coordinates are known and you want to get their intersection point:
var line1 = new RLine(new RVector(10,20), new RVector(10,100));
var line2 = new RLine(new RVector(0,50), new RVector(100,50));
var solutions = line1.getIntersectionPoints(line2);
qDebug(solutions);
Basically all shape classes (RLine, RArc, RCircle, REllipse, RSpline, etc.) are simply mathematical / geometrical constructs without any relationship to a drawing.

All entity classes (RLineEntity, RArcEntity, etc.) typically live in a document or are created with the intention to add them to a document.

Entities can be created from shapes using shapeToEntity:
var entity = shapeToEntity(document, shape);

wildspidee
Full Member
Posts: 84
Joined: Sat Nov 03, 2012 2:00 am

Re: Identify Entity Intersection with Script

Post by wildspidee » Tue Jan 20, 2015 5:05 pm

This is very good. I can create Shapes for reference geometry and never add it to the document at all.

Can I reference a Shape and an Entity in the script? For example, if Line1 is a RLineEntity and Line2 is a RLine can I still use line1.getIntersectionPoints(line2) in the code or do they both need to be Shapes at that point?

Thank you,
Lori

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

Re: Identify Entity Intersection with Script

Post by andrew » Tue Jan 20, 2015 5:12 pm

Its usually best to do math stuff purely with shapes. You can easily get the underlying geometry of an entity using castToShape:
var line = myLineEntity.castToShape();

wildspidee
Full Member
Posts: 84
Joined: Sat Nov 03, 2012 2:00 am

Re: Identify Entity Intersection with Script

Post by wildspidee » Tue Jan 20, 2015 8:31 pm

Okay. I will stick to shapes and convert them to entities when they need to be added to the document.

By the way, you have a link on your QCAD Documentation page http://www.qcad.org/en/qcad-documentation - Introduction to QCAD for pattern design / sewing (Movies) to my old website. Things have changed quite a bit and this link is dead. Although these videos are kind of old now, you can still refer to them if you like at this url https://www.fearlessmakers.com/using-qc ... ur-pillow/. It's the same videos, just a new website page.

Thanks Andrew.

Lori

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

Re: Identify Entity Intersection with Script

Post by andrew » Tue Jan 20, 2015 8:44 pm

OK, thanks. I've updated the link on our web site.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”